4D display working with Gecko board STK3600

Dependents:   Genie_mbed_Gecko

Fork of 4dGENIE by christian b

Revision:
8:b5ba0df2d0db
Parent:
7:6edb20845684
Child:
9:b74deaac80f9
diff -r 6edb20845684 -r b5ba0df2d0db mbed_genie.cpp
--- a/mbed_genie.cpp	Sat Jul 05 15:11:57 2014 +0000
+++ b/mbed_genie.cpp	Sun Jul 06 17:22:32 2014 +0000
@@ -7,6 +7,7 @@
     _reset = 0;
     _screen.baud(9600);
     _genieUserHandler = NULL;
+    RxStateTimeoutErrors = 0;
 }
 void Mbed4dGenie::Start()
 {
@@ -44,10 +45,10 @@
             }   
             else if(data == GENIE_REPORT_OBJ || data == GENIE_REPORT_EVENT)
             {
+                RxMaxTimeout = _t.read_ms() + RESYNC_PERIOD;
                 checksum = data;
                 rx_data[rxframe_count++] = data;
-                state = CommInProgress; 
-                LastResponse = NO_RESPONSE;             
+                state = CommInProgress;              
             }                     
             break;
         
@@ -148,7 +149,9 @@
     _screen.putc(index) ;              checksum ^= index ; 
     _screen.putc(checksum) ;
         
-    return  0;//Mbed4dGenie::WaitForAnswer();
+    //Here we dont wiat for a typical answer
+    //The screen will respond with an NACK if the command was not understood, otherwise it will send a report object frame    
+    return  WaitForReadAnswer();
 }
 void Mbed4dGenie::writec(char data)
 {
@@ -156,15 +159,47 @@
 }
 bool Mbed4dGenie::WaitForIdle()
 {
+    if(state != CommIdle)
+    {
+        long timeout = _t.read_ms() + TIMEOUT_PERIOD;
+        long timerReading = 0;
+        if(_t.read_ms() >= RxMaxTimeout)
+        {
+            Reset();
+            RxStateTimeoutErrors++;
+        }
+        while(timerReading < timeout && state != CommIdle)
+        {
+            timerReading = _t.read_ms();
+        }
+        LastResponse = 0;
+        return (timerReading >= timeout);
+    }
+    
+    return false;    
+}
+
+int8_t Mbed4dGenie::WaitForReadAnswer()
+{
     long timeout = _t.read_ms() + TIMEOUT_PERIOD;
     long timerReading = 0;
-    
-    while(timerReading < timeout && state != CommIdle)
+    while(state == CommIdle && LastResponse != ERROR_NAK && timerReading < timeout)
     {
         timerReading = _t.read_ms();
     }
-    LastResponse = 0;
-    return (timerReading >= timeout);
+    if(LastResponse == ERROR_NAK)//check if the screen returned a NACK
+    {
+        LastResponse = NO_RESPONSE;
+        return ERROR_NAK;
+    }
+    else if(LastResponse >= timeout) //check if we timed out while waiting for response
+    {   
+        LastResponse = NO_RESPONSE;
+        return ERROR_TIMEOUT;
+    }
+    //if we get here it means we didnt timeout and the screen did accept the command
+    LastResponse = NO_RESPONSE;
+    return ERROR_NONE;
 }
 
 int8_t Mbed4dGenie::WaitForAnswer()
@@ -179,12 +214,15 @@
            
     if(LastResponse == ERROR_NAK)
     {
-        return ERROR_NONE;
+        LastResponse = NO_RESPONSE;
+        return ERROR_NAK;
     }
     else if(LastResponse >= timeout)
     {   
+        LastResponse = NO_RESPONSE;
         return ERROR_TIMEOUT;
     }
+    LastResponse = NO_RESPONSE;
     return ERROR_NONE;
 }