asdf

Dependencies:   NokiaLCD XMIT_IR mbed

Fork of 4180_mP_WirelessPong_revC by Curtis Mulady

Committer:
cmulady
Date:
Sat Oct 06 11:34:40 2012 +0000
Revision:
15:21c512907cf6
Parent:
14:68f7cefd7359
Child:
16:bc0e213ba52d
stack memory error somewhere

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 0:c8ddcaa575ba 5
cmulady 1:9ba884d85ac6 6 #define FPS 5
cmulady 1:9ba884d85ac6 7
cmulady 3:8e492eacd346 8 /****************************************
cmulady 3:8e492eacd346 9 |=======================================|
cmulady 3:8e492eacd346 10 |MBED Connections: |
cmulady 3:8e492eacd346 11 | -p5 : DIO on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 12 | -p7 : CLK on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 13 | -p8 : CS on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 14 | -p9 : RST on Sparkfun Nokia LCD |
cmulady 12:9acca1dd0e8e 15 | -p21: CTL on Sparkfun IR Xmitter |
cmulady 12:9acca1dd0e8e 16 | -p14: OUT on Sparkfun IR Rcvr |
cmulady 12:9acca1dd0e8e 17 | -p13: GND on Sparkfun IR Xmitter |
cmulady 3:8e492eacd346 18 |=======================================|
cmulady 3:8e492eacd346 19 ****************************************/
cmulady 1:9ba884d85ac6 20
cmulady 13:17fd813ef7c9 21 //Function Prototypes
cmulady 13:17fd813ef7c9 22 void BlinkAlive(void const* arguments);
cmulady 13:17fd813ef7c9 23 void UpdateLCD(void const* arguments);
cmulady 13:17fd813ef7c9 24 void IRStuff(void const* arguments);
cmulady 13:17fd813ef7c9 25 void ISR_UARTRX(void);
cmulady 13:17fd813ef7c9 26 char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len);
cmulady 13:17fd813ef7c9 27 void MakePacket2(char* data,int len);
cmulady 13:17fd813ef7c9 28
cmulady 3:8e492eacd346 29 //Pin Setup
cmulady 13:17fd813ef7c9 30 PwmOut led1(LED1);
cmulady 13:17fd813ef7c9 31 PwmOut led2(LED2);
cmulady 3:8e492eacd346 32 DigitalOut led3(LED3);
cmulady 3:8e492eacd346 33 DigitalOut led4(LED4);
cmulady 0:c8ddcaa575ba 34 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
cmulady 13:17fd813ef7c9 35 Serial* devicerx;
cmulady 13:17fd813ef7c9 36
cmulady 13:17fd813ef7c9 37 //Serial devicetx(p13,NC);
cmulady 6:5563f0026858 38 Serial pc(USBTX,USBRX);
cmulady 5:2e08cc167fff 39 PwmOut IRLED_mod(p21);
cmulady 1:9ba884d85ac6 40
cmulady 15:21c512907cf6 41 //IRTransmitter ir_tx(p13,p21,p14,4,&ISR_UARTRX); //tx,pwm,size
cmulady 13:17fd813ef7c9 42
cmulady 3:8e492eacd346 43 //Global Vars
cmulady 12:9acca1dd0e8e 44 char text_buffer[32];
cmulady 6:5563f0026858 45 char irdatOUT[10];
cmulady 6:5563f0026858 46 char irdatIN[10];
cmulady 12:9acca1dd0e8e 47 char irdata_out=0;
cmulady 6:5563f0026858 48 char error_code=0;
cmulady 8:54dd4a3d0de9 49 Thread* threadptr_irstuff;
cmulady 9:3e4e9d6a8ad8 50 char packet_buff[10];
cmulady 10:4fcd5bdb9642 51 Mail<char,64> rx_data_mailbox;
cmulady 1:9ba884d85ac6 52
cmulady 13:17fd813ef7c9 53
cmulady 1:9ba884d85ac6 54
cmulady 1:9ba884d85ac6 55
cmulady 1:9ba884d85ac6 56 int main()
cmulady 1:9ba884d85ac6 57 {
cmulady 0:c8ddcaa575ba 58
cmulady 3:8e492eacd346 59 //LCD init
cmulady 0:c8ddcaa575ba 60 lcd.background(0x000000);
cmulady 1:9ba884d85ac6 61
cmulady 6:5563f0026858 62 //PC serial init
cmulady 6:5563f0026858 63 pc.baud(19200);
cmulady 7:c9ff6b5c8507 64 pc.printf("Starting...\n\n");
cmulady 6:5563f0026858 65
cmulady 13:17fd813ef7c9 66
cmulady 9:3e4e9d6a8ad8 67 //Variable Init
cmulady 9:3e4e9d6a8ad8 68 for(int i=0; i<10; i++) packet_buff[i]=0;
cmulady 9:3e4e9d6a8ad8 69
cmulady 12:9acca1dd0e8e 70 //Threads init
cmulady 1:9ba884d85ac6 71 Thread thread_blinkalive(BlinkAlive);
cmulady 1:9ba884d85ac6 72 Thread thread_updatelcd(UpdateLCD);
cmulady 3:8e492eacd346 73 Thread thread_irstuff(IRStuff);
cmulady 8:54dd4a3d0de9 74 threadptr_irstuff = &thread_irstuff;
cmulady 1:9ba884d85ac6 75
cmulady 1:9ba884d85ac6 76
cmulady 1:9ba884d85ac6 77
cmulady 0:c8ddcaa575ba 78 while(1) {
cmulady 13:17fd813ef7c9 79
cmulady 12:9acca1dd0e8e 80 //Use main loop to set LCD framerate
cmulady 1:9ba884d85ac6 81 thread_updatelcd.signal_set(0x1);
cmulady 1:9ba884d85ac6 82 Thread::wait(1000/FPS);
cmulady 1:9ba884d85ac6 83
cmulady 0:c8ddcaa575ba 84 }
cmulady 0:c8ddcaa575ba 85 }
cmulady 1:9ba884d85ac6 86
cmulady 1:9ba884d85ac6 87 void UpdateLCD(void const* arguments)
cmulady 1:9ba884d85ac6 88 {
cmulady 7:c9ff6b5c8507 89 while(true) {
cmulady 13:17fd813ef7c9 90
cmulady 12:9acca1dd0e8e 91 //Start flash LED
cmulady 13:17fd813ef7c9 92 led2 = 0.02;
cmulady 13:17fd813ef7c9 93
cmulady 12:9acca1dd0e8e 94 //Write debug text to screen
cmulady 1:9ba884d85ac6 95 lcd.locate(0,1);
cmulady 1:9ba884d85ac6 96 lcd.printf("Debug:");
cmulady 1:9ba884d85ac6 97 lcd.locate(0,3);
cmulady 1:9ba884d85ac6 98 time_t seconds = time(NULL);
cmulady 12:9acca1dd0e8e 99 strftime(text_buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
cmulady 12:9acca1dd0e8e 100 lcd.printf("%s", text_buffer);
cmulady 3:8e492eacd346 101 lcd.locate(0,4);
cmulady 6:5563f0026858 102 lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]);
cmulady 3:8e492eacd346 103 lcd.locate(0,5);
cmulady 6:5563f0026858 104 lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]);
cmulady 6:5563f0026858 105 lcd.locate(0,6);
cmulady 6:5563f0026858 106 lcd.printf("Error= 0x%02X", error_code);
cmulady 3:8e492eacd346 107
cmulady 12:9acca1dd0e8e 108 //End - flash LED
cmulady 13:17fd813ef7c9 109 led2 = 0.0;
cmulady 1:9ba884d85ac6 110 //End - Sleep thread
cmulady 1:9ba884d85ac6 111 Thread::signal_wait(0x1);
cmulady 7:c9ff6b5c8507 112 }
cmulady 1:9ba884d85ac6 113 }
cmulady 1:9ba884d85ac6 114
cmulady 3:8e492eacd346 115 void IRStuff(void const* arguments)
cmulady 3:8e492eacd346 116 {
cmulady 3:8e492eacd346 117 while(true) {
cmulady 7:c9ff6b5c8507 118
cmulady 12:9acca1dd0e8e 119 //If data available - Print data directly to USB port (for debug)
cmulady 15:21c512907cf6 120 /*osEvent evt = rx_data_mailbox.get();
cmulady 10:4fcd5bdb9642 121 while(evt.status == osEventMail) {
cmulady 10:4fcd5bdb9642 122 char* mail = (char*)evt.value.p;
cmulady 13:17fd813ef7c9 123 //if((*mail)==0x02) pc.printf("\n");
cmulady 13:17fd813ef7c9 124 //pc.printf("0x%02X.",*mail);
cmulady 13:17fd813ef7c9 125 error_code = CheckPacket(*mail,packet_buff,irdatIN,4);
cmulady 13:17fd813ef7c9 126 pc.printf(" ERROR=0x%02X.\n",error_code);
cmulady 13:17fd813ef7c9 127
cmulady 10:4fcd5bdb9642 128 rx_data_mailbox.free(mail);
cmulady 10:4fcd5bdb9642 129 evt = rx_data_mailbox.get();
cmulady 15:21c512907cf6 130 }*/
cmulady 15:21c512907cf6 131
cmulady 15:21c512907cf6 132 /*osEvent evt = ir_tx.ir_data_mailbox.get();
cmulady 15:21c512907cf6 133 while(evt.status == osEventMail) {
cmulady 15:21c512907cf6 134 char* mail = (char*)evt.value.p;
cmulady 15:21c512907cf6 135 //if((*mail)==0x02) pc.printf("\n");
cmulady 15:21c512907cf6 136 //pc.printf("0x%02X.",*mail);
cmulady 15:21c512907cf6 137 //error_code = CheckPacket(*mail,packet_buff,irdatIN,4);
cmulady 15:21c512907cf6 138 //pc.printf(" ERROR=0x%02X.\n",error_code);
cmulady 15:21c512907cf6 139
cmulady 15:21c512907cf6 140 ir_tx.ir_data_mailbox.free(mail);
cmulady 15:21c512907cf6 141 evt = ir_tx.ir_data_mailbox.get();
cmulady 15:21c512907cf6 142 }*/
cmulady 10:4fcd5bdb9642 143
cmulady 15:21c512907cf6 144 char error_code;
cmulady 15:21c512907cf6 145 //error_code = ir_tx.ReadPacket(irdatIN,4);
cmulady 15:21c512907cf6 146 pc.printf("0x%02X.",error_code);
cmulady 15:21c512907cf6 147
cmulady 15:21c512907cf6 148
cmulady 13:17fd813ef7c9 149
cmulady 12:9acca1dd0e8e 150 //Do not return until we have more data
cmulady 13:17fd813ef7c9 151 //Thread::signal_wait(0x1);
cmulady 13:17fd813ef7c9 152 Thread::wait(20);
cmulady 3:8e492eacd346 153 }
cmulady 4:8fdff78c13c8 154
cmulady 3:8e492eacd346 155 }
cmulady 3:8e492eacd346 156
cmulady 13:17fd813ef7c9 157 char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len)
cmulady 6:5563f0026858 158 {
cmulady 13:17fd813ef7c9 159 //Requires a packet buffer of length 'data_len'+3.
cmulady 9:3e4e9d6a8ad8 160 //Shifts data and checks each 'set' for a valid packet.
cmulady 9:3e4e9d6a8ad8 161 //Once a valid packet is receievd, the data buffer is updated with new values.
cmulady 13:17fd813ef7c9 162 //returns success(0) or failure(error code)
cmulady 13:17fd813ef7c9 163
cmulady 9:3e4e9d6a8ad8 164
cmulady 9:3e4e9d6a8ad8 165 char check=0x0;
cmulady 9:3e4e9d6a8ad8 166 //Shift All data 1 cell over
cmulady 13:17fd813ef7c9 167 for(int i=0; i<data_len+2; i++) {
cmulady 9:3e4e9d6a8ad8 168 packet_buffer[i] = packet_buffer[i+1];
cmulady 9:3e4e9d6a8ad8 169 }
cmulady 13:17fd813ef7c9 170 packet_buffer[data_len+2] = new_data;
cmulady 9:3e4e9d6a8ad8 171
cmulady 9:3e4e9d6a8ad8 172 //check for valid packet
cmulady 13:17fd813ef7c9 173 if(packet_buffer[0]!=ASCII_STX)
cmulady 9:3e4e9d6a8ad8 174 return 0x1; //bad start byte
cmulady 13:17fd813ef7c9 175 if(packet_buffer[data_len+2]!=ASCII_ETX)
cmulady 13:17fd813ef7c9 176 return 0x2; //bad end byte
cmulady 9:3e4e9d6a8ad8 177
cmulady 9:3e4e9d6a8ad8 178 for(int i=1; i<data_len+1; i++) {
cmulady 9:3e4e9d6a8ad8 179 check^=packet_buffer[i];
cmulady 9:3e4e9d6a8ad8 180 }
cmulady 15:21c512907cf6 181 if(check!=packet_buffer[data_len+1]) {
cmulady 15:21c512907cf6 182 return 0x3; //bad checksum
cmulady 9:3e4e9d6a8ad8 183 }
cmulady 13:17fd813ef7c9 184
cmulady 9:3e4e9d6a8ad8 185 return 0;
cmulady 9:3e4e9d6a8ad8 186 }
cmulady 9:3e4e9d6a8ad8 187
cmulady 13:17fd813ef7c9 188 void BlinkAlive(void const* arguments)
cmulady 13:17fd813ef7c9 189 {
cmulady 13:17fd813ef7c9 190 while(true) {
cmulady 13:17fd813ef7c9 191
cmulady 13:17fd813ef7c9 192 //Change LED1 state (debug)
cmulady 13:17fd813ef7c9 193 (led1==0.0)?led1=0.02:led1=0.0;
cmulady 13:17fd813ef7c9 194
cmulady 13:17fd813ef7c9 195 //Form a test packet and send it over IR transmitter
cmulady 13:17fd813ef7c9 196 irdatOUT[0] = 0xA5;
cmulady 13:17fd813ef7c9 197 irdatOUT[1] = ++irdata_out;
cmulady 13:17fd813ef7c9 198 irdatOUT[2] = ~(irdata_out*2);
cmulady 13:17fd813ef7c9 199 irdatOUT[3] = irdata_out*7+13;
cmulady 13:17fd813ef7c9 200 //MakePacket2(irdatOUT,4);
cmulady 15:21c512907cf6 201 //ir_tx.MakePacket(irdatOUT,4);
cmulady 13:17fd813ef7c9 202
cmulady 13:17fd813ef7c9 203 //Roughly use to set rate of data packets per second
cmulady 13:17fd813ef7c9 204 Thread::wait(50);
cmulady 13:17fd813ef7c9 205 }
cmulady 13:17fd813ef7c9 206 }
cmulady 13:17fd813ef7c9 207
cmulady 13:17fd813ef7c9 208 void MakePacket2(char* data,int len)
cmulady 13:17fd813ef7c9 209 {
cmulady 13:17fd813ef7c9 210 /*char check =0x0;
cmulady 13:17fd813ef7c9 211 (*devicerx).putc(ASCII_STX);
cmulady 13:17fd813ef7c9 212 for(int i=0; i<len; i++) {
cmulady 13:17fd813ef7c9 213 check^=data[i];
cmulady 13:17fd813ef7c9 214 (*devicerx).putc(data[i]);
cmulady 13:17fd813ef7c9 215 }
cmulady 13:17fd813ef7c9 216 (*devicerx).putc(check);
cmulady 13:17fd813ef7c9 217 (*devicerx).putc(ASCII_ETX);*/
cmulady 13:17fd813ef7c9 218 }
cmulady 13:17fd813ef7c9 219
cmulady 13:17fd813ef7c9 220
cmulady 13:17fd813ef7c9 221
cmulady 12:9acca1dd0e8e 222 //Handle Reception of RX data (mail it to appropriate thread)
cmulady 14:68f7cefd7359 223 void ISR_UARTRX()
cmulady 7:c9ff6b5c8507 224 {
cmulady 12:9acca1dd0e8e 225 //get RX data (and prevent ISR from looping forever
cmulady 7:c9ff6b5c8507 226 uint32_t RBR = LPC_UART1->RBR;
cmulady 9:3e4e9d6a8ad8 227
cmulady 12:9acca1dd0e8e 228 //write letter and put in mailbox
cmulady 15:21c512907cf6 229 /*char* mail = rx_data_mailbox.alloc();
cmulady 10:4fcd5bdb9642 230 mail[0] = (char)RBR;
cmulady 15:21c512907cf6 231 rx_data_mailbox.put(mail);*/
cmulady 15:21c512907cf6 232
cmulady 15:21c512907cf6 233 /*char* mail = ir_tx.ir_data_mailbox.alloc();
cmulady 15:21c512907cf6 234 mail[0] = (char)RBR;
cmulady 15:21c512907cf6 235 ir_tx.ir_data_mailbox.put(mail);*/
cmulady 14:68f7cefd7359 236
cmulady 15:21c512907cf6 237 //ir_tx.isr(RBR);
cmulady 10:4fcd5bdb9642 238
cmulady 12:9acca1dd0e8e 239 //Let message handler run
cmulady 15:21c512907cf6 240 //(*threadptr_irstuff).signal_set(0x1);
cmulady 8:54dd4a3d0de9 241
cmulady 7:c9ff6b5c8507 242 }
cmulady 7:c9ff6b5c8507 243