asdf
Dependencies: NokiaLCD XMIT_IR mbed
Fork of 4180_mP_WirelessPong_revC by
Diff: main.cpp
- Revision:
- 12:9acca1dd0e8e
- Parent:
- 10:4fcd5bdb9642
- Child:
- 13:17fd813ef7c9
--- a/main.cpp Fri Oct 05 21:56:33 2012 +0000 +++ b/main.cpp Fri Oct 05 22:13:55 2012 +0000 @@ -2,10 +2,8 @@ #include "rtos.h" #include "NokiaLCD.h" #include "XMIT_IR.h" -#include "queue.h" #define FPS 5 -#define UART_TIMEOUT 500 /**************************************** |=======================================| @@ -14,7 +12,9 @@ | -p7 : CLK on Sparkfun Nokia LCD | | -p8 : CS on Sparkfun Nokia LCD | | -p9 : RST on Sparkfun Nokia LCD | -| -p22: GND on Sparkfun IR Xmtr | +| -p21: CTL on Sparkfun IR Xmitter | +| -p14: OUT on Sparkfun IR Rcvr | +| -p13: GND on Sparkfun IR Xmitter | |=======================================| ****************************************/ @@ -29,15 +29,12 @@ PwmOut IRLED_mod(p21); //Global Vars -char buffer[32]; -unsigned char irdata_out=0; -unsigned char irdata_in=0; +char text_buffer[32]; char irdatOUT[10]; char irdatIN[10]; +char irdata_out=0; char error_code=0; Thread* threadptr_irstuff; -DataQueue RX_DataBuffer(1,100); -bool rx_data_available = false; char packet_buff[10]; Mail<char,64> rx_data_mailbox; @@ -46,10 +43,8 @@ void UpdateLCD(void const* arguments); void IRStuff(void const* arguments); void MakePacket(char* data,int len); -char CheckPacket(char* data, int data_size); void ISR_UARTRX(void); -void DoNothing(void const* arguments); -char CheckPacket2(char new_data, char* packet_buffer, char* data, int data_len); +char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len); int main() @@ -73,16 +68,17 @@ //Variable Init for(int i=0; i<10; i++) packet_buff[i]=0; - //Thread init + //Threads init Thread thread_blinkalive(BlinkAlive); Thread thread_updatelcd(UpdateLCD); Thread thread_irstuff(IRStuff); - Thread thread_donothing(DoNothing); threadptr_irstuff = &thread_irstuff; while(1) { + + //Use main loop to set LCD framerate thread_updatelcd.signal_set(0x1); Thread::wait(1000/FPS); @@ -92,15 +88,17 @@ 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(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds)); - lcd.printf("%s", buffer); - + 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); @@ -108,10 +106,9 @@ lcd.locate(0,6); lcd.printf("Error= 0x%02X", error_code); - - + //End - flash LED + led2 = 0; //End - Sleep thread - led2 = 0; Thread::signal_wait(0x1); } } @@ -119,27 +116,8 @@ void IRStuff(void const* arguments) { while(true) { - //error_code = CheckPacket(irdatIN,2); - - - //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x400FC0C4)); - - /*if(rx_data_available) { - if(irdatIN[0]==0x2) pc.printf("\n"); - pc.printf("0x%02X.",irdatIN[0]); - //pc.printf("0x%02X.",irdatIN[1]); - //pc.printf("0x%02X.",irdatIN[2]); - //pc.printf("0x%02X.\n",irdatIN[3]); - rx_data_available = false; - }*/ - - - /* while(device.readable()) { - error_code = CheckPacket2(device.getc(), packet_buff, irdatOUT, 2); - pc.printf(" = 0x%02X.\n",error_code); - }*/ - + //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; @@ -149,21 +127,8 @@ evt = rx_data_mailbox.get(); } - - - - //pc.printf("\n\nE=0x%02X\n\n",error_code); - /*if(error_code==0x0) { - pc.printf("0x%02X.",irdatIN[1]); - Thread::wait(10); - } else if(error_code==0x2) { - pc.printf("!"); - Thread::wait(10); - } else { - Thread::wait(10); - }*/ - //Thread::signal_wait(0x1); - Thread::wait(5); + //Do not return until we have more data + Thread::signal_wait(0x1); } } @@ -171,17 +136,21 @@ 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); - //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014)); + //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"); @@ -198,59 +167,8 @@ //pc.printf("\nDone making packet.\n"); } -char CheckPacket(char* data, int data_size) -//returns success(0) or failure(error code) -{ - Timer t; - t.reset(); - t.start(); - char tempdata=0x0; - char check=0x0; - - //pc.printf("\nChecking Packet\n\t"); - //Data available - if(!device.readable()) { - //pc.printf("no data"); - return 0x4; //no data - } - - while(!device.readable()); - //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); - //if(t.read_ms()>=UART_TIMEOUT) - // return 0x3; //timeout error - tempdata = device.getc(); - - //STX recieved - if(tempdata!=0x02) { - if(tempdata==0xFF) pc.printf("found bad data: 0x%02X",tempdata); - pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014)); - return 0x1; //bad start byte - } - - //Get Data - for(int i=0; i<data_size; i++) { - while(!device.readable()); - //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); - //if(t.read_ms()>=UART_TIMEOUT) - // return 0x3; //timeout error - data[i] = device.getc(); - check += data[i]; - } - //Get Checksum - while(!device.readable()); - //while(!device.readable() && t.read_ms()<=UART_TIMEOUT); - //if(t.read_ms()>=UART_TIMEOUT) - // return 0x3; //timeout error - tempdata = device.getc(); - if(tempdata!=check) - return 0x2; //bad checksum - - - return 0; - -} - -char CheckPacket2(char new_data, char* packet_buffer, char* data, int data_len) +//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. @@ -281,40 +199,19 @@ return 0; } -void DoNothing(void const* arguments) -{ - while(true) { - Thread::signal_wait(0x1); - } -} - +//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; - - //irdatIN[0] = (char)RBR; - + //write letter and put in mailbox char* mail = rx_data_mailbox.alloc(); mail[0] = (char)RBR; rx_data_mailbox.put(mail); - /*if(device.readable()) - irdatIN[1] = (char) LPC_UART1->RBR; - if(device.readable()) - irdatIN[2] = (char) LPC_UART1->RBR; - if(device.readable()) - irdatIN[3] = (char) LPC_UART1->RBR; - */ - - rx_data_available = true; + //Let message handler run (*threadptr_irstuff).signal_set(0x1); - - /*while(device.readable()) { - char data = device.getc(); - //pc.printf("*"); - }*/ - }