hashem
Dependencies: mbed TextLCD keypad
main.cpp@0:593c5e55dd05, 2020-11-28 (annotated)
- Committer:
- hashemjs
- Date:
- Sat Nov 28 17:04:22 2020 +0000
- Revision:
- 0:593c5e55dd05
keypad_LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hashemjs | 0:593c5e55dd05 | 1 | #include "mbed.h" |
hashemjs | 0:593c5e55dd05 | 2 | #include "Keypad.h" |
hashemjs | 0:593c5e55dd05 | 3 | #include "TextLCD.h" |
hashemjs | 0:593c5e55dd05 | 4 | #include <string> |
hashemjs | 0:593c5e55dd05 | 5 | |
hashemjs | 0:593c5e55dd05 | 6 | #define PW_SIZE 5 |
hashemjs | 0:593c5e55dd05 | 7 | #define PASSWORD "1234A" |
hashemjs | 0:593c5e55dd05 | 8 | |
hashemjs | 0:593c5e55dd05 | 9 | Serial pc(USBTX, USBRX); |
hashemjs | 0:593c5e55dd05 | 10 | |
hashemjs | 0:593c5e55dd05 | 11 | //initialize LCD display |
hashemjs | 0:593c5e55dd05 | 12 | TextLCD lcd(p5, p7, p6, p11, p12, p13, p14, TextLCD::LCD16x2); // rs, rw, e, d0-d3 |
hashemjs | 0:593c5e55dd05 | 13 | |
hashemjs | 0:593c5e55dd05 | 14 | // Define keypad values |
hashemjs | 0:593c5e55dd05 | 15 | char Keytable[] = { '1', '2', '3', 'A', // r0 |
hashemjs | 0:593c5e55dd05 | 16 | '4', '5', '6', 'B', // r1 |
hashemjs | 0:593c5e55dd05 | 17 | '7', '8', '9', 'C', // r2 |
hashemjs | 0:593c5e55dd05 | 18 | '*', '0', '#', 'D' // r3 |
hashemjs | 0:593c5e55dd05 | 19 | }; |
hashemjs | 0:593c5e55dd05 | 20 | // c0 c1 c2 c3 |
hashemjs | 0:593c5e55dd05 | 21 | |
hashemjs | 0:593c5e55dd05 | 22 | //Initialize variables |
hashemjs | 0:593c5e55dd05 | 23 | uint32_t Index; |
hashemjs | 0:593c5e55dd05 | 24 | std::string pword; |
hashemjs | 0:593c5e55dd05 | 25 | const char * parray; |
hashemjs | 0:593c5e55dd05 | 26 | int psize=0; |
hashemjs | 0:593c5e55dd05 | 27 | |
hashemjs | 0:593c5e55dd05 | 28 | uint32_t cbAfterInput(uint32_t index) { |
hashemjs | 0:593c5e55dd05 | 29 | Index = index; |
hashemjs | 0:593c5e55dd05 | 30 | |
hashemjs | 0:593c5e55dd05 | 31 | //print entered value on LCD and save password in string |
hashemjs | 0:593c5e55dd05 | 32 | if(Keytable[Index] != '#' && Keytable[Index] != '*'){ |
hashemjs | 0:593c5e55dd05 | 33 | if(psize < PW_SIZE){ |
hashemjs | 0:593c5e55dd05 | 34 | pword.push_back(Keytable[Index]); |
hashemjs | 0:593c5e55dd05 | 35 | lcd.putc(Keytable[Index]); |
hashemjs | 0:593c5e55dd05 | 36 | parray = pword.c_str(); |
hashemjs | 0:593c5e55dd05 | 37 | psize++; |
hashemjs | 0:593c5e55dd05 | 38 | } |
hashemjs | 0:593c5e55dd05 | 39 | } |
hashemjs | 0:593c5e55dd05 | 40 | |
hashemjs | 0:593c5e55dd05 | 41 | if(Keytable[Index] == '*'){ |
hashemjs | 0:593c5e55dd05 | 42 | pword = ""; |
hashemjs | 0:593c5e55dd05 | 43 | lcd.cls(); |
hashemjs | 0:593c5e55dd05 | 44 | wait(0.001); |
hashemjs | 0:593c5e55dd05 | 45 | lcd.printf("Pass:"); |
hashemjs | 0:593c5e55dd05 | 46 | lcd.locate(0,1); |
hashemjs | 0:593c5e55dd05 | 47 | psize=0; |
hashemjs | 0:593c5e55dd05 | 48 | } |
hashemjs | 0:593c5e55dd05 | 49 | if(Keytable[Index] == '#'){ |
hashemjs | 0:593c5e55dd05 | 50 | lcd.cls(); |
hashemjs | 0:593c5e55dd05 | 51 | wait(0.001); |
hashemjs | 0:593c5e55dd05 | 52 | if(pword == PASSWORD) |
hashemjs | 0:593c5e55dd05 | 53 | lcd.printf("Correct"); |
hashemjs | 0:593c5e55dd05 | 54 | //lcd.printf("pword"); |
hashemjs | 0:593c5e55dd05 | 55 | //lcd.locate(0,1); |
hashemjs | 0:593c5e55dd05 | 56 | //lcd.printf(parray); |
hashemjs | 0:593c5e55dd05 | 57 | else |
hashemjs | 0:593c5e55dd05 | 58 | lcd.printf("Incorrect"); |
hashemjs | 0:593c5e55dd05 | 59 | } |
hashemjs | 0:593c5e55dd05 | 60 | return 0; |
hashemjs | 0:593c5e55dd05 | 61 | } |
hashemjs | 0:593c5e55dd05 | 62 | |
hashemjs | 0:593c5e55dd05 | 63 | |
hashemjs | 0:593c5e55dd05 | 64 | int main() { |
hashemjs | 0:593c5e55dd05 | 65 | |
hashemjs | 0:593c5e55dd05 | 66 | lcd.cls(); |
hashemjs | 0:593c5e55dd05 | 67 | wait(0.001); |
hashemjs | 0:593c5e55dd05 | 68 | lcd.printf("Pass:"); |
hashemjs | 0:593c5e55dd05 | 69 | lcd.locate(0,1); |
hashemjs | 0:593c5e55dd05 | 70 | |
hashemjs | 0:593c5e55dd05 | 71 | // r0 r1 r2 r3 c0 c1 c2 c3 |
hashemjs | 0:593c5e55dd05 | 72 | Keypad keypad(p28, p27, p26, p25, p21, p22, p23, p24); |
hashemjs | 0:593c5e55dd05 | 73 | keypad.attach(&cbAfterInput); |
hashemjs | 0:593c5e55dd05 | 74 | keypad.start(); // energize the keypad via c0-c3 |
hashemjs | 0:593c5e55dd05 | 75 | |
hashemjs | 0:593c5e55dd05 | 76 | while (1) { |
hashemjs | 0:593c5e55dd05 | 77 | __wfi(); |
hashemjs | 0:593c5e55dd05 | 78 | pc.printf("Interrupted\r\n"); |
hashemjs | 0:593c5e55dd05 | 79 | pc.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); |
hashemjs | 0:593c5e55dd05 | 80 | } |
hashemjs | 0:593c5e55dd05 | 81 | } |