QWERTY Keypad Touchscreen LCD Demo - MBED + SmartGPU2 board

Dependencies:   SMARTGPU2 mbed

Committer:
emmanuelchio
Date:
Wed Jul 10 03:33:11 2013 +0000
Revision:
0:71a5ba330e01
Child:
1:5278445c0b86
QWERTY Touch Keypad LCD Demo - MBED + SmartGPU2 board

Who changed what in which revision?

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