asdf

Dependencies:   NokiaLCD XMIT_IR mbed

Fork of 4180_mP_WirelessPong_revC by Curtis Mulady

Committer:
cmulady
Date:
Fri Oct 05 19:38:51 2012 +0000
Revision:
9:3e4e9d6a8ad8
Parent:
8:54dd4a3d0de9
Child:
10:4fcd5bdb9642
Just more problems...

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 1:9ba884d85ac6 42
cmulady 3:8e492eacd346 43 //Function Prototypes
cmulady 1:9ba884d85ac6 44 void BlinkAlive(void const* arguments);
cmulady 1:9ba884d85ac6 45 void UpdateLCD(void const* arguments);
cmulady 3:8e492eacd346 46 void IRStuff(void const* arguments);
cmulady 6:5563f0026858 47 void MakePacket(char* data,int len);
cmulady 6:5563f0026858 48 char CheckPacket(char* data, int data_size);
cmulady 7:c9ff6b5c8507 49 void ISR_UARTRX(void);
cmulady 7:c9ff6b5c8507 50 void DoNothing(void const* arguments);
cmulady 9:3e4e9d6a8ad8 51 char CheckPacket2(char new_data, char* packet_buffer, char* data, int data_len);
cmulady 1:9ba884d85ac6 52
cmulady 1:9ba884d85ac6 53
cmulady 1:9ba884d85ac6 54 int main()
cmulady 1:9ba884d85ac6 55 {
cmulady 0:c8ddcaa575ba 56
cmulady 3:8e492eacd346 57 //LCD init
cmulady 0:c8ddcaa575ba 58 lcd.background(0x000000);
cmulady 1:9ba884d85ac6 59
cmulady 3:8e492eacd346 60 //PWM init
cmulady 3:8e492eacd346 61 IRLED_mod.period(1.0/38000.0); //38kHz Modulation Freq
cmulady 3:8e492eacd346 62 IRLED_mod = 0.5; //pulse width = 50%
cmulady 3:8e492eacd346 63
cmulady 3:8e492eacd346 64 //Serial init
cmulady 3:8e492eacd346 65 device.baud(2400);
cmulady 9:3e4e9d6a8ad8 66 //device.attach(&ISR_UARTRX,Serial::RxIrq);
cmulady 3:8e492eacd346 67
cmulady 6:5563f0026858 68 //PC serial init
cmulady 6:5563f0026858 69 pc.baud(19200);
cmulady 7:c9ff6b5c8507 70 pc.printf("Starting...\n\n");
cmulady 6:5563f0026858 71
cmulady 9:3e4e9d6a8ad8 72 //Variable Init
cmulady 9:3e4e9d6a8ad8 73 for(int i=0; i<10; i++) packet_buff[i]=0;
cmulady 9:3e4e9d6a8ad8 74
cmulady 3:8e492eacd346 75 //Thread init
cmulady 1:9ba884d85ac6 76 Thread thread_blinkalive(BlinkAlive);
cmulady 1:9ba884d85ac6 77 Thread thread_updatelcd(UpdateLCD);
cmulady 3:8e492eacd346 78 Thread thread_irstuff(IRStuff);
cmulady 7:c9ff6b5c8507 79 Thread thread_donothing(DoNothing);
cmulady 8:54dd4a3d0de9 80 threadptr_irstuff = &thread_irstuff;
cmulady 1:9ba884d85ac6 81
cmulady 1:9ba884d85ac6 82
cmulady 1:9ba884d85ac6 83
cmulady 0:c8ddcaa575ba 84 while(1) {
cmulady 1:9ba884d85ac6 85 thread_updatelcd.signal_set(0x1);
cmulady 1:9ba884d85ac6 86 Thread::wait(1000/FPS);
cmulady 1:9ba884d85ac6 87
cmulady 0:c8ddcaa575ba 88 }
cmulady 0:c8ddcaa575ba 89 }
cmulady 1:9ba884d85ac6 90
cmulady 1:9ba884d85ac6 91 void UpdateLCD(void const* arguments)
cmulady 1:9ba884d85ac6 92 {
cmulady 7:c9ff6b5c8507 93 while(true) {
cmulady 1:9ba884d85ac6 94 led2 = 1;
cmulady 1:9ba884d85ac6 95 lcd.locate(0,1);
cmulady 1:9ba884d85ac6 96 lcd.printf("Debug:");
cmulady 1:9ba884d85ac6 97
cmulady 1:9ba884d85ac6 98 lcd.locate(0,3);
cmulady 1:9ba884d85ac6 99 time_t seconds = time(NULL);
cmulady 1:9ba884d85ac6 100 strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
cmulady 1:9ba884d85ac6 101 lcd.printf("%s", buffer);
cmulady 1:9ba884d85ac6 102
cmulady 3:8e492eacd346 103 lcd.locate(0,4);
cmulady 6:5563f0026858 104 lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]);
cmulady 3:8e492eacd346 105 lcd.locate(0,5);
cmulady 6:5563f0026858 106 lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]);
cmulady 6:5563f0026858 107 lcd.locate(0,6);
cmulady 6:5563f0026858 108 lcd.printf("Error= 0x%02X", error_code);
cmulady 3:8e492eacd346 109
cmulady 3:8e492eacd346 110
cmulady 3:8e492eacd346 111
cmulady 1:9ba884d85ac6 112 //End - Sleep thread
cmulady 1:9ba884d85ac6 113 led2 = 0;
cmulady 1:9ba884d85ac6 114 Thread::signal_wait(0x1);
cmulady 7:c9ff6b5c8507 115 }
cmulady 1:9ba884d85ac6 116 }
cmulady 1:9ba884d85ac6 117
cmulady 3:8e492eacd346 118 void IRStuff(void const* arguments)
cmulady 3:8e492eacd346 119 {
cmulady 3:8e492eacd346 120 while(true) {
cmulady 8:54dd4a3d0de9 121 //error_code = CheckPacket(irdatIN,2);
cmulady 7:c9ff6b5c8507 122
cmulady 9:3e4e9d6a8ad8 123
cmulady 7:c9ff6b5c8507 124
cmulady 7:c9ff6b5c8507 125 //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x400FC0C4));
cmulady 7:c9ff6b5c8507 126
cmulady 9:3e4e9d6a8ad8 127 /*if(rx_data_available) {
cmulady 9:3e4e9d6a8ad8 128 if(irdatIN[0]==0x2) pc.printf("\n");
cmulady 8:54dd4a3d0de9 129 pc.printf("0x%02X.",irdatIN[0]);
cmulady 8:54dd4a3d0de9 130 //pc.printf("0x%02X.",irdatIN[1]);
cmulady 8:54dd4a3d0de9 131 //pc.printf("0x%02X.",irdatIN[2]);
cmulady 8:54dd4a3d0de9 132 //pc.printf("0x%02X.\n",irdatIN[3]);
cmulady 8:54dd4a3d0de9 133 rx_data_available = false;
cmulady 9:3e4e9d6a8ad8 134 }*/
cmulady 9:3e4e9d6a8ad8 135
cmulady 9:3e4e9d6a8ad8 136
cmulady 9:3e4e9d6a8ad8 137 while(device.readable()) {
cmulady 9:3e4e9d6a8ad8 138 error_code = CheckPacket2(device.getc(), packet_buff, irdatOUT, 2);
cmulady 9:3e4e9d6a8ad8 139 pc.printf(" = 0x%02X.\n",error_code);
cmulady 8:54dd4a3d0de9 140 }
cmulady 7:c9ff6b5c8507 141
cmulady 7:c9ff6b5c8507 142
cmulady 9:3e4e9d6a8ad8 143
cmulady 7:c9ff6b5c8507 144 //pc.printf("\n\nE=0x%02X\n\n",error_code);
cmulady 8:54dd4a3d0de9 145 /*if(error_code==0x0) {
cmulady 7:c9ff6b5c8507 146 pc.printf("0x%02X.",irdatIN[1]);
cmulady 7:c9ff6b5c8507 147 Thread::wait(10);
cmulady 7:c9ff6b5c8507 148 } else if(error_code==0x2) {
cmulady 7:c9ff6b5c8507 149 pc.printf("!");
cmulady 7:c9ff6b5c8507 150 Thread::wait(10);
cmulady 7:c9ff6b5c8507 151 } else {
cmulady 7:c9ff6b5c8507 152 Thread::wait(10);
cmulady 8:54dd4a3d0de9 153 }*/
cmulady 9:3e4e9d6a8ad8 154 //Thread::signal_wait(0x1);
cmulady 9:3e4e9d6a8ad8 155 Thread::wait(5);
cmulady 3:8e492eacd346 156 }
cmulady 4:8fdff78c13c8 157
cmulady 3:8e492eacd346 158 }
cmulady 3:8e492eacd346 159
cmulady 1:9ba884d85ac6 160 void BlinkAlive(void const* arguments)
cmulady 1:9ba884d85ac6 161 {
cmulady 1:9ba884d85ac6 162 while(true) {
cmulady 1:9ba884d85ac6 163 led1 = !led1;
cmulady 6:5563f0026858 164
cmulady 6:5563f0026858 165 irdatOUT[0] = 0xA5;
cmulady 6:5563f0026858 166 irdatOUT[1] = ++irdata_out;
cmulady 6:5563f0026858 167 MakePacket(irdatOUT,2);
cmulady 8:54dd4a3d0de9 168 //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014));
cmulady 6:5563f0026858 169
cmulady 9:3e4e9d6a8ad8 170 Thread::wait(6000);
cmulady 6:5563f0026858 171 }
cmulady 6:5563f0026858 172 }
cmulady 6:5563f0026858 173
cmulady 6:5563f0026858 174 void MakePacket(char* data,int len)
cmulady 6:5563f0026858 175 {
cmulady 6:5563f0026858 176 //pc.printf("\nMaking Packet:\n\t");
cmulady 6:5563f0026858 177 char check =0x0;
cmulady 6:5563f0026858 178 device.putc(0x02);
cmulady 6:5563f0026858 179 //pc.printf("0x%02X.",0x02);
cmulady 6:5563f0026858 180 for(int i=0; i<len; i++) {
cmulady 9:3e4e9d6a8ad8 181 check^=data[i];
cmulady 6:5563f0026858 182 device.putc(data[i]);
cmulady 6:5563f0026858 183 //pc.printf("0x%02X.",data[i]);
cmulady 1:9ba884d85ac6 184 }
cmulady 6:5563f0026858 185 device.putc(check);
cmulady 6:5563f0026858 186 //pc.printf("0x%02X.",check);
cmulady 6:5563f0026858 187 //pc.printf("\nDone making packet.\n");
cmulady 6:5563f0026858 188 }
cmulady 6:5563f0026858 189
cmulady 6:5563f0026858 190 char CheckPacket(char* data, int data_size)
cmulady 6:5563f0026858 191 //returns success(0) or failure(error code)
cmulady 6:5563f0026858 192 {
cmulady 6:5563f0026858 193 Timer t;
cmulady 6:5563f0026858 194 t.reset();
cmulady 6:5563f0026858 195 t.start();
cmulady 6:5563f0026858 196 char tempdata=0x0;
cmulady 6:5563f0026858 197 char check=0x0;
cmulady 6:5563f0026858 198
cmulady 7:c9ff6b5c8507 199 //pc.printf("\nChecking Packet\n\t");
cmulady 6:5563f0026858 200 //Data available
cmulady 6:5563f0026858 201 if(!device.readable()) {
cmulady 7:c9ff6b5c8507 202 //pc.printf("no data");
cmulady 6:5563f0026858 203 return 0x4; //no data
cmulady 6:5563f0026858 204 }
cmulady 6:5563f0026858 205
cmulady 6:5563f0026858 206 while(!device.readable());
cmulady 6:5563f0026858 207 //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
cmulady 6:5563f0026858 208 //if(t.read_ms()>=UART_TIMEOUT)
cmulady 6:5563f0026858 209 // return 0x3; //timeout error
cmulady 6:5563f0026858 210 tempdata = device.getc();
cmulady 6:5563f0026858 211
cmulady 6:5563f0026858 212 //STX recieved
cmulady 6:5563f0026858 213 if(tempdata!=0x02) {
cmulady 7:c9ff6b5c8507 214 if(tempdata==0xFF) pc.printf("found bad data: 0x%02X",tempdata);
cmulady 8:54dd4a3d0de9 215 pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014));
cmulady 6:5563f0026858 216 return 0x1; //bad start byte
cmulady 6:5563f0026858 217 }
cmulady 6:5563f0026858 218
cmulady 6:5563f0026858 219 //Get Data
cmulady 6:5563f0026858 220 for(int i=0; i<data_size; i++) {
cmulady 6:5563f0026858 221 while(!device.readable());
cmulady 6:5563f0026858 222 //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
cmulady 6:5563f0026858 223 //if(t.read_ms()>=UART_TIMEOUT)
cmulady 6:5563f0026858 224 // return 0x3; //timeout error
cmulady 6:5563f0026858 225 data[i] = device.getc();
cmulady 8:54dd4a3d0de9 226 check += data[i];
cmulady 6:5563f0026858 227 }
cmulady 6:5563f0026858 228 //Get Checksum
cmulady 6:5563f0026858 229 while(!device.readable());
cmulady 6:5563f0026858 230 //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
cmulady 6:5563f0026858 231 //if(t.read_ms()>=UART_TIMEOUT)
cmulady 6:5563f0026858 232 // return 0x3; //timeout error
cmulady 6:5563f0026858 233 tempdata = device.getc();
cmulady 6:5563f0026858 234 if(tempdata!=check)
cmulady 6:5563f0026858 235 return 0x2; //bad checksum
cmulady 6:5563f0026858 236
cmulady 6:5563f0026858 237
cmulady 6:5563f0026858 238 return 0;
cmulady 6:5563f0026858 239
cmulady 6:5563f0026858 240 }
cmulady 6:5563f0026858 241
cmulady 9:3e4e9d6a8ad8 242 char CheckPacket2(char new_data, char* packet_buffer, char* data, int data_len)
cmulady 9:3e4e9d6a8ad8 243 //returns success(0) or failure(error code)
cmulady 9:3e4e9d6a8ad8 244 {
cmulady 9:3e4e9d6a8ad8 245 //Requires a packet buffer of length 'data_len'+2.
cmulady 9:3e4e9d6a8ad8 246 //Shifts data and checks each 'set' for a valid packet.
cmulady 9:3e4e9d6a8ad8 247 //Once a valid packet is receievd, the data buffer is updated with new values.
cmulady 9:3e4e9d6a8ad8 248
cmulady 9:3e4e9d6a8ad8 249 char check=0x0;
cmulady 9:3e4e9d6a8ad8 250 pc.printf("Shifting: ");
cmulady 9:3e4e9d6a8ad8 251 //Shift All data 1 cell over
cmulady 9:3e4e9d6a8ad8 252 for(int i=0; i<data_len+1; i++) {
cmulady 9:3e4e9d6a8ad8 253 packet_buffer[i] = packet_buffer[i+1];
cmulady 9:3e4e9d6a8ad8 254 }
cmulady 9:3e4e9d6a8ad8 255 packet_buffer[data_len+1] = new_data;
cmulady 9:3e4e9d6a8ad8 256 for(int i=0; i<data_len+2; i++) {
cmulady 9:3e4e9d6a8ad8 257 pc.printf("0x%02X.",packet_buffer[i]);
cmulady 9:3e4e9d6a8ad8 258 }
cmulady 9:3e4e9d6a8ad8 259
cmulady 9:3e4e9d6a8ad8 260 //check for valid packet
cmulady 9:3e4e9d6a8ad8 261 if(packet_buffer[0]!=0x02)
cmulady 9:3e4e9d6a8ad8 262 return 0x1; //bad start byte
cmulady 9:3e4e9d6a8ad8 263
cmulady 9:3e4e9d6a8ad8 264 for(int i=1; i<data_len+1; i++) {
cmulady 9:3e4e9d6a8ad8 265 check^=packet_buffer[i];
cmulady 9:3e4e9d6a8ad8 266 }
cmulady 9:3e4e9d6a8ad8 267 if(check==packet_buffer[data_len+1]) {
cmulady 9:3e4e9d6a8ad8 268 return 0;
cmulady 9:3e4e9d6a8ad8 269 }
cmulady 9:3e4e9d6a8ad8 270 return 0;
cmulady 9:3e4e9d6a8ad8 271 }
cmulady 9:3e4e9d6a8ad8 272
cmulady 7:c9ff6b5c8507 273 void DoNothing(void const* arguments)
cmulady 7:c9ff6b5c8507 274 {
cmulady 7:c9ff6b5c8507 275 while(true) {
cmulady 7:c9ff6b5c8507 276 Thread::signal_wait(0x1);
cmulady 7:c9ff6b5c8507 277 }
cmulady 7:c9ff6b5c8507 278 }
cmulady 7:c9ff6b5c8507 279
cmulady 7:c9ff6b5c8507 280 void ISR_UARTRX(void)
cmulady 7:c9ff6b5c8507 281 {
cmulady 7:c9ff6b5c8507 282 uint32_t RBR = LPC_UART1->RBR;
cmulady 9:3e4e9d6a8ad8 283
cmulady 7:c9ff6b5c8507 284
cmulady 8:54dd4a3d0de9 285 irdatIN[0] = (char)RBR;
cmulady 8:54dd4a3d0de9 286 /*if(device.readable())
cmulady 8:54dd4a3d0de9 287 irdatIN[1] = (char) LPC_UART1->RBR;
cmulady 8:54dd4a3d0de9 288 if(device.readable())
cmulady 8:54dd4a3d0de9 289 irdatIN[2] = (char) LPC_UART1->RBR;
cmulady 8:54dd4a3d0de9 290 if(device.readable())
cmulady 8:54dd4a3d0de9 291 irdatIN[3] = (char) LPC_UART1->RBR;
cmulady 8:54dd4a3d0de9 292 */
cmulady 8:54dd4a3d0de9 293
cmulady 8:54dd4a3d0de9 294 rx_data_available = true;
cmulady 8:54dd4a3d0de9 295 (*threadptr_irstuff).signal_set(0x1);
cmulady 8:54dd4a3d0de9 296
cmulady 8:54dd4a3d0de9 297
cmulady 8:54dd4a3d0de9 298 /*while(device.readable()) {
cmulady 7:c9ff6b5c8507 299 char data = device.getc();
cmulady 7:c9ff6b5c8507 300 //pc.printf("*");
cmulady 8:54dd4a3d0de9 301 }*/
cmulady 7:c9ff6b5c8507 302
cmulady 7:c9ff6b5c8507 303 }
cmulady 7:c9ff6b5c8507 304