Demo of the RA8875 Keypad to enter a username and password.
Dependencies: mbed RA8875 Keypad
main.cpp@2:117fb9168afc, 2016-03-05 (annotated)
- Committer:
- WiredHome
- Date:
- Sat Mar 05 16:23:10 2016 +0000
- Revision:
- 2:117fb9168afc
- Parent:
- 1:0fea662d1826
- Child:
- 3:954431ea9b0d
Keypad Demo shows QWERTY, password entry, user defined numeric style, floating (to your choice of location on the screen) and compressed, where it sizes the keys based on your settings.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 0:5cf0d5f4ab84 | 1 | #include "mbed.h" // v112 |
WiredHome | 0:5cf0d5f4ab84 | 2 | #include "RA8875.h" |
WiredHome | 0:5cf0d5f4ab84 | 3 | #include "Keypad.h" |
WiredHome | 0:5cf0d5f4ab84 | 4 | |
WiredHome | 0:5cf0d5f4ab84 | 5 | LocalFileSystem local("local"); // Because I want <PrintScreen> |
WiredHome | 0:5cf0d5f4ab84 | 6 | Serial pc(USBTX, USBRX); // And a little feedback |
WiredHome | 0:5cf0d5f4ab84 | 7 | |
WiredHome | 0:5cf0d5f4ab84 | 8 | RA8875 lcd(p5, p6, p7, p12, NC, "tft"); // MOSI, MISO, SCK, /ChipSelect, /reset, name |
WiredHome | 0:5cf0d5f4ab84 | 9 | Keypad kp(lcd); |
WiredHome | 0:5cf0d5f4ab84 | 10 | |
WiredHome | 1:0fea662d1826 | 11 | // 789 /( |
WiredHome | 1:0fea662d1826 | 12 | // 456 *) |
WiredHome | 1:0fea662d1826 | 13 | // 123 - |
WiredHome | 1:0fea662d1826 | 14 | // 00. += |
WiredHome | 1:0fea662d1826 | 15 | const char numberkeys[] = { |
WiredHome | 1:0fea662d1826 | 16 | 5,'\x01', 10,'7',10,'8',10,'9', 5,'\x01', 10,'/',10,'(', 0,0, |
WiredHome | 1:0fea662d1826 | 17 | 5,'\x01', 10,'4',10,'5',10,'6', 5,'\x01', 10,'*',10,')', 0,0, |
WiredHome | 1:0fea662d1826 | 18 | 5,'\x01', 10,'1',10,'2',10,'3', 5,'\x01', 10,'-',10,KYBD_SYM_BS, 0,0, |
WiredHome | 1:0fea662d1826 | 19 | 5,'\x01', 20,'0', 10,'.', 5,'\x01', 10,'+',10,'=', 0,0, |
WiredHome | 1:0fea662d1826 | 20 | 0,0 |
WiredHome | 1:0fea662d1826 | 21 | }; |
WiredHome | 1:0fea662d1826 | 22 | |
WiredHome | 1:0fea662d1826 | 23 | const Keypad::keyboard_t altkeyboard = { |
WiredHome | 1:0fea662d1826 | 24 | 100, // x=100; left edge |
WiredHome | 1:0fea662d1826 | 25 | 0, // y=0; computed from bottom up |
WiredHome | 1:0fea662d1826 | 26 | 240, // width=240 |
WiredHome | 1:0fea662d1826 | 27 | 0, // height=0; bottom of screen justified |
WiredHome | 1:0fea662d1826 | 28 | 4, // rows |
WiredHome | 1:0fea662d1826 | 29 | 6, // columns |
WiredHome | 1:0fea662d1826 | 30 | numberkeys, // pointer to the keypad |
WiredHome | 1:0fea662d1826 | 31 | numberkeys |
WiredHome | 1:0fea662d1826 | 32 | }; |
WiredHome | 1:0fea662d1826 | 33 | |
WiredHome | 0:5cf0d5f4ab84 | 34 | |
WiredHome | 2:117fb9168afc | 35 | |
WiredHome | 0:5cf0d5f4ab84 | 36 | void CalibrateTS(void) |
WiredHome | 0:5cf0d5f4ab84 | 37 | { |
WiredHome | 0:5cf0d5f4ab84 | 38 | FILE * fh; |
WiredHome | 0:5cf0d5f4ab84 | 39 | tpMatrix_t matrix; |
WiredHome | 0:5cf0d5f4ab84 | 40 | RetCode_t r; |
WiredHome | 0:5cf0d5f4ab84 | 41 | |
WiredHome | 0:5cf0d5f4ab84 | 42 | r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix); |
WiredHome | 0:5cf0d5f4ab84 | 43 | if (r == noerror) { |
WiredHome | 0:5cf0d5f4ab84 | 44 | fh = fopen("/local/tpcal.cfg", "wb"); |
WiredHome | 0:5cf0d5f4ab84 | 45 | if (fh) { |
WiredHome | 0:5cf0d5f4ab84 | 46 | fwrite(&matrix, sizeof(tpMatrix_t), 1, fh); |
WiredHome | 0:5cf0d5f4ab84 | 47 | fclose(fh); |
WiredHome | 0:5cf0d5f4ab84 | 48 | } else { |
WiredHome | 0:5cf0d5f4ab84 | 49 | } |
WiredHome | 0:5cf0d5f4ab84 | 50 | } else { |
WiredHome | 0:5cf0d5f4ab84 | 51 | } |
WiredHome | 0:5cf0d5f4ab84 | 52 | } |
WiredHome | 0:5cf0d5f4ab84 | 53 | |
WiredHome | 0:5cf0d5f4ab84 | 54 | |
WiredHome | 0:5cf0d5f4ab84 | 55 | void InitTS(void) |
WiredHome | 0:5cf0d5f4ab84 | 56 | { |
WiredHome | 0:5cf0d5f4ab84 | 57 | FILE * fh; |
WiredHome | 0:5cf0d5f4ab84 | 58 | tpMatrix_t matrix; |
WiredHome | 0:5cf0d5f4ab84 | 59 | |
WiredHome | 0:5cf0d5f4ab84 | 60 | fh = fopen("/local/tpcal.cfg", "rb"); |
WiredHome | 0:5cf0d5f4ab84 | 61 | if (fh) { |
WiredHome | 0:5cf0d5f4ab84 | 62 | fread(&matrix, sizeof(tpMatrix_t), 1, fh); |
WiredHome | 0:5cf0d5f4ab84 | 63 | fclose(fh); |
WiredHome | 0:5cf0d5f4ab84 | 64 | lcd.TouchPanelSetMatrix(&matrix); |
WiredHome | 0:5cf0d5f4ab84 | 65 | pc.printf("Touch Panel calibration set\r\n"); |
WiredHome | 0:5cf0d5f4ab84 | 66 | } else { |
WiredHome | 0:5cf0d5f4ab84 | 67 | CalibrateTS(); |
WiredHome | 0:5cf0d5f4ab84 | 68 | } |
WiredHome | 0:5cf0d5f4ab84 | 69 | } |
WiredHome | 0:5cf0d5f4ab84 | 70 | |
WiredHome | 2:117fb9168afc | 71 | |
WiredHome | 2:117fb9168afc | 72 | void UsernameAndPasswordTest(void) { |
WiredHome | 2:117fb9168afc | 73 | char name1[20]; |
WiredHome | 2:117fb9168afc | 74 | char name2[20]; |
WiredHome | 2:117fb9168afc | 75 | |
WiredHome | 2:117fb9168afc | 76 | kp.SetKeyboard(); |
WiredHome | 2:117fb9168afc | 77 | lcd.puts(0,20, "Enter username and password\r\n"); |
WiredHome | 2:117fb9168afc | 78 | if (kp.GetString(name1, sizeof(name1), "Username:")) { |
WiredHome | 2:117fb9168afc | 79 | //lcd.printf("username: %s\r\n", name1); |
WiredHome | 2:117fb9168afc | 80 | if (kp.GetString(name2, sizeof(name2), "Password:", '*')) { |
WiredHome | 2:117fb9168afc | 81 | //lcd.printf("password: %s\r\n", name2); |
WiredHome | 2:117fb9168afc | 82 | kp.Erase(); |
WiredHome | 2:117fb9168afc | 83 | lcd.foreground(BrightRed); |
WiredHome | 2:117fb9168afc | 84 | lcd.background(Black); |
WiredHome | 2:117fb9168afc | 85 | lcd.cls(); |
WiredHome | 2:117fb9168afc | 86 | lcd.SetTextFontSize(2); |
WiredHome | 2:117fb9168afc | 87 | lcd.SetTextCursor(0,30); |
WiredHome | 2:117fb9168afc | 88 | lcd.printf("username: %s\r\npassword: %s\r\n", name1, name2); |
WiredHome | 2:117fb9168afc | 89 | lcd.SetTextFontSize(); |
WiredHome | 2:117fb9168afc | 90 | } |
WiredHome | 2:117fb9168afc | 91 | } else { |
WiredHome | 2:117fb9168afc | 92 | kp.Erase(); |
WiredHome | 2:117fb9168afc | 93 | pc.printf("<esc>\r\n"); |
WiredHome | 2:117fb9168afc | 94 | } |
WiredHome | 2:117fb9168afc | 95 | } |
WiredHome | 2:117fb9168afc | 96 | |
WiredHome | 2:117fb9168afc | 97 | |
WiredHome | 2:117fb9168afc | 98 | void CalculatorKeypadTest(void) { |
WiredHome | 2:117fb9168afc | 99 | char name1[20]; |
WiredHome | 2:117fb9168afc | 100 | |
WiredHome | 2:117fb9168afc | 101 | kp.SetKeyboard(&altkeyboard, '='); |
WiredHome | 2:117fb9168afc | 102 | if (kp.GetString(name1, sizeof(name1), "Calc:")) { |
WiredHome | 2:117fb9168afc | 103 | lcd.foreground(BrightRed); |
WiredHome | 2:117fb9168afc | 104 | lcd.background(Black); |
WiredHome | 2:117fb9168afc | 105 | lcd.cls(); |
WiredHome | 2:117fb9168afc | 106 | lcd.SetTextCursor(0,40); |
WiredHome | 2:117fb9168afc | 107 | lcd.printf("Calculator: %s\r\n", name1); |
WiredHome | 2:117fb9168afc | 108 | } |
WiredHome | 2:117fb9168afc | 109 | } |
WiredHome | 2:117fb9168afc | 110 | |
WiredHome | 2:117fb9168afc | 111 | void FloatingSmallQWERTYTest(loc_t x, loc_t y, dim_t w, dim_t h) { |
WiredHome | 2:117fb9168afc | 112 | Keypad::keyboard_t tiny; |
WiredHome | 2:117fb9168afc | 113 | char name1[10]; |
WiredHome | 2:117fb9168afc | 114 | |
WiredHome | 2:117fb9168afc | 115 | // copy definition and then resize it |
WiredHome | 2:117fb9168afc | 116 | memcpy(&tiny, kp.GetInternalKeypad(), sizeof(Keypad::keyboard_t)); |
WiredHome | 2:117fb9168afc | 117 | tiny.x = x; tiny.y = y; |
WiredHome | 2:117fb9168afc | 118 | tiny.width = w; tiny.height=h; |
WiredHome | 2:117fb9168afc | 119 | |
WiredHome | 2:117fb9168afc | 120 | // now select this tiny keyboard |
WiredHome | 2:117fb9168afc | 121 | kp.SetKeyboard(&tiny); |
WiredHome | 2:117fb9168afc | 122 | if (kp.GetString(name1, sizeof(name1), "Cprs:")) { |
WiredHome | 2:117fb9168afc | 123 | lcd.foreground(BrightRed); |
WiredHome | 2:117fb9168afc | 124 | lcd.background(Black); |
WiredHome | 2:117fb9168afc | 125 | lcd.cls(); |
WiredHome | 2:117fb9168afc | 126 | lcd.SetTextCursor(0,40); |
WiredHome | 2:117fb9168afc | 127 | lcd.printf("Compressed: %s\r\n", name1); |
WiredHome | 2:117fb9168afc | 128 | } |
WiredHome | 2:117fb9168afc | 129 | } |
WiredHome | 2:117fb9168afc | 130 | |
WiredHome | 2:117fb9168afc | 131 | |
WiredHome | 2:117fb9168afc | 132 | |
WiredHome | 2:117fb9168afc | 133 | |
WiredHome | 0:5cf0d5f4ab84 | 134 | int main() |
WiredHome | 0:5cf0d5f4ab84 | 135 | { |
WiredHome | 2:117fb9168afc | 136 | int testNum = 2; // starting test |
WiredHome | 0:5cf0d5f4ab84 | 137 | |
WiredHome | 0:5cf0d5f4ab84 | 138 | pc.baud(460800); // I like a snappy terminal, so crank it up! |
WiredHome | 0:5cf0d5f4ab84 | 139 | pc.printf("\r\nDev Keypad - Build " __DATE__ " " __TIME__ "\r\n"); |
WiredHome | 0:5cf0d5f4ab84 | 140 | |
WiredHome | 0:5cf0d5f4ab84 | 141 | lcd.init(); |
WiredHome | 0:5cf0d5f4ab84 | 142 | InitTS(); |
WiredHome | 0:5cf0d5f4ab84 | 143 | while(1) { |
WiredHome | 0:5cf0d5f4ab84 | 144 | lcd.foreground(Yellow); |
WiredHome | 0:5cf0d5f4ab84 | 145 | lcd.background(Black); |
WiredHome | 0:5cf0d5f4ab84 | 146 | lcd.cls(); |
WiredHome | 2:117fb9168afc | 147 | lcd.puts(0,0, "Dev Keypad - Build " __DATE__ " " __TIME__ "\r\n"); |
WiredHome | 2:117fb9168afc | 148 | switch (testNum) { |
WiredHome | 2:117fb9168afc | 149 | default: |
WiredHome | 2:117fb9168afc | 150 | case 0: |
WiredHome | 2:117fb9168afc | 151 | testNum = 0; // capture the overflow and start over... |
WiredHome | 2:117fb9168afc | 152 | UsernameAndPasswordTest(); |
WiredHome | 2:117fb9168afc | 153 | break; |
WiredHome | 2:117fb9168afc | 154 | case 1: |
WiredHome | 2:117fb9168afc | 155 | CalculatorKeypadTest(); |
WiredHome | 2:117fb9168afc | 156 | break; |
WiredHome | 2:117fb9168afc | 157 | case 2: |
WiredHome | 2:117fb9168afc | 158 | FloatingSmallQWERTYTest(50,0, 200,0); // horizontally in by 50, width 200, extend to bottom of screen |
WiredHome | 2:117fb9168afc | 159 | break; |
WiredHome | 2:117fb9168afc | 160 | case 3: |
WiredHome | 2:117fb9168afc | 161 | FloatingSmallQWERTYTest(75, 100, 220, 6 * (lcd.fontheight()+4)); // floating with the top at x=75, y=100 |
WiredHome | 2:117fb9168afc | 162 | break; |
WiredHome | 0:5cf0d5f4ab84 | 163 | } |
WiredHome | 2:117fb9168afc | 164 | testNum++; |
WiredHome | 0:5cf0d5f4ab84 | 165 | wait(5); |
WiredHome | 0:5cf0d5f4ab84 | 166 | } |
WiredHome | 0:5cf0d5f4ab84 | 167 | } |
WiredHome | 2:117fb9168afc | 168 |