the place we got to by the end of the last session

Dependencies:   LCDTFT TFT_fonts TSI mbed

Fork of ESP8266_MDM_CJS_KL25 by Oxford CWM Team

Committer:
trapsis
Date:
Fri Jun 10 15:12:48 2016 +0000
Revision:
6:00d91197dd66
Parent:
5:882898bb83ed
server final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
epgmdm 0:7176e20dbdf6 1 #include "mbed.h"
cstevens 3:2b5f88cf893d 2 #include "TSISensor.h"
trapsis 4:354d278c0d52 3 #include "LCDTFT.h"
trapsis 4:354d278c0d52 4 #include "Arial28x28.h"
trapsis 4:354d278c0d52 5
epgmdm 0:7176e20dbdf6 6 #define DEBUG
epgmdm 0:7176e20dbdf6 7 #define INFOMESSAGES
epgmdm 0:7176e20dbdf6 8 #define WARNMESSAGES
epgmdm 0:7176e20dbdf6 9 #define ERRMESSAGES
epgmdm 0:7176e20dbdf6 10
epgmdm 0:7176e20dbdf6 11 #define FUNCNAME "MAIN"
epgmdm 0:7176e20dbdf6 12
epgmdm 1:e2385bba1ab1 13 #ifdef NoDEBUG
trapsis 6:00d91197dd66 14 #define DBG(x, ...) pc.printf(" ["FUNCNAME" : pc.printf] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__);
epgmdm 0:7176e20dbdf6 15 #else
epgmdm 0:7176e20dbdf6 16 #define DBG(x, ...)
epgmdm 0:7176e20dbdf6 17 #endif
epgmdm 0:7176e20dbdf6 18
epgmdm 0:7176e20dbdf6 19 #ifdef ERRMESSAGES
epgmdm 0:7176e20dbdf6 20 #define ERR(x, ...) pc.printf(" ["FUNCNAME" : ERR] "x"\r\n", ##__VA_ARGS__);
epgmdm 0:7176e20dbdf6 21 #else
epgmdm 0:7176e20dbdf6 22 #define ERR(x, ...)
epgmdm 0:7176e20dbdf6 23 #endif
epgmdm 0:7176e20dbdf6 24
epgmdm 0:7176e20dbdf6 25 #ifdef WARNMESSAGES
epgmdm 0:7176e20dbdf6 26 #define WARN(x, ...) printf("["FUNCNAME" : WARN] "x"\r\n", ##__VA_ARGS__);
epgmdm 0:7176e20dbdf6 27 #else
epgmdm 0:7176e20dbdf6 28 #define WARN(x, ...)
epgmdm 0:7176e20dbdf6 29 #endif
epgmdm 0:7176e20dbdf6 30
epgmdm 0:7176e20dbdf6 31 #ifdef INFOMESSAGES
epgmdm 1:e2385bba1ab1 32 #define INFO(x, ...) pc.printf("["FUNCNAME" : INFO] "x"\r\n", ##__VA_ARGS__);
epgmdm 0:7176e20dbdf6 33 #else
epgmdm 0:7176e20dbdf6 34 #define INFO(x, ...)
epgmdm 0:7176e20dbdf6 35 #endif
epgmdm 0:7176e20dbdf6 36
cstevens 3:2b5f88cf893d 37 #define BUFF_SIZE 1024
cstevens 3:2b5f88cf893d 38 // PIN DEFS NOW FOR OLD FIRMWARE VERSION OF THE KL25Z
cstevens 3:2b5f88cf893d 39 Serial pc(USBTX, USBRX);
epgmdm 0:7176e20dbdf6 40 //RawSerial dev(D1, D0);
trapsis 4:354d278c0d52 41 //Serial dev(PTD3,PTD2); //tx,rx
trapsis 4:354d278c0d52 42 Serial dev(D14,D15); //tx,rx
epgmdm 0:7176e20dbdf6 43 DigitalOut led1(LED1);
cstevens 3:2b5f88cf893d 44 DigitalOut led4(LED3); // CHANGE TO LED 3
trapsis 4:354d278c0d52 45 DigitalOut reset(D0,1);
trapsis 4:354d278c0d52 46
trapsis 4:354d278c0d52 47 PortOut MyPort(PortD ,0xFF); // define a port with only the lower 8 bits included - that'llbe PTD0-PTD7 making a single 8 bit port.
trapsis 4:354d278c0d52 48 LCDTFT MyLCD(PTB0,PTB1,PTB2,PTB3,PTC2,&MyPort);//LCDTFT(PinName PIN_RD,PinName PIN_WR,PinName PIN_RS,PinName PIN_CS,PinName PIN_RESET, PortOut *PORTLCD);
cstevens 3:2b5f88cf893d 49
cstevens 3:2b5f88cf893d 50
trapsis 6:00d91197dd66 51 PwmOut led(LED_GREEN);
trapsis 6:00d91197dd66 52 TSISensor tsi;
epgmdm 0:7176e20dbdf6 53 volatile int state=0;
epgmdm 0:7176e20dbdf6 54 volatile int ready=0;
trapsis 5:882898bb83ed 55 //---
trapsis 5:882898bb83ed 56 void run();
trapsis 5:882898bb83ed 57 void countdown();
trapsis 5:882898bb83ed 58 void changeDirec();
trapsis 5:882898bb83ed 59 void nextmove();
trapsis 5:882898bb83ed 60 void initGrid();
trapsis 5:882898bb83ed 61 void updateGrid();
trapsis 5:882898bb83ed 62 void drawGrid();
trapsis 5:882898bb83ed 63 void checkposition();
trapsis 5:882898bb83ed 64 void gameover1();
trapsis 5:882898bb83ed 65 void gameover2();
trapsis 5:882898bb83ed 66 void PrettyPrint(char *message,const unsigned char *font,short xpos,short ypos,short bcol, short fcol);
trapsis 5:882898bb83ed 67 void prettyputc(char c, const unsigned char *font,short xpos,short ypos,short bcol, short fcol);
epgmdm 1:e2385bba1ab1 68
trapsis 6:00d91197dd66 69 char * resp=NULL;
trapsis 6:00d91197dd66 70
trapsis 6:00d91197dd66 71 bool GameRunning = 1;
trapsis 5:882898bb83ed 72 const int gridWidth = 64;
trapsis 5:882898bb83ed 73 const int gridHeight = 48;
trapsis 5:882898bb83ed 74 char grid [48][64];
trapsis 5:882898bb83ed 75 char snake [2][255];
trapsis 5:882898bb83ed 76 //represent the grid as a 2 dimensional array
trapsis 5:882898bb83ed 77
trapsis 5:882898bb83ed 78 char newDirectSnake = 2;
trapsis 5:882898bb83ed 79 char newDirectFood = 2;
trapsis 5:882898bb83ed 80 char newFoodVel = 0;
trapsis 5:882898bb83ed 81 //incoming values from the controller
trapsis 5:882898bb83ed 82
trapsis 5:882898bb83ed 83 int snakexpos = 20;
trapsis 5:882898bb83ed 84 int snakeypos = 20;
trapsis 5:882898bb83ed 85 char directSnake = 2;
trapsis 5:882898bb83ed 86 char snakeSize = 3;
trapsis 5:882898bb83ed 87 //establish values for the snake
trapsis 5:882898bb83ed 88
trapsis 5:882898bb83ed 89 int foodxpos = 2;
trapsis 5:882898bb83ed 90 int foodypos = 20;
trapsis 5:882898bb83ed 91 char foodvel = 1;
trapsis 5:882898bb83ed 92 char directFood = 1;
trapsis 5:882898bb83ed 93 //establish values for the food
trapsis 5:882898bb83ed 94
trapsis 5:882898bb83ed 95 char message [32];
trapsis 5:882898bb83ed 96 //for printing purposes
trapsis 5:882898bb83ed 97
trapsis 5:882898bb83ed 98 int framecount = 0;
trapsis 5:882898bb83ed 99 //number of iterations in the main loop
trapsis 5:882898bb83ed 100 //-----
epgmdm 1:e2385bba1ab1 101 char ipAddress[20];
epgmdm 1:e2385bba1ab1 102 char macAddress[32];
epgmdm 0:7176e20dbdf6 103 char *buffer;
epgmdm 0:7176e20dbdf6 104 unsigned int bufferPnt=0;
epgmdm 0:7176e20dbdf6 105
trapsis 6:00d91197dd66 106 /**
trapsis 6:00d91197dd66 107 Interpret data
trapsis 6:00d91197dd66 108 */
trapsis 6:00d91197dd66 109
trapsis 6:00d91197dd66 110 void dev_recv()
trapsis 6:00d91197dd66 111 {
trapsis 6:00d91197dd66 112 char c;
trapsis 6:00d91197dd66 113
trapsis 6:00d91197dd66 114 int count = 0;
trapsis 6:00d91197dd66 115 led1 = !led1;
trapsis 6:00d91197dd66 116 if(bufferPnt==0) {
trapsis 6:00d91197dd66 117 memset(buffer,0,BUFF_SIZE);
trapsis 6:00d91197dd66 118 }
trapsis 6:00d91197dd66 119 while(dev.readable()) {
trapsis 6:00d91197dd66 120 c = (char)dev.getc();
trapsis 6:00d91197dd66 121 #ifdef DEBUG
trapsis 6:00d91197dd66 122 pc.putc(c);
trapsis 6:00d91197dd66 123 #endif
trapsis 6:00d91197dd66 124 buffer[bufferPnt]=c;
trapsis 6:00d91197dd66 125 bufferPnt++;
trapsis 6:00d91197dd66 126 if (bufferPnt>1000) {
trapsis 6:00d91197dd66 127 ready=1;
trapsis 6:00d91197dd66 128 }
trapsis 6:00d91197dd66 129 // if ((c==0x0a)||(c==0x0d)){
trapsis 6:00d91197dd66 130 // ready=1;
trapsis 6:00d91197dd66 131 // }else
trapsis 6:00d91197dd66 132 if (c==0x0a) {
trapsis 6:00d91197dd66 133 if (bufferPnt>1) {
trapsis 6:00d91197dd66 134 if (buffer[bufferPnt -2]==0x0d) {
trapsis 6:00d91197dd66 135 ready=1;
trapsis 6:00d91197dd66 136 break;
trapsis 6:00d91197dd66 137 }
trapsis 6:00d91197dd66 138 }
trapsis 6:00d91197dd66 139 }
trapsis 6:00d91197dd66 140 if (!dev.readable()) {
trapsis 6:00d91197dd66 141 wait_us(10);
trapsis 6:00d91197dd66 142 }
trapsis 6:00d91197dd66 143 }
trapsis 6:00d91197dd66 144 }
trapsis 5:882898bb83ed 145
trapsis 6:00d91197dd66 146 void pc_recv()
trapsis 6:00d91197dd66 147 {
trapsis 6:00d91197dd66 148 char c;
trapsis 6:00d91197dd66 149 led4 = !led4;
trapsis 6:00d91197dd66 150 while(pc.readable()) {
trapsis 6:00d91197dd66 151 c=(char)pc.getc();
trapsis 6:00d91197dd66 152 dev.putc(c);
trapsis 6:00d91197dd66 153 pc.putc(c);
trapsis 6:00d91197dd66 154 if(c==13) {
trapsis 6:00d91197dd66 155 dev.putc(10);
trapsis 6:00d91197dd66 156 pc.putc(10);
trapsis 6:00d91197dd66 157 }
trapsis 6:00d91197dd66 158
trapsis 6:00d91197dd66 159 }
trapsis 6:00d91197dd66 160 }
trapsis 6:00d91197dd66 161
trapsis 6:00d91197dd66 162 char * OKResponse(char *test, const char *pattern)
trapsis 6:00d91197dd66 163 {
trapsis 6:00d91197dd66 164 char *p= strstr(test,pattern);
trapsis 6:00d91197dd66 165 if (p==NULL) {
trapsis 6:00d91197dd66 166 // pc.printf("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
trapsis 6:00d91197dd66 167 return NULL;
trapsis 6:00d91197dd66 168 } else {
trapsis 6:00d91197dd66 169 // pc.printf("YAY Test=<%s> Patter=<%s> [p=%s]",test,pattern,p);
trapsis 5:882898bb83ed 170 }
trapsis 6:00d91197dd66 171 return p;
trapsis 6:00d91197dd66 172 }
trapsis 6:00d91197dd66 173
trapsis 6:00d91197dd66 174 void commandReceived(char *buffer)
trapsis 6:00d91197dd66 175 {
trapsis 6:00d91197dd66 176
trapsis 6:00d91197dd66 177 INFO("Got <%s>",buffer);
trapsis 6:00d91197dd66 178
trapsis 6:00d91197dd66 179 /*const char s[2] = ":";
trapsis 6:00d91197dd66 180 char *token;
trapsis 6:00d91197dd66 181
trapsis 6:00d91197dd66 182 get the first token
trapsis 6:00d91197dd66 183 token = strtok(buffer, s);
trapsis 6:00d91197dd66 184 token = strtok(NULL, s);
trapsis 6:00d91197dd66 185 pc.printf("%s\n\r", token );*/
trapsis 6:00d91197dd66 186
trapsis 6:00d91197dd66 187 char identifier =buffer[9];
trapsis 6:00d91197dd66 188 if (identifier == 49) {
trapsis 6:00d91197dd66 189 newDirectFood=(buffer[13]-48);
trapsis 6:00d91197dd66 190 newFoodVel=(buffer[17]-48);
trapsis 6:00d91197dd66 191 }
trapsis 6:00d91197dd66 192
trapsis 6:00d91197dd66 193 if (identifier == 50) {
trapsis 6:00d91197dd66 194 newDirectSnake=(buffer[13]-48);
trapsis 6:00d91197dd66 195 }
trapsis 6:00d91197dd66 196
trapsis 6:00d91197dd66 197 if (identifier == 51) {
trapsis 6:00d91197dd66 198 GameRunning=(buffer[13]-48);
trapsis 6:00d91197dd66 199 }
trapsis 6:00d91197dd66 200
trapsis 6:00d91197dd66 201 pc.printf("cmd exit");
trapsis 5:882898bb83ed 202 }
trapsis 5:882898bb83ed 203
trapsis 5:882898bb83ed 204 // next two functions need moving to the library.....
trapsis 5:882898bb83ed 205 // ok - a simple sub to put in a character from the fonts defined in TFT_fonts
trapsis 5:882898bb83ed 206 // c= charactetr to put
trapsis 5:882898bb83ed 207 // font = pointer to font to use
trapsis 5:882898bb83ed 208 // x,y locatoin of bottom lh of character, bcol = background color, fcol = foreground color
trapsis 5:882898bb83ed 209 //prettyputc("m",Arial12x12,50,50,ColorBlack,ColorWhite);
trapsis 5:882898bb83ed 210 void prettyputc(char c, const unsigned char *font,short xpos,short ypos,short bcol, short fcol)
trapsis 5:882898bb83ed 211 {
trapsis 5:882898bb83ed 212 // Length,horz,vert,byte/vert
trapsis 5:882898bb83ed 213 int length,hor,vert,bpver; // number of bytes per character, horizontal pixels, vertical pixels and bytes per column
trapsis 5:882898bb83ed 214 //
trapsis 5:882898bb83ed 215 int x,y,i,j,k,ptr;
trapsis 5:882898bb83ed 216 short coltowrite;
trapsis 5:882898bb83ed 217 char byte,point;
trapsis 5:882898bb83ed 218 length=font[0];
trapsis 5:882898bb83ed 219 hor=font[1];
trapsis 5:882898bb83ed 220 vert=font[2];
trapsis 5:882898bb83ed 221 bpver=font[3];
trapsis 5:882898bb83ed 222 for(i=0; i<hor; i++) { // loop over columns
trapsis 5:882898bb83ed 223 for(j=0; j<vert; j++) {
trapsis 5:882898bb83ed 224 x=xpos+i;
trapsis 5:882898bb83ed 225 y=ypos+j; // NB assumes colums stored from bottom to top.... ?
trapsis 5:882898bb83ed 226 ptr=((c -32) * length+1) + 4+i*bpver+(j/8); // pointer in font array to start of the character we want +1 to avoid the first byte that holds the char width
trapsis 5:882898bb83ed 227 byte=(char)font[ptr];
trapsis 5:882898bb83ed 228 k=j%8; // number of the pixel in this byte
trapsis 5:882898bb83ed 229 point=byte & (1<<k); // get the next bit
trapsis 5:882898bb83ed 230 if(point>0) {
trapsis 5:882898bb83ed 231 coltowrite=fcol;
trapsis 5:882898bb83ed 232 } else {
trapsis 5:882898bb83ed 233 coltowrite=bcol;
trapsis 5:882898bb83ed 234 }
trapsis 5:882898bb83ed 235
trapsis 5:882898bb83ed 236 MyLCD.vLCDTFTPoint(x,y,coltowrite);
trapsis 5:882898bb83ed 237
trapsis 5:882898bb83ed 238 }
trapsis 5:882898bb83ed 239 }
trapsis 5:882898bb83ed 240
trapsis 5:882898bb83ed 241 }
trapsis 5:882898bb83ed 242
trapsis 5:882898bb83ed 243
trapsis 5:882898bb83ed 244
trapsis 5:882898bb83ed 245
trapsis 5:882898bb83ed 246 // ok now a function to use pretty putc to write strings whose bottom left corner are at xpo,ypos
trapsis 5:882898bb83ed 247 // general idea is that the string is first formatted byb sprintf and the PrettyPrint is called
trapsis 5:882898bb83ed 248 // bcol and fcol are the backgroun adn foreground colors
trapsis 5:882898bb83ed 249 //NB max message length = 64 characters
trapsis 5:882898bb83ed 250 void PrettyPrint(char *message,const unsigned char *font,short xpos,short ypos,short bcol, short fcol)
trapsis 5:882898bb83ed 251 {
trapsis 5:882898bb83ed 252 short x,y,messlength,i,ptr;
trapsis 5:882898bb83ed 253 messlength=strlen(message);
trapsis 5:882898bb83ed 254 if (messlength >64) messlength=64; // avoid writing too large a string....
trapsis 5:882898bb83ed 255 x=xpos;
trapsis 5:882898bb83ed 256 for(i=0; i<messlength; i++) {
trapsis 5:882898bb83ed 257 // x=xpos+i*(font[1]*8/10); // font[1]=char width 80% to avoid gaps
trapsis 5:882898bb83ed 258 //pointer = &font[((c -32) * offset) + 4]; // start of char bitmap
trapsis 5:882898bb83ed 259 // w = pointer[0]; // width of actual char
trapsis 5:882898bb83ed 260 ptr=((message[i] -32) * font[0]) + 4;
trapsis 5:882898bb83ed 261
trapsis 5:882898bb83ed 262 y=ypos; // will have to add cod and more to deal with different screen orientations
trapsis 5:882898bb83ed 263 //prettyputc(char c, const unsigned char *font,short xpos,short ypos,short bcol, short fcol)
trapsis 5:882898bb83ed 264 prettyputc(message[i],font,x,y,bcol,fcol);
trapsis 5:882898bb83ed 265 x=x+font[ptr]+2;
trapsis 5:882898bb83ed 266 }
trapsis 5:882898bb83ed 267
trapsis 5:882898bb83ed 268 }
trapsis 5:882898bb83ed 269
trapsis 5:882898bb83ed 270
trapsis 5:882898bb83ed 271 void run()
trapsis 5:882898bb83ed 272 //the main game program
trapsis 5:882898bb83ed 273 {
trapsis 5:882898bb83ed 274 while (1) {
trapsis 6:00d91197dd66 275 GameRunning = 1;
trapsis 5:882898bb83ed 276 if (GameRunning == 1) {
trapsis 6:00d91197dd66 277 pc.printf("m");
trapsis 5:882898bb83ed 278 countdown();
trapsis 6:00d91197dd66 279 pc.printf("n");
trapsis 5:882898bb83ed 280 initGrid();
trapsis 6:00d91197dd66 281 pc.printf("p");
trapsis 5:882898bb83ed 282 while (1) {
trapsis 6:00d91197dd66 283 pc.printf("r %d",ready);
trapsis 5:882898bb83ed 284 checkposition();
trapsis 6:00d91197dd66 285 if (ready == 1) {
trapsis 6:00d91197dd66 286 pc.printf("A");
trapsis 6:00d91197dd66 287 ready = 0;
trapsis 6:00d91197dd66 288 resp=OKResponse(buffer,"+IPD");
trapsis 6:00d91197dd66 289 if (resp!=NULL) {
trapsis 6:00d91197dd66 290 pc.printf("B");
trapsis 6:00d91197dd66 291 commandReceived(buffer); // Interpret data // Keep on waiting for data
trapsis 6:00d91197dd66 292 }
trapsis 6:00d91197dd66 293 }
trapsis 5:882898bb83ed 294 changeDirec();
trapsis 5:882898bb83ed 295 nextmove();
trapsis 5:882898bb83ed 296 updateGrid();
trapsis 5:882898bb83ed 297 drawGrid();
trapsis 5:882898bb83ed 298 framecount++;
trapsis 5:882898bb83ed 299 }
trapsis 5:882898bb83ed 300 }
trapsis 5:882898bb83ed 301 }
trapsis 5:882898bb83ed 302
trapsis 5:882898bb83ed 303 }
trapsis 5:882898bb83ed 304
trapsis 5:882898bb83ed 305
trapsis 5:882898bb83ed 306 void countdown()
trapsis 5:882898bb83ed 307 //countdown before game begins
trapsis 5:882898bb83ed 308 {
trapsis 5:882898bb83ed 309 MyLCD.vLCDTFTInit(1);
trapsis 5:882898bb83ed 310 MyLCD.vLCDTFTFillScreen(ColorBlack);
trapsis 5:882898bb83ed 311
trapsis 5:882898bb83ed 312 sprintf(message,"Get Ready!");
trapsis 5:882898bb83ed 313 PrettyPrint(message,Arial28x28,10,50,ColorBlack,ColorLime);
trapsis 5:882898bb83ed 314 wait(1);
trapsis 5:882898bb83ed 315
trapsis 5:882898bb83ed 316 MyLCD.vLCDTFTFillScreen(ColorBlack);
trapsis 5:882898bb83ed 317 sprintf(message,"3");
trapsis 5:882898bb83ed 318 PrettyPrint(message,Arial28x28,10,50,ColorBlack,ColorLime);
trapsis 5:882898bb83ed 319 wait(1);
trapsis 5:882898bb83ed 320
trapsis 5:882898bb83ed 321 MyLCD.vLCDTFTFillScreen(ColorBlack);
trapsis 5:882898bb83ed 322 sprintf(message,"2");
trapsis 5:882898bb83ed 323 PrettyPrint(message,Arial28x28,10,50,ColorBlack,ColorLime);
trapsis 5:882898bb83ed 324 wait(1);
trapsis 5:882898bb83ed 325
trapsis 5:882898bb83ed 326 MyLCD.vLCDTFTFillScreen(ColorBlack);
trapsis 5:882898bb83ed 327 sprintf(message,"1");
trapsis 5:882898bb83ed 328 PrettyPrint(message,Arial28x28,10,50,ColorBlack,ColorLime);
trapsis 5:882898bb83ed 329 wait(1);
trapsis 5:882898bb83ed 330
trapsis 5:882898bb83ed 331 MyLCD.vLCDTFTFillScreen(ColorBlack);
trapsis 5:882898bb83ed 332 sprintf(message,"Run!");
trapsis 5:882898bb83ed 333 PrettyPrint(message,Arial28x28,10,50,ColorBlack,ColorLime);
trapsis 5:882898bb83ed 334 wait(1);
trapsis 5:882898bb83ed 335
trapsis 5:882898bb83ed 336 }
trapsis 5:882898bb83ed 337
trapsis 5:882898bb83ed 338 void initGrid()
trapsis 5:882898bb83ed 339 //initialize the grid
trapsis 5:882898bb83ed 340 {
trapsis 5:882898bb83ed 341 MyLCD.vLCDTFTInit(1);
trapsis 5:882898bb83ed 342 MyLCD.vLCDTFTFillScreen(ColorBlack);
trapsis 5:882898bb83ed 343
trapsis 5:882898bb83ed 344 for(int x=0; x<gridHeight; x++) {
trapsis 5:882898bb83ed 345 for(int y=0; y<gridWidth; y++) {
trapsis 5:882898bb83ed 346 grid[x][y] = 0;
trapsis 5:882898bb83ed 347 }
trapsis 5:882898bb83ed 348 }
trapsis 5:882898bb83ed 349 //initialize the pixel grid as an array of zeros
trapsis 5:882898bb83ed 350 snake[0][0] = snakexpos;
trapsis 5:882898bb83ed 351 snake[1][0] = 20;
trapsis 5:882898bb83ed 352 snake[0][1] = 21;
trapsis 5:882898bb83ed 353 snake[1][1] = 20;
trapsis 5:882898bb83ed 354 snake[0][2] = 22;
trapsis 5:882898bb83ed 355 snake[1][2] = 20;
trapsis 5:882898bb83ed 356
trapsis 6:00d91197dd66 357 for(int i=3; i<255; i++) {
trapsis 5:882898bb83ed 358 grid[snake[1][i]][snake[0][i]] = 0;
trapsis 5:882898bb83ed 359 }
trapsis 5:882898bb83ed 360 //initialize the snake as horizontal with respect to its initial size (1 = tile occupied by snake)
trapsis 5:882898bb83ed 361
trapsis 5:882898bb83ed 362 grid[foodypos][foodxpos] = 2;
trapsis 5:882898bb83ed 363 //initialize the food (2 = tile occupied by food)
trapsis 5:882898bb83ed 364 }
trapsis 5:882898bb83ed 365
trapsis 5:882898bb83ed 366 void updateGrid()
trapsis 5:882898bb83ed 367 //update the grid with positions
trapsis 5:882898bb83ed 368 {
trapsis 5:882898bb83ed 369 /*if (grid[snakexpos][snakeypos] == 1)
trapsis 5:882898bb83ed 370 gameover2();
trapsis 5:882898bb83ed 371 else
trapsis 5:882898bb83ed 372 {
trapsis 6:00d91197dd66 373 */
trapsis 6:00d91197dd66 374 for(int a=snakeSize-2; a>=0; a--) {
trapsis 5:882898bb83ed 375
trapsis 6:00d91197dd66 376 snake[0][a+1] = snake [0][a];
trapsis 6:00d91197dd66 377 snake[1][a+1] = snake [1][a];
trapsis 6:00d91197dd66 378 // }
trapsis 6:00d91197dd66 379
trapsis 5:882898bb83ed 380 snake[0][0] = snakexpos;
trapsis 5:882898bb83ed 381 snake[1][0] = snakeypos;
trapsis 5:882898bb83ed 382 }
trapsis 5:882898bb83ed 383
trapsis 5:882898bb83ed 384 for(int x=0; x<gridHeight; x++) {
trapsis 5:882898bb83ed 385 for(int y=0; y<gridWidth; y++) {
trapsis 5:882898bb83ed 386 grid[x][y] = 0;
trapsis 5:882898bb83ed 387 }
trapsis 5:882898bb83ed 388 }
trapsis 5:882898bb83ed 389
trapsis 5:882898bb83ed 390 for(int i=0; i<snakeSize; i++) {
trapsis 5:882898bb83ed 391 grid[snake[1][i]][snake[0][i]] = 1;
trapsis 5:882898bb83ed 392
trapsis 5:882898bb83ed 393 }
trapsis 5:882898bb83ed 394 //update the snake, if the tile was previously occupied, snake has hit itself = gameover2
trapsis 5:882898bb83ed 395
trapsis 5:882898bb83ed 396 if (grid[foodypos][foodxpos] == 1) {
trapsis 5:882898bb83ed 397 gameover1();
trapsis 5:882898bb83ed 398 }
trapsis 5:882898bb83ed 399
trapsis 5:882898bb83ed 400 else {
trapsis 5:882898bb83ed 401 grid[foodypos][foodxpos] = 2;
trapsis 5:882898bb83ed 402 }
trapsis 5:882898bb83ed 403 //update the food, if the tile was previously occupied, food has hit snake = gameover1
trapsis 5:882898bb83ed 404 }
trapsis 5:882898bb83ed 405
trapsis 5:882898bb83ed 406
trapsis 5:882898bb83ed 407 void drawGrid()
trapsis 5:882898bb83ed 408 //draw the grid
trapsis 5:882898bb83ed 409 {
trapsis 5:882898bb83ed 410
trapsis 5:882898bb83ed 411 for(int x=0; x<gridHeight; x++) {
trapsis 5:882898bb83ed 412 for(int y=0; y<gridWidth; y++) {
trapsis 6:00d91197dd66 413
trapsis 5:882898bb83ed 414 if (grid[x][y] == 1) {
trapsis 5:882898bb83ed 415 MyLCD.vLCDTFTRectangle(5*y,5*x,5*y+5,5*x+5,1,ColorBlue);
trapsis 5:882898bb83ed 416 }
trapsis 5:882898bb83ed 417 if (grid[x][y] == 2) {
trapsis 5:882898bb83ed 418 MyLCD.vLCDTFTRectangle(5*y,5*x,5*y+5,5*x+5,1,ColorWhite);
trapsis 5:882898bb83ed 419 }
trapsis 5:882898bb83ed 420 }
trapsis 5:882898bb83ed 421 }
trapsis 5:882898bb83ed 422
trapsis 6:00d91197dd66 423 wait (0.5); //the game is 10fps (up for discussion we'll see how this goes)
trapsis 5:882898bb83ed 424 }
trapsis 5:882898bb83ed 425
trapsis 5:882898bb83ed 426 void changeDirec()
trapsis 5:882898bb83ed 427 //changing direction, the command will have to come from the controller program
trapsis 5:882898bb83ed 428 //assume 0 for up, 1 for right, 2 for down, 3 for left
trapsis 5:882898bb83ed 429 {
trapsis 5:882898bb83ed 430 foodvel = newFoodVel;
trapsis 5:882898bb83ed 431 directFood = newDirectFood;
trapsis 5:882898bb83ed 432 //assign new direction and speed to food
trapsis 5:882898bb83ed 433
trapsis 5:882898bb83ed 434 if ((newDirectSnake + directSnake)%2 == 0 && newDirectSnake != directSnake)
trapsis 6:00d91197dd66 435 gameover2();
trapsis 5:882898bb83ed 436 //the snake loses if it tries to double up on itself
trapsis 5:882898bb83ed 437
trapsis 5:882898bb83ed 438 else
trapsis 6:00d91197dd66 439 directSnake = newDirectSnake;
trapsis 5:882898bb83ed 440 //assign new direction to snake
trapsis 5:882898bb83ed 441 }
trapsis 5:882898bb83ed 442
trapsis 5:882898bb83ed 443
trapsis 5:882898bb83ed 444 void nextmove()
trapsis 5:882898bb83ed 445 //move the snake and the food by incrementing their coordinates (for the food increment by velocity)
trapsis 5:882898bb83ed 446 {
trapsis 6:00d91197dd66 447 if (framecount%5 == 0) {
trapsis 5:882898bb83ed 448 snakeSize++;
trapsis 6:00d91197dd66 449 }
trapsis 5:882898bb83ed 450 // every 5 iterations, the snake grows
trapsis 6:00d91197dd66 451
trapsis 6:00d91197dd66 452
trapsis 5:882898bb83ed 453 MyLCD.vLCDTFTRectangle(5*foodxpos,5*foodypos,5*foodxpos+5,5*foodypos+5,1,ColorBlack);
trapsis 5:882898bb83ed 454 MyLCD.vLCDTFTRectangle(5*snake[0][snakeSize-1],5*snake[1][snakeSize-1],5*snake[0][snakeSize-1]+5,5*snake[1][snakeSize-1]+5,1,ColorBlack);
trapsis 5:882898bb83ed 455
trapsis 6:00d91197dd66 456
trapsis 5:882898bb83ed 457 if (directFood == 0) {
trapsis 5:882898bb83ed 458 foodypos -= foodvel;
trapsis 5:882898bb83ed 459 }
trapsis 5:882898bb83ed 460 if (directFood == 1) {
trapsis 5:882898bb83ed 461 foodxpos += foodvel;
trapsis 5:882898bb83ed 462 }
trapsis 5:882898bb83ed 463 if (directFood == 2) {
trapsis 5:882898bb83ed 464 foodypos += foodvel;
trapsis 5:882898bb83ed 465 }
trapsis 5:882898bb83ed 466 if (directFood == 3) {
trapsis 5:882898bb83ed 467 foodxpos -= foodvel;
trapsis 5:882898bb83ed 468 }
trapsis 5:882898bb83ed 469 // check the direction of food and increment position according to velocity
trapsis 5:882898bb83ed 470
trapsis 5:882898bb83ed 471 if (directSnake == 0) {
trapsis 5:882898bb83ed 472 snakeypos -= 1;
trapsis 5:882898bb83ed 473 }
trapsis 5:882898bb83ed 474 if (directSnake == 1) {
trapsis 5:882898bb83ed 475 snakexpos += 1;
trapsis 5:882898bb83ed 476 }
trapsis 5:882898bb83ed 477 if (directSnake == 2) {
trapsis 5:882898bb83ed 478 snakeypos += 1;
trapsis 5:882898bb83ed 479 }
trapsis 5:882898bb83ed 480 if (directSnake == 3) {
trapsis 5:882898bb83ed 481 snakexpos -= 1;
trapsis 5:882898bb83ed 482 }
trapsis 5:882898bb83ed 483
trapsis 5:882898bb83ed 484 }
trapsis 5:882898bb83ed 485
trapsis 5:882898bb83ed 486 void checkposition()
trapsis 5:882898bb83ed 487 //check position to see if either player is out of bounds
trapsis 5:882898bb83ed 488 {
trapsis 5:882898bb83ed 489 if (snakeypos > gridHeight || snakeypos < 0)
trapsis 5:882898bb83ed 490 gameover2();
trapsis 5:882898bb83ed 491
trapsis 5:882898bb83ed 492 if (snakexpos > gridWidth || snakexpos < 0)
trapsis 5:882898bb83ed 493 gameover2();
trapsis 5:882898bb83ed 494
trapsis 5:882898bb83ed 495 if (foodypos > gridHeight || foodypos < 0)
trapsis 5:882898bb83ed 496 gameover1();
trapsis 5:882898bb83ed 497
trapsis 5:882898bb83ed 498 if (foodxpos > gridWidth || foodxpos < 0)
trapsis 5:882898bb83ed 499 gameover1();
trapsis 5:882898bb83ed 500
trapsis 5:882898bb83ed 501 //end the game with the appropriate game over screen if either player is out of bounds
trapsis 5:882898bb83ed 502 }
trapsis 5:882898bb83ed 503
trapsis 5:882898bb83ed 504
trapsis 5:882898bb83ed 505 void gameover1()
trapsis 5:882898bb83ed 506 //print game over message and display final score
trapsis 5:882898bb83ed 507 {
trapsis 5:882898bb83ed 508 MyLCD.vLCDTFTFillScreen(ColorBlack);
trapsis 5:882898bb83ed 509
trapsis 5:882898bb83ed 510 sprintf(message,"You are snake food!");
trapsis 5:882898bb83ed 511 PrettyPrint(message,Arial28x28,10,50,ColorBlack,ColorLime);
trapsis 5:882898bb83ed 512
trapsis 5:882898bb83ed 513
trapsis 5:882898bb83ed 514 GameRunning = 0;
trapsis 6:00d91197dd66 515
trapsis 6:00d91197dd66 516 dev.printf("AT+CIPSEND=3,3\r\n");
trapsis 6:00d91197dd66 517 wait(0.2);
trapsis 6:00d91197dd66 518 dev.printf("0\r\n");
trapsis 6:00d91197dd66 519
trapsis 6:00d91197dd66 520 wait (1000);
trapsis 6:00d91197dd66 521
trapsis 6:00d91197dd66 522 }
trapsis 6:00d91197dd66 523
trapsis 5:882898bb83ed 524
trapsis 5:882898bb83ed 525 void gameover2()
trapsis 5:882898bb83ed 526 //an alternate ending is that the snake has made a mistake and that the food wins
trapsis 5:882898bb83ed 527 {
trapsis 5:882898bb83ed 528 MyLCD.vLCDTFTFillScreen(ColorBlack);
trapsis 5:882898bb83ed 529 sprintf(message,"The snake is dead!");
trapsis 5:882898bb83ed 530 PrettyPrint(message,Arial28x28,10,50,ColorBlack,ColorLime);
trapsis 6:00d91197dd66 531
trapsis 6:00d91197dd66 532
trapsis 6:00d91197dd66 533 GameRunning = 0;
trapsis 6:00d91197dd66 534
trapsis 6:00d91197dd66 535 dev.printf("AT+CIPSEND=3,3\r\n");
trapsis 6:00d91197dd66 536 wait(0.2);
trapsis 6:00d91197dd66 537 dev.printf("0\r\n");
trapsis 6:00d91197dd66 538
trapsis 5:882898bb83ed 539 wait (1000);
trapsis 5:882898bb83ed 540
trapsis 5:882898bb83ed 541 }
trapsis 5:882898bb83ed 542
epgmdm 0:7176e20dbdf6 543
trapsis 6:00d91197dd66 544 /**
trapsis 6:00d91197dd66 545 * Set up server and run the game
trapsis 4:354d278c0d52 546 */
trapsis 5:882898bb83ed 547
epgmdm 0:7176e20dbdf6 548 int main()
epgmdm 0:7176e20dbdf6 549 {
cstevens 3:2b5f88cf893d 550 float touch;
epgmdm 0:7176e20dbdf6 551 buffer=(char *)calloc(BUFF_SIZE,1);
epgmdm 0:7176e20dbdf6 552 reset=0;
cstevens 3:2b5f88cf893d 553 int counter=0;
epgmdm 0:7176e20dbdf6 554 pc.baud(115200);
epgmdm 0:7176e20dbdf6 555 dev.baud(115200);
epgmdm 0:7176e20dbdf6 556 pc.attach(&pc_recv, Serial::RxIrq);
epgmdm 0:7176e20dbdf6 557 dev.attach(&dev_recv, Serial::RxIrq);
epgmdm 0:7176e20dbdf6 558 pc.printf("Start up\n\r");
epgmdm 0:7176e20dbdf6 559 wait(1.5);
epgmdm 0:7176e20dbdf6 560 reset=1;
epgmdm 0:7176e20dbdf6 561 pc.printf("Here \n\r");
epgmdm 0:7176e20dbdf6 562 while(1) {
epgmdm 0:7176e20dbdf6 563 if (ready) {
epgmdm 0:7176e20dbdf6 564 ready=0;
epgmdm 0:7176e20dbdf6 565 bufferPnt=0;
cstevens 3:2b5f88cf893d 566 INFO("[%d],##%s##",state,buffer);
epgmdm 0:7176e20dbdf6 567 switch (state) {
epgmdm 0:7176e20dbdf6 568 case 0: {
epgmdm 0:7176e20dbdf6 569 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 570 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 571 wait(1);
epgmdm 0:7176e20dbdf6 572 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 573 state++;
epgmdm 0:7176e20dbdf6 574 }
epgmdm 0:7176e20dbdf6 575 break;
epgmdm 0:7176e20dbdf6 576 }
epgmdm 0:7176e20dbdf6 577 case 1:
epgmdm 0:7176e20dbdf6 578 case 2: {
epgmdm 0:7176e20dbdf6 579 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 580 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 581 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 582 state++;
epgmdm 0:7176e20dbdf6 583 }
epgmdm 0:7176e20dbdf6 584 break;
epgmdm 0:7176e20dbdf6 585 }
epgmdm 0:7176e20dbdf6 586 case 3: {
epgmdm 0:7176e20dbdf6 587 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 588 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 589 dev.printf("AT+RST\r\n");
epgmdm 0:7176e20dbdf6 590 state++;
epgmdm 0:7176e20dbdf6 591 }
epgmdm 0:7176e20dbdf6 592
epgmdm 0:7176e20dbdf6 593 break;
epgmdm 0:7176e20dbdf6 594 }
epgmdm 0:7176e20dbdf6 595 case 4: {
epgmdm 0:7176e20dbdf6 596 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 597 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 598 dev.printf("AT+CWMODE=1\r\n");
epgmdm 0:7176e20dbdf6 599 state++;
epgmdm 0:7176e20dbdf6 600 }
epgmdm 0:7176e20dbdf6 601
epgmdm 0:7176e20dbdf6 602 break;
epgmdm 0:7176e20dbdf6 603 }
epgmdm 0:7176e20dbdf6 604 case 5: {
epgmdm 0:7176e20dbdf6 605 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 606 if (resp!=NULL) {
trapsis 6:00d91197dd66 607
epgmdm 0:7176e20dbdf6 608 dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
epgmdm 0:7176e20dbdf6 609 state++;
epgmdm 0:7176e20dbdf6 610 }
epgmdm 0:7176e20dbdf6 611
epgmdm 0:7176e20dbdf6 612 break;
epgmdm 0:7176e20dbdf6 613 }
epgmdm 0:7176e20dbdf6 614 case 6: {
epgmdm 0:7176e20dbdf6 615 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 616 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 617 wait(1);
epgmdm 0:7176e20dbdf6 618 dev.printf("AT+CIFSR\r\n");
epgmdm 0:7176e20dbdf6 619 state++;
epgmdm 0:7176e20dbdf6 620 }
epgmdm 0:7176e20dbdf6 621
epgmdm 0:7176e20dbdf6 622 break;
epgmdm 0:7176e20dbdf6 623 }
epgmdm 0:7176e20dbdf6 624 case 7: {
epgmdm 1:e2385bba1ab1 625 resp=OKResponse(buffer,"+CIFSR:STAIP,");
epgmdm 0:7176e20dbdf6 626 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 627 char *strt = strtok(buffer,"\"");
epgmdm 1:e2385bba1ab1 628 strcpy(ipAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 629 strtok(NULL,"\"");
trapsis 6:00d91197dd66 630 strcpy(macAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 631 INFO("mac Address = %s", macAddress);
epgmdm 1:e2385bba1ab1 632 INFO("IP Address = %s", ipAddress);
trapsis 6:00d91197dd66 633 dev.printf("AT+CIPMUX=1\r\n");
epgmdm 0:7176e20dbdf6 634 state++;
epgmdm 0:7176e20dbdf6 635 }
epgmdm 0:7176e20dbdf6 636
epgmdm 0:7176e20dbdf6 637 break;
epgmdm 0:7176e20dbdf6 638 }
epgmdm 1:e2385bba1ab1 639 case 8: {
epgmdm 1:e2385bba1ab1 640 resp=OKResponse(buffer,"OK");
epgmdm 1:e2385bba1ab1 641 if (resp!=NULL) {
trapsis 6:00d91197dd66 642 INFO("Ready");
trapsis 6:00d91197dd66 643 dev.printf("AT+CIPSERVER=1,5050\r\n"); //Set it as a server
trapsis 6:00d91197dd66 644 state++;
epgmdm 1:e2385bba1ab1 645 }
epgmdm 1:e2385bba1ab1 646
trapsis 6:00d91197dd66 647 break;
trapsis 4:354d278c0d52 648 }
trapsis 6:00d91197dd66 649
cstevens 3:2b5f88cf893d 650 case 9: {
trapsis 4:354d278c0d52 651 resp=OKResponse(buffer,"+IPD");
trapsis 4:354d278c0d52 652 if (resp!=NULL) {
trapsis 6:00d91197dd66 653 pc.printf("Here 1");
trapsis 6:00d91197dd66 654 commandReceived(buffer); // Interpret data
trapsis 6:00d91197dd66 655 pc.printf("Here 2");
trapsis 6:00d91197dd66 656 run();
trapsis 6:00d91197dd66 657 pc.printf("Here 3");
trapsis 6:00d91197dd66 658 state=9; // Keep on waiting for data
cstevens 3:2b5f88cf893d 659 }
cstevens 3:2b5f88cf893d 660
trapsis 6:00d91197dd66 661
cstevens 3:2b5f88cf893d 662
trapsis 6:00d91197dd66 663 break;
trapsis 6:00d91197dd66 664 }
trapsis 6:00d91197dd66 665
epgmdm 0:7176e20dbdf6 666
epgmdm 0:7176e20dbdf6 667
epgmdm 0:7176e20dbdf6 668 }
epgmdm 0:7176e20dbdf6 669 }
trapsis 6:00d91197dd66 670
epgmdm 0:7176e20dbdf6 671 }
epgmdm 0:7176e20dbdf6 672 }