Shows how to use a display and the touch controller. A very basic paint program.

Dependencies:   DmTftLibrary mbed

Files at this revision

API Documentation at this revision

Comitter:
displaymodule
Date:
Tue May 20 15:39:09 2014 +0000
Child:
1:c5ef2d3261d3
Commit message:
First version

Changed in this revision

Canvas.cpp Show annotated file Show diff for this revision Revisions of this file
Canvas.h Show annotated file Show diff for this revision Revisions of this file
DmTftLibrary.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Canvas.cpp	Tue May 20 15:39:09 2014 +0000
@@ -0,0 +1,47 @@
+/**********************************************************************************************
+ Copyright (c) 2014 DisplayModule. All rights reserved.
+
+ Redistribution and use of this source code, part of this source code or any compiled binary
+ based on this source code is permitted as long as the above copyright notice and following
+ disclaimer is retained.
+
+ DISCLAIMER:
+ THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
+ NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
+ ********************************************************************************************/
+
+#include "Canvas.h"
+
+Canvas::Canvas(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t fg, uint16_t bg, uint16_t borderWidth, uint16_t borderColor) :
+  _x(x), _y(y), _width(width), _height(height), _borderWidth(borderWidth), _fgColor(fg), _bgColor(bg), _borderColor(borderColor)
+{
+  _borderEnabled = _borderWidth > 0;
+}
+
+void Canvas::setBorder(uint16_t width, uint16_t color)
+{
+  _borderWidth = width;
+  _borderColor = color;
+  _borderEnabled = _borderWidth > 0;
+}
+
+void Canvas::enableBorder(bool enable)
+{
+  _borderEnabled = enable;
+}
+
+void Canvas::draw(DmTftBase* tft)
+{
+  tft->fillRectangle(_x, _y, _x+_width, _y+_height, _bgColor);
+  if (_borderEnabled) {
+    for (int i = 0; i < _borderWidth; i++) {
+      tft->drawRectangle(_x+i, _y+i, _x+_width-i, _y+_height-i, _borderColor);
+    }
+  }
+}
+
+void Canvas::drawString(DmTftBase* tft, const char* msg)
+{
+  tft->drawStringCentered(_x, _y, _width, _height, msg);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Canvas.h	Tue May 20 15:39:09 2014 +0000
@@ -0,0 +1,49 @@
+/**********************************************************************************************
+ Copyright (c) 2014 DisplayModule. All rights reserved.
+
+ Redistribution and use of this source code, part of this source code or any compiled binary
+ based on this source code is permitted as long as the above copyright notice and following
+ disclaimer is retained.
+
+ DISCLAIMER:
+ THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
+ NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
+ ********************************************************************************************/
+
+#ifndef Canvas_h
+#define Canvas_h
+
+#include "DmTftBase.h"
+
+class Canvas
+{
+public:
+
+  Canvas(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t fg = WHITE, uint16_t bg = BLACK, uint16_t borderWidth = 0, uint16_t borderColor = WHITE);
+
+  bool isInside(uint16_t x, uint16_t y) { return (x >= _x) && (y >= _y) && (x <= (_x+_width)) && (y <= (_y+_height)); }
+    
+  void setColors(uint16_t foreground, uint16_t background) { _fgColor = foreground; _bgColor = background; }
+  
+  void setBorder(uint16_t width, uint16_t color);
+  
+  void enableBorder(bool enable);
+
+  void draw(DmTftBase* tft);
+  void drawString(DmTftBase* tft, const char* msg);
+  
+  uint16_t x() { return _x; }
+  uint16_t y() { return _y; }
+  uint16_t width() { return _width; }
+  uint16_t height() { return _height; }
+
+private:
+
+  uint16_t _x, _y, _width, _height, _borderWidth;
+  uint16_t _fgColor, _bgColor, _borderColor;
+  bool _borderEnabled;
+};
+#endif
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DmTftLibrary.lib	Tue May 20 15:39:09 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/displaymodule/code/DmTftLibrary/#59be7fca4581
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue May 20 15:39:09 2014 +0000
@@ -0,0 +1,203 @@
+/**********************************************************************************************
+ Copyright (c) 2014 DisplayModule. All rights reserved.
+
+ Redistribution and use of this source code, part of this source code or any compiled binary
+ based on this source code is permitted as long as the above copyright notice and following
+ disclaimer is retained.
+
+ DISCLAIMER:
+ THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
+ NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
+ ********************************************************************************************/
+
+/******************************************************************************
+ * Includes
+ *****************************************************************************/
+
+#include "mbed.h"
+
+#include "DmTftHX8353C.h"
+#include "DmTftS6D0164.h"
+#include "DmTftIli9325.h"
+#include "DmTftIli9341.h"
+#include "DmTftSsd2119.h"
+#include "DmTouch.h"
+
+#include "Canvas.h"
+
+/******************************************************************************
+ * Typedefs and defines
+ *****************************************************************************/
+
+typedef enum {
+  Idle,
+  SelectingColor,
+  Drawing,
+} paintMode_t;
+
+typedef struct {
+  uint16_t color;
+  Canvas* canvas;
+} colorButton_t;
+
+#define log(...)
+//#define log(...) printf(__VA_ARGS__)
+
+#define MARGIN       5
+#define BOX_W       30
+#define BOX_H       30
+#define POINT_SIZE   2
+
+#define NUM_BUTTONS  (sizeof(buttons)/sizeof(buttons[0]))
+
+#if 1 /* Displays without adapter */
+#define DM_PIN_SPI_MOSI   D11
+#define DM_PIN_SPI_MISO   D12
+#define DM_PIN_SPI_SCLK   D13
+
+#define DM_PIN_CS_TOUCH   D4
+#define DM_PIN_CS_TFT     D10
+#define DM_PIN_CS_SDCARD  D8
+#define DM_PIN_CS_FLASH   D6
+#else /* Displays with adapter */
+#define DM_PIN_SPI_MOSI   A0
+#define DM_PIN_SPI_MISO   D9
+#define DM_PIN_SPI_SCLK   A1
+
+#define DM_PIN_CS_TOUCH   D8
+#define DM_PIN_CS_TFT     A3
+#define DM_PIN_CS_SDCARD  D10
+#endif
+
+/******************************************************************************
+ * Local variables
+ *****************************************************************************/
+
+//DmTftHX8353C tft;  /* DM_TFT18_101 */
+//DmTftS6D0164 tft;  /* DM_TFT22_102 */
+//DmTftIli9325 tft;  /* DM_TFT28_103 and DM_TFT24_104 */
+DmTftIli9341 tft;  /* DM_TFT28_105 */
+//DmTftSsd2119 tft;   /* DM_TFT35_107 */
+
+//DmTouch touch(DmTouch::DM_TFT28_103, false); /* For LPC4088 QuickStart Board */
+//DmTouch touch(DmTouch::DM_TFT28_103);
+//DmTouch touch(DmTouch::DM_TFT24_104, false); /* For LPC4088 QuickStart Board */
+//DmTouch touch(DmTouch::DM_TFT24_104);
+DmTouch touch(DmTouch::DM_TFT28_105);
+//DmTouch touch(DmTouch::DM_TFT35_107);
+
+DigitalInOut csTouch(DM_PIN_CS_TOUCH, PIN_OUTPUT, PullUp, 1);
+DigitalInOut csDisplay(DM_PIN_CS_TFT, PIN_OUTPUT, PullUp, 1);
+DigitalInOut csSDCard(DM_PIN_CS_SDCARD, PIN_OUTPUT, PullUp, 1);
+#ifdef DM_PIN_CS_FLASH
+  DigitalInOut csFlash(DM_PIN_CS_FLASH, PIN_OUTPUT, PullUp, 1);
+#endif  
+
+/******************************************************************************
+ * Global variables
+ *****************************************************************************/
+
+
+/******************************************************************************
+ * Main
+ *****************************************************************************/
+
+int main() {
+  uint16_t x, y, lastX, lastY;
+  bool penDown;
+  paintMode_t mode = Idle;
+  uint16_t color = WHITE;
+  int activeIdx = 4;
+  int selIdx = 0;
+
+  colorButton_t buttons[] = {
+    { RED, new Canvas(MARGIN, MARGIN, BOX_W, BOX_H, BLACK, RED, 3, GRAY1) },
+    { GREEN, new Canvas(MARGIN+(BOX_W + MARGIN), MARGIN, BOX_W, BOX_H, BLACK, GREEN, 3, GRAY1) },
+    { BLUE, new Canvas(MARGIN+(BOX_W + MARGIN)*2, MARGIN, BOX_W, BOX_H, BLACK, BLUE, 3, GRAY1) },
+    { YELLOW, new Canvas(MARGIN+(BOX_W + MARGIN)*3, MARGIN, BOX_W, BOX_H, BLACK, YELLOW, 3, GRAY1) },
+    { WHITE, new Canvas(MARGIN+(BOX_W + MARGIN)*4, MARGIN, BOX_W, BOX_H, BLACK, WHITE, 3, GRAY1) },
+  };
+  
+  tft.init();
+  
+  for (int i = 0; i < NUM_BUTTONS; i++) {
+    if (color != buttons[i].color) {
+      buttons[i].canvas->enableBorder(false);
+    }
+    buttons[i].canvas->draw(&tft);
+  }
+
+  Canvas drawArea(MARGIN, MARGIN+(BOX_H + MARGIN), tft.width(), tft.height() - MARGIN+(BOX_H + MARGIN));
+  Canvas clearButton(tft.width() - 50 - MARGIN, MARGIN, 50, BOX_H, WHITE, BLACK, 1, WHITE);
+
+  drawArea.draw(&tft);
+  clearButton.draw(&tft);
+  clearButton.drawString(&tft, "clr");
+  
+  touch.init();
+
+  lastX = lastY = 0;  
+  while(true)
+  {
+    touch.readTouchData(x, y, penDown);
+    switch (mode)
+    {
+      case Idle:
+        if (penDown)
+        {
+          if (drawArea.isInside(x, y))
+          {
+            tft.drawPoint(x, y, POINT_SIZE);
+            mode = Drawing;
+            log("start draw\n");
+          } else if (clearButton.isInside(x, y)) {
+            log("Pressed clear!\n");
+            drawArea.draw(&tft);
+          } else {
+            for (int i = 0; i < NUM_BUTTONS; i++) {
+              if (buttons[i].canvas->isInside(x, y)) {
+                mode = SelectingColor;
+                selIdx = i;
+                log("Pen down on button %d\n", i);
+              }
+            }
+          }
+        }
+        break;
+        
+      case SelectingColor:
+        if (!penDown)
+        {
+          if (buttons[selIdx].canvas->isInside(lastX, lastY)) {
+            buttons[activeIdx].canvas->enableBorder(false);
+            buttons[activeIdx].canvas->draw(&tft);
+
+            log("Pen up => actual click on color %d!\n", selIdx);
+            
+            color = buttons[selIdx].color;
+            tft.setTextColor(BLACK, color);
+
+            activeIdx = selIdx;
+            buttons[activeIdx].canvas->enableBorder(true);
+            buttons[activeIdx].canvas->draw(&tft);
+          }
+          
+          mode = Idle;
+        }
+        break;
+        
+      case Drawing:
+        if (penDown && drawArea.isInside(x, y)) {
+          tft.drawPoint(x, y, POINT_SIZE);
+          log("painting\n");
+        } else if (!penDown) {
+          mode = Idle;
+          log("end draw\n");
+        }
+        break;
+    }
+    lastX = x;
+    lastY = y;
+    wait(0.002);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Tue May 20 15:39:09 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/displaymodule/code/mbed-src/#3306e8fd8143