test export

Dependencies:   DmTftLibrary mbed

Fork of dm_calc by Display Module

main.cpp

Committer:
displaymodule
Date:
2014-09-01
Revision:
3:45df69ebc420
Parent:
2:b17b19212e5a

File content as of revision 3:45df69ebc420:

/**********************************************************************************************
 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 1
  /* 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

//#define LANDSCAPE

/******************************************************************************
 * Local variables
 *****************************************************************************/

//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, DmTouch::Software); /* For LPC4088 QuickStart Board */
//DmTouch touch(DmTouch::DM_TFT28_103);
//DmTouch touch(DmTouch::DM_TFT24_104, DmTouch::Software); /* 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  

#ifdef LANDSCAPE
  #define BUTTONS_PER_LINE   6
  #define BUTTONS_NUM_LINES  3
  /*
   *   1   2   3   4   5   +
   *   6   7   8   9   0   -
   *   *   /   =   clr
   */
  const char* captions[] = {
      "1","2","3","4","5","+",
      "6","7","8","9","0","-",
      "*","/","=","clr",
  };  
#else
  #define BUTTONS_PER_LINE   4
  #define BUTTONS_NUM_LINES  4
  /*
   *   1   2   3   +
   *   4   5   6   -
   *   7   8   9   *
   *   0   =  clr  /
   */
  const char* captions[] = {
      "1","2","3","+",
      "4","5","6","-",
      "7","8","9","*",
      "0","=","clr","/",
  };
#endif
  
Button* buttons[NUM_BUTTONS];

static char buff[25] = {0};
static bool redrawResult = true;
static bool clearResult = true;

/******************************************************************************
 * 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;
  
  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 - (BUTTONS_PER_LINE + 1)*MARGIN)/BUTTONS_PER_LINE;
  uint16_t yoff = h - (size + MARGIN)*BUTTONS_NUM_LINES;
  bool down = false;

  for (int i = 0; i < NUM_BUTTONS;i++) {
    x = MARGIN + (size + MARGIN) * (i % BUTTONS_PER_LINE);
    y = yoff + (size + MARGIN) * (i / BUTTONS_PER_LINE);
    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);
    
    /* The following printouts are for some unexplainable reason needed
       on the LPC4088 QuickStart Board in combination with the MBED online compiler.
       The printouts are not needed when using an external compiler, e.g. Keil uVision
       or LPCXpresso IDE. */
#if defined(__LPC407x_8x_177x_8x_H__)
    if (down) {
      printf("DOWN %4d, %4d\n", x, y);
    } else {
      printf("UP   %4d, %4d\n", x, y);
    }
#endif
    
    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]);
    }
    wait(0.02);
  }
}