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

Dependencies:   mbed RA8875 Keypad

Committer:
WiredHome
Date:
Sun Jul 28 03:02:11 2019 +0000
Revision:
6:e075e76b8123
Parent:
4:8c1932fcd628
Update RA lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 4:8c1932fcd628 1 #include "mbed.h" // v122
WiredHome 4:8c1932fcd628 2 #include "RA8875.h" // v127
WiredHome 4:8c1932fcd628 3 #include "Keypad.h" // v4
WiredHome 4:8c1932fcd628 4
WiredHome 4:8c1932fcd628 5 // These two defines can be enabled, or commented out
WiredHome 4:8c1932fcd628 6 #define BIG_SCREEN
WiredHome 4:8c1932fcd628 7 #define CAP_TOUCH
WiredHome 4:8c1932fcd628 8 #define LCD_C 16 // color - bits per pixel
WiredHome 0:5cf0d5f4ab84 9
WiredHome 4:8c1932fcd628 10 #ifdef CAP_TOUCH
WiredHome 4:8c1932fcd628 11 RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // MOSI,MISO,SCK,/ChipSelect,/reset, SDA,SCL,/IRQ, name
WiredHome 4:8c1932fcd628 12 #else
WiredHome 4:8c1932fcd628 13 RA8875 lcd(p5, p6, p7, p12, NC, "tft"); //MOSI, MISO, SCK, /ChipSelect, /reset, name
WiredHome 4:8c1932fcd628 14 LocalFileSystem local("local"); // access to calibration file for resistive touch.
WiredHome 4:8c1932fcd628 15 #endif
WiredHome 4:8c1932fcd628 16
WiredHome 4:8c1932fcd628 17 #ifdef BIG_SCREEN
WiredHome 4:8c1932fcd628 18 #define LCD_W 800
WiredHome 4:8c1932fcd628 19 #define LCD_H 480
WiredHome 4:8c1932fcd628 20 #else
WiredHome 4:8c1932fcd628 21 #define LCD_W 480
WiredHome 4:8c1932fcd628 22 #define LCD_H 272
WiredHome 4:8c1932fcd628 23 #endif
WiredHome 4:8c1932fcd628 24
WiredHome 0:5cf0d5f4ab84 25 Serial pc(USBTX, USBRX); // And a little feedback
WiredHome 0:5cf0d5f4ab84 26 Keypad kp(lcd);
WiredHome 0:5cf0d5f4ab84 27
WiredHome 4:8c1932fcd628 28 // Define a keyboard layout as a calculator-like keypad
WiredHome 1:0fea662d1826 29 // 789 /(
WiredHome 1:0fea662d1826 30 // 456 *)
WiredHome 1:0fea662d1826 31 // 123 -
WiredHome 1:0fea662d1826 32 // 00. +=
WiredHome 1:0fea662d1826 33 const char numberkeys[] = {
WiredHome 1:0fea662d1826 34 5,'\x01', 10,'7',10,'8',10,'9', 5,'\x01', 10,'/',10,'(', 0,0,
WiredHome 1:0fea662d1826 35 5,'\x01', 10,'4',10,'5',10,'6', 5,'\x01', 10,'*',10,')', 0,0,
WiredHome 1:0fea662d1826 36 5,'\x01', 10,'1',10,'2',10,'3', 5,'\x01', 10,'-',10,KYBD_SYM_BS, 0,0,
WiredHome 1:0fea662d1826 37 5,'\x01', 20,'0', 10,'.', 5,'\x01', 10,'+',10,'=', 0,0,
WiredHome 1:0fea662d1826 38 0,0
WiredHome 1:0fea662d1826 39 };
WiredHome 1:0fea662d1826 40
WiredHome 4:8c1932fcd628 41 // Define the implementation of that keyboard
WiredHome 1:0fea662d1826 42 const Keypad::keyboard_t altkeyboard = {
WiredHome 1:0fea662d1826 43 100, // x=100; left edge
WiredHome 1:0fea662d1826 44 0, // y=0; computed from bottom up
WiredHome 1:0fea662d1826 45 240, // width=240
WiredHome 1:0fea662d1826 46 0, // height=0; bottom of screen justified
WiredHome 1:0fea662d1826 47 4, // rows
WiredHome 1:0fea662d1826 48 6, // columns
WiredHome 1:0fea662d1826 49 numberkeys, // pointer to the keypad
WiredHome 1:0fea662d1826 50 numberkeys
WiredHome 1:0fea662d1826 51 };
WiredHome 1:0fea662d1826 52
WiredHome 0:5cf0d5f4ab84 53
WiredHome 2:117fb9168afc 54
WiredHome 0:5cf0d5f4ab84 55 void CalibrateTS(void)
WiredHome 0:5cf0d5f4ab84 56 {
WiredHome 0:5cf0d5f4ab84 57 FILE * fh;
WiredHome 0:5cf0d5f4ab84 58 tpMatrix_t matrix;
WiredHome 0:5cf0d5f4ab84 59 RetCode_t r;
WiredHome 0:5cf0d5f4ab84 60
WiredHome 0:5cf0d5f4ab84 61 r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
WiredHome 0:5cf0d5f4ab84 62 if (r == noerror) {
WiredHome 0:5cf0d5f4ab84 63 fh = fopen("/local/tpcal.cfg", "wb");
WiredHome 0:5cf0d5f4ab84 64 if (fh) {
WiredHome 0:5cf0d5f4ab84 65 fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 0:5cf0d5f4ab84 66 fclose(fh);
WiredHome 0:5cf0d5f4ab84 67 } else {
WiredHome 3:954431ea9b0d 68 lcd.printf("Cannot save calibration in /local/tpcal.cfg\r\n");
WiredHome 3:954431ea9b0d 69 wait_ms(1000);
WiredHome 0:5cf0d5f4ab84 70 }
WiredHome 0:5cf0d5f4ab84 71 } else {
WiredHome 3:954431ea9b0d 72 lcd.printf("TouchPanelCalibrate returned error code %d\r\n", r);
WiredHome 3:954431ea9b0d 73 wait_ms(2000);
WiredHome 0:5cf0d5f4ab84 74 }
WiredHome 0:5cf0d5f4ab84 75 }
WiredHome 0:5cf0d5f4ab84 76
WiredHome 0:5cf0d5f4ab84 77
WiredHome 0:5cf0d5f4ab84 78 void InitTS(void)
WiredHome 0:5cf0d5f4ab84 79 {
WiredHome 0:5cf0d5f4ab84 80 FILE * fh;
WiredHome 0:5cf0d5f4ab84 81 tpMatrix_t matrix;
WiredHome 0:5cf0d5f4ab84 82
WiredHome 0:5cf0d5f4ab84 83 fh = fopen("/local/tpcal.cfg", "rb");
WiredHome 0:5cf0d5f4ab84 84 if (fh) {
WiredHome 0:5cf0d5f4ab84 85 fread(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 0:5cf0d5f4ab84 86 fclose(fh);
WiredHome 0:5cf0d5f4ab84 87 lcd.TouchPanelSetMatrix(&matrix);
WiredHome 0:5cf0d5f4ab84 88 pc.printf("Touch Panel calibration set\r\n");
WiredHome 0:5cf0d5f4ab84 89 } else {
WiredHome 0:5cf0d5f4ab84 90 CalibrateTS();
WiredHome 0:5cf0d5f4ab84 91 }
WiredHome 0:5cf0d5f4ab84 92 }
WiredHome 0:5cf0d5f4ab84 93
WiredHome 2:117fb9168afc 94
WiredHome 2:117fb9168afc 95 void UsernameAndPasswordTest(void) {
WiredHome 2:117fb9168afc 96 char name1[20];
WiredHome 2:117fb9168afc 97 char name2[20];
WiredHome 2:117fb9168afc 98
WiredHome 2:117fb9168afc 99 kp.SetKeyboard();
WiredHome 4:8c1932fcd628 100 kp.SetKeyboardFont(0, 2); // big characters
WiredHome 2:117fb9168afc 101 lcd.puts(0,20, "Enter username and password\r\n");
WiredHome 2:117fb9168afc 102 if (kp.GetString(name1, sizeof(name1), "Username:")) {
WiredHome 2:117fb9168afc 103 //lcd.printf("username: %s\r\n", name1);
WiredHome 2:117fb9168afc 104 if (kp.GetString(name2, sizeof(name2), "Password:", '*')) {
WiredHome 2:117fb9168afc 105 //lcd.printf("password: %s\r\n", name2);
WiredHome 2:117fb9168afc 106 kp.Erase();
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.SetTextFontSize(2);
WiredHome 2:117fb9168afc 111 lcd.SetTextCursor(0,30);
WiredHome 2:117fb9168afc 112 lcd.printf("username: %s\r\npassword: %s\r\n", name1, name2);
WiredHome 2:117fb9168afc 113 lcd.SetTextFontSize();
WiredHome 2:117fb9168afc 114 }
WiredHome 2:117fb9168afc 115 } else {
WiredHome 4:8c1932fcd628 116 //kp.Erase();
WiredHome 2:117fb9168afc 117 pc.printf("<esc>\r\n");
WiredHome 2:117fb9168afc 118 }
WiredHome 2:117fb9168afc 119 }
WiredHome 2:117fb9168afc 120
WiredHome 2:117fb9168afc 121
WiredHome 2:117fb9168afc 122 void CalculatorKeypadTest(void) {
WiredHome 2:117fb9168afc 123 char name1[20];
WiredHome 2:117fb9168afc 124
WiredHome 4:8c1932fcd628 125 kp.SetKeyboard(&altkeyboard, '=', 0);
WiredHome 4:8c1932fcd628 126 kp.SetKeyboardFont(0, 4);
WiredHome 2:117fb9168afc 127 if (kp.GetString(name1, sizeof(name1), "Calc:")) {
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("Calculator: %s\r\n", name1);
WiredHome 2:117fb9168afc 133 }
WiredHome 2:117fb9168afc 134 }
WiredHome 2:117fb9168afc 135
WiredHome 2:117fb9168afc 136 void FloatingSmallQWERTYTest(loc_t x, loc_t y, dim_t w, dim_t h) {
WiredHome 2:117fb9168afc 137 Keypad::keyboard_t tiny;
WiredHome 2:117fb9168afc 138 char name1[10];
WiredHome 2:117fb9168afc 139
WiredHome 2:117fb9168afc 140 // copy definition and then resize it
WiredHome 3:954431ea9b0d 141 kp.SetKeyboard(); // select the internal keyboard
WiredHome 3:954431ea9b0d 142 memcpy(&tiny, kp.GetKeyboard(), sizeof(Keypad::keyboard_t));
WiredHome 2:117fb9168afc 143 tiny.x = x; tiny.y = y;
WiredHome 2:117fb9168afc 144 tiny.width = w; tiny.height=h;
WiredHome 2:117fb9168afc 145
WiredHome 2:117fb9168afc 146 // now select this tiny keyboard
WiredHome 2:117fb9168afc 147 kp.SetKeyboard(&tiny);
WiredHome 4:8c1932fcd628 148 kp.SetKeyboardFont(0, 1);
WiredHome 2:117fb9168afc 149 if (kp.GetString(name1, sizeof(name1), "Cprs:")) {
WiredHome 2:117fb9168afc 150 lcd.foreground(BrightRed);
WiredHome 2:117fb9168afc 151 lcd.background(Black);
WiredHome 2:117fb9168afc 152 lcd.cls();
WiredHome 2:117fb9168afc 153 lcd.SetTextCursor(0,40);
WiredHome 2:117fb9168afc 154 lcd.printf("Compressed: %s\r\n", name1);
WiredHome 2:117fb9168afc 155 }
WiredHome 2:117fb9168afc 156 }
WiredHome 2:117fb9168afc 157
WiredHome 2:117fb9168afc 158
WiredHome 2:117fb9168afc 159
WiredHome 2:117fb9168afc 160
WiredHome 0:5cf0d5f4ab84 161 int main()
WiredHome 0:5cf0d5f4ab84 162 {
WiredHome 4:8c1932fcd628 163 int testNum = 0; // starting test
WiredHome 0:5cf0d5f4ab84 164
WiredHome 0:5cf0d5f4ab84 165 pc.baud(460800); // I like a snappy terminal, so crank it up!
WiredHome 0:5cf0d5f4ab84 166 pc.printf("\r\nDev Keypad - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 0:5cf0d5f4ab84 167
WiredHome 4:8c1932fcd628 168 lcd.init(LCD_W, LCD_H, LCD_C);
WiredHome 4:8c1932fcd628 169 lcd.Backlight(0.5f);
WiredHome 4:8c1932fcd628 170
WiredHome 4:8c1932fcd628 171 #ifndef CAP_TOUCH
WiredHome 0:5cf0d5f4ab84 172 InitTS();
WiredHome 4:8c1932fcd628 173 #endif
WiredHome 0:5cf0d5f4ab84 174 while(1) {
WiredHome 0:5cf0d5f4ab84 175 lcd.foreground(Yellow);
WiredHome 0:5cf0d5f4ab84 176 lcd.background(Black);
WiredHome 0:5cf0d5f4ab84 177 lcd.cls();
WiredHome 2:117fb9168afc 178 lcd.puts(0,0, "Dev Keypad - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 2:117fb9168afc 179 switch (testNum) {
WiredHome 2:117fb9168afc 180 default:
WiredHome 2:117fb9168afc 181 case 0:
WiredHome 2:117fb9168afc 182 testNum = 0; // capture the overflow and start over...
WiredHome 2:117fb9168afc 183 UsernameAndPasswordTest();
WiredHome 2:117fb9168afc 184 break;
WiredHome 2:117fb9168afc 185 case 1:
WiredHome 2:117fb9168afc 186 CalculatorKeypadTest();
WiredHome 2:117fb9168afc 187 break;
WiredHome 2:117fb9168afc 188 case 2:
WiredHome 2:117fb9168afc 189 FloatingSmallQWERTYTest(50,0, 200,0); // horizontally in by 50, width 200, extend to bottom of screen
WiredHome 2:117fb9168afc 190 break;
WiredHome 2:117fb9168afc 191 case 3:
WiredHome 2:117fb9168afc 192 FloatingSmallQWERTYTest(75, 100, 220, 6 * (lcd.fontheight()+4)); // floating with the top at x=75, y=100
WiredHome 2:117fb9168afc 193 break;
WiredHome 0:5cf0d5f4ab84 194 }
WiredHome 2:117fb9168afc 195 testNum++;
WiredHome 0:5cf0d5f4ab84 196 wait(5);
WiredHome 0:5cf0d5f4ab84 197 }
WiredHome 0:5cf0d5f4ab84 198 }