asdf
Dependencies: NokiaLCD XMIT_IR mbed
Fork of 4180_mP_WirelessPong_revC by
main.cpp@6:5563f0026858, 2012-10-04 (annotated)
- Committer:
- cmulady
- Date:
- Thu Oct 04 23:33:35 2012 +0000
- Revision:
- 6:5563f0026858
- Parent:
- 5:2e08cc167fff
- Child:
- 7:c9ff6b5c8507
got ir packets working. strange errors popping up when i try doing lcd stuff at the same time.
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 | 6:5563f0026858 | 7 | #define UART_TIMEOUT 500 |
cmulady | 1:9ba884d85ac6 | 8 | |
cmulady | 3:8e492eacd346 | 9 | /**************************************** |
cmulady | 3:8e492eacd346 | 10 | |=======================================| |
cmulady | 3:8e492eacd346 | 11 | |MBED Connections: | |
cmulady | 3:8e492eacd346 | 12 | | -p5 : DIO on Sparkfun Nokia LCD | |
cmulady | 3:8e492eacd346 | 13 | | -p7 : CLK on Sparkfun Nokia LCD | |
cmulady | 3:8e492eacd346 | 14 | | -p8 : CS on Sparkfun Nokia LCD | |
cmulady | 3:8e492eacd346 | 15 | | -p9 : RST on Sparkfun Nokia LCD | |
cmulady | 3:8e492eacd346 | 16 | | -p22: GND on Sparkfun IR Xmtr | |
cmulady | 3:8e492eacd346 | 17 | |=======================================| |
cmulady | 3:8e492eacd346 | 18 | ****************************************/ |
cmulady | 1:9ba884d85ac6 | 19 | |
cmulady | 3:8e492eacd346 | 20 | //Pin Setup |
cmulady | 1:9ba884d85ac6 | 21 | DigitalOut led1(LED1); |
cmulady | 1:9ba884d85ac6 | 22 | DigitalOut led2(LED2); |
cmulady | 3:8e492eacd346 | 23 | DigitalOut led3(LED3); |
cmulady | 3:8e492eacd346 | 24 | DigitalOut led4(LED4); |
cmulady | 0:c8ddcaa575ba | 25 | NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type |
cmulady | 5:2e08cc167fff | 26 | Serial device(p13, p14); // tx, rx |
cmulady | 6:5563f0026858 | 27 | Serial pc(USBTX,USBRX); |
cmulady | 5:2e08cc167fff | 28 | PwmOut IRLED_mod(p21); |
cmulady | 1:9ba884d85ac6 | 29 | |
cmulady | 3:8e492eacd346 | 30 | //Global Vars |
cmulady | 1:9ba884d85ac6 | 31 | char buffer[32]; |
cmulady | 3:8e492eacd346 | 32 | unsigned char irdata_out=0; |
cmulady | 3:8e492eacd346 | 33 | unsigned char irdata_in=0; |
cmulady | 6:5563f0026858 | 34 | char irdatOUT[10]; |
cmulady | 6:5563f0026858 | 35 | char irdatIN[10]; |
cmulady | 6:5563f0026858 | 36 | char error_code=0; |
cmulady | 1:9ba884d85ac6 | 37 | |
cmulady | 3:8e492eacd346 | 38 | //Function Prototypes |
cmulady | 1:9ba884d85ac6 | 39 | void BlinkAlive(void const* arguments); |
cmulady | 1:9ba884d85ac6 | 40 | void UpdateLCD(void const* arguments); |
cmulady | 3:8e492eacd346 | 41 | void IRStuff(void const* arguments); |
cmulady | 6:5563f0026858 | 42 | void MakePacket(char* data,int len); |
cmulady | 6:5563f0026858 | 43 | char CheckPacket(char* data, int data_size); |
cmulady | 1:9ba884d85ac6 | 44 | |
cmulady | 1:9ba884d85ac6 | 45 | |
cmulady | 1:9ba884d85ac6 | 46 | int main() |
cmulady | 1:9ba884d85ac6 | 47 | { |
cmulady | 0:c8ddcaa575ba | 48 | |
cmulady | 3:8e492eacd346 | 49 | //LCD init |
cmulady | 0:c8ddcaa575ba | 50 | lcd.background(0x000000); |
cmulady | 1:9ba884d85ac6 | 51 | |
cmulady | 3:8e492eacd346 | 52 | //PWM init |
cmulady | 3:8e492eacd346 | 53 | IRLED_mod.period(1.0/38000.0); //38kHz Modulation Freq |
cmulady | 3:8e492eacd346 | 54 | IRLED_mod = 0.5; //pulse width = 50% |
cmulady | 3:8e492eacd346 | 55 | |
cmulady | 3:8e492eacd346 | 56 | //Serial init |
cmulady | 3:8e492eacd346 | 57 | device.baud(2400); |
cmulady | 3:8e492eacd346 | 58 | |
cmulady | 6:5563f0026858 | 59 | //PC serial init |
cmulady | 6:5563f0026858 | 60 | pc.baud(19200); |
cmulady | 6:5563f0026858 | 61 | |
cmulady | 3:8e492eacd346 | 62 | //Thread init |
cmulady | 1:9ba884d85ac6 | 63 | Thread thread_blinkalive(BlinkAlive); |
cmulady | 1:9ba884d85ac6 | 64 | Thread thread_updatelcd(UpdateLCD); |
cmulady | 3:8e492eacd346 | 65 | Thread thread_irstuff(IRStuff); |
cmulady | 1:9ba884d85ac6 | 66 | |
cmulady | 1:9ba884d85ac6 | 67 | |
cmulady | 1:9ba884d85ac6 | 68 | |
cmulady | 0:c8ddcaa575ba | 69 | while(1) { |
cmulady | 1:9ba884d85ac6 | 70 | thread_updatelcd.signal_set(0x1); |
cmulady | 1:9ba884d85ac6 | 71 | Thread::wait(1000/FPS); |
cmulady | 1:9ba884d85ac6 | 72 | |
cmulady | 0:c8ddcaa575ba | 73 | } |
cmulady | 0:c8ddcaa575ba | 74 | } |
cmulady | 1:9ba884d85ac6 | 75 | |
cmulady | 1:9ba884d85ac6 | 76 | void UpdateLCD(void const* arguments) |
cmulady | 1:9ba884d85ac6 | 77 | { |
cmulady | 6:5563f0026858 | 78 | /*while(true) { |
cmulady | 1:9ba884d85ac6 | 79 | led2 = 1; |
cmulady | 1:9ba884d85ac6 | 80 | lcd.locate(0,1); |
cmulady | 1:9ba884d85ac6 | 81 | lcd.printf("Debug:"); |
cmulady | 1:9ba884d85ac6 | 82 | |
cmulady | 1:9ba884d85ac6 | 83 | lcd.locate(0,3); |
cmulady | 1:9ba884d85ac6 | 84 | time_t seconds = time(NULL); |
cmulady | 1:9ba884d85ac6 | 85 | strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds)); |
cmulady | 1:9ba884d85ac6 | 86 | lcd.printf("%s", buffer); |
cmulady | 1:9ba884d85ac6 | 87 | |
cmulady | 3:8e492eacd346 | 88 | lcd.locate(0,4); |
cmulady | 6:5563f0026858 | 89 | lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]); |
cmulady | 3:8e492eacd346 | 90 | lcd.locate(0,5); |
cmulady | 6:5563f0026858 | 91 | lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]); |
cmulady | 6:5563f0026858 | 92 | lcd.locate(0,6); |
cmulady | 6:5563f0026858 | 93 | lcd.printf("Error= 0x%02X", error_code); |
cmulady | 3:8e492eacd346 | 94 | |
cmulady | 3:8e492eacd346 | 95 | |
cmulady | 3:8e492eacd346 | 96 | |
cmulady | 1:9ba884d85ac6 | 97 | //End - Sleep thread |
cmulady | 1:9ba884d85ac6 | 98 | led2 = 0; |
cmulady | 1:9ba884d85ac6 | 99 | Thread::signal_wait(0x1); |
cmulady | 6:5563f0026858 | 100 | }*/ |
cmulady | 1:9ba884d85ac6 | 101 | } |
cmulady | 1:9ba884d85ac6 | 102 | |
cmulady | 3:8e492eacd346 | 103 | void IRStuff(void const* arguments) |
cmulady | 3:8e492eacd346 | 104 | { |
cmulady | 3:8e492eacd346 | 105 | while(true) { |
cmulady | 6:5563f0026858 | 106 | //error_code = CheckPacket(irdatIN,2); |
cmulady | 6:5563f0026858 | 107 | |
cmulady | 6:5563f0026858 | 108 | while(device.readable()) |
cmulady | 6:5563f0026858 | 109 | { |
cmulady | 6:5563f0026858 | 110 | char tempdata = device.getc(); |
cmulady | 6:5563f0026858 | 111 | if(tempdata==0x02) pc.printf("\n"); |
cmulady | 6:5563f0026858 | 112 | pc.printf("0x%02X.",tempdata); |
cmulady | 3:8e492eacd346 | 113 | } |
cmulady | 6:5563f0026858 | 114 | |
cmulady | 6:5563f0026858 | 115 | //pc.printf("\n\nERROR=0x%02X\n\n",error_code); |
cmulady | 6:5563f0026858 | 116 | Thread::wait(3000); |
cmulady | 3:8e492eacd346 | 117 | } |
cmulady | 4:8fdff78c13c8 | 118 | |
cmulady | 3:8e492eacd346 | 119 | } |
cmulady | 3:8e492eacd346 | 120 | |
cmulady | 1:9ba884d85ac6 | 121 | void BlinkAlive(void const* arguments) |
cmulady | 1:9ba884d85ac6 | 122 | { |
cmulady | 1:9ba884d85ac6 | 123 | while(true) { |
cmulady | 1:9ba884d85ac6 | 124 | led1 = !led1; |
cmulady | 6:5563f0026858 | 125 | |
cmulady | 6:5563f0026858 | 126 | irdatOUT[0] = 0xA5; |
cmulady | 6:5563f0026858 | 127 | irdatOUT[1] = ++irdata_out; |
cmulady | 6:5563f0026858 | 128 | MakePacket(irdatOUT,2); |
cmulady | 6:5563f0026858 | 129 | |
cmulady | 6:5563f0026858 | 130 | Thread::wait(20); |
cmulady | 6:5563f0026858 | 131 | } |
cmulady | 6:5563f0026858 | 132 | } |
cmulady | 6:5563f0026858 | 133 | |
cmulady | 6:5563f0026858 | 134 | void MakePacket(char* data,int len) |
cmulady | 6:5563f0026858 | 135 | { |
cmulady | 6:5563f0026858 | 136 | //pc.printf("\nMaking Packet:\n\t"); |
cmulady | 6:5563f0026858 | 137 | char check =0x0; |
cmulady | 6:5563f0026858 | 138 | device.putc(0x02); |
cmulady | 6:5563f0026858 | 139 | //pc.printf("0x%02X.",0x02); |
cmulady | 6:5563f0026858 | 140 | for(int i=0; i<len; i++) { |
cmulady | 6:5563f0026858 | 141 | check^=data[i]; |
cmulady | 6:5563f0026858 | 142 | device.putc(data[i]); |
cmulady | 6:5563f0026858 | 143 | //pc.printf("0x%02X.",data[i]); |
cmulady | 1:9ba884d85ac6 | 144 | } |
cmulady | 6:5563f0026858 | 145 | device.putc(check); |
cmulady | 6:5563f0026858 | 146 | //pc.printf("0x%02X.",check); |
cmulady | 6:5563f0026858 | 147 | //pc.printf("\nDone making packet.\n"); |
cmulady | 6:5563f0026858 | 148 | } |
cmulady | 6:5563f0026858 | 149 | |
cmulady | 6:5563f0026858 | 150 | char CheckPacket(char* data, int data_size) |
cmulady | 6:5563f0026858 | 151 | //returns success(0) or failure(error code) |
cmulady | 6:5563f0026858 | 152 | { |
cmulady | 6:5563f0026858 | 153 | Timer t; |
cmulady | 6:5563f0026858 | 154 | t.reset(); |
cmulady | 6:5563f0026858 | 155 | t.start(); |
cmulady | 6:5563f0026858 | 156 | char tempdata=0x0; |
cmulady | 6:5563f0026858 | 157 | char check=0x0; |
cmulady | 6:5563f0026858 | 158 | |
cmulady | 6:5563f0026858 | 159 | pc.printf("\nChecking Packet\n\t"); |
cmulady | 6:5563f0026858 | 160 | //Data available |
cmulady | 6:5563f0026858 | 161 | if(!device.readable()) { |
cmulady | 6:5563f0026858 | 162 | pc.printf("no data"); |
cmulady | 6:5563f0026858 | 163 | return 0x4; //no data |
cmulady | 6:5563f0026858 | 164 | } |
cmulady | 6:5563f0026858 | 165 | |
cmulady | 6:5563f0026858 | 166 | while(!device.readable()); |
cmulady | 6:5563f0026858 | 167 | //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); |
cmulady | 6:5563f0026858 | 168 | //if(t.read_ms()>=UART_TIMEOUT) |
cmulady | 6:5563f0026858 | 169 | // return 0x3; //timeout error |
cmulady | 6:5563f0026858 | 170 | tempdata = device.getc(); |
cmulady | 6:5563f0026858 | 171 | |
cmulady | 6:5563f0026858 | 172 | //STX recieved |
cmulady | 6:5563f0026858 | 173 | if(tempdata!=0x02) { |
cmulady | 6:5563f0026858 | 174 | pc.printf("found bad data: 0x%02X",tempdata); |
cmulady | 6:5563f0026858 | 175 | if(tempdata==0xFF) device.getc(); //flush |
cmulady | 6:5563f0026858 | 176 | return 0x1; //bad start byte |
cmulady | 6:5563f0026858 | 177 | } |
cmulady | 6:5563f0026858 | 178 | |
cmulady | 6:5563f0026858 | 179 | //Get Data |
cmulady | 6:5563f0026858 | 180 | for(int i=0; i<data_size; i++) { |
cmulady | 6:5563f0026858 | 181 | while(!device.readable()); |
cmulady | 6:5563f0026858 | 182 | //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); |
cmulady | 6:5563f0026858 | 183 | //if(t.read_ms()>=UART_TIMEOUT) |
cmulady | 6:5563f0026858 | 184 | // return 0x3; //timeout error |
cmulady | 6:5563f0026858 | 185 | data[i] = device.getc(); |
cmulady | 6:5563f0026858 | 186 | check ^= data[i]; |
cmulady | 6:5563f0026858 | 187 | } |
cmulady | 6:5563f0026858 | 188 | //Get Checksum |
cmulady | 6:5563f0026858 | 189 | while(!device.readable()); |
cmulady | 6:5563f0026858 | 190 | //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); |
cmulady | 6:5563f0026858 | 191 | //if(t.read_ms()>=UART_TIMEOUT) |
cmulady | 6:5563f0026858 | 192 | // return 0x3; //timeout error |
cmulady | 6:5563f0026858 | 193 | tempdata = device.getc(); |
cmulady | 6:5563f0026858 | 194 | if(tempdata!=check) |
cmulady | 6:5563f0026858 | 195 | return 0x2; //bad checksum |
cmulady | 6:5563f0026858 | 196 | |
cmulady | 6:5563f0026858 | 197 | |
cmulady | 6:5563f0026858 | 198 | return 0; |
cmulady | 6:5563f0026858 | 199 | |
cmulady | 6:5563f0026858 | 200 | } |
cmulady | 6:5563f0026858 | 201 |