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

Revision:
0:3713b01e72fe
Child:
1:9a3ae682a75e
diff -r 000000000000 -r 3713b01e72fe main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue May 20 15:36:25 2014 +0000
@@ -0,0 +1,263 @@
+/**********************************************************************************************
+ 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 "Button.h"
+
+/******************************************************************************
+ * Typedefs and defines
+ *****************************************************************************/
+
+typedef enum {
+  MODE_WANT_ARG1,
+  MODE_WANT_ARG1_OR_OP,
+  MODE_WANT_ARG2,
+  MODE_WANT_ARG2_OR_OP,
+} calc_mode_t;
+
+#define MARGIN  5
+
+#define RESULT_MARGIN_X  (MARGIN*3)
+#define RESULT_MARGIN_Y  (MARGIN*3)
+
+#define NUM_BUTTONS  (sizeof(captions)/sizeof(captions[0]))
+
+#if 0 /* 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  
+
+/*
+ *   7   8   9   +
+ *   4   5   6   -
+ *   3   2   1   *
+ *   0   =  clr  /
+ */
+const char* captions[] = {
+    "7","8","9","+",
+    "4","5","6","-",
+    "3","2","1","*",
+    "0","=","clr","/",
+};
+Button* buttons[NUM_BUTTONS];
+
+static char buff[25] = {0};
+static bool redrawResult = true;
+static bool clearResult = true;
+
+#ifdef DEBUG
+  static char debug[25] = {0};
+  static char debug2[25] = {0};
+  static int debug_pos = 0;
+#endif
+
+/******************************************************************************
+ * Global variables
+ *****************************************************************************/
+
+
+/******************************************************************************
+ * Local functions
+ *****************************************************************************/
+
+void handleClick(uint32_t arg)
+{
+  static int val1 = 0;
+  static int val2 = 0;
+  static int op = 0;
+  static calc_mode_t mode = MODE_WANT_ARG1;
+  static int strpos = 0;
+  
+#ifdef DEBUG
+  debug2[debug_pos] = '0'+mode;
+  debug[debug_pos] = arg;
+  debug_pos++;
+  debug[debug_pos] = '\0';
+  debug_pos = debug_pos%25;
+#endif  
+  switch (arg)
+  {
+    case '0':
+    case '1':
+    case '2':
+    case '3':
+    case '4':
+    case '5':
+    case '6':
+    case '7':
+    case '8':
+    case '9':
+      if ((mode == MODE_WANT_ARG1) || (mode == MODE_WANT_ARG1_OR_OP)) {
+        val1 = val1*10 + (arg - '0');
+        mode = MODE_WANT_ARG1_OR_OP;
+        buff[strpos++] = arg;
+      } else if ((mode == MODE_WANT_ARG2) || (mode == MODE_WANT_ARG2_OR_OP)) {
+        val2 = val2*10 + (arg - '0');
+        mode = MODE_WANT_ARG2_OR_OP;
+        buff[strpos++] = arg;
+      }
+      break;
+    case '+':
+    case '-':
+    case '*':
+    case '/':
+      if (mode == MODE_WANT_ARG1_OR_OP) {
+        op = arg;
+        mode = MODE_WANT_ARG2;
+        buff[strpos++] = arg;
+      } else if (mode == MODE_WANT_ARG2_OR_OP) {
+        // already have "a op b", calculate it and go to "c op"
+        switch (op) {
+          case '+':  val1 = val1 + val2; break;
+          case '-':  val1 = val1 - val2; break;
+          case '*':  val1 = val1 * val2; break;
+          case '/':  val1 = val1 / val2; break;
+        }
+        op = arg;
+        val2 = 0;
+        mode = MODE_WANT_ARG2;
+        strpos = sprintf(buff, "%d%c", val1, op);
+        clearResult = true;
+      }
+      break;
+    case 'c':
+      val1 = val2 = op = 0;
+      mode = MODE_WANT_ARG1;
+      strpos = 0;
+      clearResult = true;
+      break;
+    case '=':
+    default:
+      if (mode == MODE_WANT_ARG2_OR_OP) {
+        // already have "a op b", calculate it and go to "c"
+        switch (op) {
+          case '+':  val1 = val1 + val2; break;
+          case '-':  val1 = val1 - val2; break;
+          case '*':  val1 = val1 * val2; break;
+          case '/':  val1 = val1 / val2; break;
+        }
+        mode = MODE_WANT_ARG1_OR_OP;
+        val2 = 0;
+        strpos = sprintf(buff, "%d", val1);
+        clearResult = true;
+      }
+      break;
+  }
+  buff[strpos] = '\0';
+  redrawResult = true;
+}
+
+/******************************************************************************
+ * Main
+ *****************************************************************************/
+
+int main() { 
+  uint16_t x = 0;
+  uint16_t y = 0;
+  uint16_t w = tft.width();
+  uint16_t h = tft.height();
+  uint16_t size = (w - 5*MARGIN)/4;
+  uint16_t yoff = h - (size + MARGIN)*4;
+  bool down = false;
+
+  for (int i = 0; i < NUM_BUTTONS;i++) {
+    x = MARGIN + (size + MARGIN) * (i % 4);
+    y = yoff + (size + MARGIN) * (i / 4);
+    buttons[i] = new Button(captions[i], x, y, size, size);
+    buttons[i]->setAction(handleClick, captions[i][0]);
+  }  
+  
+  tft.init();
+  tft.clearScreen(BRIGHT_RED);
+  tft.fillRectangle(RESULT_MARGIN_X, RESULT_MARGIN_Y, w-RESULT_MARGIN_X, yoff-RESULT_MARGIN_Y, BLACK);
+  touch.init();
+
+  for (int i = 0; i < NUM_BUTTONS; i++) {
+    buttons[i]->draw(&tft);
+  }
+
+  while(true) {
+    touch.readTouchData(x, y, down);
+    
+    for (int i = 0; i < NUM_BUTTONS; i++) {
+      if (buttons[i]->handle(x, y, down)) {
+        buttons[i]->draw(&tft);
+      }
+    }
+    if (clearResult) {
+      clearResult = false;
+      tft.fillRectangle(RESULT_MARGIN_X, RESULT_MARGIN_Y, w-RESULT_MARGIN_X, yoff-RESULT_MARGIN_Y, BLACK);
+    }
+    if (redrawResult) {
+      redrawResult = false;
+      tft.drawStringCentered(RESULT_MARGIN_X, RESULT_MARGIN_Y, w-RESULT_MARGIN_X, yoff-RESULT_MARGIN_Y, &buff[0]);
+#ifdef DEBUG      
+      tft.drawString(5, 5, &debug[0]);
+      tft.drawString(5, 25, &debug2[0]);
+      tft.drawNumber(5, 45, val1, 6);
+      tft.drawNumber(5, 65, val2, 6);
+      tft.drawString(5, 25, &debug2[0]);
+#endif      
+    }
+    wait(0.02);
+  }
+}