asdf
Dependencies: NokiaLCD XMIT_IR mbed
Fork of 4180_mP_WirelessPong_revC by
main.cpp@13:17fd813ef7c9, 2012-10-06 (annotated)
- Committer:
- cmulady
- Date:
- Sat Oct 06 01:44:47 2012 +0000
- Revision:
- 13:17fd813ef7c9
- Parent:
- 12:9acca1dd0e8e
- Child:
- 14:68f7cefd7359
will have to give up on making a class.
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 | 13:17fd813ef7c9 | 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 | //Serial init |
cmulady | 13:17fd813ef7c9 | 67 | /*devicerx = new Serial(p13, p14); // tx, rx |
cmulady | 13:17fd813ef7c9 | 68 | (*devicerx).baud(2400); |
cmulady | 13:17fd813ef7c9 | 69 | //devicetx.baud(2400); |
cmulady | 13:17fd813ef7c9 | 70 | (*devicerx).attach(&ISR_UARTRX,Serial::RxIrq); |
cmulady | 13:17fd813ef7c9 | 71 | IRLED_mod.period(1.0/38000.0); //38kHz Modulation Freq |
cmulady | 13:17fd813ef7c9 | 72 | IRLED_mod = 0.5; */ |
cmulady | 13:17fd813ef7c9 | 73 | |
cmulady | 13:17fd813ef7c9 | 74 | |
cmulady | 9:3e4e9d6a8ad8 | 75 | //Variable Init |
cmulady | 9:3e4e9d6a8ad8 | 76 | for(int i=0; i<10; i++) packet_buff[i]=0; |
cmulady | 9:3e4e9d6a8ad8 | 77 | |
cmulady | 12:9acca1dd0e8e | 78 | //Threads init |
cmulady | 1:9ba884d85ac6 | 79 | Thread thread_blinkalive(BlinkAlive); |
cmulady | 1:9ba884d85ac6 | 80 | Thread thread_updatelcd(UpdateLCD); |
cmulady | 3:8e492eacd346 | 81 | Thread thread_irstuff(IRStuff); |
cmulady | 8:54dd4a3d0de9 | 82 | threadptr_irstuff = &thread_irstuff; |
cmulady | 1:9ba884d85ac6 | 83 | |
cmulady | 1:9ba884d85ac6 | 84 | |
cmulady | 1:9ba884d85ac6 | 85 | |
cmulady | 0:c8ddcaa575ba | 86 | while(1) { |
cmulady | 13:17fd813ef7c9 | 87 | |
cmulady | 12:9acca1dd0e8e | 88 | //Use main loop to set LCD framerate |
cmulady | 1:9ba884d85ac6 | 89 | thread_updatelcd.signal_set(0x1); |
cmulady | 1:9ba884d85ac6 | 90 | Thread::wait(1000/FPS); |
cmulady | 1:9ba884d85ac6 | 91 | |
cmulady | 0:c8ddcaa575ba | 92 | } |
cmulady | 0:c8ddcaa575ba | 93 | } |
cmulady | 1:9ba884d85ac6 | 94 | |
cmulady | 1:9ba884d85ac6 | 95 | void UpdateLCD(void const* arguments) |
cmulady | 1:9ba884d85ac6 | 96 | { |
cmulady | 7:c9ff6b5c8507 | 97 | while(true) { |
cmulady | 13:17fd813ef7c9 | 98 | |
cmulady | 12:9acca1dd0e8e | 99 | //Start flash LED |
cmulady | 13:17fd813ef7c9 | 100 | led2 = 0.02; |
cmulady | 13:17fd813ef7c9 | 101 | |
cmulady | 12:9acca1dd0e8e | 102 | //Write debug text to screen |
cmulady | 1:9ba884d85ac6 | 103 | lcd.locate(0,1); |
cmulady | 1:9ba884d85ac6 | 104 | lcd.printf("Debug:"); |
cmulady | 1:9ba884d85ac6 | 105 | lcd.locate(0,3); |
cmulady | 1:9ba884d85ac6 | 106 | time_t seconds = time(NULL); |
cmulady | 12:9acca1dd0e8e | 107 | strftime(text_buffer, 32, "%I:%M:%S %p\n", localtime(&seconds)); |
cmulady | 12:9acca1dd0e8e | 108 | lcd.printf("%s", text_buffer); |
cmulady | 3:8e492eacd346 | 109 | lcd.locate(0,4); |
cmulady | 6:5563f0026858 | 110 | lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]); |
cmulady | 3:8e492eacd346 | 111 | lcd.locate(0,5); |
cmulady | 6:5563f0026858 | 112 | lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]); |
cmulady | 6:5563f0026858 | 113 | lcd.locate(0,6); |
cmulady | 6:5563f0026858 | 114 | lcd.printf("Error= 0x%02X", error_code); |
cmulady | 3:8e492eacd346 | 115 | |
cmulady | 12:9acca1dd0e8e | 116 | //End - flash LED |
cmulady | 13:17fd813ef7c9 | 117 | led2 = 0.0; |
cmulady | 1:9ba884d85ac6 | 118 | //End - Sleep thread |
cmulady | 1:9ba884d85ac6 | 119 | Thread::signal_wait(0x1); |
cmulady | 7:c9ff6b5c8507 | 120 | } |
cmulady | 1:9ba884d85ac6 | 121 | } |
cmulady | 1:9ba884d85ac6 | 122 | |
cmulady | 3:8e492eacd346 | 123 | void IRStuff(void const* arguments) |
cmulady | 3:8e492eacd346 | 124 | { |
cmulady | 3:8e492eacd346 | 125 | while(true) { |
cmulady | 7:c9ff6b5c8507 | 126 | |
cmulady | 12:9acca1dd0e8e | 127 | //If data available - Print data directly to USB port (for debug) |
cmulady | 13:17fd813ef7c9 | 128 | /*osEvent evt = rx_data_mailbox.get(); |
cmulady | 10:4fcd5bdb9642 | 129 | while(evt.status == osEventMail) { |
cmulady | 10:4fcd5bdb9642 | 130 | char* mail = (char*)evt.value.p; |
cmulady | 13:17fd813ef7c9 | 131 | //if((*mail)==0x02) pc.printf("\n"); |
cmulady | 13:17fd813ef7c9 | 132 | //pc.printf("0x%02X.",*mail); |
cmulady | 13:17fd813ef7c9 | 133 | error_code = CheckPacket(*mail,packet_buff,irdatIN,4); |
cmulady | 13:17fd813ef7c9 | 134 | pc.printf(" ERROR=0x%02X.\n",error_code); |
cmulady | 13:17fd813ef7c9 | 135 | |
cmulady | 10:4fcd5bdb9642 | 136 | rx_data_mailbox.free(mail); |
cmulady | 10:4fcd5bdb9642 | 137 | evt = rx_data_mailbox.get(); |
cmulady | 13:17fd813ef7c9 | 138 | }*/ |
cmulady | 10:4fcd5bdb9642 | 139 | |
cmulady | 13:17fd813ef7c9 | 140 | char* data; |
cmulady | 13:17fd813ef7c9 | 141 | while(ir_tx.ReadPacket(data)==0) |
cmulady | 13:17fd813ef7c9 | 142 | pc.printf("0x%02X.",*data); |
cmulady | 13:17fd813ef7c9 | 143 | |
cmulady | 12:9acca1dd0e8e | 144 | //Do not return until we have more data |
cmulady | 13:17fd813ef7c9 | 145 | //Thread::signal_wait(0x1); |
cmulady | 13:17fd813ef7c9 | 146 | Thread::wait(20); |
cmulady | 3:8e492eacd346 | 147 | } |
cmulady | 4:8fdff78c13c8 | 148 | |
cmulady | 3:8e492eacd346 | 149 | } |
cmulady | 3:8e492eacd346 | 150 | |
cmulady | 13:17fd813ef7c9 | 151 | char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len) |
cmulady | 6:5563f0026858 | 152 | { |
cmulady | 13:17fd813ef7c9 | 153 | //Requires a packet buffer of length 'data_len'+3. |
cmulady | 9:3e4e9d6a8ad8 | 154 | //Shifts data and checks each 'set' for a valid packet. |
cmulady | 9:3e4e9d6a8ad8 | 155 | //Once a valid packet is receievd, the data buffer is updated with new values. |
cmulady | 13:17fd813ef7c9 | 156 | //returns success(0) or failure(error code) |
cmulady | 13:17fd813ef7c9 | 157 | |
cmulady | 9:3e4e9d6a8ad8 | 158 | |
cmulady | 9:3e4e9d6a8ad8 | 159 | char check=0x0; |
cmulady | 9:3e4e9d6a8ad8 | 160 | //Shift All data 1 cell over |
cmulady | 13:17fd813ef7c9 | 161 | for(int i=0; i<data_len+2; i++) { |
cmulady | 9:3e4e9d6a8ad8 | 162 | packet_buffer[i] = packet_buffer[i+1]; |
cmulady | 9:3e4e9d6a8ad8 | 163 | } |
cmulady | 13:17fd813ef7c9 | 164 | packet_buffer[data_len+2] = new_data; |
cmulady | 9:3e4e9d6a8ad8 | 165 | |
cmulady | 9:3e4e9d6a8ad8 | 166 | //check for valid packet |
cmulady | 13:17fd813ef7c9 | 167 | if(packet_buffer[0]!=ASCII_STX) |
cmulady | 9:3e4e9d6a8ad8 | 168 | return 0x1; //bad start byte |
cmulady | 13:17fd813ef7c9 | 169 | if(packet_buffer[data_len+2]!=ASCII_ETX) |
cmulady | 13:17fd813ef7c9 | 170 | return 0x2; //bad end byte |
cmulady | 9:3e4e9d6a8ad8 | 171 | |
cmulady | 9:3e4e9d6a8ad8 | 172 | for(int i=1; i<data_len+1; i++) { |
cmulady | 9:3e4e9d6a8ad8 | 173 | check^=packet_buffer[i]; |
cmulady | 9:3e4e9d6a8ad8 | 174 | } |
cmulady | 9:3e4e9d6a8ad8 | 175 | if(check==packet_buffer[data_len+1]) { |
cmulady | 9:3e4e9d6a8ad8 | 176 | return 0; |
cmulady | 9:3e4e9d6a8ad8 | 177 | } |
cmulady | 13:17fd813ef7c9 | 178 | |
cmulady | 9:3e4e9d6a8ad8 | 179 | return 0; |
cmulady | 9:3e4e9d6a8ad8 | 180 | } |
cmulady | 9:3e4e9d6a8ad8 | 181 | |
cmulady | 13:17fd813ef7c9 | 182 | void BlinkAlive(void const* arguments) |
cmulady | 13:17fd813ef7c9 | 183 | { |
cmulady | 13:17fd813ef7c9 | 184 | while(true) { |
cmulady | 13:17fd813ef7c9 | 185 | |
cmulady | 13:17fd813ef7c9 | 186 | //Change LED1 state (debug) |
cmulady | 13:17fd813ef7c9 | 187 | (led1==0.0)?led1=0.02:led1=0.0; |
cmulady | 13:17fd813ef7c9 | 188 | |
cmulady | 13:17fd813ef7c9 | 189 | //Form a test packet and send it over IR transmitter |
cmulady | 13:17fd813ef7c9 | 190 | irdatOUT[0] = 0xA5; |
cmulady | 13:17fd813ef7c9 | 191 | irdatOUT[1] = ++irdata_out; |
cmulady | 13:17fd813ef7c9 | 192 | irdatOUT[2] = ~(irdata_out*2); |
cmulady | 13:17fd813ef7c9 | 193 | irdatOUT[3] = irdata_out*7+13; |
cmulady | 13:17fd813ef7c9 | 194 | //MakePacket2(irdatOUT,4); |
cmulady | 13:17fd813ef7c9 | 195 | ir_tx.MakePacket(irdatOUT,4); |
cmulady | 13:17fd813ef7c9 | 196 | |
cmulady | 13:17fd813ef7c9 | 197 | //Roughly use to set rate of data packets per second |
cmulady | 13:17fd813ef7c9 | 198 | Thread::wait(50); |
cmulady | 13:17fd813ef7c9 | 199 | } |
cmulady | 13:17fd813ef7c9 | 200 | } |
cmulady | 13:17fd813ef7c9 | 201 | |
cmulady | 13:17fd813ef7c9 | 202 | void MakePacket2(char* data,int len) |
cmulady | 13:17fd813ef7c9 | 203 | { |
cmulady | 13:17fd813ef7c9 | 204 | /*char check =0x0; |
cmulady | 13:17fd813ef7c9 | 205 | (*devicerx).putc(ASCII_STX); |
cmulady | 13:17fd813ef7c9 | 206 | for(int i=0; i<len; i++) { |
cmulady | 13:17fd813ef7c9 | 207 | check^=data[i]; |
cmulady | 13:17fd813ef7c9 | 208 | (*devicerx).putc(data[i]); |
cmulady | 13:17fd813ef7c9 | 209 | } |
cmulady | 13:17fd813ef7c9 | 210 | (*devicerx).putc(check); |
cmulady | 13:17fd813ef7c9 | 211 | (*devicerx).putc(ASCII_ETX);*/ |
cmulady | 13:17fd813ef7c9 | 212 | } |
cmulady | 13:17fd813ef7c9 | 213 | |
cmulady | 13:17fd813ef7c9 | 214 | |
cmulady | 13:17fd813ef7c9 | 215 | |
cmulady | 12:9acca1dd0e8e | 216 | //Handle Reception of RX data (mail it to appropriate thread) |
cmulady | 7:c9ff6b5c8507 | 217 | void ISR_UARTRX(void) |
cmulady | 7:c9ff6b5c8507 | 218 | { |
cmulady | 12:9acca1dd0e8e | 219 | //get RX data (and prevent ISR from looping forever |
cmulady | 7:c9ff6b5c8507 | 220 | uint32_t RBR = LPC_UART1->RBR; |
cmulady | 9:3e4e9d6a8ad8 | 221 | |
cmulady | 12:9acca1dd0e8e | 222 | //write letter and put in mailbox |
cmulady | 13:17fd813ef7c9 | 223 | char* mail = ir_tx.ir_data_mailbox.alloc(); |
cmulady | 10:4fcd5bdb9642 | 224 | mail[0] = (char)RBR; |
cmulady | 13:17fd813ef7c9 | 225 | ir_tx.ir_data_mailbox.put(mail); |
cmulady | 10:4fcd5bdb9642 | 226 | |
cmulady | 12:9acca1dd0e8e | 227 | //Let message handler run |
cmulady | 8:54dd4a3d0de9 | 228 | (*threadptr_irstuff).signal_set(0x1); |
cmulady | 8:54dd4a3d0de9 | 229 | |
cmulady | 7:c9ff6b5c8507 | 230 | } |
cmulady | 7:c9ff6b5c8507 | 231 |