QWERTY Keypad Touchscreen LCD Demo - MBED + SmartGPU2 board

Dependencies:   SMARTGPU2 mbed

Committer:
emmanuelchio
Date:
Fri Apr 18 01:04:14 2014 +0000
Revision:
2:2c1cb73e48d8
Parent:
1:5278445c0b86
SmartGPU2 Keypad_SG2 demo- Please select(uncomment) your smartGPU2 board under SMARTGPU2.h file before compiling!!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emmanuelchio 1:5278445c0b86 1 /**************************************************************************************/
emmanuelchio 0:71a5ba330e01 2 /**************************************************************************************/
emmanuelchio 0:71a5ba330e01 3 /*SMARTGPU2 intelligent embedded graphics processor unit
emmanuelchio 1:5278445c0b86 4 those examples are for using the SMARTGPU2 with the mbed microcontoller, just connect tx,rx,and reset
emmanuelchio 0:71a5ba330e01 5 Board:
emmanuelchio 1:5278445c0b86 6 http://www.vizictechnologies.com/
emmanuelchio 0:71a5ba330e01 7
emmanuelchio 0:71a5ba330e01 8 www.vizictechnologies.com
emmanuelchio 1:5278445c0b86 9 Vizic Technologies copyright 2014 */
emmanuelchio 1:5278445c0b86 10 /**************************************************************************************/
emmanuelchio 0:71a5ba330e01 11 /**************************************************************************************/
emmanuelchio 0:71a5ba330e01 12
emmanuelchio 0:71a5ba330e01 13 #include "mbed.h"
emmanuelchio 0:71a5ba330e01 14 #include "SMARTGPU2.h"
emmanuelchio 0:71a5ba330e01 15
emmanuelchio 0:71a5ba330e01 16 SMARTGPU2 lcd(TXPIN,RXPIN,RESETPIN); //create our object called "lcd"
emmanuelchio 0:71a5ba330e01 17
emmanuelchio 0:71a5ba330e01 18 //KEYBOARD DEFINITIONS
emmanuelchio 0:71a5ba330e01 19 #define LETTERSLOWER 0
emmanuelchio 0:71a5ba330e01 20 #define LETTERSUPPER 1
emmanuelchio 0:71a5ba330e01 21 #define NUMBERS 2
emmanuelchio 0:71a5ba330e01 22 #define SPECCHAR 3
emmanuelchio 2:2c1cb73e48d8 23 #define KEYBOARD_X_SIZE LCD_WIDTH
emmanuelchio 0:71a5ba330e01 24 #define KEYBOARD_Y_SIZE KEYBOARD_X_SIZE/2
emmanuelchio 0:71a5ba330e01 25 #define KEY_Y_TOP MAX_Y_LANDSCAPE - KEYBOARD_Y_SIZE //at the bottom of screen
emmanuelchio 0:71a5ba330e01 26 #define KEYXSIZE KEYBOARD_X_SIZE/10 //10 - columns
emmanuelchio 0:71a5ba330e01 27 #define KEYYSIZE KEYBOARD_Y_SIZE/4 //4 - rows
emmanuelchio 0:71a5ba330e01 28
emmanuelchio 0:71a5ba330e01 29 //keys definitions
emmanuelchio 0:71a5ba330e01 30 #define SPACE ' '
emmanuelchio 0:71a5ba330e01 31 #define OK 0x01
emmanuelchio 0:71a5ba330e01 32 #define DEL 0x02
emmanuelchio 0:71a5ba330e01 33 #define TYPE 0x03
emmanuelchio 0:71a5ba330e01 34 #define KEYCASE 0x04
emmanuelchio 0:71a5ba330e01 35
emmanuelchio 0:71a5ba330e01 36 //Keyboards type data, row1: 10 characters, row2: 9 characters, row3: 7 characters
emmanuelchio 0:71a5ba330e01 37 const char lettL[]= {"qwertyuiopasdfghjklzxcvbnm"}; //10-9-7 lower case
emmanuelchio 0:71a5ba330e01 38 const char lettU[]= {"QWERTYUIOPASDFGHJKLZXCVBNM"}; //10-9-7 upper case
emmanuelchio 0:71a5ba330e01 39 const char num []= {"1234567890-/:;()&@\"_.,?!'*"};//10-9-7
emmanuelchio 0:71a5ba330e01 40 const char spc []= {"[]{}#%^*+=:|~<>$-/\\_.,?!'*"};//10-9-7
emmanuelchio 0:71a5ba330e01 41 const char* keyboards[4]={lettL, lettU, num, spc};
emmanuelchio 0:71a5ba330e01 42
emmanuelchio 0:71a5ba330e01 43 /*********************************************************/
emmanuelchio 0:71a5ba330e01 44 void drawSingleKey(char key, char keyboardType, ACTIVE state){ //draws the received key as "state"(SELECTED or DESELECTED)
emmanuelchio 0:71a5ba330e01 45 unsigned int i=0;
emmanuelchio 0:71a5ba330e01 46 char *data = (char*)keyboards[keyboardType];
emmanuelchio 0:71a5ba330e01 47 char letter[2]={key,0};
emmanuelchio 0:71a5ba330e01 48
emmanuelchio 0:71a5ba330e01 49 //special case when key is ' '(space) or 0x01 "enter" or 0x02 "del" or 0x03 "type" or 0x04 "keycase"
emmanuelchio 0:71a5ba330e01 50 if(key == ' ' || key <= KEYCASE){
emmanuelchio 0:71a5ba330e01 51 switch(key){
emmanuelchio 0:71a5ba330e01 52 case ' ': //space
emmanuelchio 0:71a5ba330e01 53 lcd.objButton(KEYXSIZE+(KEYXSIZE/2), KEY_Y_TOP+(KEYYSIZE*3), (7*KEYXSIZE+(KEYXSIZE/2))+KEYXSIZE-1, KEY_Y_TOP+(KEYYSIZE*4)-1, state, "space");
emmanuelchio 0:71a5ba330e01 54 break;
emmanuelchio 0:71a5ba330e01 55 case OK: //OK
emmanuelchio 0:71a5ba330e01 56 lcd.objButton(8*KEYXSIZE+(KEYXSIZE/2), KEY_Y_TOP+(KEYYSIZE*3), KEYBOARD_X_SIZE-1, KEY_Y_TOP+(KEYYSIZE*4)-1, state, "OK");
emmanuelchio 0:71a5ba330e01 57 break;
emmanuelchio 0:71a5ba330e01 58 case DEL: //delete
emmanuelchio 0:71a5ba330e01 59 lcd.objButton(8*KEYXSIZE+(KEYXSIZE/2), KEY_Y_TOP+(KEYYSIZE*2), (8*KEYXSIZE+(KEYXSIZE/2))+KEYXSIZE-1, KEY_Y_TOP+(KEYYSIZE*3), state, "del");
emmanuelchio 0:71a5ba330e01 60 break;
emmanuelchio 0:71a5ba330e01 61 case TYPE: //keyboard type
emmanuelchio 0:71a5ba330e01 62 lcd.objButton(KEYXSIZE/2, KEY_Y_TOP+(KEYYSIZE*2), (KEYXSIZE/2)+KEYXSIZE-1, KEY_Y_TOP+(KEYYSIZE*3), state, "type");
emmanuelchio 0:71a5ba330e01 63 break;
emmanuelchio 0:71a5ba330e01 64 case KEYCASE: //letters upper/lower case
emmanuelchio 0:71a5ba330e01 65 lcd.objButton(0, KEY_Y_TOP+(KEYYSIZE*3), KEYXSIZE+(KEYXSIZE/2)-1, KEY_Y_TOP+(KEYYSIZE*4)-1, state, "case");
emmanuelchio 0:71a5ba330e01 66 break;
emmanuelchio 0:71a5ba330e01 67 default: //0x00 none
emmanuelchio 0:71a5ba330e01 68 break;
emmanuelchio 0:71a5ba330e01 69 }
emmanuelchio 0:71a5ba330e01 70 return;
emmanuelchio 0:71a5ba330e01 71 }
emmanuelchio 0:71a5ba330e01 72 //any other key case
emmanuelchio 0:71a5ba330e01 73 for(i=0;i<26;i++){ //search for the key in the received keyboardType data array
emmanuelchio 0:71a5ba330e01 74 if(key == data[i]) break;
emmanuelchio 0:71a5ba330e01 75 }
emmanuelchio 0:71a5ba330e01 76 if(i<10){
emmanuelchio 0:71a5ba330e01 77 lcd.objButton(i*KEYXSIZE, KEY_Y_TOP, (i*KEYXSIZE)+KEYXSIZE-1, KEY_Y_TOP+KEYYSIZE, state, letter);
emmanuelchio 0:71a5ba330e01 78 }else if(i<19){
emmanuelchio 0:71a5ba330e01 79 i-=10;
emmanuelchio 0:71a5ba330e01 80 lcd.objButton(i*KEYXSIZE+(KEYXSIZE/2), KEY_Y_TOP+KEYYSIZE, (i*KEYXSIZE+(KEYXSIZE/2))+KEYXSIZE-1, KEY_Y_TOP+(KEYYSIZE*2), state, letter);
emmanuelchio 0:71a5ba330e01 81 }else if(i<26){
emmanuelchio 0:71a5ba330e01 82 i-=18;
emmanuelchio 0:71a5ba330e01 83 lcd.objButton(i*KEYXSIZE+(KEYXSIZE/2), KEY_Y_TOP+(KEYYSIZE*2), (i*KEYXSIZE+(KEYXSIZE/2))+KEYXSIZE-1, KEY_Y_TOP+(KEYYSIZE*3), state, letter);
emmanuelchio 0:71a5ba330e01 84 }
emmanuelchio 0:71a5ba330e01 85 }
emmanuelchio 0:71a5ba330e01 86
emmanuelchio 0:71a5ba330e01 87 /*********************************************************/
emmanuelchio 0:71a5ba330e01 88 void drawAllKeyboard(char keyboardType){
emmanuelchio 0:71a5ba330e01 89 unsigned int i=0;
emmanuelchio 0:71a5ba330e01 90 char *data = (char*)keyboards[keyboardType];
emmanuelchio 0:71a5ba330e01 91
emmanuelchio 0:71a5ba330e01 92 for(i=0;i<26;i++){ //go through all keyboard data
emmanuelchio 0:71a5ba330e01 93 drawSingleKey(*data++, keyboardType, DESELECTED);
emmanuelchio 0:71a5ba330e01 94 }
emmanuelchio 0:71a5ba330e01 95 //draw special keys
emmanuelchio 0:71a5ba330e01 96 drawSingleKey(TYPE, keyboardType, DESELECTED);
emmanuelchio 0:71a5ba330e01 97 drawSingleKey(DEL, keyboardType, DESELECTED);
emmanuelchio 0:71a5ba330e01 98 drawSingleKey(KEYCASE, keyboardType, DESELECTED);
emmanuelchio 0:71a5ba330e01 99 drawSingleKey(SPACE, keyboardType, DESELECTED);
emmanuelchio 0:71a5ba330e01 100 drawSingleKey(OK, keyboardType, DESELECTED);
emmanuelchio 0:71a5ba330e01 101 }
emmanuelchio 0:71a5ba330e01 102
emmanuelchio 0:71a5ba330e01 103 /*********************************************************/
emmanuelchio 0:71a5ba330e01 104 char getKeyTouch(char keyboardType){ //ask for a touch and if VALID inside the keyboard returns the touched key
emmanuelchio 0:71a5ba330e01 105 char *data = (char*)keyboards[keyboardType];
emmanuelchio 0:71a5ba330e01 106 POINT p;
emmanuelchio 0:71a5ba330e01 107
emmanuelchio 0:71a5ba330e01 108 if(lcd.touchScreen(&p) == VALID){ //ask for touch, if VALID
emmanuelchio 0:71a5ba330e01 109 if(p.y > KEY_Y_TOP && p.y< (KEY_Y_TOP+KEYBOARD_Y_SIZE)){ //if touch inside keyboard
emmanuelchio 0:71a5ba330e01 110 p.y -= KEY_Y_TOP; //substract
emmanuelchio 0:71a5ba330e01 111 p.y /= (KEYBOARD_Y_SIZE/4); //obtain row
emmanuelchio 0:71a5ba330e01 112 //switch with the obtained row
emmanuelchio 0:71a5ba330e01 113 p.x--;
emmanuelchio 0:71a5ba330e01 114 switch(p.y){
emmanuelchio 0:71a5ba330e01 115 case 0: //1st row
emmanuelchio 0:71a5ba330e01 116 p.x /= KEYXSIZE; //obtain column
emmanuelchio 0:71a5ba330e01 117 break;
emmanuelchio 0:71a5ba330e01 118 case 1: //2nd row
emmanuelchio 0:71a5ba330e01 119 p.x -= (KEYXSIZE/2);
emmanuelchio 0:71a5ba330e01 120 p.x /= KEYXSIZE; //obtain column
emmanuelchio 0:71a5ba330e01 121 p.x += 10;
emmanuelchio 0:71a5ba330e01 122 break;
emmanuelchio 0:71a5ba330e01 123 case 2: //3rd row
emmanuelchio 0:71a5ba330e01 124 p.x -= (KEYXSIZE/2);
emmanuelchio 0:71a5ba330e01 125 p.x /= KEYXSIZE; //obtain column
emmanuelchio 0:71a5ba330e01 126 p.x += 18;
emmanuelchio 0:71a5ba330e01 127 if(p.x==18) return TYPE;
emmanuelchio 0:71a5ba330e01 128 if(p.x==26) return DEL;
emmanuelchio 0:71a5ba330e01 129 break;
emmanuelchio 0:71a5ba330e01 130 default: //4rt row
emmanuelchio 0:71a5ba330e01 131 p.x -= (KEYXSIZE/2);
emmanuelchio 0:71a5ba330e01 132 p.x /= KEYXSIZE; //obtain column
emmanuelchio 0:71a5ba330e01 133 if(p.x==0) return KEYCASE;
emmanuelchio 0:71a5ba330e01 134 if(p.x>=8) return OK;
emmanuelchio 0:71a5ba330e01 135 return ' ';
emmanuelchio 0:71a5ba330e01 136 break;
emmanuelchio 0:71a5ba330e01 137 }
emmanuelchio 0:71a5ba330e01 138 return *(data+p.x);
emmanuelchio 0:71a5ba330e01 139 }
emmanuelchio 0:71a5ba330e01 140 }
emmanuelchio 0:71a5ba330e01 141 return 0;
emmanuelchio 0:71a5ba330e01 142 }
emmanuelchio 0:71a5ba330e01 143
emmanuelchio 0:71a5ba330e01 144 /***************************************************/
emmanuelchio 0:71a5ba330e01 145 /***************************************************/
emmanuelchio 0:71a5ba330e01 146 void initializeSmartGPU2(void){ //Initialize SMARTGPU2 Board
emmanuelchio 0:71a5ba330e01 147 lcd.reset(); //physically reset SMARTGPU2
emmanuelchio 0:71a5ba330e01 148 lcd.start(); //initialize the SMARTGPU2 processor
emmanuelchio 0:71a5ba330e01 149 }
emmanuelchio 0:71a5ba330e01 150
emmanuelchio 0:71a5ba330e01 151 /***************************************************/
emmanuelchio 0:71a5ba330e01 152 /***************************************************/
emmanuelchio 0:71a5ba330e01 153 /***************************************************/
emmanuelchio 0:71a5ba330e01 154 /***************************************************/
emmanuelchio 0:71a5ba330e01 155 int main() {
emmanuelchio 0:71a5ba330e01 156 unsigned int currentX=5, lastX=5, currentY=5;
emmanuelchio 0:71a5ba330e01 157 char key = 0, currentKeyboard = LETTERSLOWER;
emmanuelchio 0:71a5ba330e01 158
emmanuelchio 0:71a5ba330e01 159 initializeSmartGPU2(); //Init communication with SmartGPU2 board
emmanuelchio 0:71a5ba330e01 160
emmanuelchio 0:71a5ba330e01 161 lcd.baudChange(BAUD7); //set a fast baud! for fast drawing
emmanuelchio 0:71a5ba330e01 162 lcd.drawGradientRect(0, 0, MAX_X_LANDSCAPE, MAX_Y_LANDSCAPE, MAGENTA, BLACK, VERTICAL); //draw a background
emmanuelchio 0:71a5ba330e01 163
emmanuelchio 0:71a5ba330e01 164 lcd.drawRectangle(5, 5, MAX_X_LANDSCAPE-5, KEY_Y_TOP-5, WHITE, FILL); //draw text background
emmanuelchio 0:71a5ba330e01 165 lcd.setTextColour(BLACK); //set text colour as black
emmanuelchio 0:71a5ba330e01 166 lcd.setTextSize(FONT2); //set text size FONT2
emmanuelchio 0:71a5ba330e01 167
emmanuelchio 0:71a5ba330e01 168 drawAllKeyboard(currentKeyboard); //draw all keyboard
emmanuelchio 0:71a5ba330e01 169
emmanuelchio 0:71a5ba330e01 170 while(1){
emmanuelchio 0:71a5ba330e01 171 while((key = getKeyTouch(currentKeyboard)) == 0x00); //loop until get a valid key
emmanuelchio 0:71a5ba330e01 172 //once obtained a valid key
emmanuelchio 0:71a5ba330e01 173 if(key!=OK && key!=DEL && key!=TYPE && key!=KEYCASE){ //only print if key is not special key
emmanuelchio 0:71a5ba330e01 174 lcd.putLetter(lastX, currentY, key, &currentX); //print key on lastX and save updated value in currentX
emmanuelchio 2:2c1cb73e48d8 175 if(currentX<=lastX){ //if currentX couldn't advance, means end of X row
emmanuelchio 0:71a5ba330e01 176 currentY += 20; //jump 1 row in Y axis
emmanuelchio 0:71a5ba330e01 177 if(currentY >= (KEY_Y_TOP-5)){ //if we reach the start of the keyboard
emmanuelchio 0:71a5ba330e01 178 currentY=5;
emmanuelchio 0:71a5ba330e01 179 lcd.drawRectangle(5, 5, MAX_X_LANDSCAPE-5, KEY_Y_TOP-5, WHITE, FILL); //draw text background
emmanuelchio 0:71a5ba330e01 180 }
emmanuelchio 0:71a5ba330e01 181 lastX=5; //reset lastX
emmanuelchio 0:71a5ba330e01 182 lcd.putLetter(lastX, currentY, key, &currentX); //print key on new lastX and currentY
emmanuelchio 0:71a5ba330e01 183 }
emmanuelchio 0:71a5ba330e01 184 lastX=currentX; //get new value
emmanuelchio 0:71a5ba330e01 185 }else{
emmanuelchio 0:71a5ba330e01 186 switch(key){
emmanuelchio 0:71a5ba330e01 187 case TYPE:
emmanuelchio 0:71a5ba330e01 188 if(currentKeyboard == LETTERSLOWER || currentKeyboard == LETTERSUPPER) currentKeyboard = NUMBERS; //go to next type
emmanuelchio 0:71a5ba330e01 189 else if(currentKeyboard == NUMBERS) currentKeyboard = SPECCHAR;
emmanuelchio 0:71a5ba330e01 190 else if (currentKeyboard == SPECCHAR) currentKeyboard = LETTERSLOWER;
emmanuelchio 0:71a5ba330e01 191 drawAllKeyboard(currentKeyboard); //update all keyboard
emmanuelchio 0:71a5ba330e01 192 break;
emmanuelchio 0:71a5ba330e01 193 case DEL:
emmanuelchio 0:71a5ba330e01 194 lcd.drawRectangle(5, 5, MAX_X_LANDSCAPE-5, KEY_Y_TOP-5, WHITE, FILL); //draw text background
emmanuelchio 0:71a5ba330e01 195 lastX=5; //reset lastX
emmanuelchio 0:71a5ba330e01 196 currentY=5; //reset currentY
emmanuelchio 0:71a5ba330e01 197 break;
emmanuelchio 0:71a5ba330e01 198 case KEYCASE:
emmanuelchio 0:71a5ba330e01 199 if(currentKeyboard == LETTERSLOWER) currentKeyboard = LETTERSUPPER;
emmanuelchio 0:71a5ba330e01 200 else if(currentKeyboard == LETTERSUPPER) currentKeyboard = LETTERSLOWER;
emmanuelchio 0:71a5ba330e01 201 drawAllKeyboard(currentKeyboard); //update all keyboard
emmanuelchio 0:71a5ba330e01 202 break;
emmanuelchio 0:71a5ba330e01 203 default: //key == OK
emmanuelchio 0:71a5ba330e01 204 //go to main menu - exit keyboard - or any other required action
emmanuelchio 0:71a5ba330e01 205 break;
emmanuelchio 0:71a5ba330e01 206 }
emmanuelchio 0:71a5ba330e01 207 }
emmanuelchio 0:71a5ba330e01 208 //draw the animated key
emmanuelchio 0:71a5ba330e01 209 drawSingleKey(key, currentKeyboard, SELECTED); //draw the obtained key button as SELECTED
emmanuelchio 2:2c1cb73e48d8 210 delay(200); //wait 200ms with key as SELECTED
emmanuelchio 0:71a5ba330e01 211 drawSingleKey(key, currentKeyboard, DESELECTED); //draw the obtained key button as DESELECTED
emmanuelchio 0:71a5ba330e01 212 }
emmanuelchio 0:71a5ba330e01 213 }