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

ezLCDLib.cpp

Committer:
codeman
Date:
2013-04-28
Revision:
2:fc7d151593f1
Parent:
1:c7659c8af0d3
Child:
3:28e71ce23bfd

File content as of revision 2:fc7d151593f1:

#include "mbed.h"
#include "ezLCDLib.h"

void Tx_interrupt();
void Rx_interrupt();
void send_line();
void read_line();

// 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;

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)
{
    char tmp[16];// be careful with the length of the buffer
    char *tp = tmp;
    int i;
    unsigned v;
    int sign;

    sign = (radix == 10 && value < 0);
    if (sign)   v = -value;
    else    v = (unsigned)value;

    while (v || tp == tmp) {
        i = v % radix;
        v /= radix; // v/=radix uses less CPU clocks than v=v/radix does
        if (i < 10)
            *tp++ = i+'0';
        else
            *tp++ = i + 'a' - 10;
    }

    if (sign)
        *sp++ = '-';
    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]) _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 )
{
    stripSpace(str);
    return atoi(str );
}

void ezLCD3::getString( char *str )
{
    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 )
{
    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 {
        return false;
    }
}

void ezLCD3::cls()
{
    sendInt(Clr_Screen);
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::cls(int bColor)
{
    sendInt(Clr_Screen);
    _ser.putc(' ');
    sendInt(bColor);
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::cls(int bColor, int fColor)
{
    sendInt(Clr_Screen);
    _ser.putc(' ');
    sendInt(bColor);
    _ser.putc(' ');
    sendInt(fColor);
    _ser.putc('\r');
    waitForCR();
}
void ezLCD3::color(int fColor)
{
    //pc.printf("color");
    sendInt(Color);
    _ser.putc(' ');
    sendInt(fColor);
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::pos(int col, int row)
{
    //posXY(XSIZE*col, (YSIZE*row));
}

void ezLCD3::xy(int x, int y)
{
    sendInt(XY);
    _ser.putc(' ');
    sendInt(x);
    _ser.putc(' ');
    sendInt(y);
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::plot( void )
{
    sendInt(Plot);
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::plot( int x, int y )
{
    sendInt(Plot);
    _ser.putc(' ');
    sendInt(x);
    _ser.putc(' ');
    sendInt(y);
    _ser.putc('\r');
    waitForCR();
}
void ezLCD3::line(int x, int y )
{
    sendInt(Line);
    _ser.putc(' ');
    sendInt(x);
    _ser.putc(' ');
    sendInt(y);
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::circle(int radius, bool filled)
{
    sendInt(Circle);
    _ser.putc(' ');
    sendInt(radius);
    if(filled) {
        _ser.putc(' ');
        _ser.putc('f');
    }
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::box(int x, int y, bool filled)
{
    sendInt(Box);
    _ser.putc(' ');
    sendInt(x);
    _ser.putc(' ');
    sendInt(y);
    if(filled) {
        _ser.putc(' ');
        _ser.putc('f');
    }
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::light(int i)
{
    //pc.printf("light");
    if (i >= 0 && i <= 100) {
        sendInt(Light);
        putc(' ');
        sendInt( i );
        putc('\r');
        waitForCR();
    }
}

int ezLCD3::getXmax( void )
{
    sendInt(Xmax);
    _ser.putc('\r');
    getString( buf );
    return getInt( buf );
}
int ezLCD3::getYmax( void )
{
    sendInt(Ymax);
    _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::getStringID( int ID , char *str)
{
    sendInt(StringID);
    _ser.putc(' ');
    sendInt(ID);
    _ser.putc('\r');
    getString(str);
}

void ezLCD3::reverseMode()
{
    putc(0x7c);
    putc(0x12);
}

void ezLCD3::resolution(int type)
{

}

void ezLCD3::print( char *str )
{
    unsigned char c;
    sendInt(Print);
    _ser.putc(' ');
    _ser.putc('\"');
    while( (c = *str++) )
        _ser.putc(c);
    _ser.putc('\"');
    _ser.putc('\r');
    waitForCR();
}

void ezLCD3::resolution(int x, int y)
{
    _xMax = x;
    _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');
    _ser.putc('5');
    _ser.putc(' ');
    _ser.putc('\"');
    _ser.putc(c);
    _ser.putc('\"');
    _ser.putc('\r');
    waitForCR();
    return (c);
}

int ezLCD3::_getc(void)
{
    char r = 0;
    return(r);
}

int ezLCD3::putc ( int c )
{
    return(_ser.putc(c));
}

int ezLCD3::getc (void)
{
    return(_ser.getc());
}

void ezLCD3::lcdbaud(int b)
{
    if (b > 0 && b < 7) {
        putc(0x7c);
        putc(0x07);
        putc(b+'0');
    }
}