asdf
Dependencies: NokiaLCD XMIT_IR mbed
Fork of 4180_mP_WirelessPong_revC by
main.cpp
- Committer:
- cmulady
- Date:
- 2012-10-05
- Revision:
- 12:9acca1dd0e8e
- Parent:
- 10:4fcd5bdb9642
- Child:
- 13:17fd813ef7c9
File content as of revision 12:9acca1dd0e8e:
#include "mbed.h" #include "rtos.h" #include "NokiaLCD.h" #include "XMIT_IR.h" #define FPS 5 /**************************************** |=======================================| |MBED Connections: | | -p5 : DIO on Sparkfun Nokia LCD | | -p7 : CLK on Sparkfun Nokia LCD | | -p8 : CS on Sparkfun Nokia LCD | | -p9 : RST on Sparkfun Nokia LCD | | -p21: CTL on Sparkfun IR Xmitter | | -p14: OUT on Sparkfun IR Rcvr | | -p13: GND on Sparkfun IR Xmitter | |=======================================| ****************************************/ //Pin Setup DigitalOut led1(LED1); DigitalOut 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 pc(USBTX,USBRX); PwmOut IRLED_mod(p21); //Global Vars char text_buffer[32]; char irdatOUT[10]; char irdatIN[10]; char irdata_out=0; char error_code=0; Thread* threadptr_irstuff; 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() { //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"); //Variable Init for(int i=0; i<10; i++) packet_buff[i]=0; //Threads init Thread thread_blinkalive(BlinkAlive); Thread thread_updatelcd(UpdateLCD); Thread thread_irstuff(IRStuff); threadptr_irstuff = &thread_irstuff; while(1) { //Use main loop to set LCD framerate thread_updatelcd.signal_set(0x1); Thread::wait(1000/FPS); } } void UpdateLCD(void const* arguments) { while(true) { //Start flash LED led2 = 1; //Write debug text to screen lcd.locate(0,1); lcd.printf("Debug:"); lcd.locate(0,3); time_t seconds = time(NULL); strftime(text_buffer, 32, "%I:%M:%S %p\n", localtime(&seconds)); lcd.printf("%s", text_buffer); lcd.locate(0,4); lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]); lcd.locate(0,5); lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]); lcd.locate(0,6); lcd.printf("Error= 0x%02X", error_code); //End - flash LED led2 = 0; //End - Sleep thread Thread::signal_wait(0x1); } } void IRStuff(void const* arguments) { while(true) { //If data available - Print data directly to USB port (for debug) 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); rx_data_mailbox.free(mail); evt = rx_data_mailbox.get(); } //Do not return until we have more data Thread::signal_wait(0x1); } } 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) { //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. //Shifts data and checks each 'set' for a valid packet. //Once a valid packet is receievd, the data buffer is updated with new values. char check=0x0; pc.printf("Shifting: "); //Shift All data 1 cell over for(int i=0; i<data_len+1; 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]); } //check for valid packet if(packet_buffer[0]!=0x02) return 0x1; //bad start byte for(int i=1; i<data_len+1; i++) { check^=packet_buffer[i]; } if(check==packet_buffer[data_len+1]) { return 0; } return 0; } //Handle Reception of RX data (mail it to appropriate thread) void ISR_UARTRX(void) { //get RX data (and prevent ISR from looping forever uint32_t RBR = LPC_UART1->RBR; //write letter and put in mailbox char* mail = rx_data_mailbox.alloc(); mail[0] = (char)RBR; rx_data_mailbox.put(mail); //Let message handler run (*threadptr_irstuff).signal_set(0x1); }