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 10:38:32 2016 +0000
Revision:
4:354d278c0d52
Parent:
3:2b5f88cf893d
Child:
5:882898bb83ed
Server game integration v1.0

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
epgmdm 0:7176e20dbdf6 14 #define DBG(x, ...) pc.printf(" ["FUNCNAME" : DBG] "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
cstevens 3:2b5f88cf893d 51 PwmOut led(LED_GREEN);
cstevens 3:2b5f88cf893d 52 TSISensor tsi;
epgmdm 0:7176e20dbdf6 53 volatile int state=0;
epgmdm 0:7176e20dbdf6 54 volatile int ready=0;
epgmdm 1:e2385bba1ab1 55
epgmdm 1:e2385bba1ab1 56 char ipAddress[20];
epgmdm 1:e2385bba1ab1 57 char macAddress[32];
epgmdm 0:7176e20dbdf6 58 char *buffer;
epgmdm 0:7176e20dbdf6 59 unsigned int bufferPnt=0;
epgmdm 0:7176e20dbdf6 60
epgmdm 0:7176e20dbdf6 61 void dev_recv()
epgmdm 0:7176e20dbdf6 62 {
epgmdm 0:7176e20dbdf6 63 char c;
epgmdm 0:7176e20dbdf6 64
epgmdm 0:7176e20dbdf6 65 int count = 0;
epgmdm 0:7176e20dbdf6 66 led1 = !led1;
epgmdm 0:7176e20dbdf6 67 if(bufferPnt==0) {
epgmdm 0:7176e20dbdf6 68 memset(buffer,0,BUFF_SIZE);
epgmdm 0:7176e20dbdf6 69 }
epgmdm 0:7176e20dbdf6 70 while(dev.readable()) {
epgmdm 0:7176e20dbdf6 71 c = (char)dev.getc();
epgmdm 1:e2385bba1ab1 72 #ifdef DEBUG
cstevens 3:2b5f88cf893d 73 pc.putc(c);
epgmdm 1:e2385bba1ab1 74 #endif
epgmdm 0:7176e20dbdf6 75 buffer[bufferPnt]=c;
epgmdm 0:7176e20dbdf6 76 bufferPnt++;
cstevens 3:2b5f88cf893d 77 if (bufferPnt>1000) {
epgmdm 0:7176e20dbdf6 78 ready=1;
epgmdm 1:e2385bba1ab1 79 }
cstevens 3:2b5f88cf893d 80 // if ((c==0x0a)||(c==0x0d)){
cstevens 3:2b5f88cf893d 81 // ready=1;
cstevens 3:2b5f88cf893d 82 // }else
epgmdm 0:7176e20dbdf6 83 if (c==0x0a) {
epgmdm 0:7176e20dbdf6 84 if (bufferPnt>1) {
epgmdm 0:7176e20dbdf6 85 if (buffer[bufferPnt -2]==0x0d) {
epgmdm 0:7176e20dbdf6 86 ready=1;
epgmdm 0:7176e20dbdf6 87 break;
epgmdm 0:7176e20dbdf6 88 }
epgmdm 0:7176e20dbdf6 89 }
epgmdm 0:7176e20dbdf6 90 }
epgmdm 1:e2385bba1ab1 91 if (!dev.readable()) {
cstevens 3:2b5f88cf893d 92 wait_us(10);
epgmdm 0:7176e20dbdf6 93 }
epgmdm 0:7176e20dbdf6 94 }
epgmdm 0:7176e20dbdf6 95 }
epgmdm 0:7176e20dbdf6 96
epgmdm 0:7176e20dbdf6 97 void pc_recv()
epgmdm 0:7176e20dbdf6 98 {
epgmdm 0:7176e20dbdf6 99 char c;
epgmdm 0:7176e20dbdf6 100 led4 = !led4;
epgmdm 0:7176e20dbdf6 101 while(pc.readable()) {
epgmdm 0:7176e20dbdf6 102 c=(char)pc.getc();
epgmdm 0:7176e20dbdf6 103 dev.putc(c);
epgmdm 0:7176e20dbdf6 104 pc.putc(c);
cstevens 3:2b5f88cf893d 105 if(c==13) {
cstevens 3:2b5f88cf893d 106 dev.putc(10);
cstevens 3:2b5f88cf893d 107 pc.putc(10);
cstevens 3:2b5f88cf893d 108 }
epgmdm 0:7176e20dbdf6 109
epgmdm 0:7176e20dbdf6 110 }
epgmdm 0:7176e20dbdf6 111 }
epgmdm 0:7176e20dbdf6 112
epgmdm 0:7176e20dbdf6 113 char * OKResponse(char *test, const char *pattern)
epgmdm 0:7176e20dbdf6 114 {
epgmdm 0:7176e20dbdf6 115 char *p= strstr(test,pattern);
epgmdm 0:7176e20dbdf6 116 if (p==NULL) {
epgmdm 0:7176e20dbdf6 117 // DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
epgmdm 0:7176e20dbdf6 118 return NULL;
epgmdm 0:7176e20dbdf6 119 } else {
epgmdm 0:7176e20dbdf6 120 // DBG("YAY Test=<%s> Patter=<%s> [p=%s]",test,pattern,p);
epgmdm 0:7176e20dbdf6 121 }
epgmdm 0:7176e20dbdf6 122 return p;
epgmdm 0:7176e20dbdf6 123 }
trapsis 4:354d278c0d52 124 /*Game programme
trapsis 4:354d278c0d52 125 */
trapsis 4:354d278c0d52 126 void commandReceived(char* buffer){
trapsis 4:354d278c0d52 127 char identifier =buffer[0];
trapsis 4:354d278c0d52 128 if (identifier == 1) {
trapsis 4:354d278c0d52 129 newFoodDirect=buffer[4];
trapsis 4:354d278c0d52 130 newFoodVel=buffer[8];
trapsis 4:354d278c0d52 131 }
trapsis 4:354d278c0d52 132
trapsis 4:354d278c0d52 133 if (identifier == 2) {
trapsis 4:354d278c0d52 134 newSnakeDirect=buffer[4];
trapsis 4:354d278c0d52 135 }
trapsis 4:354d278c0d52 136
trapsis 4:354d278c0d52 137 if (identifier == 3) {
trapsis 4:354d278c0d52 138 GameRunning=buffer[4];
trapsis 4:354d278c0d52 139 }
trapsis 4:354d278c0d52 140
epgmdm 0:7176e20dbdf6 141 int main()
epgmdm 0:7176e20dbdf6 142 {
cstevens 3:2b5f88cf893d 143 float touch;
epgmdm 0:7176e20dbdf6 144 buffer=(char *)calloc(BUFF_SIZE,1);
epgmdm 0:7176e20dbdf6 145 reset=0;
cstevens 3:2b5f88cf893d 146 int counter=0;
epgmdm 0:7176e20dbdf6 147 pc.baud(115200);
epgmdm 0:7176e20dbdf6 148 dev.baud(115200);
epgmdm 0:7176e20dbdf6 149 pc.attach(&pc_recv, Serial::RxIrq);
epgmdm 0:7176e20dbdf6 150 dev.attach(&dev_recv, Serial::RxIrq);
epgmdm 0:7176e20dbdf6 151 pc.printf("Start up\n\r");
epgmdm 0:7176e20dbdf6 152 wait(1.5);
epgmdm 0:7176e20dbdf6 153 reset=1;
epgmdm 0:7176e20dbdf6 154 char * resp=NULL;
epgmdm 0:7176e20dbdf6 155 pc.printf("Here \n\r");
epgmdm 0:7176e20dbdf6 156 while(1) {
epgmdm 0:7176e20dbdf6 157 if (ready) {
epgmdm 0:7176e20dbdf6 158 ready=0;
epgmdm 0:7176e20dbdf6 159 bufferPnt=0;
cstevens 3:2b5f88cf893d 160 INFO("[%d],##%s##",state,buffer);
epgmdm 0:7176e20dbdf6 161 switch (state) {
epgmdm 0:7176e20dbdf6 162 case 0: {
epgmdm 0:7176e20dbdf6 163 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 164 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 165 wait(1);
epgmdm 0:7176e20dbdf6 166 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 167 state++;
epgmdm 0:7176e20dbdf6 168 }
epgmdm 0:7176e20dbdf6 169 break;
epgmdm 0:7176e20dbdf6 170 }
epgmdm 0:7176e20dbdf6 171 case 1:
epgmdm 0:7176e20dbdf6 172 case 2: {
epgmdm 0:7176e20dbdf6 173 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 174 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 175 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 176 state++;
epgmdm 0:7176e20dbdf6 177 }
epgmdm 0:7176e20dbdf6 178 break;
epgmdm 0:7176e20dbdf6 179 }
epgmdm 0:7176e20dbdf6 180 case 3: {
epgmdm 0:7176e20dbdf6 181 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 182 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 183 dev.printf("AT+RST\r\n");
epgmdm 0:7176e20dbdf6 184 state++;
epgmdm 0:7176e20dbdf6 185 }
epgmdm 0:7176e20dbdf6 186
epgmdm 0:7176e20dbdf6 187 break;
epgmdm 0:7176e20dbdf6 188 }
epgmdm 0:7176e20dbdf6 189 case 4: {
epgmdm 0:7176e20dbdf6 190 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 191 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 192 dev.printf("AT+CWMODE=1\r\n");
epgmdm 0:7176e20dbdf6 193 state++;
epgmdm 0:7176e20dbdf6 194 }
epgmdm 0:7176e20dbdf6 195
epgmdm 0:7176e20dbdf6 196 break;
epgmdm 0:7176e20dbdf6 197 }
epgmdm 0:7176e20dbdf6 198 case 5: {
epgmdm 0:7176e20dbdf6 199 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 200 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 201
epgmdm 0:7176e20dbdf6 202 dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
epgmdm 0:7176e20dbdf6 203 state++;
epgmdm 0:7176e20dbdf6 204 }
epgmdm 0:7176e20dbdf6 205
epgmdm 0:7176e20dbdf6 206 break;
epgmdm 0:7176e20dbdf6 207 }
epgmdm 0:7176e20dbdf6 208 case 6: {
epgmdm 0:7176e20dbdf6 209 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 210 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 211 wait(1);
epgmdm 0:7176e20dbdf6 212 dev.printf("AT+CIFSR\r\n");
epgmdm 0:7176e20dbdf6 213 state++;
epgmdm 0:7176e20dbdf6 214 }
epgmdm 0:7176e20dbdf6 215
epgmdm 0:7176e20dbdf6 216 break;
epgmdm 0:7176e20dbdf6 217 }
epgmdm 0:7176e20dbdf6 218 case 7: {
epgmdm 1:e2385bba1ab1 219 resp=OKResponse(buffer,"+CIFSR:STAIP,");
epgmdm 0:7176e20dbdf6 220 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 221 char *strt = strtok(buffer,"\"");
epgmdm 1:e2385bba1ab1 222 strcpy(ipAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 223 strtok(NULL,"\"");
epgmdm 1:e2385bba1ab1 224 strcpy(macAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 225 INFO("mac Address = %s", macAddress);
epgmdm 1:e2385bba1ab1 226 INFO("IP Address = %s", ipAddress);
epgmdm 1:e2385bba1ab1 227 dev.printf("AT+CIPMUX=1\r\n");
epgmdm 0:7176e20dbdf6 228 state++;
epgmdm 0:7176e20dbdf6 229 }
epgmdm 0:7176e20dbdf6 230
epgmdm 0:7176e20dbdf6 231 break;
epgmdm 0:7176e20dbdf6 232 }
epgmdm 1:e2385bba1ab1 233 case 8: {
epgmdm 1:e2385bba1ab1 234 resp=OKResponse(buffer,"OK");
epgmdm 1:e2385bba1ab1 235 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 236 INFO("Ready");
trapsis 4:354d278c0d52 237 dev.printf("AT+CIPSERVER=1,5050\r\n"); //Set it as a server
epgmdm 1:e2385bba1ab1 238 state++;
epgmdm 1:e2385bba1ab1 239 }
epgmdm 1:e2385bba1ab1 240
epgmdm 1:e2385bba1ab1 241 break;
trapsis 4:354d278c0d52 242 }
trapsis 4:354d278c0d52 243
cstevens 3:2b5f88cf893d 244 case 9: {
trapsis 4:354d278c0d52 245 resp=OKResponse(buffer,"+IPD");
trapsis 4:354d278c0d52 246 if (resp!=NULL) {
trapsis 4:354d278c0d52 247 INFO("Got <%s>",buffer);
trapsis 4:354d278c0d52 248 commandReceived(buffer); // Interpret data, line 119
trapsis 4:354d278c0d52 249 state=9; // Keep on waiting for data
trapsis 4:354d278c0d52 250 }
trapsis 4:354d278c0d52 251
trapsis 4:354d278c0d52 252
trapsis 4:354d278c0d52 253
trapsis 4:354d278c0d52 254 break;
trapsis 4:354d278c0d52 255 }
trapsis 4:354d278c0d52 256
trapsis 4:354d278c0d52 257 /* case 9: {
cstevens 3:2b5f88cf893d 258 resp=OKResponse(buffer,"OK");
cstevens 3:2b5f88cf893d 259 if (resp!=NULL) {
cstevens 3:2b5f88cf893d 260 INFO("Ready");
cstevens 3:2b5f88cf893d 261 state++;
cstevens 3:2b5f88cf893d 262 dev.printf("AT+CIPSEND=4,10\r\n");
cstevens 3:2b5f88cf893d 263 }
cstevens 3:2b5f88cf893d 264
cstevens 3:2b5f88cf893d 265 break;
cstevens 3:2b5f88cf893d 266 }
cstevens 3:2b5f88cf893d 267 case 10: {
cstevens 3:2b5f88cf893d 268 resp=OKResponse(buffer,">");
cstevens 3:2b5f88cf893d 269 if (resp!=NULL) {
cstevens 3:2b5f88cf893d 270 INFO("SENDING");
cstevens 3:2b5f88cf893d 271 state++;
cstevens 3:2b5f88cf893d 272 touch=tsi.readPercentage();
cstevens 3:2b5f88cf893d 273 dev.printf("TSI=%1.2f\n\r",touch);
cstevens 3:2b5f88cf893d 274 pc.printf(" ############ tsi=%1.2f\n\r",touch);
cstevens 3:2b5f88cf893d 275 //INFO("sensor=%1.2f",float)tsi.readPercentage());
cstevens 3:2b5f88cf893d 276 }
cstevens 3:2b5f88cf893d 277 case 11: {
cstevens 3:2b5f88cf893d 278 resp=OKResponse(buffer,"SEND OK");
cstevens 3:2b5f88cf893d 279 if (resp!=NULL) {
cstevens 3:2b5f88cf893d 280 INFO("SEND OK");
cstevens 3:2b5f88cf893d 281 state=9;
cstevens 3:2b5f88cf893d 282 //wait(2);
cstevens 3:2b5f88cf893d 283 }
cstevens 3:2b5f88cf893d 284 wait(1);
cstevens 3:2b5f88cf893d 285 state=9;
cstevens 3:2b5f88cf893d 286 }
cstevens 3:2b5f88cf893d 287
cstevens 3:2b5f88cf893d 288 break;
trapsis 4:354d278c0d52 289 }*/
epgmdm 0:7176e20dbdf6 290
epgmdm 0:7176e20dbdf6 291
epgmdm 0:7176e20dbdf6 292 }
epgmdm 0:7176e20dbdf6 293 }
cstevens 3:2b5f88cf893d 294 //__WFI(); // DELETED AS IT KILLS THE KL25 RESPONSIVITY
epgmdm 0:7176e20dbdf6 295 }
epgmdm 0:7176e20dbdf6 296 }