Shows how to use a display and the touch controller. A very basic paint program.
Dependencies: DmTftLibrary mbed
main.cpp@0:2ee293544fc1, 2014-05-20 (annotated)
- Committer:
- displaymodule
- Date:
- Tue May 20 15:39:09 2014 +0000
- Revision:
- 0:2ee293544fc1
- Child:
- 1:c5ef2d3261d3
First version
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:2ee293544fc1 | 43 | #define log(...) |
displaymodule | 0:2ee293544fc1 | 44 | //#define log(...) printf(__VA_ARGS__) |
displaymodule | 0:2ee293544fc1 | 45 | |
displaymodule | 0:2ee293544fc1 | 46 | #define MARGIN 5 |
displaymodule | 0:2ee293544fc1 | 47 | #define BOX_W 30 |
displaymodule | 0:2ee293544fc1 | 48 | #define BOX_H 30 |
displaymodule | 0:2ee293544fc1 | 49 | #define POINT_SIZE 2 |
displaymodule | 0:2ee293544fc1 | 50 | |
displaymodule | 0:2ee293544fc1 | 51 | #define NUM_BUTTONS (sizeof(buttons)/sizeof(buttons[0])) |
displaymodule | 0:2ee293544fc1 | 52 | |
displaymodule | 0:2ee293544fc1 | 53 | #if 1 /* Displays without adapter */ |
displaymodule | 0:2ee293544fc1 | 54 | #define DM_PIN_SPI_MOSI D11 |
displaymodule | 0:2ee293544fc1 | 55 | #define DM_PIN_SPI_MISO D12 |
displaymodule | 0:2ee293544fc1 | 56 | #define DM_PIN_SPI_SCLK D13 |
displaymodule | 0:2ee293544fc1 | 57 | |
displaymodule | 0:2ee293544fc1 | 58 | #define DM_PIN_CS_TOUCH D4 |
displaymodule | 0:2ee293544fc1 | 59 | #define DM_PIN_CS_TFT D10 |
displaymodule | 0:2ee293544fc1 | 60 | #define DM_PIN_CS_SDCARD D8 |
displaymodule | 0:2ee293544fc1 | 61 | #define DM_PIN_CS_FLASH D6 |
displaymodule | 0:2ee293544fc1 | 62 | #else /* Displays with adapter */ |
displaymodule | 0:2ee293544fc1 | 63 | #define DM_PIN_SPI_MOSI A0 |
displaymodule | 0:2ee293544fc1 | 64 | #define DM_PIN_SPI_MISO D9 |
displaymodule | 0:2ee293544fc1 | 65 | #define DM_PIN_SPI_SCLK A1 |
displaymodule | 0:2ee293544fc1 | 66 | |
displaymodule | 0:2ee293544fc1 | 67 | #define DM_PIN_CS_TOUCH D8 |
displaymodule | 0:2ee293544fc1 | 68 | #define DM_PIN_CS_TFT A3 |
displaymodule | 0:2ee293544fc1 | 69 | #define DM_PIN_CS_SDCARD D10 |
displaymodule | 0:2ee293544fc1 | 70 | #endif |
displaymodule | 0:2ee293544fc1 | 71 | |
displaymodule | 0:2ee293544fc1 | 72 | /****************************************************************************** |
displaymodule | 0:2ee293544fc1 | 73 | * Local variables |
displaymodule | 0:2ee293544fc1 | 74 | *****************************************************************************/ |
displaymodule | 0:2ee293544fc1 | 75 | |
displaymodule | 0:2ee293544fc1 | 76 | //DmTftHX8353C tft; /* DM_TFT18_101 */ |
displaymodule | 0:2ee293544fc1 | 77 | //DmTftS6D0164 tft; /* DM_TFT22_102 */ |
displaymodule | 0:2ee293544fc1 | 78 | //DmTftIli9325 tft; /* DM_TFT28_103 and DM_TFT24_104 */ |
displaymodule | 0:2ee293544fc1 | 79 | DmTftIli9341 tft; /* DM_TFT28_105 */ |
displaymodule | 0:2ee293544fc1 | 80 | //DmTftSsd2119 tft; /* DM_TFT35_107 */ |
displaymodule | 0:2ee293544fc1 | 81 | |
displaymodule | 0:2ee293544fc1 | 82 | //DmTouch touch(DmTouch::DM_TFT28_103, false); /* For LPC4088 QuickStart Board */ |
displaymodule | 0:2ee293544fc1 | 83 | //DmTouch touch(DmTouch::DM_TFT28_103); |
displaymodule | 0:2ee293544fc1 | 84 | //DmTouch touch(DmTouch::DM_TFT24_104, false); /* For LPC4088 QuickStart Board */ |
displaymodule | 0:2ee293544fc1 | 85 | //DmTouch touch(DmTouch::DM_TFT24_104); |
displaymodule | 0:2ee293544fc1 | 86 | DmTouch touch(DmTouch::DM_TFT28_105); |
displaymodule | 0:2ee293544fc1 | 87 | //DmTouch touch(DmTouch::DM_TFT35_107); |
displaymodule | 0:2ee293544fc1 | 88 | |
displaymodule | 0:2ee293544fc1 | 89 | DigitalInOut csTouch(DM_PIN_CS_TOUCH, PIN_OUTPUT, PullUp, 1); |
displaymodule | 0:2ee293544fc1 | 90 | DigitalInOut csDisplay(DM_PIN_CS_TFT, PIN_OUTPUT, PullUp, 1); |
displaymodule | 0:2ee293544fc1 | 91 | DigitalInOut csSDCard(DM_PIN_CS_SDCARD, PIN_OUTPUT, PullUp, 1); |
displaymodule | 0:2ee293544fc1 | 92 | #ifdef DM_PIN_CS_FLASH |
displaymodule | 0:2ee293544fc1 | 93 | DigitalInOut csFlash(DM_PIN_CS_FLASH, PIN_OUTPUT, PullUp, 1); |
displaymodule | 0:2ee293544fc1 | 94 | #endif |
displaymodule | 0:2ee293544fc1 | 95 | |
displaymodule | 0:2ee293544fc1 | 96 | /****************************************************************************** |
displaymodule | 0:2ee293544fc1 | 97 | * Global variables |
displaymodule | 0:2ee293544fc1 | 98 | *****************************************************************************/ |
displaymodule | 0:2ee293544fc1 | 99 | |
displaymodule | 0:2ee293544fc1 | 100 | |
displaymodule | 0:2ee293544fc1 | 101 | /****************************************************************************** |
displaymodule | 0:2ee293544fc1 | 102 | * Main |
displaymodule | 0:2ee293544fc1 | 103 | *****************************************************************************/ |
displaymodule | 0:2ee293544fc1 | 104 | |
displaymodule | 0:2ee293544fc1 | 105 | int main() { |
displaymodule | 0:2ee293544fc1 | 106 | uint16_t x, y, lastX, lastY; |
displaymodule | 0:2ee293544fc1 | 107 | bool penDown; |
displaymodule | 0:2ee293544fc1 | 108 | paintMode_t mode = Idle; |
displaymodule | 0:2ee293544fc1 | 109 | uint16_t color = WHITE; |
displaymodule | 0:2ee293544fc1 | 110 | int activeIdx = 4; |
displaymodule | 0:2ee293544fc1 | 111 | int selIdx = 0; |
displaymodule | 0:2ee293544fc1 | 112 | |
displaymodule | 0:2ee293544fc1 | 113 | colorButton_t buttons[] = { |
displaymodule | 0:2ee293544fc1 | 114 | { RED, new Canvas(MARGIN, MARGIN, BOX_W, BOX_H, BLACK, RED, 3, GRAY1) }, |
displaymodule | 0:2ee293544fc1 | 115 | { GREEN, new Canvas(MARGIN+(BOX_W + MARGIN), MARGIN, BOX_W, BOX_H, BLACK, GREEN, 3, GRAY1) }, |
displaymodule | 0:2ee293544fc1 | 116 | { BLUE, new Canvas(MARGIN+(BOX_W + MARGIN)*2, MARGIN, BOX_W, BOX_H, BLACK, BLUE, 3, GRAY1) }, |
displaymodule | 0:2ee293544fc1 | 117 | { YELLOW, new Canvas(MARGIN+(BOX_W + MARGIN)*3, MARGIN, BOX_W, BOX_H, BLACK, YELLOW, 3, GRAY1) }, |
displaymodule | 0:2ee293544fc1 | 118 | { WHITE, new Canvas(MARGIN+(BOX_W + MARGIN)*4, MARGIN, BOX_W, BOX_H, BLACK, WHITE, 3, GRAY1) }, |
displaymodule | 0:2ee293544fc1 | 119 | }; |
displaymodule | 0:2ee293544fc1 | 120 | |
displaymodule | 0:2ee293544fc1 | 121 | tft.init(); |
displaymodule | 0:2ee293544fc1 | 122 | |
displaymodule | 0:2ee293544fc1 | 123 | for (int i = 0; i < NUM_BUTTONS; i++) { |
displaymodule | 0:2ee293544fc1 | 124 | if (color != buttons[i].color) { |
displaymodule | 0:2ee293544fc1 | 125 | buttons[i].canvas->enableBorder(false); |
displaymodule | 0:2ee293544fc1 | 126 | } |
displaymodule | 0:2ee293544fc1 | 127 | buttons[i].canvas->draw(&tft); |
displaymodule | 0:2ee293544fc1 | 128 | } |
displaymodule | 0:2ee293544fc1 | 129 | |
displaymodule | 0:2ee293544fc1 | 130 | Canvas drawArea(MARGIN, MARGIN+(BOX_H + MARGIN), tft.width(), tft.height() - MARGIN+(BOX_H + MARGIN)); |
displaymodule | 0:2ee293544fc1 | 131 | Canvas clearButton(tft.width() - 50 - MARGIN, MARGIN, 50, BOX_H, WHITE, BLACK, 1, WHITE); |
displaymodule | 0:2ee293544fc1 | 132 | |
displaymodule | 0:2ee293544fc1 | 133 | drawArea.draw(&tft); |
displaymodule | 0:2ee293544fc1 | 134 | clearButton.draw(&tft); |
displaymodule | 0:2ee293544fc1 | 135 | clearButton.drawString(&tft, "clr"); |
displaymodule | 0:2ee293544fc1 | 136 | |
displaymodule | 0:2ee293544fc1 | 137 | touch.init(); |
displaymodule | 0:2ee293544fc1 | 138 | |
displaymodule | 0:2ee293544fc1 | 139 | lastX = lastY = 0; |
displaymodule | 0:2ee293544fc1 | 140 | while(true) |
displaymodule | 0:2ee293544fc1 | 141 | { |
displaymodule | 0:2ee293544fc1 | 142 | touch.readTouchData(x, y, penDown); |
displaymodule | 0:2ee293544fc1 | 143 | switch (mode) |
displaymodule | 0:2ee293544fc1 | 144 | { |
displaymodule | 0:2ee293544fc1 | 145 | case Idle: |
displaymodule | 0:2ee293544fc1 | 146 | if (penDown) |
displaymodule | 0:2ee293544fc1 | 147 | { |
displaymodule | 0:2ee293544fc1 | 148 | if (drawArea.isInside(x, y)) |
displaymodule | 0:2ee293544fc1 | 149 | { |
displaymodule | 0:2ee293544fc1 | 150 | tft.drawPoint(x, y, POINT_SIZE); |
displaymodule | 0:2ee293544fc1 | 151 | mode = Drawing; |
displaymodule | 0:2ee293544fc1 | 152 | log("start draw\n"); |
displaymodule | 0:2ee293544fc1 | 153 | } else if (clearButton.isInside(x, y)) { |
displaymodule | 0:2ee293544fc1 | 154 | log("Pressed clear!\n"); |
displaymodule | 0:2ee293544fc1 | 155 | drawArea.draw(&tft); |
displaymodule | 0:2ee293544fc1 | 156 | } else { |
displaymodule | 0:2ee293544fc1 | 157 | for (int i = 0; i < NUM_BUTTONS; i++) { |
displaymodule | 0:2ee293544fc1 | 158 | if (buttons[i].canvas->isInside(x, y)) { |
displaymodule | 0:2ee293544fc1 | 159 | mode = SelectingColor; |
displaymodule | 0:2ee293544fc1 | 160 | selIdx = i; |
displaymodule | 0:2ee293544fc1 | 161 | log("Pen down on button %d\n", i); |
displaymodule | 0:2ee293544fc1 | 162 | } |
displaymodule | 0:2ee293544fc1 | 163 | } |
displaymodule | 0:2ee293544fc1 | 164 | } |
displaymodule | 0:2ee293544fc1 | 165 | } |
displaymodule | 0:2ee293544fc1 | 166 | break; |
displaymodule | 0:2ee293544fc1 | 167 | |
displaymodule | 0:2ee293544fc1 | 168 | case SelectingColor: |
displaymodule | 0:2ee293544fc1 | 169 | if (!penDown) |
displaymodule | 0:2ee293544fc1 | 170 | { |
displaymodule | 0:2ee293544fc1 | 171 | if (buttons[selIdx].canvas->isInside(lastX, lastY)) { |
displaymodule | 0:2ee293544fc1 | 172 | buttons[activeIdx].canvas->enableBorder(false); |
displaymodule | 0:2ee293544fc1 | 173 | buttons[activeIdx].canvas->draw(&tft); |
displaymodule | 0:2ee293544fc1 | 174 | |
displaymodule | 0:2ee293544fc1 | 175 | log("Pen up => actual click on color %d!\n", selIdx); |
displaymodule | 0:2ee293544fc1 | 176 | |
displaymodule | 0:2ee293544fc1 | 177 | color = buttons[selIdx].color; |
displaymodule | 0:2ee293544fc1 | 178 | tft.setTextColor(BLACK, color); |
displaymodule | 0:2ee293544fc1 | 179 | |
displaymodule | 0:2ee293544fc1 | 180 | activeIdx = selIdx; |
displaymodule | 0:2ee293544fc1 | 181 | buttons[activeIdx].canvas->enableBorder(true); |
displaymodule | 0:2ee293544fc1 | 182 | buttons[activeIdx].canvas->draw(&tft); |
displaymodule | 0:2ee293544fc1 | 183 | } |
displaymodule | 0:2ee293544fc1 | 184 | |
displaymodule | 0:2ee293544fc1 | 185 | mode = Idle; |
displaymodule | 0:2ee293544fc1 | 186 | } |
displaymodule | 0:2ee293544fc1 | 187 | break; |
displaymodule | 0:2ee293544fc1 | 188 | |
displaymodule | 0:2ee293544fc1 | 189 | case Drawing: |
displaymodule | 0:2ee293544fc1 | 190 | if (penDown && drawArea.isInside(x, y)) { |
displaymodule | 0:2ee293544fc1 | 191 | tft.drawPoint(x, y, POINT_SIZE); |
displaymodule | 0:2ee293544fc1 | 192 | log("painting\n"); |
displaymodule | 0:2ee293544fc1 | 193 | } else if (!penDown) { |
displaymodule | 0:2ee293544fc1 | 194 | mode = Idle; |
displaymodule | 0:2ee293544fc1 | 195 | log("end draw\n"); |
displaymodule | 0:2ee293544fc1 | 196 | } |
displaymodule | 0:2ee293544fc1 | 197 | break; |
displaymodule | 0:2ee293544fc1 | 198 | } |
displaymodule | 0:2ee293544fc1 | 199 | lastX = x; |
displaymodule | 0:2ee293544fc1 | 200 | lastY = y; |
displaymodule | 0:2ee293544fc1 | 201 | wait(0.002); |
displaymodule | 0:2ee293544fc1 | 202 | } |
displaymodule | 0:2ee293544fc1 | 203 | } |