asdf
Dependencies: NokiaLCD XMIT_IR mbed
Fork of 4180_mP_WirelessPong_revC by
main.cpp@8:54dd4a3d0de9, 2012-10-05 (annotated)
- Committer:
- cmulady
- Date:
- Fri Oct 05 13:02:52 2012 +0000
- Revision:
- 8:54dd4a3d0de9
- Parent:
- 7:c9ff6b5c8507
- Child:
- 9:3e4e9d6a8ad8
Finally integrated serial interrupts with OS. Will clean up code, then start writing queue library and IRxmit library.
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 | 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 | 1:9ba884d85ac6 | 41 | |
cmulady | 3:8e492eacd346 | 42 | //Function Prototypes |
cmulady | 1:9ba884d85ac6 | 43 | void BlinkAlive(void const* arguments); |
cmulady | 1:9ba884d85ac6 | 44 | void UpdateLCD(void const* arguments); |
cmulady | 3:8e492eacd346 | 45 | void IRStuff(void const* arguments); |
cmulady | 6:5563f0026858 | 46 | void MakePacket(char* data,int len); |
cmulady | 6:5563f0026858 | 47 | char CheckPacket(char* data, int data_size); |
cmulady | 7:c9ff6b5c8507 | 48 | void ISR_UARTRX(void); |
cmulady | 7:c9ff6b5c8507 | 49 | void DoNothing(void const* arguments); |
cmulady | 1:9ba884d85ac6 | 50 | |
cmulady | 1:9ba884d85ac6 | 51 | |
cmulady | 1:9ba884d85ac6 | 52 | int main() |
cmulady | 1:9ba884d85ac6 | 53 | { |
cmulady | 0:c8ddcaa575ba | 54 | |
cmulady | 3:8e492eacd346 | 55 | //LCD init |
cmulady | 0:c8ddcaa575ba | 56 | lcd.background(0x000000); |
cmulady | 1:9ba884d85ac6 | 57 | |
cmulady | 3:8e492eacd346 | 58 | //PWM init |
cmulady | 3:8e492eacd346 | 59 | IRLED_mod.period(1.0/38000.0); //38kHz Modulation Freq |
cmulady | 3:8e492eacd346 | 60 | IRLED_mod = 0.5; //pulse width = 50% |
cmulady | 3:8e492eacd346 | 61 | |
cmulady | 3:8e492eacd346 | 62 | //Serial init |
cmulady | 3:8e492eacd346 | 63 | device.baud(2400); |
cmulady | 8:54dd4a3d0de9 | 64 | device.attach(&ISR_UARTRX,Serial::RxIrq); |
cmulady | 3:8e492eacd346 | 65 | |
cmulady | 6:5563f0026858 | 66 | //PC serial init |
cmulady | 6:5563f0026858 | 67 | pc.baud(19200); |
cmulady | 7:c9ff6b5c8507 | 68 | pc.printf("Starting...\n\n"); |
cmulady | 6:5563f0026858 | 69 | |
cmulady | 3:8e492eacd346 | 70 | //Thread 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 | 7:c9ff6b5c8507 | 74 | Thread thread_donothing(DoNothing); |
cmulady | 8:54dd4a3d0de9 | 75 | threadptr_irstuff = &thread_irstuff; |
cmulady | 1:9ba884d85ac6 | 76 | |
cmulady | 1:9ba884d85ac6 | 77 | |
cmulady | 1:9ba884d85ac6 | 78 | |
cmulady | 0:c8ddcaa575ba | 79 | while(1) { |
cmulady | 1:9ba884d85ac6 | 80 | thread_updatelcd.signal_set(0x1); |
cmulady | 1:9ba884d85ac6 | 81 | Thread::wait(1000/FPS); |
cmulady | 1:9ba884d85ac6 | 82 | |
cmulady | 0:c8ddcaa575ba | 83 | } |
cmulady | 0:c8ddcaa575ba | 84 | } |
cmulady | 1:9ba884d85ac6 | 85 | |
cmulady | 1:9ba884d85ac6 | 86 | void UpdateLCD(void const* arguments) |
cmulady | 1:9ba884d85ac6 | 87 | { |
cmulady | 7:c9ff6b5c8507 | 88 | while(true) { |
cmulady | 1:9ba884d85ac6 | 89 | led2 = 1; |
cmulady | 1:9ba884d85ac6 | 90 | lcd.locate(0,1); |
cmulady | 1:9ba884d85ac6 | 91 | lcd.printf("Debug:"); |
cmulady | 1:9ba884d85ac6 | 92 | |
cmulady | 1:9ba884d85ac6 | 93 | lcd.locate(0,3); |
cmulady | 1:9ba884d85ac6 | 94 | time_t seconds = time(NULL); |
cmulady | 1:9ba884d85ac6 | 95 | strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds)); |
cmulady | 1:9ba884d85ac6 | 96 | lcd.printf("%s", buffer); |
cmulady | 1:9ba884d85ac6 | 97 | |
cmulady | 3:8e492eacd346 | 98 | lcd.locate(0,4); |
cmulady | 6:5563f0026858 | 99 | lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]); |
cmulady | 3:8e492eacd346 | 100 | lcd.locate(0,5); |
cmulady | 6:5563f0026858 | 101 | lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]); |
cmulady | 6:5563f0026858 | 102 | lcd.locate(0,6); |
cmulady | 6:5563f0026858 | 103 | lcd.printf("Error= 0x%02X", error_code); |
cmulady | 3:8e492eacd346 | 104 | |
cmulady | 3:8e492eacd346 | 105 | |
cmulady | 3:8e492eacd346 | 106 | |
cmulady | 1:9ba884d85ac6 | 107 | //End - Sleep thread |
cmulady | 1:9ba884d85ac6 | 108 | led2 = 0; |
cmulady | 1:9ba884d85ac6 | 109 | Thread::signal_wait(0x1); |
cmulady | 7:c9ff6b5c8507 | 110 | } |
cmulady | 1:9ba884d85ac6 | 111 | } |
cmulady | 1:9ba884d85ac6 | 112 | |
cmulady | 3:8e492eacd346 | 113 | void IRStuff(void const* arguments) |
cmulady | 3:8e492eacd346 | 114 | { |
cmulady | 3:8e492eacd346 | 115 | while(true) { |
cmulady | 8:54dd4a3d0de9 | 116 | //error_code = CheckPacket(irdatIN,2); |
cmulady | 7:c9ff6b5c8507 | 117 | |
cmulady | 7:c9ff6b5c8507 | 118 | /*while(device.readable()) { |
cmulady | 6:5563f0026858 | 119 | char tempdata = device.getc(); |
cmulady | 6:5563f0026858 | 120 | if(tempdata==0x02) pc.printf("\n"); |
cmulady | 6:5563f0026858 | 121 | pc.printf("0x%02X.",tempdata); |
cmulady | 7:c9ff6b5c8507 | 122 | }*/ |
cmulady | 7:c9ff6b5c8507 | 123 | |
cmulady | 7:c9ff6b5c8507 | 124 | //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x400FC0C4)); |
cmulady | 7:c9ff6b5c8507 | 125 | |
cmulady | 8:54dd4a3d0de9 | 126 | if(rx_data_available) { |
cmulady | 8:54dd4a3d0de9 | 127 | if(irdatIN[0]==0x2) pc.printf("\n"); |
cmulady | 8:54dd4a3d0de9 | 128 | pc.printf("0x%02X.",irdatIN[0]); |
cmulady | 8:54dd4a3d0de9 | 129 | //pc.printf("0x%02X.",irdatIN[1]); |
cmulady | 8:54dd4a3d0de9 | 130 | //pc.printf("0x%02X.",irdatIN[2]); |
cmulady | 8:54dd4a3d0de9 | 131 | //pc.printf("0x%02X.\n",irdatIN[3]); |
cmulady | 8:54dd4a3d0de9 | 132 | rx_data_available = false; |
cmulady | 8:54dd4a3d0de9 | 133 | } |
cmulady | 7:c9ff6b5c8507 | 134 | |
cmulady | 7:c9ff6b5c8507 | 135 | |
cmulady | 7:c9ff6b5c8507 | 136 | //pc.printf("\n\nE=0x%02X\n\n",error_code); |
cmulady | 8:54dd4a3d0de9 | 137 | /*if(error_code==0x0) { |
cmulady | 7:c9ff6b5c8507 | 138 | pc.printf("0x%02X.",irdatIN[1]); |
cmulady | 7:c9ff6b5c8507 | 139 | Thread::wait(10); |
cmulady | 7:c9ff6b5c8507 | 140 | } else if(error_code==0x2) { |
cmulady | 7:c9ff6b5c8507 | 141 | pc.printf("!"); |
cmulady | 7:c9ff6b5c8507 | 142 | Thread::wait(10); |
cmulady | 7:c9ff6b5c8507 | 143 | } else { |
cmulady | 7:c9ff6b5c8507 | 144 | Thread::wait(10); |
cmulady | 8:54dd4a3d0de9 | 145 | }*/ |
cmulady | 8:54dd4a3d0de9 | 146 | Thread::signal_wait(0x1); |
cmulady | 3:8e492eacd346 | 147 | } |
cmulady | 4:8fdff78c13c8 | 148 | |
cmulady | 3:8e492eacd346 | 149 | } |
cmulady | 3:8e492eacd346 | 150 | |
cmulady | 1:9ba884d85ac6 | 151 | void BlinkAlive(void const* arguments) |
cmulady | 1:9ba884d85ac6 | 152 | { |
cmulady | 1:9ba884d85ac6 | 153 | while(true) { |
cmulady | 1:9ba884d85ac6 | 154 | led1 = !led1; |
cmulady | 6:5563f0026858 | 155 | |
cmulady | 6:5563f0026858 | 156 | irdatOUT[0] = 0xA5; |
cmulady | 6:5563f0026858 | 157 | irdatOUT[1] = ++irdata_out; |
cmulady | 6:5563f0026858 | 158 | MakePacket(irdatOUT,2); |
cmulady | 8:54dd4a3d0de9 | 159 | //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014)); |
cmulady | 6:5563f0026858 | 160 | |
cmulady | 6:5563f0026858 | 161 | Thread::wait(20); |
cmulady | 6:5563f0026858 | 162 | } |
cmulady | 6:5563f0026858 | 163 | } |
cmulady | 6:5563f0026858 | 164 | |
cmulady | 6:5563f0026858 | 165 | void MakePacket(char* data,int len) |
cmulady | 6:5563f0026858 | 166 | { |
cmulady | 6:5563f0026858 | 167 | //pc.printf("\nMaking Packet:\n\t"); |
cmulady | 6:5563f0026858 | 168 | char check =0x0; |
cmulady | 6:5563f0026858 | 169 | device.putc(0x02); |
cmulady | 6:5563f0026858 | 170 | //pc.printf("0x%02X.",0x02); |
cmulady | 6:5563f0026858 | 171 | for(int i=0; i<len; i++) { |
cmulady | 8:54dd4a3d0de9 | 172 | check+=data[i]; |
cmulady | 6:5563f0026858 | 173 | device.putc(data[i]); |
cmulady | 6:5563f0026858 | 174 | //pc.printf("0x%02X.",data[i]); |
cmulady | 1:9ba884d85ac6 | 175 | } |
cmulady | 6:5563f0026858 | 176 | device.putc(check); |
cmulady | 6:5563f0026858 | 177 | //pc.printf("0x%02X.",check); |
cmulady | 6:5563f0026858 | 178 | //pc.printf("\nDone making packet.\n"); |
cmulady | 6:5563f0026858 | 179 | } |
cmulady | 6:5563f0026858 | 180 | |
cmulady | 6:5563f0026858 | 181 | char CheckPacket(char* data, int data_size) |
cmulady | 6:5563f0026858 | 182 | //returns success(0) or failure(error code) |
cmulady | 6:5563f0026858 | 183 | { |
cmulady | 6:5563f0026858 | 184 | Timer t; |
cmulady | 6:5563f0026858 | 185 | t.reset(); |
cmulady | 6:5563f0026858 | 186 | t.start(); |
cmulady | 6:5563f0026858 | 187 | char tempdata=0x0; |
cmulady | 6:5563f0026858 | 188 | char check=0x0; |
cmulady | 6:5563f0026858 | 189 | |
cmulady | 7:c9ff6b5c8507 | 190 | //pc.printf("\nChecking Packet\n\t"); |
cmulady | 6:5563f0026858 | 191 | //Data available |
cmulady | 6:5563f0026858 | 192 | if(!device.readable()) { |
cmulady | 7:c9ff6b5c8507 | 193 | //pc.printf("no data"); |
cmulady | 6:5563f0026858 | 194 | return 0x4; //no data |
cmulady | 6:5563f0026858 | 195 | } |
cmulady | 6:5563f0026858 | 196 | |
cmulady | 6:5563f0026858 | 197 | while(!device.readable()); |
cmulady | 6:5563f0026858 | 198 | //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); |
cmulady | 6:5563f0026858 | 199 | //if(t.read_ms()>=UART_TIMEOUT) |
cmulady | 6:5563f0026858 | 200 | // return 0x3; //timeout error |
cmulady | 6:5563f0026858 | 201 | tempdata = device.getc(); |
cmulady | 6:5563f0026858 | 202 | |
cmulady | 6:5563f0026858 | 203 | //STX recieved |
cmulady | 6:5563f0026858 | 204 | if(tempdata!=0x02) { |
cmulady | 7:c9ff6b5c8507 | 205 | if(tempdata==0xFF) pc.printf("found bad data: 0x%02X",tempdata); |
cmulady | 8:54dd4a3d0de9 | 206 | pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014)); |
cmulady | 6:5563f0026858 | 207 | return 0x1; //bad start byte |
cmulady | 6:5563f0026858 | 208 | } |
cmulady | 6:5563f0026858 | 209 | |
cmulady | 6:5563f0026858 | 210 | //Get Data |
cmulady | 6:5563f0026858 | 211 | for(int i=0; i<data_size; i++) { |
cmulady | 6:5563f0026858 | 212 | while(!device.readable()); |
cmulady | 6:5563f0026858 | 213 | //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); |
cmulady | 6:5563f0026858 | 214 | //if(t.read_ms()>=UART_TIMEOUT) |
cmulady | 6:5563f0026858 | 215 | // return 0x3; //timeout error |
cmulady | 6:5563f0026858 | 216 | data[i] = device.getc(); |
cmulady | 8:54dd4a3d0de9 | 217 | check += data[i]; |
cmulady | 6:5563f0026858 | 218 | } |
cmulady | 6:5563f0026858 | 219 | //Get Checksum |
cmulady | 6:5563f0026858 | 220 | while(!device.readable()); |
cmulady | 6:5563f0026858 | 221 | //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); |
cmulady | 6:5563f0026858 | 222 | //if(t.read_ms()>=UART_TIMEOUT) |
cmulady | 6:5563f0026858 | 223 | // return 0x3; //timeout error |
cmulady | 6:5563f0026858 | 224 | tempdata = device.getc(); |
cmulady | 6:5563f0026858 | 225 | if(tempdata!=check) |
cmulady | 6:5563f0026858 | 226 | return 0x2; //bad checksum |
cmulady | 6:5563f0026858 | 227 | |
cmulady | 6:5563f0026858 | 228 | |
cmulady | 6:5563f0026858 | 229 | return 0; |
cmulady | 6:5563f0026858 | 230 | |
cmulady | 6:5563f0026858 | 231 | } |
cmulady | 6:5563f0026858 | 232 | |
cmulady | 7:c9ff6b5c8507 | 233 | void DoNothing(void const* arguments) |
cmulady | 7:c9ff6b5c8507 | 234 | { |
cmulady | 7:c9ff6b5c8507 | 235 | while(true) { |
cmulady | 7:c9ff6b5c8507 | 236 | Thread::signal_wait(0x1); |
cmulady | 7:c9ff6b5c8507 | 237 | } |
cmulady | 7:c9ff6b5c8507 | 238 | } |
cmulady | 7:c9ff6b5c8507 | 239 | |
cmulady | 7:c9ff6b5c8507 | 240 | void ISR_UARTRX(void) |
cmulady | 7:c9ff6b5c8507 | 241 | { |
cmulady | 7:c9ff6b5c8507 | 242 | uint32_t RBR = LPC_UART1->RBR; |
cmulady | 8:54dd4a3d0de9 | 243 | irdatIN[0] = 0; |
cmulady | 8:54dd4a3d0de9 | 244 | irdatIN[1] = 0; |
cmulady | 8:54dd4a3d0de9 | 245 | irdatIN[2] = 0; |
cmulady | 8:54dd4a3d0de9 | 246 | irdatIN[3] = 0; |
cmulady | 7:c9ff6b5c8507 | 247 | |
cmulady | 8:54dd4a3d0de9 | 248 | irdatIN[0] = (char)RBR; |
cmulady | 8:54dd4a3d0de9 | 249 | /*if(device.readable()) |
cmulady | 8:54dd4a3d0de9 | 250 | irdatIN[1] = (char) LPC_UART1->RBR; |
cmulady | 8:54dd4a3d0de9 | 251 | if(device.readable()) |
cmulady | 8:54dd4a3d0de9 | 252 | irdatIN[2] = (char) LPC_UART1->RBR; |
cmulady | 8:54dd4a3d0de9 | 253 | if(device.readable()) |
cmulady | 8:54dd4a3d0de9 | 254 | irdatIN[3] = (char) LPC_UART1->RBR; |
cmulady | 8:54dd4a3d0de9 | 255 | */ |
cmulady | 8:54dd4a3d0de9 | 256 | |
cmulady | 8:54dd4a3d0de9 | 257 | rx_data_available = true; |
cmulady | 8:54dd4a3d0de9 | 258 | (*threadptr_irstuff).signal_set(0x1); |
cmulady | 8:54dd4a3d0de9 | 259 | |
cmulady | 8:54dd4a3d0de9 | 260 | |
cmulady | 8:54dd4a3d0de9 | 261 | /*while(device.readable()) { |
cmulady | 7:c9ff6b5c8507 | 262 | char data = device.getc(); |
cmulady | 7:c9ff6b5c8507 | 263 | //pc.printf("*"); |
cmulady | 8:54dd4a3d0de9 | 264 | }*/ |
cmulady | 7:c9ff6b5c8507 | 265 | |
cmulady | 7:c9ff6b5c8507 | 266 | } |
cmulady | 7:c9ff6b5c8507 | 267 |