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

Dependencies:   DmTftLibrary mbed

Committer:
displaymodule
Date:
Mon Sep 01 10:57:38 2014 +0000
Revision:
4:34d4f7e491bc
Parent:
3:84a858adc7ff
Removed dependency on mbed-src for LPC1549

Who changed what in which revision?

UserRevisionLine numberNew contents of line
displaymodule 0:2ee293544fc1 1 /**********************************************************************************************
displaymodule 0:2ee293544fc1 2 Copyright (c) 2014 DisplayModule. All rights reserved.
displaymodule 0:2ee293544fc1 3
displaymodule 0:2ee293544fc1 4 Redistribution and use of this source code, part of this source code or any compiled binary
displaymodule 0:2ee293544fc1 5 based on this source code is permitted as long as the above copyright notice and following
displaymodule 0:2ee293544fc1 6 disclaimer is retained.
displaymodule 0:2ee293544fc1 7
displaymodule 0:2ee293544fc1 8 DISCLAIMER:
displaymodule 0:2ee293544fc1 9 THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
displaymodule 0:2ee293544fc1 10 NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
displaymodule 0:2ee293544fc1 11 ********************************************************************************************/
displaymodule 0:2ee293544fc1 12
displaymodule 0:2ee293544fc1 13 /******************************************************************************
displaymodule 0:2ee293544fc1 14 * Includes
displaymodule 0:2ee293544fc1 15 *****************************************************************************/
displaymodule 0:2ee293544fc1 16
displaymodule 0:2ee293544fc1 17 #include "mbed.h"
displaymodule 0:2ee293544fc1 18
displaymodule 0:2ee293544fc1 19 #include "DmTftHX8353C.h"
displaymodule 0:2ee293544fc1 20 #include "DmTftS6D0164.h"
displaymodule 0:2ee293544fc1 21 #include "DmTftIli9325.h"
displaymodule 0:2ee293544fc1 22 #include "DmTftIli9341.h"
displaymodule 0:2ee293544fc1 23 #include "DmTftSsd2119.h"
displaymodule 0:2ee293544fc1 24 #include "DmTouch.h"
displaymodule 0:2ee293544fc1 25
displaymodule 0:2ee293544fc1 26 #include "Canvas.h"
displaymodule 0:2ee293544fc1 27
displaymodule 0:2ee293544fc1 28 /******************************************************************************
displaymodule 0:2ee293544fc1 29 * Typedefs and defines
displaymodule 0:2ee293544fc1 30 *****************************************************************************/
displaymodule 0:2ee293544fc1 31
displaymodule 0:2ee293544fc1 32 typedef enum {
displaymodule 0:2ee293544fc1 33 Idle,
displaymodule 0:2ee293544fc1 34 SelectingColor,
displaymodule 0:2ee293544fc1 35 Drawing,
displaymodule 0:2ee293544fc1 36 } paintMode_t;
displaymodule 0:2ee293544fc1 37
displaymodule 0:2ee293544fc1 38 typedef struct {
displaymodule 0:2ee293544fc1 39 uint16_t color;
displaymodule 0:2ee293544fc1 40 Canvas* canvas;
displaymodule 0:2ee293544fc1 41 } colorButton_t;
displaymodule 0:2ee293544fc1 42
displaymodule 1:c5ef2d3261d3 43 /* Note that there are restrictions on which platforms that can use printf
displaymodule 1:c5ef2d3261d3 44 in combinations with the DmTftLibrary. Some platforms (e.g. LPC1549 LPCXpresso)
displaymodule 1:c5ef2d3261d3 45 use the same pins for USBRX/USBTX and display control. Printing will
displaymodule 1:c5ef2d3261d3 46 cause the display to not work. Read more about this on the display's notebook
displaymodule 1:c5ef2d3261d3 47 page. */
displaymodule 1:c5ef2d3261d3 48 //#define log(...) printf(__VA_ARGS__)
displaymodule 0:2ee293544fc1 49 #define log(...)
displaymodule 0:2ee293544fc1 50
displaymodule 0:2ee293544fc1 51 #define MARGIN 5
displaymodule 0:2ee293544fc1 52 #define BOX_W 30
displaymodule 0:2ee293544fc1 53 #define BOX_H 30
displaymodule 0:2ee293544fc1 54 #define POINT_SIZE 2
displaymodule 0:2ee293544fc1 55
displaymodule 0:2ee293544fc1 56 #define NUM_BUTTONS (sizeof(buttons)/sizeof(buttons[0]))
displaymodule 0:2ee293544fc1 57
displaymodule 2:de14389741d5 58 #if 0
displaymodule 1:c5ef2d3261d3 59 /* Displays without adapter */
displaymodule 1:c5ef2d3261d3 60 #define DM_PIN_SPI_MOSI D11
displaymodule 1:c5ef2d3261d3 61 #define DM_PIN_SPI_MISO D12
displaymodule 1:c5ef2d3261d3 62 #define DM_PIN_SPI_SCLK D13
displaymodule 1:c5ef2d3261d3 63 #define DM_PIN_CS_TOUCH D4
displaymodule 1:c5ef2d3261d3 64 #define DM_PIN_CS_TFT D10
displaymodule 1:c5ef2d3261d3 65 #define DM_PIN_CS_SDCARD D8
displaymodule 1:c5ef2d3261d3 66 #define DM_PIN_CS_FLASH D6
displaymodule 1:c5ef2d3261d3 67 #else
displaymodule 1:c5ef2d3261d3 68 /* Displays with adapter */
displaymodule 1:c5ef2d3261d3 69 #define DM_PIN_SPI_MOSI A0
displaymodule 1:c5ef2d3261d3 70 #define DM_PIN_SPI_MISO D9
displaymodule 1:c5ef2d3261d3 71 #define DM_PIN_SPI_SCLK A1
displaymodule 1:c5ef2d3261d3 72 #define DM_PIN_CS_TOUCH D8
displaymodule 1:c5ef2d3261d3 73 #define DM_PIN_CS_TFT A3
displaymodule 1:c5ef2d3261d3 74 #define DM_PIN_CS_SDCARD D10
displaymodule 0:2ee293544fc1 75 #endif
displaymodule 0:2ee293544fc1 76
displaymodule 0:2ee293544fc1 77 /******************************************************************************
displaymodule 0:2ee293544fc1 78 * Local variables
displaymodule 0:2ee293544fc1 79 *****************************************************************************/
displaymodule 0:2ee293544fc1 80
displaymodule 2:de14389741d5 81 DmTftIli9325 tft; /* DM_TFT28_103 and DM_TFT24_104 */
displaymodule 1:c5ef2d3261d3 82 //DmTftIli9341 tft; /* DM_TFT28_105 */
displaymodule 2:de14389741d5 83 //DmTftSsd2119 tft; /* DM_TFT35_107 */
displaymodule 0:2ee293544fc1 84
displaymodule 3:84a858adc7ff 85 //DmTouch touch(DmTouch::DM_TFT28_103, DmTouch::Software); /* For LPC4088 QuickStart Board */
displaymodule 3:84a858adc7ff 86 DmTouch touch(DmTouch::DM_TFT28_103);
displaymodule 1:c5ef2d3261d3 87 //DmTouch touch(DmTouch::DM_TFT24_104, DmTouch::Software); /* For LPC4088 QuickStart Board */
displaymodule 0:2ee293544fc1 88 //DmTouch touch(DmTouch::DM_TFT24_104);
displaymodule 1:c5ef2d3261d3 89 //DmTouch touch(DmTouch::DM_TFT28_105);
displaymodule 2:de14389741d5 90 //DmTouch touch(DmTouch::DM_TFT35_107);
displaymodule 0:2ee293544fc1 91
displaymodule 0:2ee293544fc1 92 DigitalInOut csTouch(DM_PIN_CS_TOUCH, PIN_OUTPUT, PullUp, 1);
displaymodule 0:2ee293544fc1 93 DigitalInOut csDisplay(DM_PIN_CS_TFT, PIN_OUTPUT, PullUp, 1);
displaymodule 0:2ee293544fc1 94 DigitalInOut csSDCard(DM_PIN_CS_SDCARD, PIN_OUTPUT, PullUp, 1);
displaymodule 0:2ee293544fc1 95 #ifdef DM_PIN_CS_FLASH
displaymodule 0:2ee293544fc1 96 DigitalInOut csFlash(DM_PIN_CS_FLASH, PIN_OUTPUT, PullUp, 1);
displaymodule 0:2ee293544fc1 97 #endif
displaymodule 0:2ee293544fc1 98
displaymodule 0:2ee293544fc1 99 /******************************************************************************
displaymodule 0:2ee293544fc1 100 * Global variables
displaymodule 0:2ee293544fc1 101 *****************************************************************************/
displaymodule 0:2ee293544fc1 102
displaymodule 0:2ee293544fc1 103
displaymodule 0:2ee293544fc1 104 /******************************************************************************
displaymodule 0:2ee293544fc1 105 * Main
displaymodule 0:2ee293544fc1 106 *****************************************************************************/
displaymodule 0:2ee293544fc1 107
displaymodule 0:2ee293544fc1 108 int main() {
displaymodule 0:2ee293544fc1 109 uint16_t x, y, lastX, lastY;
displaymodule 0:2ee293544fc1 110 bool penDown;
displaymodule 0:2ee293544fc1 111 paintMode_t mode = Idle;
displaymodule 0:2ee293544fc1 112 uint16_t color = WHITE;
displaymodule 0:2ee293544fc1 113 int activeIdx = 4;
displaymodule 0:2ee293544fc1 114 int selIdx = 0;
displaymodule 0:2ee293544fc1 115
displaymodule 0:2ee293544fc1 116 colorButton_t buttons[] = {
displaymodule 0:2ee293544fc1 117 { RED, new Canvas(MARGIN, MARGIN, BOX_W, BOX_H, BLACK, RED, 3, GRAY1) },
displaymodule 0:2ee293544fc1 118 { GREEN, new Canvas(MARGIN+(BOX_W + MARGIN), MARGIN, BOX_W, BOX_H, BLACK, GREEN, 3, GRAY1) },
displaymodule 0:2ee293544fc1 119 { BLUE, new Canvas(MARGIN+(BOX_W + MARGIN)*2, MARGIN, BOX_W, BOX_H, BLACK, BLUE, 3, GRAY1) },
displaymodule 0:2ee293544fc1 120 { YELLOW, new Canvas(MARGIN+(BOX_W + MARGIN)*3, MARGIN, BOX_W, BOX_H, BLACK, YELLOW, 3, GRAY1) },
displaymodule 0:2ee293544fc1 121 { WHITE, new Canvas(MARGIN+(BOX_W + MARGIN)*4, MARGIN, BOX_W, BOX_H, BLACK, WHITE, 3, GRAY1) },
displaymodule 0:2ee293544fc1 122 };
displaymodule 0:2ee293544fc1 123
displaymodule 0:2ee293544fc1 124 tft.init();
displaymodule 0:2ee293544fc1 125
displaymodule 0:2ee293544fc1 126 for (int i = 0; i < NUM_BUTTONS; i++) {
displaymodule 0:2ee293544fc1 127 if (color != buttons[i].color) {
displaymodule 0:2ee293544fc1 128 buttons[i].canvas->enableBorder(false);
displaymodule 0:2ee293544fc1 129 }
displaymodule 0:2ee293544fc1 130 buttons[i].canvas->draw(&tft);
displaymodule 0:2ee293544fc1 131 }
displaymodule 0:2ee293544fc1 132
displaymodule 0:2ee293544fc1 133 Canvas drawArea(MARGIN, MARGIN+(BOX_H + MARGIN), tft.width(), tft.height() - MARGIN+(BOX_H + MARGIN));
displaymodule 0:2ee293544fc1 134 Canvas clearButton(tft.width() - 50 - MARGIN, MARGIN, 50, BOX_H, WHITE, BLACK, 1, WHITE);
displaymodule 0:2ee293544fc1 135
displaymodule 0:2ee293544fc1 136 drawArea.draw(&tft);
displaymodule 0:2ee293544fc1 137 clearButton.draw(&tft);
displaymodule 0:2ee293544fc1 138 clearButton.drawString(&tft, "clr");
displaymodule 0:2ee293544fc1 139
displaymodule 0:2ee293544fc1 140 touch.init();
displaymodule 0:2ee293544fc1 141
displaymodule 0:2ee293544fc1 142 lastX = lastY = 0;
displaymodule 0:2ee293544fc1 143 while(true)
displaymodule 0:2ee293544fc1 144 {
displaymodule 0:2ee293544fc1 145 touch.readTouchData(x, y, penDown);
displaymodule 0:2ee293544fc1 146 switch (mode)
displaymodule 0:2ee293544fc1 147 {
displaymodule 0:2ee293544fc1 148 case Idle:
displaymodule 0:2ee293544fc1 149 if (penDown)
displaymodule 0:2ee293544fc1 150 {
displaymodule 0:2ee293544fc1 151 if (drawArea.isInside(x, y))
displaymodule 0:2ee293544fc1 152 {
displaymodule 0:2ee293544fc1 153 tft.drawPoint(x, y, POINT_SIZE);
displaymodule 0:2ee293544fc1 154 mode = Drawing;
displaymodule 0:2ee293544fc1 155 log("start draw\n");
displaymodule 0:2ee293544fc1 156 } else if (clearButton.isInside(x, y)) {
displaymodule 0:2ee293544fc1 157 log("Pressed clear!\n");
displaymodule 0:2ee293544fc1 158 drawArea.draw(&tft);
displaymodule 0:2ee293544fc1 159 } else {
displaymodule 0:2ee293544fc1 160 for (int i = 0; i < NUM_BUTTONS; i++) {
displaymodule 0:2ee293544fc1 161 if (buttons[i].canvas->isInside(x, y)) {
displaymodule 0:2ee293544fc1 162 mode = SelectingColor;
displaymodule 0:2ee293544fc1 163 selIdx = i;
displaymodule 0:2ee293544fc1 164 log("Pen down on button %d\n", i);
displaymodule 0:2ee293544fc1 165 }
displaymodule 0:2ee293544fc1 166 }
displaymodule 0:2ee293544fc1 167 }
displaymodule 0:2ee293544fc1 168 }
displaymodule 0:2ee293544fc1 169 break;
displaymodule 0:2ee293544fc1 170
displaymodule 0:2ee293544fc1 171 case SelectingColor:
displaymodule 0:2ee293544fc1 172 if (!penDown)
displaymodule 0:2ee293544fc1 173 {
displaymodule 0:2ee293544fc1 174 if (buttons[selIdx].canvas->isInside(lastX, lastY)) {
displaymodule 0:2ee293544fc1 175 buttons[activeIdx].canvas->enableBorder(false);
displaymodule 0:2ee293544fc1 176 buttons[activeIdx].canvas->draw(&tft);
displaymodule 0:2ee293544fc1 177
displaymodule 0:2ee293544fc1 178 log("Pen up => actual click on color %d!\n", selIdx);
displaymodule 0:2ee293544fc1 179
displaymodule 0:2ee293544fc1 180 color = buttons[selIdx].color;
displaymodule 0:2ee293544fc1 181 tft.setTextColor(BLACK, color);
displaymodule 0:2ee293544fc1 182
displaymodule 0:2ee293544fc1 183 activeIdx = selIdx;
displaymodule 0:2ee293544fc1 184 buttons[activeIdx].canvas->enableBorder(true);
displaymodule 0:2ee293544fc1 185 buttons[activeIdx].canvas->draw(&tft);
displaymodule 0:2ee293544fc1 186 }
displaymodule 0:2ee293544fc1 187
displaymodule 0:2ee293544fc1 188 mode = Idle;
displaymodule 0:2ee293544fc1 189 }
displaymodule 0:2ee293544fc1 190 break;
displaymodule 0:2ee293544fc1 191
displaymodule 0:2ee293544fc1 192 case Drawing:
displaymodule 0:2ee293544fc1 193 if (penDown && drawArea.isInside(x, y)) {
displaymodule 0:2ee293544fc1 194 tft.drawPoint(x, y, POINT_SIZE);
displaymodule 0:2ee293544fc1 195 log("painting\n");
displaymodule 0:2ee293544fc1 196 } else if (!penDown) {
displaymodule 0:2ee293544fc1 197 mode = Idle;
displaymodule 0:2ee293544fc1 198 log("end draw\n");
displaymodule 0:2ee293544fc1 199 }
displaymodule 0:2ee293544fc1 200 break;
displaymodule 0:2ee293544fc1 201 }
displaymodule 0:2ee293544fc1 202 lastX = x;
displaymodule 0:2ee293544fc1 203 lastY = y;
displaymodule 0:2ee293544fc1 204 wait(0.002);
displaymodule 0:2ee293544fc1 205 }
displaymodule 0:2ee293544fc1 206 }