Skip to content

Commit 124fc39

Browse files
committed
Update to Version 1.0.1
1 parent 1f71356 commit 124fc39

9 files changed

Lines changed: 358 additions & 2 deletions

File tree

123-mode-2.png

21.2 KB
Loading

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ This library provides with following examples:
120120
***Figure 3.*** *LCD 1602 Wiring*
121121

122122
- ### Typewriting Demo on LCD 1602
123-
This example demonstrates the typewriting style just like on old Nokia phone. ***Fig. 3.*** and ***Fig. 4.*** illustrate all available characters which can be accessed. Character in the parentheses accessed through long-tap or hold the relevant key for at least one second. Hardware wiring also can be followed on ***Fig. 2.*** and ***Fig. 3.***
123+
This example demonstrates the typewriting style just like on old Nokia phone. ***Fig. 4.*** and ***Fig. 5.*** illustrate all available characters which can be accessed. Character in the parentheses accessed through long-tap or hold the relevant key for at least one second. Hardware wiring also can be followed on ***Fig. 2.*** and ***Fig. 3.***
124124

125125

126126

@@ -132,6 +132,19 @@ This library provides with following examples:
132132

133133
***Figure 5.*** *Keypad Style in Numeric Mode*
134134

135+
- ### Input String on LCD 1602
136+
If the previous example demonstrates the typewriting style just like on old Nokia phone, this example captures the entered string from the keypad into a string buffer and displays them to Serial Monitor. ***Fig. 6.*** and ***Fig. 7.*** illustrate all available characters which can be accessed. Character in the parentheses accessed through long-tap or hold the relevant key for at least one second. Hardware wiring also can be followed on ***Fig. 2.*** and ***Fig. 3.***
137+
138+
139+
140+
![](abc-mode-2.png?raw=true)
141+
142+
***Figure 6.*** *Keypad Style in Alphabet Mode*
143+
144+
![](123-mode-2.png?raw=true)
145+
146+
***Figure 7.*** *Keypad Style in Numeric Mode*
147+
135148
 
136149

137150
 

abc-mode-2.png

29.5 KB
Loading
21.2 KB
Loading
Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
/*
2+
Input String on LCD 1602
3+
4+
5+
Created 10 August 2017
6+
@Gorontalo, Indonesia
7+
by ZulNs
8+
*/
9+
10+
#include <MultitapKeypad.h>
11+
#include <LiquidCrystal.h>
12+
13+
const uint16_t BACKLIGHT_PERIODS = 10000;
14+
15+
const byte ROW0 = 4;
16+
const byte ROW1 = 5;
17+
const byte ROW2 = 6;
18+
const byte ROW3 = 7;
19+
const byte COL0 = 8;
20+
const byte COL1 = 9;
21+
const byte COL2 = 10;
22+
const byte COL3 = 11;
23+
24+
const byte LCD_RS = A4;
25+
const byte LCD_EN = A5;
26+
const byte LCD_D4 = A0;
27+
const byte LCD_D5 = A1;
28+
const byte LCD_D6 = A2;
29+
const byte LCD_D7 = A3;
30+
const byte BACKLIGHT = 3;
31+
const byte BEEP = 2;
32+
33+
const byte CHR_BOUND = 3;
34+
const byte BACKSPACE = 8;
35+
const byte CLEARSCREEN = 12;
36+
const byte CARRIAGE_RETURN = 13;
37+
const byte CAPSLOCK_ON = 17;
38+
const byte CAPSLOCK_OFF = 18;
39+
const byte NUMLOCK_ON = 19;
40+
const byte NUMLOCK_OFF = 20;
41+
42+
const char SYMBOL[] PROGMEM = {
43+
',', '.', ';', ':', '!',
44+
'?', '\'', '"','-', '+',
45+
'*', '/', '=', '%', '^',
46+
'(', ')', '#', '@', '$',
47+
'[', ']', '{', '}', CHR_BOUND
48+
};
49+
const char ALPHABET[] PROGMEM = {
50+
'A', 'B', 'C', CHR_BOUND, CHR_BOUND,
51+
'D', 'E', 'F', CHR_BOUND, CHR_BOUND,
52+
'G', 'H', 'I', CHR_BOUND, CHR_BOUND,
53+
'J', 'K', 'L', CHR_BOUND, CHR_BOUND,
54+
'M', 'N', 'O', CHR_BOUND, CHR_BOUND,
55+
'P', 'Q', 'R', 'S', CHR_BOUND,
56+
'T', 'U', 'V', CHR_BOUND, CHR_BOUND,
57+
'W', 'X', 'Y', 'Z', CHR_BOUND
58+
};
59+
60+
boolean alphaMode = true;
61+
boolean upperCaseMode = true;
62+
boolean autoOffBacklight = false;
63+
boolean isEndOfDisplay = false;
64+
byte startCursorPos;
65+
byte endCursorPos;
66+
byte cursorPos;
67+
byte chrCtr;
68+
unsigned long backlightTimeout;
69+
char strBuffer[16];
70+
71+
// creates lcd as LiquidCrystal object
72+
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
73+
// creates kpd as MultitapKeypad object
74+
// for matrix 4 x 3 keypad
75+
// MultitapKeypad kpd(ROW0, ROW1, ROW2, ROW3, COL0, COL1, COL2);
76+
// for matrix 4 x 4 keypad
77+
MultitapKeypad kpd(ROW0, ROW1, ROW2, ROW3, COL0, COL1, COL2, COL3);
78+
// creates key as Key object
79+
Key key;
80+
81+
void setup() {
82+
kpd.attachFunction(otherProcess);
83+
pinMode(BACKLIGHT, OUTPUT);
84+
digitalWrite(BACKLIGHT, LOW);
85+
pinMode(LED_BUILTIN, OUTPUT);
86+
lcd.begin(16, 2);
87+
Serial.begin(9600);
88+
89+
Serial.println(F("Input Characters Demo..."));
90+
lcd.print(F("Input Characters"));
91+
lcd.setCursor(0, 1);
92+
lcd.print(F("Demo..."));
93+
delay(2000);
94+
tone(BEEP, 4000, 50);
95+
96+
lcd.clear();
97+
lcd.print("Your choice:");
98+
lcd.setCursor(0, 1);
99+
lcd.print("1.Y 2.N 3.Any");
100+
char chr;
101+
while (true) {
102+
chr = getAKey();
103+
if (chr >= '1' && chr <= '3') {
104+
tone(BEEP, 5000, 20);
105+
break;
106+
}
107+
tone(BEEP, 4000, 100);
108+
}
109+
Serial.print(F("Your choice: "));
110+
Serial.println(chr);
111+
112+
lcd.clear();
113+
lcd.cursor();
114+
lcd.blink();
115+
116+
lcd.setCursor(0, 1);
117+
lcd.print(F("Name:"));
118+
getString(21, 31);
119+
Serial.print(F("Username: "));
120+
Serial.println(strBuffer);
121+
122+
lcd.clear();
123+
lcd.setCursor(0, 1);
124+
lcd.print(F("Code:"));
125+
getString(21, 26);
126+
Serial.print(F("Password: "));
127+
Serial.println(strBuffer);
128+
129+
lcd.clear();
130+
lcd.noCursor();
131+
lcd.noBlink();
132+
lcd.print(F("Please open the"));
133+
lcd.setCursor(0, 1);
134+
lcd.print(F("Serial Monitor"));
135+
}
136+
137+
void loop() {
138+
delay(1000);
139+
// do your stuff here...
140+
}
141+
142+
char getAKey() {
143+
while(true) {
144+
key = kpd.getKey();
145+
if (key.state == KEY_DOWN || key.state == MULTI_KEY_DOWN)
146+
return key.character;
147+
}
148+
}
149+
150+
void getString(byte startPos, byte endPos) {
151+
char chr;
152+
byte strSize = endPos - startPos + 1;
153+
startCursorPos = startPos;
154+
endCursorPos = endPos;
155+
cursorPos = startPos;
156+
setCursorPos();
157+
chrCtr = 0;
158+
displayInputMode();
159+
while (true) {
160+
key = kpd.getKey();
161+
switch (key.state) {
162+
case KEY_DOWN:
163+
case MULTI_TAP:
164+
tone(BEEP, 5000, 20);
165+
digitalWrite(LED_BUILTIN, HIGH);
166+
switch (key.code) {
167+
case KEY_1:
168+
chr = alphaMode ? getSymbol(key.tapCounter) : '1';
169+
break;
170+
case KEY_ASTERISK:
171+
chr = alphaMode ? 0 : key.character;
172+
break;
173+
case KEY_0:
174+
chr = alphaMode ? ' ' : '0';
175+
break;
176+
case KEY_NUMBER_SIGN:
177+
if (alphaMode) {
178+
upperCaseMode = !upperCaseMode;
179+
chr = upperCaseMode ? CAPSLOCK_ON : CAPSLOCK_OFF;
180+
}
181+
else
182+
chr = key.character;
183+
break;
184+
case KEY_A:
185+
chr = BACKSPACE;
186+
break;
187+
case KEY_B:
188+
case KEY_C:
189+
chr = 0;
190+
break;
191+
case KEY_D:
192+
strBuffer[chrCtr] = 0;
193+
return;
194+
default:
195+
chr = alphaMode ? getAlphabet(key.character, key.tapCounter) : key.character;
196+
}
197+
if (!upperCaseMode && chr >= 'A')
198+
chr += 32; // makes lower case
199+
if (key.state == MULTI_TAP && alphaMode && key.character >= '1' && key.character <= '9') {
200+
printToLcd(BACKSPACE);
201+
chrCtr--;
202+
}
203+
if (chr > 0) {
204+
if (chr == BACKSPACE)
205+
chrCtr--;
206+
if (chr >= ' ') {
207+
strBuffer[chrCtr] = chr;
208+
if (chrCtr < strSize)
209+
chrCtr++;
210+
}
211+
printToLcd(chr);
212+
}
213+
autoOffBacklight = false;
214+
digitalWrite(BACKLIGHT, LOW);
215+
break;
216+
case LONG_TAP:
217+
switch (key.code) {
218+
case KEY_A:
219+
chr = CLEARSCREEN;
220+
break;
221+
case KEY_NUMBER_SIGN:
222+
if (alphaMode)
223+
upperCaseMode = !upperCaseMode;
224+
alphaMode = !alphaMode;
225+
chr = alphaMode ? NUMLOCK_OFF : NUMLOCK_ON;
226+
break;
227+
default:
228+
chr = key.character;
229+
}
230+
if (chr < ' ' || alphaMode && chr >= '0' && chr <= '9') {
231+
tone(BEEP, 5000, 20);
232+
if (chr >= '0' || chr == NUMLOCK_OFF) {
233+
printToLcd(BACKSPACE);
234+
chrCtr--;
235+
}
236+
if (chr == CLEARSCREEN)
237+
chrCtr = 0;
238+
if (chr >= ' ') {
239+
strBuffer[chrCtr] = chr;
240+
if (chrCtr < strSize)
241+
chrCtr++;
242+
}
243+
printToLcd(chr);
244+
}
245+
break;
246+
case MULTI_KEY_DOWN:
247+
tone(BEEP, 4000, 100);
248+
digitalWrite(LED_BUILTIN, LOW);
249+
break;
250+
case KEY_UP:
251+
digitalWrite(LED_BUILTIN, LOW);
252+
backlightTimeout = millis() + BACKLIGHT_PERIODS;
253+
autoOffBacklight = true;
254+
}
255+
}
256+
}
257+
258+
void otherProcess(void) {
259+
if (autoOffBacklight && millis() >= backlightTimeout) {
260+
digitalWrite(BACKLIGHT, HIGH);
261+
autoOffBacklight = false;
262+
}
263+
}
264+
265+
void printToLcd(char chr) {
266+
switch (chr) {
267+
case BACKSPACE:
268+
if (cursorPos > startCursorPos) {
269+
if (!isEndOfDisplay)
270+
decCursorPos();
271+
setCursorPos();
272+
lcd.print(F(" "));
273+
setCursorPos();
274+
isEndOfDisplay = false;
275+
}
276+
break;
277+
case CLEARSCREEN:
278+
while (cursorPos > startCursorPos) {
279+
decCursorPos();
280+
setCursorPos();
281+
lcd.print(F(" "));
282+
setCursorPos();
283+
}
284+
break;
285+
case CAPSLOCK_ON:
286+
case CAPSLOCK_OFF:
287+
case NUMLOCK_ON:
288+
case NUMLOCK_OFF:
289+
displayInputMode();
290+
break;
291+
default:
292+
if (cursorPos == endCursorPos)
293+
isEndOfDisplay = true;
294+
lcd.print(chr);
295+
incCursorPos();
296+
setCursorPos();
297+
}
298+
}
299+
300+
void displayInputMode(void) {
301+
lcd.setCursor(13, 0);
302+
if (alphaMode) {
303+
if (upperCaseMode)
304+
lcd.print(F("ABC"));
305+
else
306+
lcd.print(F("abc"));
307+
}
308+
else
309+
lcd.print(F("123"));
310+
setCursorPos();
311+
}
312+
313+
void incCursorPos(void) {
314+
if (cursorPos < endCursorPos) cursorPos++;
315+
}
316+
317+
void decCursorPos( void ) {
318+
if (cursorPos > startCursorPos) cursorPos--;
319+
}
320+
321+
void setCursorPos( void ) {
322+
lcd.setCursor(cursorPos % 16, cursorPos / 16);
323+
}
324+
325+
byte getSymbol(byte ctr) {
326+
byte sym = pgm_read_byte_near(SYMBOL + ctr);
327+
if (sym == CHR_BOUND) {
328+
sym = pgm_read_byte_near(SYMBOL);
329+
kpd.resetTapCounter();
330+
}
331+
return sym;
332+
}
333+
334+
byte getAlphabet(byte chr, byte ctr) {
335+
chr = (chr - '2') * 5;
336+
byte alpha = pgm_read_byte_near(ALPHABET + chr + ctr);
337+
if (alpha == CHR_BOUND) {
338+
alpha = pgm_read_byte_near(ALPHABET + chr);
339+
kpd.resetTapCounter();
340+
}
341+
return alpha;
342+
}
343+
29.5 KB
Loading
16.7 KB
Loading
15.4 KB
Loading

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MultitapKeypad
2-
version=1.0.0
2+
version=1.0.1
33
author=ZulNs
44
maintainer=ZulNs <zul.nasibu@gmail.com>
55
sentence=A library (driver) for matrix 4x3 or 4x4 keypad supports multi-tap and long-tap.

0 commit comments

Comments
 (0)