miniProject-Wireless Pong

Committer:
cmulady
Date:
Fri Oct 05 21:55:09 2012 +0000
Revision:
10:4fcd5bdb9642
Parent:
9:3e4e9d6a8ad8
NOW it works. Using mail method to pass data from IRQ to message handler. Now I will start cleaning code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cmulady 0:c8ddcaa575ba 1 #include "mbed.h"
cmulady 0:c8ddcaa575ba 2 #include "rtos.h"
cmulady 0:c8ddcaa575ba 3 #include "NokiaLCD.h"
cmulady 1:9ba884d85ac6 4 #include "XMIT_IR.h"
cmulady 7:c9ff6b5c8507 5 #include "queue.h"
cmulady 0:c8ddcaa575ba 6
cmulady 1:9ba884d85ac6 7 #define FPS 5
cmulady 6:5563f0026858 8 #define UART_TIMEOUT 500
cmulady 1:9ba884d85ac6 9
cmulady 3:8e492eacd346 10 /****************************************
cmulady 3:8e492eacd346 11 |=======================================|
cmulady 3:8e492eacd346 12 |MBED Connections: |
cmulady 3:8e492eacd346 13 | -p5 : DIO on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 14 | -p7 : CLK on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 15 | -p8 : CS on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 16 | -p9 : RST on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 17 | -p22: GND on Sparkfun IR Xmtr |
cmulady 3:8e492eacd346 18 |=======================================|
cmulady 3:8e492eacd346 19 ****************************************/
cmulady 1:9ba884d85ac6 20
cmulady 3:8e492eacd346 21 //Pin Setup
cmulady 1:9ba884d85ac6 22 DigitalOut led1(LED1);
cmulady 1:9ba884d85ac6 23 DigitalOut led2(LED2);
cmulady 3:8e492eacd346 24 DigitalOut led3(LED3);
cmulady 3:8e492eacd346 25 DigitalOut led4(LED4);
cmulady 0:c8ddcaa575ba 26 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
cmulady 5:2e08cc167fff 27 Serial device(p13, p14); // tx, rx
cmulady 6:5563f0026858 28 Serial pc(USBTX,USBRX);
cmulady 5:2e08cc167fff 29 PwmOut IRLED_mod(p21);
cmulady 1:9ba884d85ac6 30
cmulady 3:8e492eacd346 31 //Global Vars
cmulady 1:9ba884d85ac6 32 char buffer[32];
cmulady 3:8e492eacd346 33 unsigned char irdata_out=0;
cmulady 3:8e492eacd346 34 unsigned char irdata_in=0;
cmulady 6:5563f0026858 35 char irdatOUT[10];
cmulady 6:5563f0026858 36 char irdatIN[10];
cmulady 6:5563f0026858 37 char error_code=0;
cmulady 8:54dd4a3d0de9 38 Thread* threadptr_irstuff;
cmulady 7:c9ff6b5c8507 39 DataQueue RX_DataBuffer(1,100);
cmulady 8:54dd4a3d0de9 40 bool rx_data_available = false;
cmulady 9:3e4e9d6a8ad8 41 char packet_buff[10];
cmulady 10:4fcd5bdb9642 42 Mail<char,64> rx_data_mailbox;
cmulady 1:9ba884d85ac6 43
cmulady 3:8e492eacd346 44 //Function Prototypes
cmulady 1:9ba884d85ac6 45 void BlinkAlive(void const* arguments);
cmulady 1:9ba884d85ac6 46 void UpdateLCD(void const* arguments);
cmulady 3:8e492eacd346 47 void IRStuff(void const* arguments);
cmulady 6:5563f0026858 48 void MakePacket(char* data,int len);
cmulady 6:5563f0026858 49 char CheckPacket(char* data, int data_size);
cmulady 7:c9ff6b5c8507 50 void ISR_UARTRX(void);
cmulady 7:c9ff6b5c8507 51 void DoNothing(void const* arguments);
cmulady 9:3e4e9d6a8ad8 52 char CheckPacket2(char new_data, char* packet_buffer, char* data, int data_len);
cmulady 1:9ba884d85ac6 53
cmulady 1:9ba884d85ac6 54
cmulady 1:9ba884d85ac6 55 int main()
cmulady 1:9ba884d85ac6 56 {
cmulady 0:c8ddcaa575ba 57
cmulady 3:8e492eacd346 58 //LCD init
cmulady 0:c8ddcaa575ba 59 lcd.background(0x000000);
cmulady 1:9ba884d85ac6 60
cmulady 3:8e492eacd346 61 //PWM init
cmulady 3:8e492eacd346 62 IRLED_mod.period(1.0/38000.0); //38kHz Modulation Freq
cmulady 3:8e492eacd346 63 IRLED_mod = 0.5; //pulse width = 50%
cmulady 3:8e492eacd346 64
cmulady 3:8e492eacd346 65 //Serial init
cmulady 3:8e492eacd346 66 device.baud(2400);
cmulady 10:4fcd5bdb9642 67 device.attach(&ISR_UARTRX,Serial::RxIrq);
cmulady 3:8e492eacd346 68
cmulady 6:5563f0026858 69 //PC serial init
cmulady 6:5563f0026858 70 pc.baud(19200);
cmulady 7:c9ff6b5c8507 71 pc.printf("Starting...\n\n");
cmulady 6:5563f0026858 72
cmulady 9:3e4e9d6a8ad8 73 //Variable Init
cmulady 9:3e4e9d6a8ad8 74 for(int i=0; i<10; i++) packet_buff[i]=0;
cmulady 9:3e4e9d6a8ad8 75
cmulady 3:8e492eacd346 76 //Thread init
cmulady 1:9ba884d85ac6 77 Thread thread_blinkalive(BlinkAlive);
cmulady 1:9ba884d85ac6 78 Thread thread_updatelcd(UpdateLCD);
cmulady 3:8e492eacd346 79 Thread thread_irstuff(IRStuff);
cmulady 7:c9ff6b5c8507 80 Thread thread_donothing(DoNothing);
cmulady 8:54dd4a3d0de9 81 threadptr_irstuff = &thread_irstuff;
cmulady 1:9ba884d85ac6 82
cmulady 1:9ba884d85ac6 83
cmulady 1:9ba884d85ac6 84
cmulady 0:c8ddcaa575ba 85 while(1) {
cmulady 1:9ba884d85ac6 86 thread_updatelcd.signal_set(0x1);
cmulady 1:9ba884d85ac6 87 Thread::wait(1000/FPS);
cmulady 1:9ba884d85ac6 88
cmulady 0:c8ddcaa575ba 89 }
cmulady 0:c8ddcaa575ba 90 }
cmulady 1:9ba884d85ac6 91
cmulady 1:9ba884d85ac6 92 void UpdateLCD(void const* arguments)
cmulady 1:9ba884d85ac6 93 {
cmulady 7:c9ff6b5c8507 94 while(true) {
cmulady 1:9ba884d85ac6 95 led2 = 1;
cmulady 1:9ba884d85ac6 96 lcd.locate(0,1);
cmulady 1:9ba884d85ac6 97 lcd.printf("Debug:");
cmulady 1:9ba884d85ac6 98
cmulady 1:9ba884d85ac6 99 lcd.locate(0,3);
cmulady 1:9ba884d85ac6 100 time_t seconds = time(NULL);
cmulady 1:9ba884d85ac6 101 strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
cmulady 1:9ba884d85ac6 102 lcd.printf("%s", buffer);
cmulady 1:9ba884d85ac6 103
cmulady 3:8e492eacd346 104 lcd.locate(0,4);
cmulady 6:5563f0026858 105 lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]);
cmulady 3:8e492eacd346 106 lcd.locate(0,5);
cmulady 6:5563f0026858 107 lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]);
cmulady 6:5563f0026858 108 lcd.locate(0,6);
cmulady 6:5563f0026858 109 lcd.printf("Error= 0x%02X", error_code);
cmulady 3:8e492eacd346 110
cmulady 3:8e492eacd346 111
cmulady 3:8e492eacd346 112
cmulady 1:9ba884d85ac6 113 //End - Sleep thread
cmulady 1:9ba884d85ac6 114 led2 = 0;
cmulady 1:9ba884d85ac6 115 Thread::signal_wait(0x1);
cmulady 7:c9ff6b5c8507 116 }
cmulady 1:9ba884d85ac6 117 }
cmulady 1:9ba884d85ac6 118
cmulady 3:8e492eacd346 119 void IRStuff(void const* arguments)
cmulady 3:8e492eacd346 120 {
cmulady 3:8e492eacd346 121 while(true) {
cmulady 8:54dd4a3d0de9 122 //error_code = CheckPacket(irdatIN,2);
cmulady 7:c9ff6b5c8507 123
cmulady 9:3e4e9d6a8ad8 124
cmulady 7:c9ff6b5c8507 125
cmulady 7:c9ff6b5c8507 126 //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x400FC0C4));
cmulady 7:c9ff6b5c8507 127
cmulady 9:3e4e9d6a8ad8 128 /*if(rx_data_available) {
cmulady 9:3e4e9d6a8ad8 129 if(irdatIN[0]==0x2) pc.printf("\n");
cmulady 8:54dd4a3d0de9 130 pc.printf("0x%02X.",irdatIN[0]);
cmulady 8:54dd4a3d0de9 131 //pc.printf("0x%02X.",irdatIN[1]);
cmulady 8:54dd4a3d0de9 132 //pc.printf("0x%02X.",irdatIN[2]);
cmulady 8:54dd4a3d0de9 133 //pc.printf("0x%02X.\n",irdatIN[3]);
cmulady 8:54dd4a3d0de9 134 rx_data_available = false;
cmulady 9:3e4e9d6a8ad8 135 }*/
cmulady 9:3e4e9d6a8ad8 136
cmulady 9:3e4e9d6a8ad8 137
cmulady 10:4fcd5bdb9642 138 /* while(device.readable()) {
cmulady 10:4fcd5bdb9642 139 error_code = CheckPacket2(device.getc(), packet_buff, irdatOUT, 2);
cmulady 10:4fcd5bdb9642 140 pc.printf(" = 0x%02X.\n",error_code);
cmulady 10:4fcd5bdb9642 141 }*/
cmulady 10:4fcd5bdb9642 142
cmulady 10:4fcd5bdb9642 143 osEvent evt = rx_data_mailbox.get();
cmulady 10:4fcd5bdb9642 144 while(evt.status == osEventMail) {
cmulady 10:4fcd5bdb9642 145 char* mail = (char*)evt.value.p;
cmulady 10:4fcd5bdb9642 146 if((*mail)==0x02) pc.printf("\n");
cmulady 10:4fcd5bdb9642 147 pc.printf("0x%02X.",*mail);
cmulady 10:4fcd5bdb9642 148 rx_data_mailbox.free(mail);
cmulady 10:4fcd5bdb9642 149 evt = rx_data_mailbox.get();
cmulady 8:54dd4a3d0de9 150 }
cmulady 10:4fcd5bdb9642 151
cmulady 7:c9ff6b5c8507 152
cmulady 7:c9ff6b5c8507 153
cmulady 9:3e4e9d6a8ad8 154
cmulady 7:c9ff6b5c8507 155 //pc.printf("\n\nE=0x%02X\n\n",error_code);
cmulady 8:54dd4a3d0de9 156 /*if(error_code==0x0) {
cmulady 7:c9ff6b5c8507 157 pc.printf("0x%02X.",irdatIN[1]);
cmulady 7:c9ff6b5c8507 158 Thread::wait(10);
cmulady 7:c9ff6b5c8507 159 } else if(error_code==0x2) {
cmulady 7:c9ff6b5c8507 160 pc.printf("!");
cmulady 7:c9ff6b5c8507 161 Thread::wait(10);
cmulady 7:c9ff6b5c8507 162 } else {
cmulady 7:c9ff6b5c8507 163 Thread::wait(10);
cmulady 8:54dd4a3d0de9 164 }*/
cmulady 9:3e4e9d6a8ad8 165 //Thread::signal_wait(0x1);
cmulady 9:3e4e9d6a8ad8 166 Thread::wait(5);
cmulady 3:8e492eacd346 167 }
cmulady 4:8fdff78c13c8 168
cmulady 3:8e492eacd346 169 }
cmulady 3:8e492eacd346 170
cmulady 1:9ba884d85ac6 171 void BlinkAlive(void const* arguments)
cmulady 1:9ba884d85ac6 172 {
cmulady 1:9ba884d85ac6 173 while(true) {
cmulady 1:9ba884d85ac6 174 led1 = !led1;
cmulady 6:5563f0026858 175
cmulady 6:5563f0026858 176 irdatOUT[0] = 0xA5;
cmulady 6:5563f0026858 177 irdatOUT[1] = ++irdata_out;
cmulady 6:5563f0026858 178 MakePacket(irdatOUT,2);
cmulady 8:54dd4a3d0de9 179 //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014));
cmulady 6:5563f0026858 180
cmulady 10:4fcd5bdb9642 181 Thread::wait(20);
cmulady 6:5563f0026858 182 }
cmulady 6:5563f0026858 183 }
cmulady 6:5563f0026858 184
cmulady 6:5563f0026858 185 void MakePacket(char* data,int len)
cmulady 6:5563f0026858 186 {
cmulady 6:5563f0026858 187 //pc.printf("\nMaking Packet:\n\t");
cmulady 6:5563f0026858 188 char check =0x0;
cmulady 6:5563f0026858 189 device.putc(0x02);
cmulady 6:5563f0026858 190 //pc.printf("0x%02X.",0x02);
cmulady 6:5563f0026858 191 for(int i=0; i<len; i++) {
cmulady 9:3e4e9d6a8ad8 192 check^=data[i];
cmulady 6:5563f0026858 193 device.putc(data[i]);
cmulady 6:5563f0026858 194 //pc.printf("0x%02X.",data[i]);
cmulady 1:9ba884d85ac6 195 }
cmulady 6:5563f0026858 196 device.putc(check);
cmulady 6:5563f0026858 197 //pc.printf("0x%02X.",check);
cmulady 6:5563f0026858 198 //pc.printf("\nDone making packet.\n");
cmulady 6:5563f0026858 199 }
cmulady 6:5563f0026858 200
cmulady 6:5563f0026858 201 char CheckPacket(char* data, int data_size)
cmulady 6:5563f0026858 202 //returns success(0) or failure(error code)
cmulady 6:5563f0026858 203 {
cmulady 6:5563f0026858 204 Timer t;
cmulady 6:5563f0026858 205 t.reset();
cmulady 6:5563f0026858 206 t.start();
cmulady 6:5563f0026858 207 char tempdata=0x0;
cmulady 6:5563f0026858 208 char check=0x0;
cmulady 6:5563f0026858 209
cmulady 7:c9ff6b5c8507 210 //pc.printf("\nChecking Packet\n\t");
cmulady 6:5563f0026858 211 //Data available
cmulady 6:5563f0026858 212 if(!device.readable()) {
cmulady 7:c9ff6b5c8507 213 //pc.printf("no data");
cmulady 6:5563f0026858 214 return 0x4; //no data
cmulady 6:5563f0026858 215 }
cmulady 6:5563f0026858 216
cmulady 6:5563f0026858 217 while(!device.readable());
cmulady 6:5563f0026858 218 //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
cmulady 6:5563f0026858 219 //if(t.read_ms()>=UART_TIMEOUT)
cmulady 6:5563f0026858 220 // return 0x3; //timeout error
cmulady 6:5563f0026858 221 tempdata = device.getc();
cmulady 6:5563f0026858 222
cmulady 6:5563f0026858 223 //STX recieved
cmulady 6:5563f0026858 224 if(tempdata!=0x02) {
cmulady 7:c9ff6b5c8507 225 if(tempdata==0xFF) pc.printf("found bad data: 0x%02X",tempdata);
cmulady 8:54dd4a3d0de9 226 pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014));
cmulady 6:5563f0026858 227 return 0x1; //bad start byte
cmulady 6:5563f0026858 228 }
cmulady 6:5563f0026858 229
cmulady 6:5563f0026858 230 //Get Data
cmulady 6:5563f0026858 231 for(int i=0; i<data_size; i++) {
cmulady 6:5563f0026858 232 while(!device.readable());
cmulady 6:5563f0026858 233 //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
cmulady 6:5563f0026858 234 //if(t.read_ms()>=UART_TIMEOUT)
cmulady 6:5563f0026858 235 // return 0x3; //timeout error
cmulady 6:5563f0026858 236 data[i] = device.getc();
cmulady 8:54dd4a3d0de9 237 check += data[i];
cmulady 6:5563f0026858 238 }
cmulady 6:5563f0026858 239 //Get Checksum
cmulady 6:5563f0026858 240 while(!device.readable());
cmulady 6:5563f0026858 241 //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
cmulady 6:5563f0026858 242 //if(t.read_ms()>=UART_TIMEOUT)
cmulady 6:5563f0026858 243 // return 0x3; //timeout error
cmulady 6:5563f0026858 244 tempdata = device.getc();
cmulady 6:5563f0026858 245 if(tempdata!=check)
cmulady 6:5563f0026858 246 return 0x2; //bad checksum
cmulady 6:5563f0026858 247
cmulady 6:5563f0026858 248
cmulady 6:5563f0026858 249 return 0;
cmulady 6:5563f0026858 250
cmulady 6:5563f0026858 251 }
cmulady 6:5563f0026858 252
cmulady 9:3e4e9d6a8ad8 253 char CheckPacket2(char new_data, char* packet_buffer, char* data, int data_len)
cmulady 9:3e4e9d6a8ad8 254 //returns success(0) or failure(error code)
cmulady 9:3e4e9d6a8ad8 255 {
cmulady 9:3e4e9d6a8ad8 256 //Requires a packet buffer of length 'data_len'+2.
cmulady 9:3e4e9d6a8ad8 257 //Shifts data and checks each 'set' for a valid packet.
cmulady 9:3e4e9d6a8ad8 258 //Once a valid packet is receievd, the data buffer is updated with new values.
cmulady 9:3e4e9d6a8ad8 259
cmulady 9:3e4e9d6a8ad8 260 char check=0x0;
cmulady 9:3e4e9d6a8ad8 261 pc.printf("Shifting: ");
cmulady 9:3e4e9d6a8ad8 262 //Shift All data 1 cell over
cmulady 9:3e4e9d6a8ad8 263 for(int i=0; i<data_len+1; i++) {
cmulady 9:3e4e9d6a8ad8 264 packet_buffer[i] = packet_buffer[i+1];
cmulady 9:3e4e9d6a8ad8 265 }
cmulady 9:3e4e9d6a8ad8 266 packet_buffer[data_len+1] = new_data;
cmulady 9:3e4e9d6a8ad8 267 for(int i=0; i<data_len+2; i++) {
cmulady 9:3e4e9d6a8ad8 268 pc.printf("0x%02X.",packet_buffer[i]);
cmulady 9:3e4e9d6a8ad8 269 }
cmulady 9:3e4e9d6a8ad8 270
cmulady 9:3e4e9d6a8ad8 271 //check for valid packet
cmulady 9:3e4e9d6a8ad8 272 if(packet_buffer[0]!=0x02)
cmulady 9:3e4e9d6a8ad8 273 return 0x1; //bad start byte
cmulady 9:3e4e9d6a8ad8 274
cmulady 9:3e4e9d6a8ad8 275 for(int i=1; i<data_len+1; i++) {
cmulady 9:3e4e9d6a8ad8 276 check^=packet_buffer[i];
cmulady 9:3e4e9d6a8ad8 277 }
cmulady 9:3e4e9d6a8ad8 278 if(check==packet_buffer[data_len+1]) {
cmulady 9:3e4e9d6a8ad8 279 return 0;
cmulady 9:3e4e9d6a8ad8 280 }
cmulady 9:3e4e9d6a8ad8 281 return 0;
cmulady 9:3e4e9d6a8ad8 282 }
cmulady 9:3e4e9d6a8ad8 283
cmulady 7:c9ff6b5c8507 284 void DoNothing(void const* arguments)
cmulady 7:c9ff6b5c8507 285 {
cmulady 7:c9ff6b5c8507 286 while(true) {
cmulady 7:c9ff6b5c8507 287 Thread::signal_wait(0x1);
cmulady 7:c9ff6b5c8507 288 }
cmulady 7:c9ff6b5c8507 289 }
cmulady 7:c9ff6b5c8507 290
cmulady 7:c9ff6b5c8507 291 void ISR_UARTRX(void)
cmulady 7:c9ff6b5c8507 292 {
cmulady 7:c9ff6b5c8507 293 uint32_t RBR = LPC_UART1->RBR;
cmulady 9:3e4e9d6a8ad8 294
cmulady 7:c9ff6b5c8507 295
cmulady 10:4fcd5bdb9642 296 //irdatIN[0] = (char)RBR;
cmulady 10:4fcd5bdb9642 297
cmulady 10:4fcd5bdb9642 298 char* mail = rx_data_mailbox.alloc();
cmulady 10:4fcd5bdb9642 299 mail[0] = (char)RBR;
cmulady 10:4fcd5bdb9642 300 rx_data_mailbox.put(mail);
cmulady 10:4fcd5bdb9642 301
cmulady 8:54dd4a3d0de9 302 /*if(device.readable())
cmulady 8:54dd4a3d0de9 303 irdatIN[1] = (char) LPC_UART1->RBR;
cmulady 8:54dd4a3d0de9 304 if(device.readable())
cmulady 8:54dd4a3d0de9 305 irdatIN[2] = (char) LPC_UART1->RBR;
cmulady 8:54dd4a3d0de9 306 if(device.readable())
cmulady 8:54dd4a3d0de9 307 irdatIN[3] = (char) LPC_UART1->RBR;
cmulady 8:54dd4a3d0de9 308 */
cmulady 8:54dd4a3d0de9 309
cmulady 8:54dd4a3d0de9 310 rx_data_available = true;
cmulady 8:54dd4a3d0de9 311 (*threadptr_irstuff).signal_set(0x1);
cmulady 8:54dd4a3d0de9 312
cmulady 8:54dd4a3d0de9 313
cmulady 8:54dd4a3d0de9 314 /*while(device.readable()) {
cmulady 7:c9ff6b5c8507 315 char data = device.getc();
cmulady 7:c9ff6b5c8507 316 //pc.printf("*");
cmulady 8:54dd4a3d0de9 317 }*/
cmulady 7:c9ff6b5c8507 318
cmulady 7:c9ff6b5c8507 319 }
cmulady 7:c9ff6b5c8507 320