Demo of the RA8875 Keypad to enter a username and password.

Dependencies:   mbed RA8875 Keypad

Committer:
WiredHome
Date:
Sat Mar 05 16:50:26 2016 +0000
Revision:
3:954431ea9b0d
Parent:
2:117fb9168afc
Child:
4:8c1932fcd628
improve error messages in demo when touch calibration fails. Update for one keypad API that was unnecessary, and eliminated.

Who changed what in which revision?

UserRevisionLine numberNew 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 3:954431ea9b0d 49 lcd.printf("Cannot save calibration in /local/tpcal.cfg\r\n");
WiredHome 3:954431ea9b0d 50 wait_ms(1000);
WiredHome 0:5cf0d5f4ab84 51 }
WiredHome 0:5cf0d5f4ab84 52 } else {
WiredHome 3:954431ea9b0d 53 lcd.printf("TouchPanelCalibrate returned error code %d\r\n", r);
WiredHome 3:954431ea9b0d 54 wait_ms(2000);
WiredHome 0:5cf0d5f4ab84 55 }
WiredHome 0:5cf0d5f4ab84 56 }
WiredHome 0:5cf0d5f4ab84 57
WiredHome 0:5cf0d5f4ab84 58
WiredHome 0:5cf0d5f4ab84 59 void InitTS(void)
WiredHome 0:5cf0d5f4ab84 60 {
WiredHome 0:5cf0d5f4ab84 61 FILE * fh;
WiredHome 0:5cf0d5f4ab84 62 tpMatrix_t matrix;
WiredHome 0:5cf0d5f4ab84 63
WiredHome 0:5cf0d5f4ab84 64 fh = fopen("/local/tpcal.cfg", "rb");
WiredHome 0:5cf0d5f4ab84 65 if (fh) {
WiredHome 0:5cf0d5f4ab84 66 fread(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 0:5cf0d5f4ab84 67 fclose(fh);
WiredHome 0:5cf0d5f4ab84 68 lcd.TouchPanelSetMatrix(&matrix);
WiredHome 0:5cf0d5f4ab84 69 pc.printf("Touch Panel calibration set\r\n");
WiredHome 0:5cf0d5f4ab84 70 } else {
WiredHome 0:5cf0d5f4ab84 71 CalibrateTS();
WiredHome 0:5cf0d5f4ab84 72 }
WiredHome 0:5cf0d5f4ab84 73 }
WiredHome 0:5cf0d5f4ab84 74
WiredHome 2:117fb9168afc 75
WiredHome 2:117fb9168afc 76 void UsernameAndPasswordTest(void) {
WiredHome 2:117fb9168afc 77 char name1[20];
WiredHome 2:117fb9168afc 78 char name2[20];
WiredHome 2:117fb9168afc 79
WiredHome 2:117fb9168afc 80 kp.SetKeyboard();
WiredHome 2:117fb9168afc 81 lcd.puts(0,20, "Enter username and password\r\n");
WiredHome 2:117fb9168afc 82 if (kp.GetString(name1, sizeof(name1), "Username:")) {
WiredHome 2:117fb9168afc 83 //lcd.printf("username: %s\r\n", name1);
WiredHome 2:117fb9168afc 84 if (kp.GetString(name2, sizeof(name2), "Password:", '*')) {
WiredHome 2:117fb9168afc 85 //lcd.printf("password: %s\r\n", name2);
WiredHome 2:117fb9168afc 86 kp.Erase();
WiredHome 2:117fb9168afc 87 lcd.foreground(BrightRed);
WiredHome 2:117fb9168afc 88 lcd.background(Black);
WiredHome 2:117fb9168afc 89 lcd.cls();
WiredHome 2:117fb9168afc 90 lcd.SetTextFontSize(2);
WiredHome 2:117fb9168afc 91 lcd.SetTextCursor(0,30);
WiredHome 2:117fb9168afc 92 lcd.printf("username: %s\r\npassword: %s\r\n", name1, name2);
WiredHome 2:117fb9168afc 93 lcd.SetTextFontSize();
WiredHome 2:117fb9168afc 94 }
WiredHome 2:117fb9168afc 95 } else {
WiredHome 2:117fb9168afc 96 kp.Erase();
WiredHome 2:117fb9168afc 97 pc.printf("<esc>\r\n");
WiredHome 2:117fb9168afc 98 }
WiredHome 2:117fb9168afc 99 }
WiredHome 2:117fb9168afc 100
WiredHome 2:117fb9168afc 101
WiredHome 2:117fb9168afc 102 void CalculatorKeypadTest(void) {
WiredHome 2:117fb9168afc 103 char name1[20];
WiredHome 2:117fb9168afc 104
WiredHome 2:117fb9168afc 105 kp.SetKeyboard(&altkeyboard, '=');
WiredHome 2:117fb9168afc 106 if (kp.GetString(name1, sizeof(name1), "Calc:")) {
WiredHome 2:117fb9168afc 107 lcd.foreground(BrightRed);
WiredHome 2:117fb9168afc 108 lcd.background(Black);
WiredHome 2:117fb9168afc 109 lcd.cls();
WiredHome 2:117fb9168afc 110 lcd.SetTextCursor(0,40);
WiredHome 2:117fb9168afc 111 lcd.printf("Calculator: %s\r\n", name1);
WiredHome 2:117fb9168afc 112 }
WiredHome 2:117fb9168afc 113 }
WiredHome 2:117fb9168afc 114
WiredHome 2:117fb9168afc 115 void FloatingSmallQWERTYTest(loc_t x, loc_t y, dim_t w, dim_t h) {
WiredHome 2:117fb9168afc 116 Keypad::keyboard_t tiny;
WiredHome 2:117fb9168afc 117 char name1[10];
WiredHome 2:117fb9168afc 118
WiredHome 2:117fb9168afc 119 // copy definition and then resize it
WiredHome 3:954431ea9b0d 120 kp.SetKeyboard(); // select the internal keyboard
WiredHome 3:954431ea9b0d 121 memcpy(&tiny, kp.GetKeyboard(), sizeof(Keypad::keyboard_t));
WiredHome 2:117fb9168afc 122 tiny.x = x; tiny.y = y;
WiredHome 2:117fb9168afc 123 tiny.width = w; tiny.height=h;
WiredHome 2:117fb9168afc 124
WiredHome 2:117fb9168afc 125 // now select this tiny keyboard
WiredHome 2:117fb9168afc 126 kp.SetKeyboard(&tiny);
WiredHome 2:117fb9168afc 127 if (kp.GetString(name1, sizeof(name1), "Cprs:")) {
WiredHome 2:117fb9168afc 128 lcd.foreground(BrightRed);
WiredHome 2:117fb9168afc 129 lcd.background(Black);
WiredHome 2:117fb9168afc 130 lcd.cls();
WiredHome 2:117fb9168afc 131 lcd.SetTextCursor(0,40);
WiredHome 2:117fb9168afc 132 lcd.printf("Compressed: %s\r\n", name1);
WiredHome 2:117fb9168afc 133 }
WiredHome 2:117fb9168afc 134 }
WiredHome 2:117fb9168afc 135
WiredHome 2:117fb9168afc 136
WiredHome 2:117fb9168afc 137
WiredHome 2:117fb9168afc 138
WiredHome 0:5cf0d5f4ab84 139 int main()
WiredHome 0:5cf0d5f4ab84 140 {
WiredHome 2:117fb9168afc 141 int testNum = 2; // starting test
WiredHome 0:5cf0d5f4ab84 142
WiredHome 0:5cf0d5f4ab84 143 pc.baud(460800); // I like a snappy terminal, so crank it up!
WiredHome 0:5cf0d5f4ab84 144 pc.printf("\r\nDev Keypad - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 0:5cf0d5f4ab84 145
WiredHome 0:5cf0d5f4ab84 146 lcd.init();
WiredHome 0:5cf0d5f4ab84 147 InitTS();
WiredHome 0:5cf0d5f4ab84 148 while(1) {
WiredHome 0:5cf0d5f4ab84 149 lcd.foreground(Yellow);
WiredHome 0:5cf0d5f4ab84 150 lcd.background(Black);
WiredHome 0:5cf0d5f4ab84 151 lcd.cls();
WiredHome 2:117fb9168afc 152 lcd.puts(0,0, "Dev Keypad - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 2:117fb9168afc 153 switch (testNum) {
WiredHome 2:117fb9168afc 154 default:
WiredHome 2:117fb9168afc 155 case 0:
WiredHome 2:117fb9168afc 156 testNum = 0; // capture the overflow and start over...
WiredHome 2:117fb9168afc 157 UsernameAndPasswordTest();
WiredHome 2:117fb9168afc 158 break;
WiredHome 2:117fb9168afc 159 case 1:
WiredHome 2:117fb9168afc 160 CalculatorKeypadTest();
WiredHome 2:117fb9168afc 161 break;
WiredHome 2:117fb9168afc 162 case 2:
WiredHome 2:117fb9168afc 163 FloatingSmallQWERTYTest(50,0, 200,0); // horizontally in by 50, width 200, extend to bottom of screen
WiredHome 2:117fb9168afc 164 break;
WiredHome 2:117fb9168afc 165 case 3:
WiredHome 2:117fb9168afc 166 FloatingSmallQWERTYTest(75, 100, 220, 6 * (lcd.fontheight()+4)); // floating with the top at x=75, y=100
WiredHome 2:117fb9168afc 167 break;
WiredHome 0:5cf0d5f4ab84 168 }
WiredHome 2:117fb9168afc 169 testNum++;
WiredHome 0:5cf0d5f4ab84 170 wait(5);
WiredHome 0:5cf0d5f4ab84 171 }
WiredHome 0:5cf0d5f4ab84 172 }