asdf
Dependencies: NokiaLCD XMIT_IR mbed
Fork of 4180_mP_WirelessPong_revC by
main.cpp@17:f92e4a384637, 2012-10-06 (annotated)
- Committer:
- cmulady
- Date:
- Sat Oct 06 13:40:39 2012 +0000
- Revision:
- 17:f92e4a384637
- Parent:
- 16:bc0e213ba52d
- Child:
- 18:98839f78fc01
mutex lock = black magic; all problems fixed
Who changed what in which revision?
User | Revision | Line number | New 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 | 16:bc0e213ba52d | 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 | 17:f92e4a384637 | 52 | Mutex data_update_mutex; |
cmulady | 1:9ba884d85ac6 | 53 | |
cmulady | 13:17fd813ef7c9 | 54 | |
cmulady | 1:9ba884d85ac6 | 55 | |
cmulady | 1:9ba884d85ac6 | 56 | |
cmulady | 1:9ba884d85ac6 | 57 | int main() |
cmulady | 1:9ba884d85ac6 | 58 | { |
cmulady | 0:c8ddcaa575ba | 59 | |
cmulady | 3:8e492eacd346 | 60 | //LCD init |
cmulady | 0:c8ddcaa575ba | 61 | lcd.background(0x000000); |
cmulady | 1:9ba884d85ac6 | 62 | |
cmulady | 6:5563f0026858 | 63 | //PC serial init |
cmulady | 6:5563f0026858 | 64 | pc.baud(19200); |
cmulady | 7:c9ff6b5c8507 | 65 | pc.printf("Starting...\n\n"); |
cmulady | 6:5563f0026858 | 66 | |
cmulady | 16:bc0e213ba52d | 67 | //IR_TRX module |
cmulady | 16:bc0e213ba52d | 68 | ir_tx.set_debug_port(&pc); |
cmulady | 16:bc0e213ba52d | 69 | |
cmulady | 13:17fd813ef7c9 | 70 | |
cmulady | 9:3e4e9d6a8ad8 | 71 | //Variable Init |
cmulady | 9:3e4e9d6a8ad8 | 72 | for(int i=0; i<10; i++) packet_buff[i]=0; |
cmulady | 9:3e4e9d6a8ad8 | 73 | |
cmulady | 12:9acca1dd0e8e | 74 | //Threads init |
cmulady | 1:9ba884d85ac6 | 75 | Thread thread_blinkalive(BlinkAlive); |
cmulady | 1:9ba884d85ac6 | 76 | Thread thread_updatelcd(UpdateLCD); |
cmulady | 3:8e492eacd346 | 77 | Thread thread_irstuff(IRStuff); |
cmulady | 8:54dd4a3d0de9 | 78 | threadptr_irstuff = &thread_irstuff; |
cmulady | 1:9ba884d85ac6 | 79 | |
cmulady | 1:9ba884d85ac6 | 80 | |
cmulady | 1:9ba884d85ac6 | 81 | |
cmulady | 0:c8ddcaa575ba | 82 | while(1) { |
cmulady | 13:17fd813ef7c9 | 83 | |
cmulady | 12:9acca1dd0e8e | 84 | //Use main loop to set LCD framerate |
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 | 13:17fd813ef7c9 | 94 | |
cmulady | 12:9acca1dd0e8e | 95 | //Start flash LED |
cmulady | 13:17fd813ef7c9 | 96 | led2 = 0.02; |
cmulady | 17:f92e4a384637 | 97 | data_update_mutex.lock(); |
cmulady | 17:f92e4a384637 | 98 | char irdatbuff[4]; |
cmulady | 17:f92e4a384637 | 99 | for(int i=0; i<4; i++) irdatbuff[i] = irdatIN[i]; |
cmulady | 17:f92e4a384637 | 100 | data_update_mutex.unlock(); |
cmulady | 17:f92e4a384637 | 101 | |
cmulady | 17:f92e4a384637 | 102 | if(irdatbuff[0]!=0xA5 && irdatbuff[0]!=0x00) { |
cmulady | 17:f92e4a384637 | 103 | lcd.locate(0,7); |
cmulady | 17:f92e4a384637 | 104 | lcd.printf("Big Problem! 0x%02X",irdatbuff[0]); |
cmulady | 17:f92e4a384637 | 105 | while(1); |
cmulady | 17:f92e4a384637 | 106 | } |
cmulady | 17:f92e4a384637 | 107 | |
cmulady | 13:17fd813ef7c9 | 108 | |
cmulady | 12:9acca1dd0e8e | 109 | //Write debug text to screen |
cmulady | 1:9ba884d85ac6 | 110 | lcd.locate(0,1); |
cmulady | 1:9ba884d85ac6 | 111 | lcd.printf("Debug:"); |
cmulady | 1:9ba884d85ac6 | 112 | lcd.locate(0,3); |
cmulady | 1:9ba884d85ac6 | 113 | time_t seconds = time(NULL); |
cmulady | 12:9acca1dd0e8e | 114 | strftime(text_buffer, 32, "%I:%M:%S %p\n", localtime(&seconds)); |
cmulady | 12:9acca1dd0e8e | 115 | lcd.printf("%s", text_buffer); |
cmulady | 3:8e492eacd346 | 116 | lcd.locate(0,4); |
cmulady | 6:5563f0026858 | 117 | lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]); |
cmulady | 3:8e492eacd346 | 118 | lcd.locate(0,5); |
cmulady | 17:f92e4a384637 | 119 | lcd.printf("IR_IN= 0x%02X,0x%02X", irdatbuff[0],irdatbuff[1]); |
cmulady | 6:5563f0026858 | 120 | lcd.locate(0,6); |
cmulady | 6:5563f0026858 | 121 | lcd.printf("Error= 0x%02X", error_code); |
cmulady | 3:8e492eacd346 | 122 | |
cmulady | 12:9acca1dd0e8e | 123 | //End - flash LED |
cmulady | 13:17fd813ef7c9 | 124 | led2 = 0.0; |
cmulady | 1:9ba884d85ac6 | 125 | //End - Sleep thread |
cmulady | 1:9ba884d85ac6 | 126 | Thread::signal_wait(0x1); |
cmulady | 7:c9ff6b5c8507 | 127 | } |
cmulady | 1:9ba884d85ac6 | 128 | } |
cmulady | 1:9ba884d85ac6 | 129 | |
cmulady | 3:8e492eacd346 | 130 | void IRStuff(void const* arguments) |
cmulady | 3:8e492eacd346 | 131 | { |
cmulady | 3:8e492eacd346 | 132 | while(true) { |
cmulady | 7:c9ff6b5c8507 | 133 | |
cmulady | 12:9acca1dd0e8e | 134 | //If data available - Print data directly to USB port (for debug) |
cmulady | 16:bc0e213ba52d | 135 | |
cmulady | 16:bc0e213ba52d | 136 | char result; |
cmulady | 17:f92e4a384637 | 137 | char temp_buff[4]; |
cmulady | 17:f92e4a384637 | 138 | |
cmulady | 17:f92e4a384637 | 139 | //buffer data into temp register |
cmulady | 17:f92e4a384637 | 140 | result = ir_tx.ReadPacket(temp_buff,4); |
cmulady | 17:f92e4a384637 | 141 | |
cmulady | 16:bc0e213ba52d | 142 | if(result == 0x1) { |
cmulady | 16:bc0e213ba52d | 143 | pc.printf("\n"); |
cmulady | 17:f92e4a384637 | 144 | //update data - mutex |
cmulady | 17:f92e4a384637 | 145 | data_update_mutex.lock(); |
cmulady | 17:f92e4a384637 | 146 | for(int i=0; i<4; i++) { |
cmulady | 17:f92e4a384637 | 147 | irdatIN[i] = temp_buff[i]; |
cmulady | 17:f92e4a384637 | 148 | } |
cmulady | 17:f92e4a384637 | 149 | data_update_mutex.unlock(); |
cmulady | 16:bc0e213ba52d | 150 | for(int i=0; i<4; i++) { |
cmulady | 16:bc0e213ba52d | 151 | pc.printf("0x%02X.",irdatIN[i]); |
cmulady | 16:bc0e213ba52d | 152 | } |
cmulady | 16:bc0e213ba52d | 153 | } |
cmulady | 16:bc0e213ba52d | 154 | |
cmulady | 15:21c512907cf6 | 155 | |
cmulady | 13:17fd813ef7c9 | 156 | |
cmulady | 12:9acca1dd0e8e | 157 | //Do not return until we have more data |
cmulady | 13:17fd813ef7c9 | 158 | //Thread::signal_wait(0x1); |
cmulady | 16:bc0e213ba52d | 159 | Thread::wait(10); |
cmulady | 3:8e492eacd346 | 160 | } |
cmulady | 4:8fdff78c13c8 | 161 | |
cmulady | 3:8e492eacd346 | 162 | } |
cmulady | 3:8e492eacd346 | 163 | |
cmulady | 13:17fd813ef7c9 | 164 | char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len) |
cmulady | 6:5563f0026858 | 165 | { |
cmulady | 13:17fd813ef7c9 | 166 | //Requires a packet buffer of length 'data_len'+3. |
cmulady | 9:3e4e9d6a8ad8 | 167 | //Shifts data and checks each 'set' for a valid packet. |
cmulady | 9:3e4e9d6a8ad8 | 168 | //Once a valid packet is receievd, the data buffer is updated with new values. |
cmulady | 13:17fd813ef7c9 | 169 | //returns success(0) or failure(error code) |
cmulady | 13:17fd813ef7c9 | 170 | |
cmulady | 9:3e4e9d6a8ad8 | 171 | |
cmulady | 9:3e4e9d6a8ad8 | 172 | char check=0x0; |
cmulady | 9:3e4e9d6a8ad8 | 173 | //Shift All data 1 cell over |
cmulady | 13:17fd813ef7c9 | 174 | for(int i=0; i<data_len+2; i++) { |
cmulady | 9:3e4e9d6a8ad8 | 175 | packet_buffer[i] = packet_buffer[i+1]; |
cmulady | 9:3e4e9d6a8ad8 | 176 | } |
cmulady | 13:17fd813ef7c9 | 177 | packet_buffer[data_len+2] = new_data; |
cmulady | 9:3e4e9d6a8ad8 | 178 | |
cmulady | 9:3e4e9d6a8ad8 | 179 | //check for valid packet |
cmulady | 13:17fd813ef7c9 | 180 | if(packet_buffer[0]!=ASCII_STX) |
cmulady | 9:3e4e9d6a8ad8 | 181 | return 0x1; //bad start byte |
cmulady | 13:17fd813ef7c9 | 182 | if(packet_buffer[data_len+2]!=ASCII_ETX) |
cmulady | 13:17fd813ef7c9 | 183 | return 0x2; //bad end byte |
cmulady | 9:3e4e9d6a8ad8 | 184 | |
cmulady | 9:3e4e9d6a8ad8 | 185 | for(int i=1; i<data_len+1; i++) { |
cmulady | 9:3e4e9d6a8ad8 | 186 | check^=packet_buffer[i]; |
cmulady | 9:3e4e9d6a8ad8 | 187 | } |
cmulady | 15:21c512907cf6 | 188 | if(check!=packet_buffer[data_len+1]) { |
cmulady | 15:21c512907cf6 | 189 | return 0x3; //bad checksum |
cmulady | 9:3e4e9d6a8ad8 | 190 | } |
cmulady | 13:17fd813ef7c9 | 191 | |
cmulady | 9:3e4e9d6a8ad8 | 192 | return 0; |
cmulady | 9:3e4e9d6a8ad8 | 193 | } |
cmulady | 9:3e4e9d6a8ad8 | 194 | |
cmulady | 13:17fd813ef7c9 | 195 | void BlinkAlive(void const* arguments) |
cmulady | 13:17fd813ef7c9 | 196 | { |
cmulady | 13:17fd813ef7c9 | 197 | while(true) { |
cmulady | 13:17fd813ef7c9 | 198 | |
cmulady | 13:17fd813ef7c9 | 199 | //Change LED1 state (debug) |
cmulady | 13:17fd813ef7c9 | 200 | (led1==0.0)?led1=0.02:led1=0.0; |
cmulady | 13:17fd813ef7c9 | 201 | |
cmulady | 13:17fd813ef7c9 | 202 | //Form a test packet and send it over IR transmitter |
cmulady | 13:17fd813ef7c9 | 203 | irdatOUT[0] = 0xA5; |
cmulady | 13:17fd813ef7c9 | 204 | irdatOUT[1] = ++irdata_out; |
cmulady | 13:17fd813ef7c9 | 205 | irdatOUT[2] = ~(irdata_out*2); |
cmulady | 13:17fd813ef7c9 | 206 | irdatOUT[3] = irdata_out*7+13; |
cmulady | 13:17fd813ef7c9 | 207 | //MakePacket2(irdatOUT,4); |
cmulady | 16:bc0e213ba52d | 208 | ir_tx.MakePacket(irdatOUT,4); |
cmulady | 13:17fd813ef7c9 | 209 | |
cmulady | 13:17fd813ef7c9 | 210 | //Roughly use to set rate of data packets per second |
cmulady | 16:bc0e213ba52d | 211 | Thread::wait(200); |
cmulady | 13:17fd813ef7c9 | 212 | } |
cmulady | 13:17fd813ef7c9 | 213 | } |
cmulady | 13:17fd813ef7c9 | 214 | |
cmulady | 13:17fd813ef7c9 | 215 | void MakePacket2(char* data,int len) |
cmulady | 13:17fd813ef7c9 | 216 | { |
cmulady | 13:17fd813ef7c9 | 217 | /*char check =0x0; |
cmulady | 13:17fd813ef7c9 | 218 | (*devicerx).putc(ASCII_STX); |
cmulady | 13:17fd813ef7c9 | 219 | for(int i=0; i<len; i++) { |
cmulady | 13:17fd813ef7c9 | 220 | check^=data[i]; |
cmulady | 13:17fd813ef7c9 | 221 | (*devicerx).putc(data[i]); |
cmulady | 13:17fd813ef7c9 | 222 | } |
cmulady | 13:17fd813ef7c9 | 223 | (*devicerx).putc(check); |
cmulady | 13:17fd813ef7c9 | 224 | (*devicerx).putc(ASCII_ETX);*/ |
cmulady | 13:17fd813ef7c9 | 225 | } |
cmulady | 13:17fd813ef7c9 | 226 | |
cmulady | 13:17fd813ef7c9 | 227 | |
cmulady | 13:17fd813ef7c9 | 228 | |
cmulady | 12:9acca1dd0e8e | 229 | //Handle Reception of RX data (mail it to appropriate thread) |
cmulady | 14:68f7cefd7359 | 230 | void ISR_UARTRX() |
cmulady | 7:c9ff6b5c8507 | 231 | { |
cmulady | 12:9acca1dd0e8e | 232 | //get RX data (and prevent ISR from looping forever |
cmulady | 7:c9ff6b5c8507 | 233 | uint32_t RBR = LPC_UART1->RBR; |
cmulady | 9:3e4e9d6a8ad8 | 234 | |
cmulady | 12:9acca1dd0e8e | 235 | //write letter and put in mailbox |
cmulady | 15:21c512907cf6 | 236 | /*char* mail = rx_data_mailbox.alloc(); |
cmulady | 10:4fcd5bdb9642 | 237 | mail[0] = (char)RBR; |
cmulady | 15:21c512907cf6 | 238 | rx_data_mailbox.put(mail);*/ |
cmulady | 15:21c512907cf6 | 239 | |
cmulady | 15:21c512907cf6 | 240 | /*char* mail = ir_tx.ir_data_mailbox.alloc(); |
cmulady | 15:21c512907cf6 | 241 | mail[0] = (char)RBR; |
cmulady | 15:21c512907cf6 | 242 | ir_tx.ir_data_mailbox.put(mail);*/ |
cmulady | 16:bc0e213ba52d | 243 | |
cmulady | 16:bc0e213ba52d | 244 | ir_tx.isr(RBR); |
cmulady | 10:4fcd5bdb9642 | 245 | |
cmulady | 12:9acca1dd0e8e | 246 | //Let message handler run |
cmulady | 15:21c512907cf6 | 247 | //(*threadptr_irstuff).signal_set(0x1); |
cmulady | 8:54dd4a3d0de9 | 248 | |
cmulady | 7:c9ff6b5c8507 | 249 | } |
cmulady | 7:c9ff6b5c8507 | 250 |