Shish

Dependencies:   mbed USBDevice GLCD

Committer:
pepperu96
Date:
Fri Nov 05 11:31:35 2021 +0000
Revision:
5:2dce07562de8
Parent:
4:fc3364c83df6
Child:
6:3a0fd4eef371
final_project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
montie97 0:9ae27a35a771 1 #include "mbed.h"
montie97 0:9ae27a35a771 2 #include "GLCD.h"
montie97 0:9ae27a35a771 3 #include "GLCD.c"
montie97 0:9ae27a35a771 4 #include "AsciiLib.c"
montie97 0:9ae27a35a771 5 #include "HzLib.c"
montie97 0:9ae27a35a771 6 #include "USBSerial.h"
montie97 0:9ae27a35a771 7 #include "TouchPanel.h"
pepperu96 5:2dce07562de8 8 #include "DeepNeuralNetwork.h"
pepperu96 5:2dce07562de8 9
pepperu96 2:4b3d056967a8 10 #define DEBUG
montie97 0:9ae27a35a771 11
pepperu96 5:2dce07562de8 12 DeepNeuralNetwork dnn;
pepperu96 5:2dce07562de8 13
pepperu96 5:2dce07562de8 14 const int sample_2[28][28] = {
pepperu96 5:2dce07562de8 15 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 16 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 17 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 18 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 19 {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 20 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 21 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 22 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 23 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 24 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 25 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 26 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 27 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 28 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 29 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 30 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 31 {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 32 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 33 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 34 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0},
pepperu96 5:2dce07562de8 35 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
pepperu96 5:2dce07562de8 36 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 37 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 38 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 39 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 40 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 41 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
pepperu96 5:2dce07562de8 42 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
pepperu96 5:2dce07562de8 43 };
pepperu96 5:2dce07562de8 44
pepperu96 5:2dce07562de8 45 uint8_t *letter;
pepperu96 5:2dce07562de8 46
pepperu96 5:2dce07562de8 47 uint8_t *letter0 = (uint8_t *)"0";
pepperu96 5:2dce07562de8 48 uint8_t *letter1 = (uint8_t *)"1";
pepperu96 5:2dce07562de8 49 uint8_t *letter2 = (uint8_t *)"2";
pepperu96 5:2dce07562de8 50 uint8_t *letter3 = (uint8_t *)"3";
pepperu96 5:2dce07562de8 51 uint8_t *letter4 = (uint8_t *)"4";
pepperu96 5:2dce07562de8 52 uint8_t *letter5 = (uint8_t *)"5";
pepperu96 5:2dce07562de8 53 uint8_t *letter6 = (uint8_t *)"6";
pepperu96 5:2dce07562de8 54 uint8_t *letter7 = (uint8_t *)"7";
pepperu96 5:2dce07562de8 55 uint8_t *letter8 = (uint8_t *)"8";
pepperu96 5:2dce07562de8 56 uint8_t *letter9 = (uint8_t *)"9";
pepperu96 5:2dce07562de8 57
pepperu96 5:2dce07562de8 58 uint8_t *letterA = (uint8_t *)"A";
pepperu96 5:2dce07562de8 59 uint8_t *letterB = (uint8_t *)"B";
pepperu96 5:2dce07562de8 60 uint8_t *letterC = (uint8_t *)"C";
pepperu96 5:2dce07562de8 61 uint8_t *letterD = (uint8_t *)"D";
pepperu96 5:2dce07562de8 62 uint8_t *letterE = (uint8_t *)"E";
pepperu96 5:2dce07562de8 63 uint8_t *letterF = (uint8_t *)"F";
pepperu96 5:2dce07562de8 64 uint8_t *letterG = (uint8_t *)"G";
pepperu96 5:2dce07562de8 65 uint8_t *letterH = (uint8_t *)"H";
pepperu96 5:2dce07562de8 66 uint8_t *letterI = (uint8_t *)"I";
pepperu96 5:2dce07562de8 67 uint8_t *letterJ = (uint8_t *)"J";
pepperu96 5:2dce07562de8 68 uint8_t *letterK = (uint8_t *)"K";
pepperu96 5:2dce07562de8 69 uint8_t *letterL = (uint8_t *)"L";
pepperu96 5:2dce07562de8 70 uint8_t *letterM = (uint8_t *)"M";
pepperu96 5:2dce07562de8 71 uint8_t *letterN = (uint8_t *)"N";
pepperu96 5:2dce07562de8 72 uint8_t *letterO = (uint8_t *)"O";
pepperu96 5:2dce07562de8 73 uint8_t *letterP = (uint8_t *)"P";
pepperu96 5:2dce07562de8 74 uint8_t *letterQ = (uint8_t *)"Q";
pepperu96 5:2dce07562de8 75 uint8_t *letterR = (uint8_t *)"R";
pepperu96 5:2dce07562de8 76 uint8_t *letterS = (uint8_t *)"S";
pepperu96 5:2dce07562de8 77 uint8_t *letterT = (uint8_t *)"T";
pepperu96 5:2dce07562de8 78 uint8_t *letterU = (uint8_t *)"U";
pepperu96 5:2dce07562de8 79 uint8_t *letterV = (uint8_t *)"V";
pepperu96 5:2dce07562de8 80 uint8_t *letterW = (uint8_t *)"W";
pepperu96 5:2dce07562de8 81 uint8_t *letterX = (uint8_t *)"X";
pepperu96 5:2dce07562de8 82 uint8_t *letterY = (uint8_t *)"Y";
pepperu96 5:2dce07562de8 83 uint8_t *letterZ = (uint8_t *)"Z";
pepperu96 5:2dce07562de8 84
montie97 0:9ae27a35a771 85 //FUNZIONI GIORGIO
montie97 0:9ae27a35a771 86 void readtouch();
pepperu96 5:2dce07562de8 87
montie97 0:9ae27a35a771 88 //VARIABILI GIORGIO
montie97 0:9ae27a35a771 89 Coordinate tocco;
montie97 0:9ae27a35a771 90 int x,y,k,avg_x,avg_y,ready;
pepperu96 5:2dce07562de8 91
montie97 0:9ae27a35a771 92 int startGame=0;
montie97 0:9ae27a35a771 93 int isWritten=0;
montie97 0:9ae27a35a771 94 int inferenceStarted=0;
pepperu96 5:2dce07562de8 95 int drawnPoints[28][28];
pepperu96 5:2dce07562de8 96 int threshold = 32;
montie97 0:9ae27a35a771 97 USBSerial serial;
pepperu96 5:2dce07562de8 98
protopapaandrea 1:a32ccf18194d 99 int charPos;
pepperu96 5:2dce07562de8 100
montie97 0:9ae27a35a771 101 /*Buttons*/
montie97 0:9ae27a35a771 102 typedef enum {space,infer,del} button;
pepperu96 5:2dce07562de8 103
montie97 0:9ae27a35a771 104 /* Private function prototypes -----------------------------------------------*/
montie97 0:9ae27a35a771 105 void spaceText(int x1, int x2, int yText, int spessore, uint16_t color);
montie97 0:9ae27a35a771 106 void clearPad(void);
montie97 0:9ae27a35a771 107 void showCompression(void);
montie97 0:9ae27a35a771 108 void clearBar(void);
montie97 0:9ae27a35a771 109 void drawButtons(void);
montie97 0:9ae27a35a771 110 void drawButton(button b, uint16_t color);
montie97 0:9ae27a35a771 111 void start(void);
montie97 0:9ae27a35a771 112 void drawTriangle(int posX, int posY, int lungMax);
protopapaandrea 1:a32ccf18194d 113 void readTouch(void);
pepperu96 5:2dce07562de8 114
montie97 0:9ae27a35a771 115 int main() {
montie97 0:9ae27a35a771 116 LCD_Initialization();
montie97 0:9ae27a35a771 117 TP_Init();
protopapaandrea 1:a32ccf18194d 118 TouchPanel_Calibrate();
protopapaandrea 1:a32ccf18194d 119
montie97 0:9ae27a35a771 120 LCD_Clear(Blue2);
montie97 0:9ae27a35a771 121
montie97 0:9ae27a35a771 122 clearBar();
montie97 0:9ae27a35a771 123 drawButtons();
montie97 0:9ae27a35a771 124 start();
montie97 0:9ae27a35a771 125
montie97 0:9ae27a35a771 126 while(1){
protopapaandrea 1:a32ccf18194d 127 readTouch();
protopapaandrea 1:a32ccf18194d 128 wait_us(500);
montie97 0:9ae27a35a771 129 }
montie97 0:9ae27a35a771 130 }
pepperu96 5:2dce07562de8 131
montie97 0:9ae27a35a771 132 /*******************************************************************************
montie97 0:9ae27a35a771 133 * Function Name : spaceText
montie97 0:9ae27a35a771 134 * Description :
montie97 0:9ae27a35a771 135 * Input : None
montie97 0:9ae27a35a771 136 * Output : None
montie97 0:9ae27a35a771 137 * Return : None
montie97 0:9ae27a35a771 138 * Attention : None
montie97 0:9ae27a35a771 139 *******************************************************************************/
montie97 0:9ae27a35a771 140 void spaceText(int x1, int x2, int yText, int spessore, uint16_t color){ //Spazio di un unico colore
montie97 0:9ae27a35a771 141 int i;
montie97 0:9ae27a35a771 142 for(i=spessore;i>0;i--){
montie97 0:9ae27a35a771 143 LCD_DrawLine(x1,yText-i,x2,yText-i,color);
montie97 0:9ae27a35a771 144 }
montie97 0:9ae27a35a771 145 return;
montie97 0:9ae27a35a771 146 }
pepperu96 5:2dce07562de8 147
montie97 0:9ae27a35a771 148 /*******************************************************************************
montie97 0:9ae27a35a771 149 * Function Name : clearPad
montie97 0:9ae27a35a771 150 * Description :
montie97 0:9ae27a35a771 151 * Input : None
montie97 0:9ae27a35a771 152 * Output : None
montie97 0:9ae27a35a771 153 * Return : None
montie97 0:9ae27a35a771 154 * Attention : None
montie97 0:9ae27a35a771 155 *******************************************************************************/
montie97 0:9ae27a35a771 156 void clearPad(){
montie97 0:9ae27a35a771 157 int i,j;
montie97 0:9ae27a35a771 158 for(i=0;i<224;i++){
montie97 0:9ae27a35a771 159 LCD_DrawLine(8,52+i,232,52+i,White);
montie97 0:9ae27a35a771 160 }
montie97 0:9ae27a35a771 161 for(i=0;i<28;i++) //also clear the matrix
pepperu96 5:2dce07562de8 162 for(j=0;j<28;j++)
montie97 0:9ae27a35a771 163 drawnPoints[i][j]=0;
montie97 0:9ae27a35a771 164
montie97 0:9ae27a35a771 165 return;
montie97 0:9ae27a35a771 166 }
pepperu96 5:2dce07562de8 167
montie97 0:9ae27a35a771 168 /*******************************************************************************
montie97 0:9ae27a35a771 169 * Function Name : showCompression
montie97 0:9ae27a35a771 170 * Description :
montie97 0:9ae27a35a771 171 * Input : None
montie97 0:9ae27a35a771 172 * Output : None
montie97 0:9ae27a35a771 173 * Return : None
montie97 0:9ae27a35a771 174 * Attention : None
montie97 0:9ae27a35a771 175 *******************************************************************************/
montie97 0:9ae27a35a771 176 void showCompression(){
montie97 0:9ae27a35a771 177 int i,j;
montie97 0:9ae27a35a771 178 for(i=0;i<224;i++){ //clean the pad
montie97 0:9ae27a35a771 179 LCD_DrawLine(8,52+i,232,52+i,White);
montie97 0:9ae27a35a771 180 }
montie97 0:9ae27a35a771 181 for(i=0;i<28;i++)
montie97 0:9ae27a35a771 182 for(j=0;j<28;j++)
pepperu96 5:2dce07562de8 183 if(drawnPoints[i][j]==1)
montie97 0:9ae27a35a771 184 LCD_SetPoint(i+110,j+150,Black);
montie97 0:9ae27a35a771 185
montie97 0:9ae27a35a771 186 return;
montie97 0:9ae27a35a771 187 }
pepperu96 5:2dce07562de8 188
montie97 0:9ae27a35a771 189 /*******************************************************************************
montie97 0:9ae27a35a771 190 * Function Name : clearBar
montie97 0:9ae27a35a771 191 * Description :
montie97 0:9ae27a35a771 192 * Input : None
montie97 0:9ae27a35a771 193 * Output : None
montie97 0:9ae27a35a771 194 * Return : None
montie97 0:9ae27a35a771 195 * Attention : None
montie97 0:9ae27a35a771 196 *******************************************************************************/
montie97 0:9ae27a35a771 197 void clearBar(){
montie97 0:9ae27a35a771 198 int i;
montie97 0:9ae27a35a771 199 for(i=0;i<32;i++){
montie97 0:9ae27a35a771 200 LCD_DrawLine(8,10+i,232,10+i,White);
montie97 0:9ae27a35a771 201 }
montie97 0:9ae27a35a771 202 return;
montie97 0:9ae27a35a771 203 }
pepperu96 5:2dce07562de8 204
montie97 0:9ae27a35a771 205 /*******************************************************************************
montie97 0:9ae27a35a771 206 * Function Name : drawButton
montie97 0:9ae27a35a771 207 * Description :
montie97 0:9ae27a35a771 208 * Input : None
montie97 0:9ae27a35a771 209 * Output : None
montie97 0:9ae27a35a771 210 * Return : None
montie97 0:9ae27a35a771 211 * Attention : None
montie97 0:9ae27a35a771 212 *******************************************************************************/
montie97 0:9ae27a35a771 213 void drawButton(button b, uint16_t color){
montie97 0:9ae27a35a771 214 int i;
montie97 0:9ae27a35a771 215 switch(b){
montie97 0:9ae27a35a771 216 case space: //Space Button
montie97 0:9ae27a35a771 217 spaceText(8,87,291,8,color);
montie97 0:9ae27a35a771 218 GUI_Text(8, 291, (uint8_t *) " Space ", Blue, color,1);
montie97 0:9ae27a35a771 219 spaceText(8,87,291+16+8,8,color);
montie97 0:9ae27a35a771 220 break;
montie97 0:9ae27a35a771 221 case infer: //Infer Button
montie97 0:9ae27a35a771 222 for(i=0;i<32;i++){
montie97 0:9ae27a35a771 223 LCD_DrawLine(100,283+i,139,283+i,color);
montie97 0:9ae27a35a771 224 }
montie97 0:9ae27a35a771 225 drawTriangle(112,289,10);
montie97 0:9ae27a35a771 226 break;
montie97 0:9ae27a35a771 227 case del: //Delete Button
montie97 0:9ae27a35a771 228 spaceText(153,232,291,8,color);
montie97 0:9ae27a35a771 229 GUI_Text(153, 291, (uint8_t *) " Delete ", Blue, color,1);
montie97 0:9ae27a35a771 230 spaceText(153,232,291+16+8,8,color);
montie97 0:9ae27a35a771 231 break;
montie97 0:9ae27a35a771 232 default:
montie97 0:9ae27a35a771 233 break;
montie97 0:9ae27a35a771 234 }
montie97 0:9ae27a35a771 235 return;
montie97 0:9ae27a35a771 236 }
pepperu96 5:2dce07562de8 237
montie97 0:9ae27a35a771 238 /*******************************************************************************
montie97 0:9ae27a35a771 239 * Function Name : drawButtons
montie97 0:9ae27a35a771 240 * Description :
montie97 0:9ae27a35a771 241 * Input : None
montie97 0:9ae27a35a771 242 * Output : None
montie97 0:9ae27a35a771 243 * Return : None
montie97 0:9ae27a35a771 244 * Attention : None
montie97 0:9ae27a35a771 245 *******************************************************************************/
montie97 0:9ae27a35a771 246 void drawButtons(){
montie97 0:9ae27a35a771 247 drawButton(space,White);
montie97 0:9ae27a35a771 248 drawButton(infer,White);
montie97 0:9ae27a35a771 249 drawButton(del,White);
montie97 0:9ae27a35a771 250 return;
montie97 0:9ae27a35a771 251 }
pepperu96 5:2dce07562de8 252
montie97 0:9ae27a35a771 253 /*******************************************************************************
montie97 0:9ae27a35a771 254 * Function Name : drawTriangle
montie97 0:9ae27a35a771 255 * Description :
montie97 0:9ae27a35a771 256 * Input : None
montie97 0:9ae27a35a771 257 * Output : None
montie97 0:9ae27a35a771 258 * Return : None
montie97 0:9ae27a35a771 259 * Attention : None
montie97 0:9ae27a35a771 260 *******************************************************************************/
montie97 0:9ae27a35a771 261 void drawTriangle(int posX, int posY, int lungMax){
montie97 0:9ae27a35a771 262 int i;
montie97 0:9ae27a35a771 263 uint16_t color=Green2;
montie97 0:9ae27a35a771 264 for(i=0;i<lungMax;i++){ //Direction EAST
montie97 0:9ae27a35a771 265 LCD_DrawLine(posX+5,posY+i+1,posX+5+i,posY+i+1,color);
montie97 0:9ae27a35a771 266 }
montie97 0:9ae27a35a771 267 for(i=0;i<lungMax-1;i++){
montie97 0:9ae27a35a771 268 LCD_DrawLine(posX+5,posY+lungMax+i+1,posX+5+(lungMax-2)-i,posY+lungMax+i+1,color);
montie97 0:9ae27a35a771 269 }
montie97 0:9ae27a35a771 270 }
pepperu96 5:2dce07562de8 271
montie97 0:9ae27a35a771 272 /*******************************************************************************
montie97 0:9ae27a35a771 273 * Function Name : start
montie97 0:9ae27a35a771 274 * Description :
montie97 0:9ae27a35a771 275 * Input : None
montie97 0:9ae27a35a771 276 * Output : None
montie97 0:9ae27a35a771 277 * Return : None
montie97 0:9ae27a35a771 278 * Attention : None
montie97 0:9ae27a35a771 279 *******************************************************************************/
montie97 0:9ae27a35a771 280 void start(){
montie97 0:9ae27a35a771 281 //disable_timer(0);
montie97 0:9ae27a35a771 282 //reset_timer(0);
montie97 0:9ae27a35a771 283
montie97 0:9ae27a35a771 284 clearPad();
montie97 0:9ae27a35a771 285 GUI_Text(22, 90, (uint8_t *) "Touch to", Black, White, 3);
montie97 0:9ae27a35a771 286 GUI_Text(32, 135, (uint8_t *)" start", Black, White, 3);
montie97 0:9ae27a35a771 287 GUI_Text(28, 185, (uint8_t *) "writing!", Black, White, 3);
montie97 0:9ae27a35a771 288
montie97 0:9ae27a35a771 289 startGame=0;
montie97 0:9ae27a35a771 290 isWritten=0;
montie97 0:9ae27a35a771 291 inferenceStarted=0;
montie97 0:9ae27a35a771 292 //enable_timer(0);
montie97 0:9ae27a35a771 293
montie97 0:9ae27a35a771 294 return;
montie97 0:9ae27a35a771 295 }
montie97 0:9ae27a35a771 296
pepperu96 5:2dce07562de8 297 int predict() {
pepperu96 5:2dce07562de8 298 int image[28][28];
pepperu96 5:2dce07562de8 299 for (int i=0; i<28; i++)
pepperu96 5:2dce07562de8 300 for(int j=0; j<28; j++)
pepperu96 5:2dce07562de8 301 image[i][j] = drawnPoints[j][i];
pepperu96 5:2dce07562de8 302
pepperu96 5:2dce07562de8 303 int prediction = dnn.inference(image);
pepperu96 5:2dce07562de8 304
pepperu96 5:2dce07562de8 305 if (prediction == 0)
pepperu96 5:2dce07562de8 306 letter = letter0;
pepperu96 5:2dce07562de8 307 else if (prediction == 1)
pepperu96 5:2dce07562de8 308 letter = letter1;
pepperu96 5:2dce07562de8 309 else if (prediction == 2)
pepperu96 5:2dce07562de8 310 letter = letter2;
pepperu96 5:2dce07562de8 311 else if (prediction == 3)
pepperu96 5:2dce07562de8 312 letter = letter3;
pepperu96 5:2dce07562de8 313 else if (prediction == 4)
pepperu96 5:2dce07562de8 314 letter = letter4;
pepperu96 5:2dce07562de8 315 else if (prediction == 5)
pepperu96 5:2dce07562de8 316 letter = letter5;
pepperu96 5:2dce07562de8 317 else if (prediction == 6)
pepperu96 5:2dce07562de8 318 letter = letter6;
pepperu96 5:2dce07562de8 319 else if (prediction == 7)
pepperu96 5:2dce07562de8 320 letter = letter7;
pepperu96 5:2dce07562de8 321 else if (prediction == 8)
pepperu96 5:2dce07562de8 322 letter = letter8;
pepperu96 5:2dce07562de8 323 else if (prediction == 9)
pepperu96 5:2dce07562de8 324 letter = letter9;
pepperu96 5:2dce07562de8 325 else if (prediction == 10)
pepperu96 5:2dce07562de8 326 letter = letterA;
pepperu96 5:2dce07562de8 327 else if (prediction == 11)
pepperu96 5:2dce07562de8 328 letter = letterB;
pepperu96 5:2dce07562de8 329 else if (prediction == 12)
pepperu96 5:2dce07562de8 330 letter = letterC;
pepperu96 5:2dce07562de8 331 else if (prediction == 13)
pepperu96 5:2dce07562de8 332 letter = letterD;
pepperu96 5:2dce07562de8 333 else if (prediction == 14)
pepperu96 5:2dce07562de8 334 letter = letterE;
pepperu96 5:2dce07562de8 335 else if (prediction == 15)
pepperu96 5:2dce07562de8 336 letter = letterF;
pepperu96 5:2dce07562de8 337 else if (prediction == 16)
pepperu96 5:2dce07562de8 338 letter = letterG;
pepperu96 5:2dce07562de8 339 else if (prediction == 17)
pepperu96 5:2dce07562de8 340 letter = letterH;
pepperu96 5:2dce07562de8 341 else if (prediction == 18)
pepperu96 5:2dce07562de8 342 letter = letterI;
pepperu96 5:2dce07562de8 343 else if (prediction == 19)
pepperu96 5:2dce07562de8 344 letter = letterJ;
pepperu96 5:2dce07562de8 345 else if (prediction == 20)
pepperu96 5:2dce07562de8 346 letter = letterK;
pepperu96 5:2dce07562de8 347 else if (prediction == 21)
pepperu96 5:2dce07562de8 348 letter = letterL;
pepperu96 5:2dce07562de8 349 else if (prediction == 22)
pepperu96 5:2dce07562de8 350 letter = letterM;
pepperu96 5:2dce07562de8 351 else if (prediction == 23)
pepperu96 5:2dce07562de8 352 letter = letterN;
pepperu96 5:2dce07562de8 353 else if (prediction == 24)
pepperu96 5:2dce07562de8 354 letter = letterO;
pepperu96 5:2dce07562de8 355 else if (prediction == 25)
pepperu96 5:2dce07562de8 356 letter = letterP;
pepperu96 5:2dce07562de8 357 else if (prediction == 26)
pepperu96 5:2dce07562de8 358 letter = letterQ;
pepperu96 5:2dce07562de8 359 else if (prediction == 27)
pepperu96 5:2dce07562de8 360 letter = letterR;
pepperu96 5:2dce07562de8 361 else if (prediction == 28)
pepperu96 5:2dce07562de8 362 letter = letterS;
pepperu96 5:2dce07562de8 363 else if (prediction == 29)
pepperu96 5:2dce07562de8 364 letter = letterT;
pepperu96 5:2dce07562de8 365 else if (prediction == 30)
pepperu96 5:2dce07562de8 366 letter = letterU;
pepperu96 5:2dce07562de8 367 else if (prediction == 31)
pepperu96 5:2dce07562de8 368 letter = letterV;
pepperu96 5:2dce07562de8 369 else if (prediction == 32)
pepperu96 5:2dce07562de8 370 letter = letterW;
pepperu96 5:2dce07562de8 371 else if (prediction == 33)
pepperu96 5:2dce07562de8 372 letter = letterX;
pepperu96 5:2dce07562de8 373 else if (prediction == 34)
pepperu96 5:2dce07562de8 374 letter = letterY;
pepperu96 5:2dce07562de8 375 else if (prediction == 35)
pepperu96 5:2dce07562de8 376 letter = letterZ;
pepperu96 5:2dce07562de8 377
pepperu96 5:2dce07562de8 378 return prediction;
pepperu96 5:2dce07562de8 379 }
pepperu96 5:2dce07562de8 380
montie97 0:9ae27a35a771 381 // FUNZIONI GIORGIO
pepperu96 5:2dce07562de8 382
montie97 0:9ae27a35a771 383 void readtouch(){
montie97 0:9ae27a35a771 384 if(Read_X()!=4095){
montie97 0:9ae27a35a771 385 wait(0.1);
montie97 0:9ae27a35a771 386 for(k=0; k<400;k++){
montie97 0:9ae27a35a771 387 x=Read_Y();
montie97 0:9ae27a35a771 388 y=Read_X();
montie97 0:9ae27a35a771 389 x=(x-300)*240/3900;
montie97 0:9ae27a35a771 390 y=(y-220)*320/3800;
montie97 0:9ae27a35a771 391 avg_x+=x;
montie97 0:9ae27a35a771 392 avg_y+=y;
montie97 0:9ae27a35a771 393 }
montie97 0:9ae27a35a771 394 avg_x=avg_x/400;
montie97 0:9ae27a35a771 395 avg_y=avg_y/400;
montie97 0:9ae27a35a771 396 tocco.x=avg_x+10;
montie97 0:9ae27a35a771 397 tocco.y=avg_y+19;
montie97 0:9ae27a35a771 398 ready=1;
montie97 0:9ae27a35a771 399 }
montie97 0:9ae27a35a771 400 else
montie97 0:9ae27a35a771 401 ready=0;
protopapaandrea 1:a32ccf18194d 402 }
pepperu96 5:2dce07562de8 403
protopapaandrea 1:a32ccf18194d 404 void readTouch(){
pepperu96 5:2dce07562de8 405 int i,j;
protopapaandrea 1:a32ccf18194d 406 Coordinate p;
protopapaandrea 1:a32ccf18194d 407 getDisplayPoint(&display, Read_Ads7846(), &matrix ) ; //resto in attesa di un punto che venga toccato
protopapaandrea 1:a32ccf18194d 408 if(display.x <= 240 && display.x > 0){
protopapaandrea 1:a32ccf18194d 409 if(display.y >= 52 && display.y <= 275 && display.x >=8 && display.x <= 232){//punto in zona alta (start writing)
protopapaandrea 1:a32ccf18194d 410 if(!startGame){
protopapaandrea 1:a32ccf18194d 411 startGame=1;
protopapaandrea 1:a32ccf18194d 412 clearPad();
protopapaandrea 1:a32ccf18194d 413 charPos=0;
protopapaandrea 1:a32ccf18194d 414 }
protopapaandrea 1:a32ccf18194d 415 else{//game already started: handwriting input
protopapaandrea 1:a32ccf18194d 416 if(charPos<26*8){ //topbar bound: otherwise the topbar is full
protopapaandrea 1:a32ccf18194d 417 #ifdef DEBUG
protopapaandrea 1:a32ccf18194d 418 if(!isWritten)
protopapaandrea 1:a32ccf18194d 419 clearPad();//clear to delete the testing result printed on screen before
protopapaandrea 1:a32ccf18194d 420 #endif
protopapaandrea 1:a32ccf18194d 421 isWritten=1;
protopapaandrea 1:a32ccf18194d 422 p.x=display.x-8;
protopapaandrea 1:a32ccf18194d 423 p.y=display.y-52;
pepperu96 5:2dce07562de8 424 for(i=-1;i<2;i++)
protopapaandrea 1:a32ccf18194d 425 for(j=-1;j<2;j++){
protopapaandrea 1:a32ccf18194d 426 if((p.x+i)>=0&&(p.y+j)>=0&&(p.x+i)<224&&(p.y+j)<224){
protopapaandrea 1:a32ccf18194d 427 TP_DrawPoint(display.x+i,display.y+j);
pepperu96 5:2dce07562de8 428 drawnPoints[(p.x+i)/8][(p.y+j)/8]++; //count +1 for the macrocell containing this pixel
protopapaandrea 1:a32ccf18194d 429 }
protopapaandrea 1:a32ccf18194d 430 }
protopapaandrea 1:a32ccf18194d 431 }
protopapaandrea 1:a32ccf18194d 432 }
protopapaandrea 1:a32ccf18194d 433 }
protopapaandrea 1:a32ccf18194d 434 else{
protopapaandrea 1:a32ccf18194d 435 if(display.y >= 283 && display.y <= 315 && display.x >=8 && display.x <= 87 && startGame && !isWritten && !inferenceStarted){//punto in tasto Space
protopapaandrea 1:a32ccf18194d 436 if(charPos<26*8){ //topbar bound: otherwise the topbar is full
protopapaandrea 1:a32ccf18194d 437 drawButton(space,Yellow);
pepperu96 5:2dce07562de8 438
protopapaandrea 1:a32ccf18194d 439 //space operation: writing a whitespace into the topbar
protopapaandrea 1:a32ccf18194d 440
protopapaandrea 1:a32ccf18194d 441 //--TO BE COMPLETED--//
protopapaandrea 1:a32ccf18194d 442 if(charPos!=0)
protopapaandrea 1:a32ccf18194d 443 LCD_DrawLine(15+charPos-8, 35, 15+charPos+8-8, 35, White); //delete last cursor
protopapaandrea 1:a32ccf18194d 444 GUI_Text(15+charPos, 20, (uint8_t *)" ", Black, White,1); //testing space on topbar
protopapaandrea 1:a32ccf18194d 445 LCD_DrawLine(15+charPos, 35, 15+charPos+8, 35, Black); //cursor
protopapaandrea 1:a32ccf18194d 446 charPos+=8;
protopapaandrea 1:a32ccf18194d 447
protopapaandrea 1:a32ccf18194d 448 clearPad(); //useful only to see the same response time of the other buttons
protopapaandrea 1:a32ccf18194d 449
protopapaandrea 1:a32ccf18194d 450 drawButton(space,White);
protopapaandrea 1:a32ccf18194d 451 }
protopapaandrea 1:a32ccf18194d 452 }
protopapaandrea 1:a32ccf18194d 453 else{
protopapaandrea 1:a32ccf18194d 454 if(display.y >= 283 && display.y <= 315 && display.x >=100 && display.x <= 139 && startGame && isWritten &&!inferenceStarted){//punto in tasto Infer
protopapaandrea 1:a32ccf18194d 455 //wait flag to able
protopapaandrea 1:a32ccf18194d 456
protopapaandrea 1:a32ccf18194d 457 drawButton(infer,Yellow);
protopapaandrea 1:a32ccf18194d 458
protopapaandrea 1:a32ccf18194d 459 inferenceStarted=1;
protopapaandrea 1:a32ccf18194d 460 //infer operation: compress the matrix and send the final 28x28 matrix to the inference step
protopapaandrea 1:a32ccf18194d 461
protopapaandrea 1:a32ccf18194d 462 //--TO BE COMPLETED--//
protopapaandrea 1:a32ccf18194d 463 for(i=0;i<28;i++) //if the counter of the macrocell is greater than threshold, set the final cell to 1, otherwise it would be a 0-cell
protopapaandrea 1:a32ccf18194d 464 for(j=0;j<28;j++)
pepperu96 5:2dce07562de8 465 drawnPoints[i][j]=drawnPoints[i][j]>=threshold?1:0;
pepperu96 5:2dce07562de8 466
protopapaandrea 1:a32ccf18194d 467
protopapaandrea 1:a32ccf18194d 468 #ifdef DEBUG
protopapaandrea 1:a32ccf18194d 469 showCompression();
pepperu96 5:2dce07562de8 470 #endif
pepperu96 5:2dce07562de8 471
pepperu96 5:2dce07562de8 472 // Send the image via USB to predict on PC
pepperu96 2:4b3d056967a8 473 for(i=0;i<28;i++) {
pepperu96 2:4b3d056967a8 474 for(j=0;j<28;j++) {
pepperu96 5:2dce07562de8 475 serial.printf("%d",static_cast<int>(drawnPoints[j][i]));
pepperu96 2:4b3d056967a8 476 }
pepperu96 2:4b3d056967a8 477 }
protopapaandrea 1:a32ccf18194d 478
pepperu96 5:2dce07562de8 479 // Predict on board
pepperu96 5:2dce07562de8 480 int prediction = predict();
pepperu96 5:2dce07562de8 481 // Send the prediction via USB to show the prediction
pepperu96 5:2dce07562de8 482 wait(0.5);
pepperu96 5:2dce07562de8 483 serial.printf("%02d", prediction);
pepperu96 5:2dce07562de8 484
pepperu96 5:2dce07562de8 485 // Visualize the prediction on top bar
pepperu96 5:2dce07562de8 486 if(charPos!=0)
pepperu96 5:2dce07562de8 487 LCD_DrawLine(15+charPos-8, 35, 15+charPos+8-8, 35, White); //delete last cursor
pepperu96 5:2dce07562de8 488 GUI_Text(15+charPos, 20, letter, Black, White,1); //testing text on topbar
pepperu96 5:2dce07562de8 489 LCD_DrawLine(15+charPos, 35, 15+charPos+8, 35, Black); //cursor
pepperu96 5:2dce07562de8 490 charPos+=8;
pepperu96 5:2dce07562de8 491
pepperu96 5:2dce07562de8 492
pepperu96 5:2dce07562de8 493 #ifndef DEBUG
pepperu96 3:2bf723da1732 494 clearPad(); //here the matix is also cleaned
pepperu96 3:2bf723da1732 495 #endif
pepperu96 3:2bf723da1732 496
protopapaandrea 1:a32ccf18194d 497 isWritten=0;
protopapaandrea 1:a32ccf18194d 498 inferenceStarted=0;
protopapaandrea 1:a32ccf18194d 499
protopapaandrea 1:a32ccf18194d 500
protopapaandrea 1:a32ccf18194d 501 drawButton(infer,White);
protopapaandrea 1:a32ccf18194d 502 }
protopapaandrea 1:a32ccf18194d 503 else{
protopapaandrea 1:a32ccf18194d 504 if(display.y >= 283 && display.y <= 315 && display.x >=153 && display.x <= 232 && startGame &&!inferenceStarted){//punto in tasto Delete
protopapaandrea 1:a32ccf18194d 505 //wait flag to able
protopapaandrea 1:a32ccf18194d 506
protopapaandrea 1:a32ccf18194d 507 drawButton(del,Yellow);
protopapaandrea 1:a32ccf18194d 508
protopapaandrea 1:a32ccf18194d 509 if(isWritten){ //delete operation - handwriting on pad, before inference: clear the pad
protopapaandrea 1:a32ccf18194d 510 clearPad();
protopapaandrea 1:a32ccf18194d 511 isWritten=0;
protopapaandrea 1:a32ccf18194d 512 }
protopapaandrea 1:a32ccf18194d 513 else{ //delete operation - no handwriting on pad, after inference: delete last char printed on the topbar
protopapaandrea 1:a32ccf18194d 514 //--TO BE COMPLETED--//
protopapaandrea 1:a32ccf18194d 515 clearPad(); //useful only to see the same response time of the other cases
protopapaandrea 1:a32ccf18194d 516 if(charPos!=0){
protopapaandrea 1:a32ccf18194d 517 LCD_DrawLine(15+charPos-8, 35, 15+charPos+8-8, 35, White); //delete last cursor
protopapaandrea 1:a32ccf18194d 518 GUI_Text(15+charPos-8, 20, (uint8_t *)" ", Black, White,1); //testing text on topbar
protopapaandrea 1:a32ccf18194d 519 if(charPos!=8){ //otherwise we are going to the first character, no cursor
protopapaandrea 1:a32ccf18194d 520 LCD_DrawLine(15+charPos-16, 35, 15+charPos-8, 35, Black); //cursor
protopapaandrea 1:a32ccf18194d 521 }
protopapaandrea 1:a32ccf18194d 522 charPos-=8;
protopapaandrea 1:a32ccf18194d 523 }
protopapaandrea 1:a32ccf18194d 524 }
protopapaandrea 1:a32ccf18194d 525
protopapaandrea 1:a32ccf18194d 526 drawButton(del,White);
protopapaandrea 1:a32ccf18194d 527 }
protopapaandrea 1:a32ccf18194d 528 }
protopapaandrea 1:a32ccf18194d 529 }
protopapaandrea 1:a32ccf18194d 530 }
protopapaandrea 1:a32ccf18194d 531 }
protopapaandrea 1:a32ccf18194d 532 else{
protopapaandrea 1:a32ccf18194d 533 //do nothing if touch returns values out of bounds
protopapaandrea 1:a32ccf18194d 534 }
protopapaandrea 1:a32ccf18194d 535 return;
montie97 0:9ae27a35a771 536 }