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

Dependencies:   DmTftLibrary mbed

Committer:
displaymodule
Date:
Fri Jul 04 10:35:27 2014 +0000
Revision:
1:c5ef2d3261d3
Parent:
0:2ee293544fc1
Child:
2:de14389741d5
Misc bugfixes

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 1:c5ef2d3261d3 58 #if 1
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 0:2ee293544fc1 81 //DmTftHX8353C tft; /* DM_TFT18_101 */
displaymodule 0:2ee293544fc1 82 //DmTftS6D0164 tft; /* DM_TFT22_102 */
displaymodule 0:2ee293544fc1 83 //DmTftIli9325 tft; /* DM_TFT28_103 and DM_TFT24_104 */
displaymodule 1:c5ef2d3261d3 84 //DmTftIli9341 tft; /* DM_TFT28_105 */
displaymodule 1:c5ef2d3261d3 85 DmTftSsd2119 tft; /* DM_TFT35_107 */
displaymodule 0:2ee293544fc1 86
displaymodule 1:c5ef2d3261d3 87 //DmTouch touch(DmTouch::DM_TFT28_103, DmTouch::Software); /* For LPC4088 QuickStart Board */
displaymodule 0:2ee293544fc1 88 //DmTouch touch(DmTouch::DM_TFT28_103);
displaymodule 1:c5ef2d3261d3 89 //DmTouch touch(DmTouch::DM_TFT24_104, DmTouch::Software); /* For LPC4088 QuickStart Board */
displaymodule 0:2ee293544fc1 90 //DmTouch touch(DmTouch::DM_TFT24_104);
displaymodule 1:c5ef2d3261d3 91 //DmTouch touch(DmTouch::DM_TFT28_105);
displaymodule 1:c5ef2d3261d3 92 DmTouch touch(DmTouch::DM_TFT35_107);
displaymodule 0:2ee293544fc1 93
displaymodule 0:2ee293544fc1 94 DigitalInOut csTouch(DM_PIN_CS_TOUCH, PIN_OUTPUT, PullUp, 1);
displaymodule 0:2ee293544fc1 95 DigitalInOut csDisplay(DM_PIN_CS_TFT, PIN_OUTPUT, PullUp, 1);
displaymodule 0:2ee293544fc1 96 DigitalInOut csSDCard(DM_PIN_CS_SDCARD, PIN_OUTPUT, PullUp, 1);
displaymodule 0:2ee293544fc1 97 #ifdef DM_PIN_CS_FLASH
displaymodule 0:2ee293544fc1 98 DigitalInOut csFlash(DM_PIN_CS_FLASH, PIN_OUTPUT, PullUp, 1);
displaymodule 0:2ee293544fc1 99 #endif
displaymodule 0:2ee293544fc1 100
displaymodule 0:2ee293544fc1 101 /******************************************************************************
displaymodule 0:2ee293544fc1 102 * Global variables
displaymodule 0:2ee293544fc1 103 *****************************************************************************/
displaymodule 0:2ee293544fc1 104
displaymodule 0:2ee293544fc1 105
displaymodule 0:2ee293544fc1 106 /******************************************************************************
displaymodule 0:2ee293544fc1 107 * Main
displaymodule 0:2ee293544fc1 108 *****************************************************************************/
displaymodule 0:2ee293544fc1 109
displaymodule 0:2ee293544fc1 110 int main() {
displaymodule 0:2ee293544fc1 111 uint16_t x, y, lastX, lastY;
displaymodule 0:2ee293544fc1 112 bool penDown;
displaymodule 0:2ee293544fc1 113 paintMode_t mode = Idle;
displaymodule 0:2ee293544fc1 114 uint16_t color = WHITE;
displaymodule 0:2ee293544fc1 115 int activeIdx = 4;
displaymodule 0:2ee293544fc1 116 int selIdx = 0;
displaymodule 0:2ee293544fc1 117
displaymodule 0:2ee293544fc1 118 colorButton_t buttons[] = {
displaymodule 0:2ee293544fc1 119 { RED, new Canvas(MARGIN, MARGIN, BOX_W, BOX_H, BLACK, RED, 3, GRAY1) },
displaymodule 0:2ee293544fc1 120 { GREEN, new Canvas(MARGIN+(BOX_W + MARGIN), MARGIN, BOX_W, BOX_H, BLACK, GREEN, 3, GRAY1) },
displaymodule 0:2ee293544fc1 121 { BLUE, new Canvas(MARGIN+(BOX_W + MARGIN)*2, MARGIN, BOX_W, BOX_H, BLACK, BLUE, 3, GRAY1) },
displaymodule 0:2ee293544fc1 122 { YELLOW, new Canvas(MARGIN+(BOX_W + MARGIN)*3, MARGIN, BOX_W, BOX_H, BLACK, YELLOW, 3, GRAY1) },
displaymodule 0:2ee293544fc1 123 { WHITE, new Canvas(MARGIN+(BOX_W + MARGIN)*4, MARGIN, BOX_W, BOX_H, BLACK, WHITE, 3, GRAY1) },
displaymodule 0:2ee293544fc1 124 };
displaymodule 0:2ee293544fc1 125
displaymodule 0:2ee293544fc1 126 tft.init();
displaymodule 0:2ee293544fc1 127
displaymodule 0:2ee293544fc1 128 for (int i = 0; i < NUM_BUTTONS; i++) {
displaymodule 0:2ee293544fc1 129 if (color != buttons[i].color) {
displaymodule 0:2ee293544fc1 130 buttons[i].canvas->enableBorder(false);
displaymodule 0:2ee293544fc1 131 }
displaymodule 0:2ee293544fc1 132 buttons[i].canvas->draw(&tft);
displaymodule 0:2ee293544fc1 133 }
displaymodule 0:2ee293544fc1 134
displaymodule 0:2ee293544fc1 135 Canvas drawArea(MARGIN, MARGIN+(BOX_H + MARGIN), tft.width(), tft.height() - MARGIN+(BOX_H + MARGIN));
displaymodule 0:2ee293544fc1 136 Canvas clearButton(tft.width() - 50 - MARGIN, MARGIN, 50, BOX_H, WHITE, BLACK, 1, WHITE);
displaymodule 0:2ee293544fc1 137
displaymodule 0:2ee293544fc1 138 drawArea.draw(&tft);
displaymodule 0:2ee293544fc1 139 clearButton.draw(&tft);
displaymodule 0:2ee293544fc1 140 clearButton.drawString(&tft, "clr");
displaymodule 0:2ee293544fc1 141
displaymodule 0:2ee293544fc1 142 touch.init();
displaymodule 0:2ee293544fc1 143
displaymodule 0:2ee293544fc1 144 lastX = lastY = 0;
displaymodule 0:2ee293544fc1 145 while(true)
displaymodule 0:2ee293544fc1 146 {
displaymodule 0:2ee293544fc1 147 touch.readTouchData(x, y, penDown);
displaymodule 0:2ee293544fc1 148 switch (mode)
displaymodule 0:2ee293544fc1 149 {
displaymodule 0:2ee293544fc1 150 case Idle:
displaymodule 0:2ee293544fc1 151 if (penDown)
displaymodule 0:2ee293544fc1 152 {
displaymodule 0:2ee293544fc1 153 if (drawArea.isInside(x, y))
displaymodule 0:2ee293544fc1 154 {
displaymodule 0:2ee293544fc1 155 tft.drawPoint(x, y, POINT_SIZE);
displaymodule 0:2ee293544fc1 156 mode = Drawing;
displaymodule 0:2ee293544fc1 157 log("start draw\n");
displaymodule 0:2ee293544fc1 158 } else if (clearButton.isInside(x, y)) {
displaymodule 0:2ee293544fc1 159 log("Pressed clear!\n");
displaymodule 0:2ee293544fc1 160 drawArea.draw(&tft);
displaymodule 0:2ee293544fc1 161 } else {
displaymodule 0:2ee293544fc1 162 for (int i = 0; i < NUM_BUTTONS; i++) {
displaymodule 0:2ee293544fc1 163 if (buttons[i].canvas->isInside(x, y)) {
displaymodule 0:2ee293544fc1 164 mode = SelectingColor;
displaymodule 0:2ee293544fc1 165 selIdx = i;
displaymodule 0:2ee293544fc1 166 log("Pen down on button %d\n", i);
displaymodule 0:2ee293544fc1 167 }
displaymodule 0:2ee293544fc1 168 }
displaymodule 0:2ee293544fc1 169 }
displaymodule 0:2ee293544fc1 170 }
displaymodule 0:2ee293544fc1 171 break;
displaymodule 0:2ee293544fc1 172
displaymodule 0:2ee293544fc1 173 case SelectingColor:
displaymodule 0:2ee293544fc1 174 if (!penDown)
displaymodule 0:2ee293544fc1 175 {
displaymodule 0:2ee293544fc1 176 if (buttons[selIdx].canvas->isInside(lastX, lastY)) {
displaymodule 0:2ee293544fc1 177 buttons[activeIdx].canvas->enableBorder(false);
displaymodule 0:2ee293544fc1 178 buttons[activeIdx].canvas->draw(&tft);
displaymodule 0:2ee293544fc1 179
displaymodule 0:2ee293544fc1 180 log("Pen up => actual click on color %d!\n", selIdx);
displaymodule 0:2ee293544fc1 181
displaymodule 0:2ee293544fc1 182 color = buttons[selIdx].color;
displaymodule 0:2ee293544fc1 183 tft.setTextColor(BLACK, color);
displaymodule 0:2ee293544fc1 184
displaymodule 0:2ee293544fc1 185 activeIdx = selIdx;
displaymodule 0:2ee293544fc1 186 buttons[activeIdx].canvas->enableBorder(true);
displaymodule 0:2ee293544fc1 187 buttons[activeIdx].canvas->draw(&tft);
displaymodule 0:2ee293544fc1 188 }
displaymodule 0:2ee293544fc1 189
displaymodule 0:2ee293544fc1 190 mode = Idle;
displaymodule 0:2ee293544fc1 191 }
displaymodule 0:2ee293544fc1 192 break;
displaymodule 0:2ee293544fc1 193
displaymodule 0:2ee293544fc1 194 case Drawing:
displaymodule 0:2ee293544fc1 195 if (penDown && drawArea.isInside(x, y)) {
displaymodule 0:2ee293544fc1 196 tft.drawPoint(x, y, POINT_SIZE);
displaymodule 0:2ee293544fc1 197 log("painting\n");
displaymodule 0:2ee293544fc1 198 } else if (!penDown) {
displaymodule 0:2ee293544fc1 199 mode = Idle;
displaymodule 0:2ee293544fc1 200 log("end draw\n");
displaymodule 0:2ee293544fc1 201 }
displaymodule 0:2ee293544fc1 202 break;
displaymodule 0:2ee293544fc1 203 }
displaymodule 0:2ee293544fc1 204 lastX = x;
displaymodule 0:2ee293544fc1 205 lastY = y;
displaymodule 0:2ee293544fc1 206 wait(0.002);
displaymodule 0:2ee293544fc1 207 }
displaymodule 0:2ee293544fc1 208 }