Library for EarthLCD ezLCD3xx line of products

Dependents:   ezLCDTest

/media/uploads/codeman/front.jpg /media/uploads/codeman/all.jpg /media/uploads/codeman/arlcd.jpg /media/uploads/codeman/side.jpg

Revision:
1:c7659c8af0d3
Parent:
0:65606abcae36
Child:
2:fc7d151593f1
--- a/ezLCDLib.cpp	Mon Apr 22 06:33:57 2013 +0000
+++ b/ezLCDLib.cpp	Sun Apr 28 07:41:27 2013 +0000
@@ -1,14 +1,47 @@
 #include "mbed.h"
 #include "ezLCDLib.h"
 
-#define XSIZE 6
-#define YSIZE 9
-#define BUFF_LEN 10
+void Tx_interrupt();
+void Rx_interrupt();
+void send_line();
+void read_line();
+
+//Serial pc(USBTX, USBRX);
+// Circular buffers for serial TX and RX data - used by interrupt routines
+const int buffer_size = 64;
+// might need to increase buffer size for high baud rates
+char tx_buffer[buffer_size];
+char rx_buffer[buffer_size];
+// Circular buffer pointers
+// volatile makes read-modify-write atomic
+volatile int tx_in=0;
+volatile int tx_out=0;
+volatile int rx_in=0;
+volatile int rx_out=0;
+// Line buffers for sprintf and sscanf
+char tx_line[80];
+char rx_line[80];
+
+char buf[64];
+
 ezLCD3::ezLCD3(PinName tx, PinName rx) : Stream("ezLCD3") , _ser(tx, rx)
 {
     _ser.baud(115200);               // default baud rate
+    _ser.attach(this,&ezLCD3::Rx_interrupt, Serial::RxIrq);
 }
 
+void ezLCD3::Rx_interrupt( void )
+{
+// Loop just in case more than one character is in UART's receive FIFO buffer
+// Stop if buffer full
+    while ((_ser.readable()) || (((rx_in + 1) % buffer_size) == rx_out)) {
+        rx_buffer[rx_in] = _ser.getc();
+// Uncomment to Echo to USB serial to watch data flow
+//        monitor_device.putc(rx_buffer[rx_in]);
+        rx_in = (rx_in + 1) % buffer_size;
+    }
+    return;
+}
 
 /* itoa:  convert n to characters in s */
 void ezLCD3::itoa(int value, char *sp, int radix)
@@ -37,35 +70,78 @@
     while (tp > tmp)
         *sp++ = *--tp;
 }
+
+bool ezLCD3::sync( void )
+{
+    rx_in=0;
+    rx_out=0;
+    _ser.putc('\r');
+    if(waitForCR())
+        return true;
+    else
+        return false;
+
+}
 void ezLCD3::sendInt( int i )
 {
     char value[5]= {0, 0, 0, 0, 0};
     itoa(i, value, 10);
-    if(value[0]) putc(value[0]);
-    if(value[1]) putc(value[1]);
-    if(value[2]) putc(value[2]);
-    if(value[3]) putc(value[3]);
-    if(value[4]) putc(value[4]);
+    if(value[0]) _ser.putc(value[0]);
+    if(value[1]) _ser.putc(value[1]);
+    if(value[2]) _ser.putc(value[2]);
+    if(value[3]) _ser.putc(value[3]);
+    if(value[4]) _ser.putc(value[4]);
 }
-/*
+
+void ezLCD3::stripSpace(char* str)
+{
+  char* i = str;
+  char* j = str;
+  while(*j != 0)
+  {
+    *i = *j++;
+    if(*i != ' ')
+      i++;
+  }
+  *i = 0;
+}
+
 int ezLCD3::getInt( char *str )
 {
-    return atoi(str);
+    stripSpace(str);
+    return atoi(str );
 }
+
 void ezLCD3::getString( char *str )
 {
-    char value[5]= {0, 0, 0, 0, 0};
-    int counter;
-    
+    char c=0;
+    unsigned long timeOut=0;
+    do {
+        if(rx_in != rx_out) {
+            c=rx_buffer[rx_out];
+            rx_out = (rx_out + 1) % buffer_size;
+            *str++ = c;
+        }
+        timeOut++;
+    } while(c!='\r' && timeOut != 0xfffff);
+    *str--;
+    *str = 0;
 }
-*/
+
 bool ezLCD3::waitForCR( void )
 {
-    while(! _ser.readable());
-    if(_ser.getc() == '\r')
+    char c=0;
+    unsigned long timeOut=0;
+    while(rx_in == rx_out) {
+        if(timeOut++ > 0xfffff) return false;
+    }
+    c=rx_buffer[rx_out];
+    rx_out = (rx_out + 1) % buffer_size;
+    if(c == '\r') {
         return true;
-    else
+    } else {
         return false;
+    }
 }
 
 void ezLCD3::cls()
@@ -96,6 +172,7 @@
 }
 void ezLCD3::color(int fColor)
 {
+    //pc.printf("color");
     sendInt(Color);
     _ser.putc(' ');
     sendInt(fColor);
@@ -116,6 +193,7 @@
     _ser.putc(' ');
     sendInt(y);
     _ser.putc('\r');
+    waitForCR();
 }
 
 void ezLCD3::plot( void )
@@ -144,7 +222,6 @@
     sendInt(y);
     _ser.putc('\r');
     waitForCR();
-
 }
 
 void ezLCD3::circle(int radius, bool filled)
@@ -177,6 +254,7 @@
 
 void ezLCD3::light(int i)
 {
+    //pc.printf("light");
     if (i >= 0 && i <= 100) {
         sendInt(Light);
         putc(' ');
@@ -185,29 +263,46 @@
         waitForCR();
     }
 }
-/*
-int ezLCD3::getMaxX( void )
+
+int ezLCD3::getXmax( void )
 {
     sendInt(Xmax);
-    putc('\r');
-    waitForCR();
-
+    _ser.putc('\r');
+    getString( buf );
+    return getInt( buf );
 }
-int ezLCD3::getMaxY( void )
+int ezLCD3::getYmax( void )
 {
     sendInt(Ymax);
-    putc('\r');
+    _ser.putc('\r');
+    getString( buf );
+    return getInt( buf );
+}
+
+void ezLCD3::setStringID( int ID, char *str )
+{
+    unsigned char c;
+    sendInt(StringID);
+    _ser.putc(' ');
+    sendInt(ID);
+    _ser.putc(' ');
+    _ser.putc('\"');
+    while( (c = *str++) )
+        _ser.putc(c);
+    _ser.putc('\"');
+    _ser.putc('\r');
     waitForCR();
 }
 
-void ezLCD3::string( int ID ) {
-
+void ezLCD3::getStringID( int ID , char *str)
+{
+    sendInt(StringID);
+    _ser.putc(' ');
+    sendInt(ID);
+    _ser.putc('\r');
+    getString(str);
 }
 
-void ezLCD3::string( int ID , char *str) {
-
-}
-*/
 void ezLCD3::reverseMode()
 {
     putc(0x7c);
@@ -216,14 +311,7 @@
 
 void ezLCD3::resolution(int type)
 {
-    switch (type) {
-        case LCD_128x64 :
-            resolution(128, 64);
-            break;
-        case LCD_160x128 :
-            resolution(160, 128);
-            break;
-    }
+
 }
 
 void ezLCD3::print( char *str )
@@ -245,6 +333,96 @@
     _yMax = y;
 }
 
+//ameter [ID][x][y][width][height][options][value][min][max][theme][stringID][type]
+void ezLCD3::ameter( int ID, int x, int y, int w, int h, int options, int value, int min, int max, int theme, int stringID, int type)
+{
+    sendInt(Set_AMeter);
+    _ser.putc(' ');
+    sendInt(ID);
+    _ser.putc(' ');
+    sendInt(x);
+    _ser.putc(' ');
+    sendInt(y);
+    _ser.putc(' ');
+    sendInt(w);
+    _ser.putc(' ');
+    sendInt(h);
+    _ser.putc(' ');
+    sendInt(options);
+    _ser.putc(' ');
+    sendInt(value);
+    _ser.putc(' ');
+    sendInt(min);
+    _ser.putc(' ');
+    sendInt(max);
+    _ser.putc(' ');
+    sendInt(theme);
+    _ser.putc(' ');
+    sendInt(stringID);
+    _ser.putc(' ');
+    sendInt(type);
+    _ser.putc('\r');
+    waitForCR();
+}
+
+void ezLCD3::touchZone( int ID, int x, int y, int w, int h, bool option)
+{
+    sendInt(Set_TouchZone);
+    _ser.putc(' ');
+    sendInt(ID);
+    _ser.putc(' ');
+    sendInt(x);
+    _ser.putc(' ');
+    sendInt(y);
+    _ser.putc(' ');
+    sendInt(w);
+    _ser.putc(' ');
+    sendInt(h);
+    _ser.putc(' ');
+    if(option)
+        sendInt('1');
+    else
+        sendInt('0');
+    _ser.putc('\r');
+    waitForCR();
+}
+
+void ezLCD3::button( int ID, int x, int y, int w, int h, int option, int align, int radius, int theme, int stringID )
+{
+    sendInt(Set_Button);
+    _ser.putc(' ');
+    sendInt(ID);
+    _ser.putc(' ');
+    sendInt(x);
+    _ser.putc(' ');
+    sendInt(y);
+    _ser.putc(' ');
+    sendInt(w);
+    _ser.putc(' ');
+    sendInt(h);
+    _ser.putc(' ');
+    sendInt(option);
+    _ser.putc(' ');
+    sendInt(align);
+    _ser.putc(' ');
+    sendInt(radius);
+    _ser.putc(' ');
+    sendInt(theme);
+    _ser.putc(' ');
+    sendInt(stringID);
+    _ser.putc('\r');
+    waitForCR();
+}
+
+unsigned int ezLCD3::wstack( int type )
+{
+    sendInt( Wstack );
+    _ser.putc(' ');
+    sendInt( type );
+    _ser.putc('\r');
+    getString( buf );
+    return getInt( buf );
+}
 int ezLCD3::_putc( int c)
 {
     _ser.putc('2');