asdf
Dependencies: NokiaLCD XMIT_IR mbed
Fork of 4180_mP_WirelessPong_revC by
Revision 13:17fd813ef7c9, committed 2012-10-06
- Comitter:
- cmulady
- Date:
- Sat Oct 06 01:44:47 2012 +0000
- Parent:
- 12:9acca1dd0e8e
- Child:
- 14:68f7cefd7359
- Commit message:
- will have to give up on making a class.
Changed in this revision
XMIT_IR.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/XMIT_IR.lib Fri Oct 05 22:13:55 2012 +0000 +++ b/XMIT_IR.lib Sat Oct 06 01:44:47 2012 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/cmulady/code/XMIT_IR/#556f9be6047d +http://mbed.org/users/cmulady/code/XMIT_IR/#ae1d2167a10c
--- a/main.cpp Fri Oct 05 22:13:55 2012 +0000 +++ b/main.cpp Sat Oct 06 01:44:47 2012 +0000 @@ -18,16 +18,28 @@ |=======================================| ****************************************/ +//Function Prototypes +void BlinkAlive(void const* arguments); +void UpdateLCD(void const* arguments); +void IRStuff(void const* arguments); +void ISR_UARTRX(void); +char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len); +void MakePacket2(char* data,int len); + //Pin Setup -DigitalOut led1(LED1); -DigitalOut led2(LED2); +PwmOut led1(LED1); +PwmOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type -Serial device(p13, p14); // tx, rx +Serial* devicerx; + +//Serial devicetx(p13,NC); Serial pc(USBTX,USBRX); PwmOut IRLED_mod(p21); +IRTransmitter ir_tx(p13,p21,p14,4,&ISR_UARTRX); //tx,pwm,size + //Global Vars char text_buffer[32]; char irdatOUT[10]; @@ -38,13 +50,7 @@ char packet_buff[10]; Mail<char,64> rx_data_mailbox; -//Function Prototypes -void BlinkAlive(void const* arguments); -void UpdateLCD(void const* arguments); -void IRStuff(void const* arguments); -void MakePacket(char* data,int len); -void ISR_UARTRX(void); -char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len); + int main() @@ -53,18 +59,19 @@ //LCD init lcd.background(0x000000); - //PWM init - IRLED_mod.period(1.0/38000.0); //38kHz Modulation Freq - IRLED_mod = 0.5; //pulse width = 50% - - //Serial init - device.baud(2400); - device.attach(&ISR_UARTRX,Serial::RxIrq); - //PC serial init pc.baud(19200); pc.printf("Starting...\n\n"); + //Serial init + /*devicerx = new Serial(p13, p14); // tx, rx + (*devicerx).baud(2400); + //devicetx.baud(2400); + (*devicerx).attach(&ISR_UARTRX,Serial::RxIrq); + IRLED_mod.period(1.0/38000.0); //38kHz Modulation Freq + IRLED_mod = 0.5; */ + + //Variable Init for(int i=0; i<10; i++) packet_buff[i]=0; @@ -77,7 +84,7 @@ while(1) { - + //Use main loop to set LCD framerate thread_updatelcd.signal_set(0x1); Thread::wait(1000/FPS); @@ -88,10 +95,10 @@ void UpdateLCD(void const* arguments) { while(true) { - + //Start flash LED - led2 = 1; - + led2 = 0.02; + //Write debug text to screen lcd.locate(0,1); lcd.printf("Debug:"); @@ -107,7 +114,7 @@ lcd.printf("Error= 0x%02X", error_code); //End - flash LED - led2 = 0; + led2 = 0.0; //End - Sleep thread Thread::signal_wait(0x1); } @@ -118,77 +125,49 @@ while(true) { //If data available - Print data directly to USB port (for debug) - osEvent evt = rx_data_mailbox.get(); + /*osEvent evt = rx_data_mailbox.get(); while(evt.status == osEventMail) { char* mail = (char*)evt.value.p; - if((*mail)==0x02) pc.printf("\n"); - pc.printf("0x%02X.",*mail); + //if((*mail)==0x02) pc.printf("\n"); + //pc.printf("0x%02X.",*mail); + error_code = CheckPacket(*mail,packet_buff,irdatIN,4); + pc.printf(" ERROR=0x%02X.\n",error_code); + rx_data_mailbox.free(mail); evt = rx_data_mailbox.get(); - } + }*/ + char* data; + while(ir_tx.ReadPacket(data)==0) + pc.printf("0x%02X.",*data); + //Do not return until we have more data - Thread::signal_wait(0x1); + //Thread::signal_wait(0x1); + Thread::wait(20); } } -void BlinkAlive(void const* arguments) -{ - while(true) { - - //Change LED1 state (debug) - led1 = !led1; - - //Form a test packet and send it over IR transmitter - irdatOUT[0] = 0xA5; - irdatOUT[1] = ++irdata_out; - MakePacket(irdatOUT,2); - - //Roughly use to set rate of data packets per second - Thread::wait(20); - } -} - -//TURN THIS INTO CLASS FUNCTION -void MakePacket(char* data,int len) +char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len) { - //pc.printf("\nMaking Packet:\n\t"); - char check =0x0; - device.putc(0x02); - //pc.printf("0x%02X.",0x02); - for(int i=0; i<len; i++) { - check^=data[i]; - device.putc(data[i]); - //pc.printf("0x%02X.",data[i]); - } - device.putc(check); - //pc.printf("0x%02X.",check); - //pc.printf("\nDone making packet.\n"); -} - -//TURN THIS INTO CLASS FUNCTION -char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len) -//returns success(0) or failure(error code) -{ - //Requires a packet buffer of length 'data_len'+2. + //Requires a packet buffer of length 'data_len'+3. //Shifts data and checks each 'set' for a valid packet. //Once a valid packet is receievd, the data buffer is updated with new values. + //returns success(0) or failure(error code) + char check=0x0; - pc.printf("Shifting: "); //Shift All data 1 cell over - for(int i=0; i<data_len+1; i++) { + for(int i=0; i<data_len+2; i++) { packet_buffer[i] = packet_buffer[i+1]; } - packet_buffer[data_len+1] = new_data; - for(int i=0; i<data_len+2; i++) { - pc.printf("0x%02X.",packet_buffer[i]); - } + packet_buffer[data_len+2] = new_data; //check for valid packet - if(packet_buffer[0]!=0x02) + if(packet_buffer[0]!=ASCII_STX) return 0x1; //bad start byte + if(packet_buffer[data_len+2]!=ASCII_ETX) + return 0x2; //bad end byte for(int i=1; i<data_len+1; i++) { check^=packet_buffer[i]; @@ -196,9 +175,44 @@ if(check==packet_buffer[data_len+1]) { return 0; } + return 0; } +void BlinkAlive(void const* arguments) +{ + while(true) { + + //Change LED1 state (debug) + (led1==0.0)?led1=0.02:led1=0.0; + + //Form a test packet and send it over IR transmitter + irdatOUT[0] = 0xA5; + irdatOUT[1] = ++irdata_out; + irdatOUT[2] = ~(irdata_out*2); + irdatOUT[3] = irdata_out*7+13; + //MakePacket2(irdatOUT,4); + ir_tx.MakePacket(irdatOUT,4); + + //Roughly use to set rate of data packets per second + Thread::wait(50); + } +} + +void MakePacket2(char* data,int len) +{ + /*char check =0x0; + (*devicerx).putc(ASCII_STX); + for(int i=0; i<len; i++) { + check^=data[i]; + (*devicerx).putc(data[i]); + } + (*devicerx).putc(check); + (*devicerx).putc(ASCII_ETX);*/ +} + + + //Handle Reception of RX data (mail it to appropriate thread) void ISR_UARTRX(void) { @@ -206,9 +220,9 @@ uint32_t RBR = LPC_UART1->RBR; //write letter and put in mailbox - char* mail = rx_data_mailbox.alloc(); + char* mail = ir_tx.ir_data_mailbox.alloc(); mail[0] = (char)RBR; - rx_data_mailbox.put(mail); + ir_tx.ir_data_mailbox.put(mail); //Let message handler run (*threadptr_irstuff).signal_set(0x1);