asdf

Dependencies:   NokiaLCD XMIT_IR mbed

Fork of 4180_mP_WirelessPong_revC by Curtis Mulady

Revision:
9:3e4e9d6a8ad8
Parent:
8:54dd4a3d0de9
Child:
10:4fcd5bdb9642
diff -r 54dd4a3d0de9 -r 3e4e9d6a8ad8 main.cpp
--- a/main.cpp	Fri Oct 05 13:02:52 2012 +0000
+++ b/main.cpp	Fri Oct 05 19:38:51 2012 +0000
@@ -38,6 +38,7 @@
 Thread* threadptr_irstuff;
 DataQueue RX_DataBuffer(1,100);
 bool rx_data_available = false;
+char packet_buff[10];
 
 //Function Prototypes
 void BlinkAlive(void const* arguments);
@@ -47,6 +48,7 @@
 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);
 
 
 int main()
@@ -61,12 +63,15 @@
 
     //Serial init
     device.baud(2400);
-    device.attach(&ISR_UARTRX,Serial::RxIrq);
+    //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;
+
     //Thread init
     Thread thread_blinkalive(BlinkAlive);
     Thread thread_updatelcd(UpdateLCD);
@@ -115,24 +120,27 @@
     while(true) {
         //error_code = CheckPacket(irdatIN,2);
 
-        /*while(device.readable()) {
-            char tempdata = device.getc();
-            if(tempdata==0x02) pc.printf("\n");
-            pc.printf("0x%02X.",tempdata);
-        }*/
+
 
         //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x400FC0C4));
 
-        if(rx_data_available) {
-        if(irdatIN[0]==0x2) pc.printf("\n");
+        /*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);
         }
 
 
+
         //pc.printf("\n\nE=0x%02X\n\n",error_code);
         /*if(error_code==0x0) {
             pc.printf("0x%02X.",irdatIN[1]);
@@ -143,7 +151,8 @@
         } else {
             Thread::wait(10);
         }*/
-        Thread::signal_wait(0x1);
+        //Thread::signal_wait(0x1);
+        Thread::wait(5);
     }
 
 }
@@ -158,7 +167,7 @@
         MakePacket(irdatOUT,2);
         //pc.printf("UART_STATE: 0x%08X",*((unsigned int *)0x40010014));
 
-        Thread::wait(20);
+        Thread::wait(6000);
     }
 }
 
@@ -169,7 +178,7 @@
     device.putc(0x02);
     //pc.printf("0x%02X.",0x02);
     for(int i=0; i<len; i++) {
-        check+=data[i];
+        check^=data[i];
         device.putc(data[i]);
         //pc.printf("0x%02X.",data[i]);
     }
@@ -230,6 +239,37 @@
 
 }
 
+char  CheckPacket2(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;
+}
+
 void DoNothing(void const* arguments)
 {
     while(true) {
@@ -240,10 +280,7 @@
 void ISR_UARTRX(void)
 {
     uint32_t RBR = LPC_UART1->RBR;
-    irdatIN[0] = 0;
-    irdatIN[1] = 0;
-    irdatIN[2] = 0;
-    irdatIN[3] = 0;
+
 
     irdatIN[0] = (char)RBR;
     /*if(device.readable())