asdf

Dependencies:   NokiaLCD XMIT_IR mbed

Fork of 4180_mP_WirelessPong_revC by Curtis Mulady

Revision:
6:5563f0026858
Parent:
5:2e08cc167fff
Child:
7:c9ff6b5c8507
--- a/main.cpp	Thu Oct 04 18:33:25 2012 +0000
+++ b/main.cpp	Thu Oct 04 23:33:35 2012 +0000
@@ -4,6 +4,7 @@
 #include "XMIT_IR.h"
 
 #define FPS 5
+#define UART_TIMEOUT 500
 
 /****************************************
 |=======================================|
@@ -23,17 +24,23 @@
 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 buffer[32];
 unsigned char irdata_out=0;
 unsigned char irdata_in=0;
+char irdatOUT[10];
+char irdatIN[10];
+char error_code=0;
 
 //Function Prototypes
 void BlinkAlive(void const* arguments);
 void UpdateLCD(void const* arguments);
 void IRStuff(void const* arguments);
+void MakePacket(char* data,int len);
+char CheckPacket(char* data, int data_size);
 
 
 int main()
@@ -49,6 +56,9 @@
     //Serial init
     device.baud(2400);
 
+    //PC serial init
+    pc.baud(19200);
+
     //Thread init
     Thread thread_blinkalive(BlinkAlive);
     Thread thread_updatelcd(UpdateLCD);
@@ -65,7 +75,7 @@
 
 void UpdateLCD(void const* arguments)
 {
-    while(true) {
+    /*while(true) {
         led2 = 1;
         lcd.locate(0,1);
         lcd.printf("Debug:");
@@ -76,25 +86,34 @@
         lcd.printf("%s", buffer);
 
         lcd.locate(0,4);
-        lcd.printf("IR_OUT=0x%X", irdata_out);
+        lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]);
         lcd.locate(0,5);
-        lcd.printf("IR_IN= 0x%X", irdata_in);
+        lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]);
+        lcd.locate(0,6);
+        lcd.printf("Error= 0x%02X", error_code);
 
 
 
         //End - Sleep thread
         led2 = 0;
         Thread::signal_wait(0x1);
-    }
+    }*/
 }
 
 void IRStuff(void const* arguments)
 {
     while(true) {
-        if(device.readable()) {
-            irdata_in = device.getc();
+        //error_code = CheckPacket(irdatIN,2);
+        
+        while(device.readable())
+        {
+            char tempdata = device.getc();
+            if(tempdata==0x02) pc.printf("\n");
+            pc.printf("0x%02X.",tempdata);
         }
-        Thread::wait(10);
+        
+        //pc.printf("\n\nERROR=0x%02X\n\n",error_code);
+        Thread::wait(3000);
     }
 
 }
@@ -103,8 +122,80 @@
 {
     while(true) {
         led1 = !led1;
-        irdata_out++;
-        device.putc(irdata_out);
-        Thread::wait(1000);
+
+        irdatOUT[0] = 0xA5;
+        irdatOUT[1] = ++irdata_out;
+        MakePacket(irdatOUT,2);
+
+        Thread::wait(20);
+    }
+}
+
+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]);
     }
-}
\ No newline at end of file
+    device.putc(check);
+    //pc.printf("0x%02X.",check);
+    //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) {
+        pc.printf("found bad data: 0x%02X",tempdata);
+        if(tempdata==0xFF) device.getc(); //flush
+        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;
+
+}
+