Display Module / Mbed 2 deprecated dm_calc

Dependencies:   DmTftLibrary mbed

Files at this revision

API Documentation at this revision

Comitter:
displaymodule
Date:
Tue May 20 15:36:25 2014 +0000
Child:
1:9a3ae682a75e
Commit message:
First version

Changed in this revision

Button.cpp Show annotated file Show diff for this revision Revisions of this file
Button.h Show annotated file Show diff for this revision Revisions of this file
DmTftLibrary.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Button.cpp	Tue May 20 15:36:25 2014 +0000
@@ -0,0 +1,56 @@
+#include "Button.h"
+
+Button::Button(const char* caption, uint16_t x, uint16_t y, uint16_t width, uint16_t height) :
+  _caption(caption), _x0(x), _y0(y), _x1(x+width), _y1(y+height), _bgCol(BLACK), 
+  _fgCol(WHITE), _bgColPressed(CYAN), _fgColPressed(BLACK)
+{
+  _enabled = true;
+  _pressed = false;
+  _func = NULL;
+}
+
+void Button::setColors(uint16_t bg, uint16_t fg, uint16_t bgPressed, uint16_t fgPressed)
+{
+  _bgCol = bg;
+  _fgCol = fg;
+  _bgColPressed = bgPressed;
+  _fgColPressed = fgPressed;
+}
+
+bool Button::handle(uint16_t x, uint16_t y, bool pressed)
+{
+  bool needsRepaint = false;
+  if (_enabled) {
+    if ((x >= _x0) && (y >= _y0) && (x <= _x1) && (y <= _y1)) {
+      if (pressed && !_pressed) {
+        // user pressing inside area
+        needsRepaint = true;
+        _pressed = true;
+      } else if (!pressed && _pressed) {
+        // user released inside area => click
+        needsRepaint = true;
+        _pressed = false;
+        if (_func != NULL) {
+          _func(_funcArg);
+        }       
+      }
+    }
+  }
+  return needsRepaint;
+}
+
+void Button::draw(DmTftBase* tft)
+{
+  if (_pressed) {
+    tft->fillRectangle(_x0+1, _y0+1, _x1-1, _y1-1, _bgColPressed);
+    tft->drawRectangle(_x0, _y0, _x1, _y1, _fgColPressed);
+    tft->setTextColor(_bgColPressed, _fgColPressed);
+    tft->drawStringCentered(_x0, _y0, _x1-_x0, _y1-_y0, _caption);
+  } else {
+    tft->fillRectangle(_x0+1, _y0+1, _x1-1, _y1-1, _bgCol);
+    tft->drawRectangle(_x0, _y0, _x1, _y1, _fgCol);
+    tft->setTextColor(_bgCol, _fgCol);
+    tft->drawStringCentered(_x0, _y0, _x1-_x0, _y1-_y0, _caption);
+  }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Button.h	Tue May 20 15:36:25 2014 +0000
@@ -0,0 +1,27 @@
+#ifndef BUTTON_h
+#define BUTTON_h
+
+#include "DmTftBase.h"
+
+typedef void (*cbFunc)(uint32_t);
+
+class Button {
+public:
+  Button(const char* caption, uint16_t x, uint16_t y, uint16_t width, uint16_t height);
+  void setAction(cbFunc func, uint32_t arg) { _func = func; _funcArg = arg; }
+  void setCaption(const char* caption) { _caption = caption; }
+  void setColors(uint16_t bg, uint16_t fg, uint16_t bgPressed, uint16_t fgPressed);
+  bool handle(uint16_t x, uint16_t y, bool pressed);
+  void draw(DmTftBase* tft);
+
+private:
+  const char* _caption;
+  uint16_t _x0, _y0, _x1, _y1;
+  uint16_t _bgCol, _fgCol, _bgColPressed, _fgColPressed;
+  bool _enabled, _pressed;
+  cbFunc _func;
+  uint32_t _funcArg;
+};
+
+#endif /* BUTTON_h */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DmTftLibrary.lib	Tue May 20 15:36:25 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/displaymodule/code/DmTftLibrary/#59be7fca4581
--- /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);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Tue May 20 15:36:25 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/displaymodule/code/mbed-src/#3306e8fd8143