Basic calculator example to show the LPC4088 Experiment Base Board with a touch LCD from DisplayModule

Dependencies:   DmTftLibrary mbed

Fork of dm_calc by Display Module

Committer:
displaymodule
Date:
Fri Jul 04 10:33:40 2014 +0000
Revision:
1:9a3ae682a75e
Parent:
0:3713b01e72fe
Child:
2:b17b19212e5a
Misc bugfixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
displaymodule 0:3713b01e72fe 1 /**********************************************************************************************
displaymodule 0:3713b01e72fe 2 Copyright (c) 2014 DisplayModule. All rights reserved.
displaymodule 0:3713b01e72fe 3
displaymodule 0:3713b01e72fe 4 Redistribution and use of this source code, part of this source code or any compiled binary
displaymodule 0:3713b01e72fe 5 based on this source code is permitted as long as the above copyright notice and following
displaymodule 0:3713b01e72fe 6 disclaimer is retained.
displaymodule 0:3713b01e72fe 7
displaymodule 0:3713b01e72fe 8 DISCLAIMER:
displaymodule 0:3713b01e72fe 9 THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
displaymodule 0:3713b01e72fe 10 NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
displaymodule 0:3713b01e72fe 11 ********************************************************************************************/
displaymodule 0:3713b01e72fe 12
displaymodule 0:3713b01e72fe 13 /******************************************************************************
displaymodule 0:3713b01e72fe 14 * Includes
displaymodule 0:3713b01e72fe 15 *****************************************************************************/
displaymodule 0:3713b01e72fe 16
displaymodule 0:3713b01e72fe 17 #include "mbed.h"
displaymodule 0:3713b01e72fe 18
displaymodule 0:3713b01e72fe 19 #include "DmTftHX8353C.h"
displaymodule 0:3713b01e72fe 20 #include "DmTftS6D0164.h"
displaymodule 0:3713b01e72fe 21 #include "DmTftIli9325.h"
displaymodule 0:3713b01e72fe 22 #include "DmTftIli9341.h"
displaymodule 0:3713b01e72fe 23 #include "DmTftSsd2119.h"
displaymodule 0:3713b01e72fe 24 #include "DmTouch.h"
displaymodule 0:3713b01e72fe 25
displaymodule 0:3713b01e72fe 26 #include "Button.h"
displaymodule 0:3713b01e72fe 27
displaymodule 0:3713b01e72fe 28 /******************************************************************************
displaymodule 0:3713b01e72fe 29 * Typedefs and defines
displaymodule 0:3713b01e72fe 30 *****************************************************************************/
displaymodule 0:3713b01e72fe 31
displaymodule 0:3713b01e72fe 32 typedef enum {
displaymodule 0:3713b01e72fe 33 MODE_WANT_ARG1,
displaymodule 0:3713b01e72fe 34 MODE_WANT_ARG1_OR_OP,
displaymodule 0:3713b01e72fe 35 MODE_WANT_ARG2,
displaymodule 0:3713b01e72fe 36 MODE_WANT_ARG2_OR_OP,
displaymodule 0:3713b01e72fe 37 } calc_mode_t;
displaymodule 0:3713b01e72fe 38
displaymodule 0:3713b01e72fe 39 #define MARGIN 5
displaymodule 0:3713b01e72fe 40
displaymodule 0:3713b01e72fe 41 #define RESULT_MARGIN_X (MARGIN*3)
displaymodule 0:3713b01e72fe 42 #define RESULT_MARGIN_Y (MARGIN*3)
displaymodule 0:3713b01e72fe 43
displaymodule 0:3713b01e72fe 44 #define NUM_BUTTONS (sizeof(captions)/sizeof(captions[0]))
displaymodule 0:3713b01e72fe 45
displaymodule 1:9a3ae682a75e 46 #if 1
displaymodule 1:9a3ae682a75e 47 /* Displays without adapter */
displaymodule 1:9a3ae682a75e 48 #define DM_PIN_SPI_MOSI D11
displaymodule 1:9a3ae682a75e 49 #define DM_PIN_SPI_MISO D12
displaymodule 1:9a3ae682a75e 50 #define DM_PIN_SPI_SCLK D13
displaymodule 1:9a3ae682a75e 51 #define DM_PIN_CS_TOUCH D4
displaymodule 1:9a3ae682a75e 52 #define DM_PIN_CS_TFT D10
displaymodule 1:9a3ae682a75e 53 #define DM_PIN_CS_SDCARD D8
displaymodule 1:9a3ae682a75e 54 #define DM_PIN_CS_FLASH D6
displaymodule 1:9a3ae682a75e 55 #else
displaymodule 1:9a3ae682a75e 56 /* Displays with adapter */
displaymodule 1:9a3ae682a75e 57 #define DM_PIN_SPI_MOSI A0
displaymodule 1:9a3ae682a75e 58 #define DM_PIN_SPI_MISO D9
displaymodule 1:9a3ae682a75e 59 #define DM_PIN_SPI_SCLK A1
displaymodule 1:9a3ae682a75e 60 #define DM_PIN_CS_TOUCH D8
displaymodule 1:9a3ae682a75e 61 #define DM_PIN_CS_TFT A3
displaymodule 1:9a3ae682a75e 62 #define DM_PIN_CS_SDCARD D10
displaymodule 1:9a3ae682a75e 63 #endif
displaymodule 0:3713b01e72fe 64
displaymodule 1:9a3ae682a75e 65 //#define LANDSCAPE
displaymodule 0:3713b01e72fe 66
displaymodule 0:3713b01e72fe 67 /******************************************************************************
displaymodule 0:3713b01e72fe 68 * Local variables
displaymodule 0:3713b01e72fe 69 *****************************************************************************/
displaymodule 0:3713b01e72fe 70
displaymodule 0:3713b01e72fe 71 //DmTftHX8353C tft; /* DM_TFT18_101 */
displaymodule 0:3713b01e72fe 72 //DmTftS6D0164 tft; /* DM_TFT22_102 */
displaymodule 1:9a3ae682a75e 73 //DmTftIli9325 tft; /* DM_TFT28_103 and DM_TFT24_104 */
displaymodule 1:9a3ae682a75e 74 DmTftIli9341 tft; /* DM_TFT28_105 */
displaymodule 0:3713b01e72fe 75 //DmTftSsd2119 tft; /* DM_TFT35_107 */
displaymodule 0:3713b01e72fe 76
displaymodule 1:9a3ae682a75e 77 //DmTouch touch(DmTouch::DM_TFT28_103, DmTouch::Software); /* For LPC4088 QuickStart Board */
displaymodule 0:3713b01e72fe 78 //DmTouch touch(DmTouch::DM_TFT28_103);
displaymodule 1:9a3ae682a75e 79 //DmTouch touch(DmTouch::DM_TFT24_104, DmTouch::Software); /* For LPC4088 QuickStart Board */
displaymodule 1:9a3ae682a75e 80 //DmTouch touch(DmTouch::DM_TFT24_104);
displaymodule 1:9a3ae682a75e 81 DmTouch touch(DmTouch::DM_TFT28_105);
displaymodule 0:3713b01e72fe 82 //DmTouch touch(DmTouch::DM_TFT35_107);
displaymodule 0:3713b01e72fe 83
displaymodule 0:3713b01e72fe 84 DigitalInOut csTouch(DM_PIN_CS_TOUCH, PIN_OUTPUT, PullUp, 1);
displaymodule 0:3713b01e72fe 85 DigitalInOut csDisplay(DM_PIN_CS_TFT, PIN_OUTPUT, PullUp, 1);
displaymodule 0:3713b01e72fe 86 DigitalInOut csSDCard(DM_PIN_CS_SDCARD, PIN_OUTPUT, PullUp, 1);
displaymodule 0:3713b01e72fe 87 #ifdef DM_PIN_CS_FLASH
displaymodule 0:3713b01e72fe 88 DigitalInOut csFlash(DM_PIN_CS_FLASH, PIN_OUTPUT, PullUp, 1);
displaymodule 0:3713b01e72fe 89 #endif
displaymodule 0:3713b01e72fe 90
displaymodule 1:9a3ae682a75e 91 #ifdef LANDSCAPE
displaymodule 1:9a3ae682a75e 92 #define BUTTONS_PER_LINE 6
displaymodule 1:9a3ae682a75e 93 #define BUTTONS_NUM_LINES 3
displaymodule 1:9a3ae682a75e 94 /*
displaymodule 1:9a3ae682a75e 95 * 1 2 3 4 5 +
displaymodule 1:9a3ae682a75e 96 * 6 7 8 9 0 -
displaymodule 1:9a3ae682a75e 97 * * / = clr
displaymodule 1:9a3ae682a75e 98 */
displaymodule 1:9a3ae682a75e 99 const char* captions[] = {
displaymodule 1:9a3ae682a75e 100 "1","2","3","4","5","+",
displaymodule 1:9a3ae682a75e 101 "6","7","8","9","0","-",
displaymodule 1:9a3ae682a75e 102 "*","/","=","clr",
displaymodule 1:9a3ae682a75e 103 };
displaymodule 1:9a3ae682a75e 104 #else
displaymodule 1:9a3ae682a75e 105 #define BUTTONS_PER_LINE 4
displaymodule 1:9a3ae682a75e 106 #define BUTTONS_NUM_LINES 4
displaymodule 1:9a3ae682a75e 107 /*
displaymodule 1:9a3ae682a75e 108 * 1 2 3 +
displaymodule 1:9a3ae682a75e 109 * 4 5 6 -
displaymodule 1:9a3ae682a75e 110 * 7 8 9 *
displaymodule 1:9a3ae682a75e 111 * 0 = clr /
displaymodule 1:9a3ae682a75e 112 */
displaymodule 1:9a3ae682a75e 113 const char* captions[] = {
displaymodule 1:9a3ae682a75e 114 "1","2","3","+",
displaymodule 1:9a3ae682a75e 115 "4","5","6","-",
displaymodule 1:9a3ae682a75e 116 "7","8","9","*",
displaymodule 1:9a3ae682a75e 117 "0","=","clr","/",
displaymodule 1:9a3ae682a75e 118 };
displaymodule 1:9a3ae682a75e 119 #endif
displaymodule 1:9a3ae682a75e 120
displaymodule 0:3713b01e72fe 121 Button* buttons[NUM_BUTTONS];
displaymodule 0:3713b01e72fe 122
displaymodule 0:3713b01e72fe 123 static char buff[25] = {0};
displaymodule 0:3713b01e72fe 124 static bool redrawResult = true;
displaymodule 0:3713b01e72fe 125 static bool clearResult = true;
displaymodule 0:3713b01e72fe 126
displaymodule 0:3713b01e72fe 127 #ifdef DEBUG
displaymodule 0:3713b01e72fe 128 static char debug[25] = {0};
displaymodule 0:3713b01e72fe 129 static char debug2[25] = {0};
displaymodule 0:3713b01e72fe 130 static int debug_pos = 0;
displaymodule 0:3713b01e72fe 131 #endif
displaymodule 0:3713b01e72fe 132
displaymodule 0:3713b01e72fe 133 /******************************************************************************
displaymodule 0:3713b01e72fe 134 * Global variables
displaymodule 0:3713b01e72fe 135 *****************************************************************************/
displaymodule 0:3713b01e72fe 136
displaymodule 0:3713b01e72fe 137
displaymodule 0:3713b01e72fe 138 /******************************************************************************
displaymodule 0:3713b01e72fe 139 * Local functions
displaymodule 0:3713b01e72fe 140 *****************************************************************************/
displaymodule 0:3713b01e72fe 141
displaymodule 0:3713b01e72fe 142 void handleClick(uint32_t arg)
displaymodule 0:3713b01e72fe 143 {
displaymodule 0:3713b01e72fe 144 static int val1 = 0;
displaymodule 0:3713b01e72fe 145 static int val2 = 0;
displaymodule 0:3713b01e72fe 146 static int op = 0;
displaymodule 0:3713b01e72fe 147 static calc_mode_t mode = MODE_WANT_ARG1;
displaymodule 0:3713b01e72fe 148 static int strpos = 0;
displaymodule 0:3713b01e72fe 149
displaymodule 0:3713b01e72fe 150 #ifdef DEBUG
displaymodule 0:3713b01e72fe 151 debug2[debug_pos] = '0'+mode;
displaymodule 0:3713b01e72fe 152 debug[debug_pos] = arg;
displaymodule 0:3713b01e72fe 153 debug_pos++;
displaymodule 0:3713b01e72fe 154 debug[debug_pos] = '\0';
displaymodule 0:3713b01e72fe 155 debug_pos = debug_pos%25;
displaymodule 0:3713b01e72fe 156 #endif
displaymodule 0:3713b01e72fe 157 switch (arg)
displaymodule 0:3713b01e72fe 158 {
displaymodule 0:3713b01e72fe 159 case '0':
displaymodule 0:3713b01e72fe 160 case '1':
displaymodule 0:3713b01e72fe 161 case '2':
displaymodule 0:3713b01e72fe 162 case '3':
displaymodule 0:3713b01e72fe 163 case '4':
displaymodule 0:3713b01e72fe 164 case '5':
displaymodule 0:3713b01e72fe 165 case '6':
displaymodule 0:3713b01e72fe 166 case '7':
displaymodule 0:3713b01e72fe 167 case '8':
displaymodule 0:3713b01e72fe 168 case '9':
displaymodule 0:3713b01e72fe 169 if ((mode == MODE_WANT_ARG1) || (mode == MODE_WANT_ARG1_OR_OP)) {
displaymodule 0:3713b01e72fe 170 val1 = val1*10 + (arg - '0');
displaymodule 0:3713b01e72fe 171 mode = MODE_WANT_ARG1_OR_OP;
displaymodule 0:3713b01e72fe 172 buff[strpos++] = arg;
displaymodule 0:3713b01e72fe 173 } else if ((mode == MODE_WANT_ARG2) || (mode == MODE_WANT_ARG2_OR_OP)) {
displaymodule 0:3713b01e72fe 174 val2 = val2*10 + (arg - '0');
displaymodule 0:3713b01e72fe 175 mode = MODE_WANT_ARG2_OR_OP;
displaymodule 0:3713b01e72fe 176 buff[strpos++] = arg;
displaymodule 0:3713b01e72fe 177 }
displaymodule 0:3713b01e72fe 178 break;
displaymodule 0:3713b01e72fe 179 case '+':
displaymodule 0:3713b01e72fe 180 case '-':
displaymodule 0:3713b01e72fe 181 case '*':
displaymodule 0:3713b01e72fe 182 case '/':
displaymodule 0:3713b01e72fe 183 if (mode == MODE_WANT_ARG1_OR_OP) {
displaymodule 0:3713b01e72fe 184 op = arg;
displaymodule 0:3713b01e72fe 185 mode = MODE_WANT_ARG2;
displaymodule 0:3713b01e72fe 186 buff[strpos++] = arg;
displaymodule 0:3713b01e72fe 187 } else if (mode == MODE_WANT_ARG2_OR_OP) {
displaymodule 0:3713b01e72fe 188 // already have "a op b", calculate it and go to "c op"
displaymodule 0:3713b01e72fe 189 switch (op) {
displaymodule 0:3713b01e72fe 190 case '+': val1 = val1 + val2; break;
displaymodule 0:3713b01e72fe 191 case '-': val1 = val1 - val2; break;
displaymodule 0:3713b01e72fe 192 case '*': val1 = val1 * val2; break;
displaymodule 0:3713b01e72fe 193 case '/': val1 = val1 / val2; break;
displaymodule 0:3713b01e72fe 194 }
displaymodule 0:3713b01e72fe 195 op = arg;
displaymodule 0:3713b01e72fe 196 val2 = 0;
displaymodule 0:3713b01e72fe 197 mode = MODE_WANT_ARG2;
displaymodule 0:3713b01e72fe 198 strpos = sprintf(buff, "%d%c", val1, op);
displaymodule 0:3713b01e72fe 199 clearResult = true;
displaymodule 0:3713b01e72fe 200 }
displaymodule 0:3713b01e72fe 201 break;
displaymodule 0:3713b01e72fe 202 case 'c':
displaymodule 0:3713b01e72fe 203 val1 = val2 = op = 0;
displaymodule 0:3713b01e72fe 204 mode = MODE_WANT_ARG1;
displaymodule 0:3713b01e72fe 205 strpos = 0;
displaymodule 0:3713b01e72fe 206 clearResult = true;
displaymodule 0:3713b01e72fe 207 break;
displaymodule 0:3713b01e72fe 208 case '=':
displaymodule 0:3713b01e72fe 209 default:
displaymodule 0:3713b01e72fe 210 if (mode == MODE_WANT_ARG2_OR_OP) {
displaymodule 0:3713b01e72fe 211 // already have "a op b", calculate it and go to "c"
displaymodule 0:3713b01e72fe 212 switch (op) {
displaymodule 0:3713b01e72fe 213 case '+': val1 = val1 + val2; break;
displaymodule 0:3713b01e72fe 214 case '-': val1 = val1 - val2; break;
displaymodule 0:3713b01e72fe 215 case '*': val1 = val1 * val2; break;
displaymodule 0:3713b01e72fe 216 case '/': val1 = val1 / val2; break;
displaymodule 0:3713b01e72fe 217 }
displaymodule 0:3713b01e72fe 218 mode = MODE_WANT_ARG1_OR_OP;
displaymodule 0:3713b01e72fe 219 val2 = 0;
displaymodule 0:3713b01e72fe 220 strpos = sprintf(buff, "%d", val1);
displaymodule 0:3713b01e72fe 221 clearResult = true;
displaymodule 0:3713b01e72fe 222 }
displaymodule 0:3713b01e72fe 223 break;
displaymodule 0:3713b01e72fe 224 }
displaymodule 0:3713b01e72fe 225 buff[strpos] = '\0';
displaymodule 0:3713b01e72fe 226 redrawResult = true;
displaymodule 0:3713b01e72fe 227 }
displaymodule 0:3713b01e72fe 228
displaymodule 0:3713b01e72fe 229 /******************************************************************************
displaymodule 0:3713b01e72fe 230 * Main
displaymodule 0:3713b01e72fe 231 *****************************************************************************/
displaymodule 0:3713b01e72fe 232
displaymodule 0:3713b01e72fe 233 int main() {
displaymodule 0:3713b01e72fe 234 uint16_t x = 0;
displaymodule 0:3713b01e72fe 235 uint16_t y = 0;
displaymodule 0:3713b01e72fe 236 uint16_t w = tft.width();
displaymodule 0:3713b01e72fe 237 uint16_t h = tft.height();
displaymodule 1:9a3ae682a75e 238 uint16_t size = (w - (BUTTONS_PER_LINE + 1)*MARGIN)/BUTTONS_PER_LINE;
displaymodule 1:9a3ae682a75e 239 uint16_t yoff = h - (size + MARGIN)*BUTTONS_NUM_LINES;
displaymodule 0:3713b01e72fe 240 bool down = false;
displaymodule 0:3713b01e72fe 241
displaymodule 0:3713b01e72fe 242 for (int i = 0; i < NUM_BUTTONS;i++) {
displaymodule 1:9a3ae682a75e 243 x = MARGIN + (size + MARGIN) * (i % BUTTONS_PER_LINE);
displaymodule 1:9a3ae682a75e 244 y = yoff + (size + MARGIN) * (i / BUTTONS_PER_LINE);
displaymodule 0:3713b01e72fe 245 buttons[i] = new Button(captions[i], x, y, size, size);
displaymodule 0:3713b01e72fe 246 buttons[i]->setAction(handleClick, captions[i][0]);
displaymodule 0:3713b01e72fe 247 }
displaymodule 0:3713b01e72fe 248
displaymodule 0:3713b01e72fe 249 tft.init();
displaymodule 0:3713b01e72fe 250 tft.clearScreen(BRIGHT_RED);
displaymodule 0:3713b01e72fe 251 tft.fillRectangle(RESULT_MARGIN_X, RESULT_MARGIN_Y, w-RESULT_MARGIN_X, yoff-RESULT_MARGIN_Y, BLACK);
displaymodule 0:3713b01e72fe 252 touch.init();
displaymodule 0:3713b01e72fe 253
displaymodule 0:3713b01e72fe 254 for (int i = 0; i < NUM_BUTTONS; i++) {
displaymodule 0:3713b01e72fe 255 buttons[i]->draw(&tft);
displaymodule 0:3713b01e72fe 256 }
displaymodule 0:3713b01e72fe 257
displaymodule 0:3713b01e72fe 258 while(true) {
displaymodule 0:3713b01e72fe 259 touch.readTouchData(x, y, down);
displaymodule 0:3713b01e72fe 260
displaymodule 0:3713b01e72fe 261 for (int i = 0; i < NUM_BUTTONS; i++) {
displaymodule 0:3713b01e72fe 262 if (buttons[i]->handle(x, y, down)) {
displaymodule 0:3713b01e72fe 263 buttons[i]->draw(&tft);
displaymodule 0:3713b01e72fe 264 }
displaymodule 0:3713b01e72fe 265 }
displaymodule 0:3713b01e72fe 266 if (clearResult) {
displaymodule 0:3713b01e72fe 267 clearResult = false;
displaymodule 0:3713b01e72fe 268 tft.fillRectangle(RESULT_MARGIN_X, RESULT_MARGIN_Y, w-RESULT_MARGIN_X, yoff-RESULT_MARGIN_Y, BLACK);
displaymodule 0:3713b01e72fe 269 }
displaymodule 0:3713b01e72fe 270 if (redrawResult) {
displaymodule 0:3713b01e72fe 271 redrawResult = false;
displaymodule 0:3713b01e72fe 272 tft.drawStringCentered(RESULT_MARGIN_X, RESULT_MARGIN_Y, w-RESULT_MARGIN_X, yoff-RESULT_MARGIN_Y, &buff[0]);
displaymodule 0:3713b01e72fe 273 #ifdef DEBUG
displaymodule 0:3713b01e72fe 274 tft.drawString(5, 5, &debug[0]);
displaymodule 0:3713b01e72fe 275 tft.drawString(5, 25, &debug2[0]);
displaymodule 0:3713b01e72fe 276 tft.drawNumber(5, 45, val1, 6);
displaymodule 0:3713b01e72fe 277 tft.drawNumber(5, 65, val2, 6);
displaymodule 0:3713b01e72fe 278 tft.drawString(5, 25, &debug2[0]);
displaymodule 0:3713b01e72fe 279 #endif
displaymodule 0:3713b01e72fe 280 }
displaymodule 0:3713b01e72fe 281 wait(0.02);
displaymodule 0:3713b01e72fe 282 }
displaymodule 0:3713b01e72fe 283 }