-
Notifications
You must be signed in to change notification settings - Fork 0
/
3keypad.ino
218 lines (206 loc) · 4.6 KB
/
3keypad.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
void backspace() {
// prevent backspacing beyond first cursor position
if (cursor == 1) {
return;
}
uint8_t cursorX;
uint8_t cursorY;
cursor--;
cursorX = cursor;
if (cursorX > 16) {
cursorY = 1;
cursorX = cursorX - 16;
} else {
cursorY = 0;
}
// set cursor to previous position (subtract one since cursor is numbered from 1)
lcd.setCursor(cursorX - 1, cursorY);
// print a space
lcd.print(' ');
// set cursor back to the newly blank space
lcd.setCursor(cursorX - 1, cursorY);
}
// take action based on key pressed;
// character keys run updateMultipress;
// other keys cause direct action
void determineMultipress(char key) {
switch (key) {
case 'A': // A B C | 1
updateMultipress(1);
break;
case 'D': // D E F | 2
updateMultipress(2);
break;
case 'G': // G H I | 3
updateMultipress(3);
break;
case 'a': // alphabetical keyboard
numeric = false;
break;
case 'J': // J K L | 4
updateMultipress(5);
break;
case 'M': // M N O | 5
updateMultipress(6);
break;
case 'P': // P Q R | 6
updateMultipress(7);
break;
case 'n': // numeric keyboard
numeric = true;
break;
case 'S': // S T U | 7
updateMultipress(9);
break;
case 'V': // V W X | 8
updateMultipress(10);
break;
case 'Y': // Y Z , | 9
updateMultipress(11);
break;
case '^': // capital shift
capitalShift = true;
break;
case ':': // : ) ( | '
updateMultipress(13);
break;
case '.': // . ! ? | 0
updateMultipress(14);
break;
case '>': // send message
sendToPeer(messageBuffer);
// do not clear messageBuffer or LCD until a reply is received
break;
case ' ': // confirm character | space (if last character was confirmed) | backspace (if capital shifted)
if (multipress[1] == 0) {
// if last character was confirmed, send a space
updateMultipress(16);
}
typeKey = true;
break;
}
}
// update global multipress[] with the last key pressed and the
// number of times it has been pressed consecutively
void updateMultipress(uint8_t keyNumber) {
uint8_t pressCount;
if (multipress[0] == keyNumber) {
// if this key was pressed previously, increment pressCount
pressCount = multipress[1] + 1;
// if pressCount exceeds 3, reset it to 1
if (pressCount > 3) {
pressCount = 1;
}
} else {
// if this key was not pressed previously, reset pressCount
pressCount = 1;
}
// update multipress
multipress[0] = keyNumber;
multipress[1] = pressCount;
}
// returns current character based on information in global multipress[]
char calcMP(bool numeric) {
char trio[3];
// translate key # to letters
switch (multipress[0]) {
case 1:
if (numeric) {
return '1';
}
trio[0] = 'a';
trio[1] = 'b';
trio[2] = 'c';
break;
case 2:
if (numeric) {
return '2';
}
trio[0] = 'd';
trio[1] = 'e';
trio[2] = 'f';
break;
case 3:
if (numeric) {
return '3';
}
trio[0] = 'g';
trio[1] = 'h';
trio[2] = 'i';
break;
case 5:
if (numeric) {
return '4';
}
trio[0] = 'j';
trio[1] = 'k';
trio[2] = 'l';
break;
case 6:
if (numeric) {
return '5';
}
trio[0] = 'm';
trio[1] = 'n';
trio[2] = 'o';
break;
case 7:
if (numeric) {
return '6';
}
trio[0] = 'p';
trio[1] = 'q';
trio[2] = 'r';
break;
case 9:
if (numeric) {
return '7';
}
trio[0] = 's';
trio[1] = 't';
trio[2] = 'u';
break;
case 10:
if (numeric) {
return '8';
}
trio[0] = 'v';
trio[1] = 'w';
trio[2] = 'x';
break;
case 11:
if (numeric) {
return '9';
}
trio[0] = 'y';
trio[1] = 'z';
trio[2] = ',';
break;
case 13:
if (numeric) {
return '\'';
}
trio[0] = ':';
trio[1] = ')';
trio[2] = '(';
break;
case 14:
if (numeric) {
return '0';
}
trio[0] = '.';
trio[1] = '!';
trio[2] = '?';
break;
case 16:
trio[0] = ' '; // rest of trio does not need to be defined (once multipress exceeds 1 for a space, it is printed and multipress is reset)
}
return trio[multipress[1] - 1];
}
// block until a key is pressed
void blockUntilKey() {
key = NO_KEY;
while (key == NO_KEY) {
key = keypad.getKey();
}
}