Library for 4D systems touchscreen

Files at this revision

API Documentation at this revision

Comitter:
ashleymills
Date:
Tue Aug 21 14:40:18 2012 +0000
Commit message:
4D systems touchscreen lib

Changed in this revision

TouchScreen.cpp Show annotated file Show diff for this revision Revisions of this file
TouchScreen.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TouchScreen.cpp	Tue Aug 21 14:40:18 2012 +0000
@@ -0,0 +1,417 @@
+/**
+ * @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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TouchScreen.h	Tue Aug 21 14:40:18 2012 +0000
@@ -0,0 +1,123 @@
+/***************************
+ * Created by Ashley Mills *
+ ***************************/
+#pragma once
+#include "mbed.h"
+#include "rtos.h"
+
+#define lowByte(w) ((uint8_t) ((w) & 0xff))
+#define highByte(w) ((uint8_t) ((w) >> 8))
+
+typedef enum {
+    NO_TOUCH_ACTIVITY=0,
+    TOUCH_PRESS,
+    TOUCH_RELEASE,
+    TOUCH_MOVING
+} eTouchStatus;
+
+typedef enum {
+    LANDSCAPE=0,
+    LANDSCAPE_R,
+    NATIVE,
+    PORTRAIT_R
+} eScreenOrientation;
+
+typedef enum {
+    BAUD_9600,
+    BAUD_14400,
+    BAUD_19200,
+    BAUD_57600,
+    BAUD_115200,
+    BAUD_256000
+} eBaudRate;
+
+typedef struct {
+    uint16_t x;
+    uint16_t y;
+} TouchPoint;
+
+typedef enum {
+    INTERNAL_FONT_5x7,
+    INTERNAL_FONT_8x8,
+    INTERNAL_FONT_8x12,
+    INTERNAL_FONT_12x16
+} eInternalFont;
+
+class TouchScreen : Serial {
+    public:
+        static const int WIDTH  = 320;
+        static const int HEIGHT = 240;
+
+        // constructors
+        TouchScreen(PinName tx, PinName rx, PinName resetPin, const char *name = NULL);
+        TouchScreen(PinName tx, PinName rx, PinName resetPin, eBaudRate baud, const char *name = NULL);
+        bool begin();
+        
+        // drawing commands
+        void drawCircle(uint16_t xPos, uint16_t yPos, uint16_t radius, uint16_t color);
+        void drawPixel(uint16_t xPos, uint16_t yPos, uint16_t color);
+        void clearScreen();
+        void setBackgroundColor(uint16_t color);
+        void setPenSolid();
+        void setPenWireframe();
+        void drawImage(char *name, uint16_t x, uint16_t y);
+        void drawGraphicsFormatString(uint16_t x, uint16_t y, eInternalFont font, uint16_t textColor, uint8_t charWidth, uint8_t charHeight, char* text);
+        
+        void drawRectangle(uint16_t topLeftX, uint16_t topLeftY, uint16_t botRightX, uint16_t botRightY, uint16_t color);
+        void drawRectangleWH(uint16_t topLeftX, uint16_t topLeftY, uint16_t width, uint16_t height, uint16_t color);
+        void drawRectangleWH(uint16_t topLeftX, uint16_t topLeftY, uint16_t width, uint16_t height);
+        void drawLine(uint16_t topLeftX, uint16_t topLeftY, uint16_t botRightX, uint16_t botRightY, uint16_t color);
+
+        // drawing text
+        void drawTextFormatString(uint8_t row, uint8_t column, eInternalFont font, uint16_t textColor, char* text);
+        void drawChar(char c, uint16_t x, uint16_t y, uint16_t color, uint8_t width, uint8_t height);
+        void drawText(char *s, uint16_t x, uint16_t y);
+        void setFont(uint8_t fontIndex);
+
+        // color creation
+        uint16_t createRGB(uint8_t r, uint8_t g, uint8_t b);
+        uint16_t createRGBClassic(int r, int g, int b);
+        uint16_t createRGBF(float r, float g, float b);
+
+        // touch commands
+        void enableTouch();
+        void disableTouch();
+        eTouchStatus getTouchStatus();
+        TouchPoint getLastTouch();
+        void reset();
+
+        // misc commands
+        void getVersion();
+        void setOrientation(eScreenOrientation o);
+        bool setBaudRate(eBaudRate baud);
+        void setBacklight(bool state);
+        void setContrast(uint8_t value);
+
+        void setFillColor(uint16_t c);
+        void setStrokeColor(uint16_t c);
+
+    private:
+        void dropByte();
+        void dropBytes(uint16_t numBytes);
+        void dropByteAndWaitUntilReadable();
+        bool gack();
+        bool waitUntilReadable();
+
+        DigitalOut *_resetPin;
+        uint16_t _fillColor;
+        uint16_t _strokeColor;
+        bool _initOK;
+};
+
+
+/*
+Header (6 bytes as 3 words)
+word1 (Hi:Lo) = width : horizontal size of the image
+word2 (Hi:Lo) = height : vertical size of the image
+Word3 = colour_mode : 16dec = 65K colour mode, 16bits/2bytes per pixel (msb, lsb)
+
+The header is then followed by the pixel data
+Pixel1�PixelN: Image pixel data and N is the total number of pixels
+                         N = height x width when colour_mode = 0
+                         N = height x width x 2 when colour_mode = 1
+*/