Mbed 4dGenie class
Dependents: Genie_Test 039847382-S3_DS1621_and_LCD_V1
This is a work in progress.
4dGenie class to use with 4dLCD screens that are using the genie environment.
There are still some rare occasions where the 4dLCD might crash, for now i have no solution to this except doing a reset of the 4dLCD.
Please make sure to have the most up to date PmmC loaded on the screen.
usage example :
Mbed4dGenie test program
#include "mbed.h" #include "mbed_genie.h" DigitalOut myled(LED1); /* The Mbed4dGenie class requires 3 parameters 1 - Tx pin 2 - Rx pin 3 - Reset pin */ Mbed4dGenie lcd4d(PTE0,PTE1,PTB9); int main() { int temp = 0; printf("Mbed Genie demo \n\r"); lcd4d.Start(); /* for example, in this loop we increment the thermometer0 object from 0 to 100 */ while(1) { if(temp >= 100) { temp = -1; } else { temp++; } lcd4d.genieWriteObject(GENIE_OBJ_LED_DIGITS,1,temp); myled = 1; wait(0.05); myled = 0; wait(0.05); } }
Diff: mbed_genie.cpp
- Revision:
- 8:b5ba0df2d0db
- Parent:
- 7:6edb20845684
- Child:
- 9:b74deaac80f9
--- 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; }