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