Library for 4D systems touchscreen

TouchScreen.cpp

Committer:
ashleymills
Date:
2012-08-21
Revision:
0:edf5f3d8645e

File content as of revision 0:edf5f3d8645e:

/**
 * @file
 * @version
 * @author Ashley.Mills@vodafone.com
 * @brief Touchscreen control.
 */

#include "TouchScreen.h"

#define WAIT_TIMEOUT_MS 1000

TouchScreen::TouchScreen(PinName tx, PinName rx, PinName resetPin, const char *name) : Serial(tx,rx,name) {
    baud(9600);
    _resetPin = new DigitalOut(resetPin);
    _resetPin->write(1);
    reset();
    _initOK = false;
    putc('U');
    _initOK = gack();
    _fillColor = createRGBClassic(21,21,21);
    _strokeColor = createRGBClassic(221,221,221);
}

TouchScreen::TouchScreen(PinName tx, PinName rx, PinName resetPin, eBaudRate baudRate, const char *name) : Serial (tx,rx,name) {
    baud(9600);
    _resetPin = new DigitalOut(resetPin);
    _resetPin->write(1);
    reset();
    _initOK = false;
    putc('U');
    if((_initOK=gack())) {
        setBaudRate(baudRate);
    } 
    _fillColor = createRGBClassic(21,21,21);
    _strokeColor = createRGBClassic(221,221,221);
}

void TouchScreen::reset() {
   _resetPin->write(0);
   Thread::wait(2);
   _resetPin->write(1);
   Thread::wait(3000);
}

void TouchScreen::clearScreen() {
    putc('E');
    gack();
}

bool TouchScreen::setBaudRate(eBaudRate baudRate) {
    long newRate = 9600;
    uint8_t rateCommand = (uint8_t)0x06;
    putc('Q');
    switch(baudRate) {
        case BAUD_9600:
            rateCommand   = (uint8_t)0x06;
            newRate = 9600;
        break;

        case BAUD_14400:
            rateCommand   = (uint8_t)0x07;
            newRate = 14400;
        break;

        case BAUD_19200:
            rateCommand   = (uint8_t)0x08;
            newRate = 19200;
        break;

        case BAUD_57600:
            rateCommand   = (uint8_t)0x0C;
            newRate = 57600;
        break;

        case BAUD_115200:
            rateCommand   = (uint8_t)0x0D;
            newRate = 115200;
        break;

        case BAUD_256000:
            rateCommand   = (uint8_t)0x0F;
            newRate = 256000;
        break;
    }
    putc((uint8_t)rateCommand);
    // doesn't seem to want to acknowledge at either the old baud rate or the new baud rate
    dropBytes(1);
    baud(newRate);
    return true;
}

void TouchScreen::setPenSolid() {
    putc('p');
    putc((uint8_t)0x0);
    gack();
}

void TouchScreen::setPenWireframe() {
    putc('p');
    putc((uint8_t)0x1);
    gack();
}

void TouchScreen::setBackgroundColor(uint16_t color) {
    putc('K');
    putc(highByte(color));
    putc(lowByte(color));
    gack();
}

bool TouchScreen::waitUntilReadable() {
    long timeOut = WAIT_TIMEOUT_MS;
    Timer timer;
    timer.start();
    while(1) {
        if(readable())
            return true;
        if(timer.read_ms()>timeOut)
            return false;
    }
}

bool TouchScreen::gack() {
    if(waitUntilReadable()) {
       uint8_t b = getc();
       return (b==0x06);
    }
    return false;
}

void TouchScreen::drawPixel(uint16_t xPos, uint16_t yPos, uint16_t color) {
    putc('P');
    putc(highByte(xPos));   // x pos
    putc(lowByte(xPos));

    putc(highByte(yPos));   // y pos
    putc(lowByte(yPos));

    putc(highByte(color));  // color
    putc(lowByte(color));
    gack();
}

void TouchScreen::drawCircle(uint16_t xPos, uint16_t yPos, uint16_t radius, uint16_t color) {
    putc('C');
    putc(highByte(xPos));   // x pos
    putc(lowByte(xPos));

    putc(highByte(yPos));   // y pos
    putc(lowByte(yPos));

    putc(highByte(radius)); // radius
    putc(lowByte(radius));

    putc(highByte(color));  // color
    putc(lowByte(color));
    gack();
}

uint16_t TouchScreen::createRGBClassic(int r, int g, int b) {
    return createRGBF(r/255.0f,g/255.0f,b/255.0f);
}

uint16_t TouchScreen::createRGB(uint8_t r, uint8_t g, uint8_t b) {
    if(r>31) r = 31;
    if(g>63) g = 63;
    if(b>31) b = 31;
    uint16_t o = 0;
    o |= (r<<0xb);
    o |= (g<<0x5);
    o |=  b;
    return o;
}

uint16_t TouchScreen::createRGBF(float r, float g, float b) {
    return createRGB((uint8_t)(r*31.0f),(uint8_t)(g*63.0f),(uint8_t)(b*31.0f));
}

void TouchScreen::enableTouch() {
    putc('Y');
    putc((uint8_t)0x5);
    putc((uint8_t)0x0);
    gack();
}

void TouchScreen::disableTouch() {
    putc('Y');
    putc((uint8_t)0x5);
    putc((uint8_t)0x1);
    gack();
}

void TouchScreen::setOrientation(eScreenOrientation o) {
    putc('Y');
    putc((uint8_t)0x4);
    putc((uint8_t)o);
    gack();
}

void TouchScreen::getVersion() {
    putc('V');
    putc((uint8_t)0x1);
    dropBytes((uint16_t)5);
}

void TouchScreen::dropBytes(uint16_t numBytes) {
    while(numBytes--) {
        dropByte();
    }
}

void TouchScreen::drawImage(char *name, uint16_t x, uint16_t y) {
    putc('@');
    putc('m');
    printf(name);
    putc((uint8_t)0);
    putc(highByte((uint16_t)x));
    putc(lowByte((uint16_t)x));
    putc(highByte((uint16_t)y));
    putc(lowByte((uint16_t)y));
    putc((uint8_t)0x00);
    putc((uint8_t)0x00);
    putc((uint8_t)0x00);
    putc((uint8_t)0x00);
    gack();
}

void TouchScreen::dropByte() {
    if(waitUntilReadable()) {
        getc();
    }
}

void TouchScreen::dropByteAndWaitUntilReadable() {
    if(waitUntilReadable()) {
       getc();
    }
    waitUntilReadable();
}

eTouchStatus TouchScreen::getTouchStatus() {
    putc('o');
    putc((uint8_t)0x4);
    dropByteAndWaitUntilReadable();
    uint8_t touchStatus = getc();
    dropByte();
    dropByte();
    return (eTouchStatus)touchStatus;
}

TouchPoint TouchScreen::getLastTouch() {
    TouchPoint p;

    putc('o');
    putc((uint8_t)0x05);
    waitUntilReadable();
    uint8_t v = getc();
    p.x = (v<<0x8);
    waitUntilReadable();
    p.x += getc();
    waitUntilReadable();
    v = getc();
    p.y = (v<<0x8);
    waitUntilReadable();
    p.y += getc();
    return p;
}

void TouchScreen::setBacklight(bool state) {
    putc('Y');
    putc((uint8_t)0x00);
    if(state) {
        putc((uint8_t)0x01);
    } else {
        putc((uint8_t)0x00);
    }
    gack();
}

void TouchScreen::setContrast(uint8_t value) {
    if(value>0x0F)
        value = 0x0F;
    putc('Y');
    putc((uint8_t)0x02);
    putc(value);
    gack();
}

void TouchScreen::drawGraphicsFormatString(uint16_t x, uint16_t y, eInternalFont font, uint16_t textColor, uint8_t charWidth, uint8_t charHeight, char* text) {
    putc('S');
    putc((uint8_t)highByte(x));
    putc((uint8_t)lowByte(x));
    putc((uint8_t)highByte(y));
    putc((uint8_t)lowByte(y));
    putc((uint8_t)font);
    putc((uint8_t)highByte(textColor));
    putc((uint8_t)lowByte(textColor));
    putc((uint8_t)charWidth);
    putc((uint8_t)charHeight);
    if(strlen(text)==256) { // change to own implementation of strnlen
        text[256] = 0;
    }
    printf(text);
    putc((uint8_t)0x00);
    gack();
}


void TouchScreen::drawTextFormatString(uint8_t row, uint8_t column, eInternalFont font, uint16_t textColor, char* text) {
    putc('s');
    putc((uint8_t)column);
    putc((uint8_t)row);
    putc((uint8_t)font);
    putc((uint8_t)highByte(textColor));
    putc((uint8_t)lowByte(textColor));
    if(strlen(text)==256) { // change to own implementation of strnlen
        text[256] = 0;
    }
    printf(text);
    putc((uint8_t)0x00);
    gack();
}

void TouchScreen::drawRectangle(uint16_t topLeftX, uint16_t topLeftY, uint16_t botRightX, uint16_t botRightY, uint16_t color) {
    putc('r');
    putc(highByte(topLeftX));
    putc(lowByte (topLeftX));
    putc(highByte(topLeftY));
    putc(lowByte (topLeftY));
    putc(highByte(botRightX));
    putc(lowByte (botRightX));
    putc(highByte(botRightY));
    putc(lowByte (botRightY));
    putc(highByte(color));
    putc(lowByte (color));
    gack();
}

void TouchScreen::drawRectangleWH(uint16_t topLeftX, uint16_t topLeftY, uint16_t width, uint16_t height, uint16_t color) {
    uint16_t botRightX = topLeftX + width;
    uint16_t botRightY = topLeftY + height;
    putc('r');
    putc(highByte(topLeftX));
    putc(lowByte (topLeftX));
    putc(highByte(topLeftY));
    putc(lowByte (topLeftY));
    putc(highByte(botRightX));
    putc(lowByte (botRightX));
    putc(highByte(botRightY));
    putc(lowByte (botRightY));
    putc(highByte(color));
    putc(lowByte (color));
    gack();
}

void TouchScreen::drawRectangleWH(uint16_t topLeftX, uint16_t topLeftY, uint16_t width, uint16_t height) {
    setPenSolid();
    drawRectangleWH(topLeftX,topLeftY,width,height,_fillColor);
    setPenWireframe();
    drawRectangleWH(topLeftX,topLeftY,width,height,_strokeColor);
    setPenSolid();
}


void TouchScreen::drawLine(uint16_t topLeftX, uint16_t topLeftY, uint16_t botRightX, uint16_t botRightY, uint16_t color) {
    putc('L');
    putc(highByte(topLeftX));
    putc(lowByte (topLeftX));
    putc(highByte(topLeftY));
    putc(lowByte (topLeftY));
    putc(highByte(botRightX));
    putc(lowByte (botRightX));
    putc(highByte(botRightY));
    putc(lowByte (botRightY));
    putc(highByte(color));
    putc(lowByte (color));
    gack();
}

void TouchScreen::drawChar(char c, uint16_t x, uint16_t y, uint16_t color, uint8_t width, uint8_t height) {
    putc('t');
    putc(c);
    putc(highByte(x));
    putc(lowByte (x));
    putc(highByte(y));
    putc(lowByte (y));
    putc(highByte(color));
    putc(lowByte (color));
    putc(width);
    putc(height);
    gack();
}


void TouchScreen::setFont(uint8_t fontIndex) {
    putc('F');
    putc(fontIndex);
    gack();
}

void TouchScreen::drawText(char *s, uint16_t x, uint16_t y) {
    drawGraphicsFormatString(
        x,y,
        INTERNAL_FONT_8x8,
        _strokeColor,
        1,1,
        s
    );
}

void TouchScreen::setFillColor(uint16_t c) {
    _fillColor = c;
}

void TouchScreen::setStrokeColor(uint16_t c) {
    _strokeColor = c;
}