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:
Tue May 20 15:36:25 2014 +0000
Revision:
0:3713b01e72fe
Child:
2:b17b19212e5a
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
displaymodule 0:3713b01e72fe 1 #include "Button.h"
displaymodule 0:3713b01e72fe 2
displaymodule 0:3713b01e72fe 3 Button::Button(const char* caption, uint16_t x, uint16_t y, uint16_t width, uint16_t height) :
displaymodule 0:3713b01e72fe 4 _caption(caption), _x0(x), _y0(y), _x1(x+width), _y1(y+height), _bgCol(BLACK),
displaymodule 0:3713b01e72fe 5 _fgCol(WHITE), _bgColPressed(CYAN), _fgColPressed(BLACK)
displaymodule 0:3713b01e72fe 6 {
displaymodule 0:3713b01e72fe 7 _enabled = true;
displaymodule 0:3713b01e72fe 8 _pressed = false;
displaymodule 0:3713b01e72fe 9 _func = NULL;
displaymodule 0:3713b01e72fe 10 }
displaymodule 0:3713b01e72fe 11
displaymodule 0:3713b01e72fe 12 void Button::setColors(uint16_t bg, uint16_t fg, uint16_t bgPressed, uint16_t fgPressed)
displaymodule 0:3713b01e72fe 13 {
displaymodule 0:3713b01e72fe 14 _bgCol = bg;
displaymodule 0:3713b01e72fe 15 _fgCol = fg;
displaymodule 0:3713b01e72fe 16 _bgColPressed = bgPressed;
displaymodule 0:3713b01e72fe 17 _fgColPressed = fgPressed;
displaymodule 0:3713b01e72fe 18 }
displaymodule 0:3713b01e72fe 19
displaymodule 0:3713b01e72fe 20 bool Button::handle(uint16_t x, uint16_t y, bool pressed)
displaymodule 0:3713b01e72fe 21 {
displaymodule 0:3713b01e72fe 22 bool needsRepaint = false;
displaymodule 0:3713b01e72fe 23 if (_enabled) {
displaymodule 0:3713b01e72fe 24 if ((x >= _x0) && (y >= _y0) && (x <= _x1) && (y <= _y1)) {
displaymodule 0:3713b01e72fe 25 if (pressed && !_pressed) {
displaymodule 0:3713b01e72fe 26 // user pressing inside area
displaymodule 0:3713b01e72fe 27 needsRepaint = true;
displaymodule 0:3713b01e72fe 28 _pressed = true;
displaymodule 0:3713b01e72fe 29 } else if (!pressed && _pressed) {
displaymodule 0:3713b01e72fe 30 // user released inside area => click
displaymodule 0:3713b01e72fe 31 needsRepaint = true;
displaymodule 0:3713b01e72fe 32 _pressed = false;
displaymodule 0:3713b01e72fe 33 if (_func != NULL) {
displaymodule 0:3713b01e72fe 34 _func(_funcArg);
displaymodule 0:3713b01e72fe 35 }
displaymodule 0:3713b01e72fe 36 }
displaymodule 0:3713b01e72fe 37 }
displaymodule 0:3713b01e72fe 38 }
displaymodule 0:3713b01e72fe 39 return needsRepaint;
displaymodule 0:3713b01e72fe 40 }
displaymodule 0:3713b01e72fe 41
displaymodule 0:3713b01e72fe 42 void Button::draw(DmTftBase* tft)
displaymodule 0:3713b01e72fe 43 {
displaymodule 0:3713b01e72fe 44 if (_pressed) {
displaymodule 0:3713b01e72fe 45 tft->fillRectangle(_x0+1, _y0+1, _x1-1, _y1-1, _bgColPressed);
displaymodule 0:3713b01e72fe 46 tft->drawRectangle(_x0, _y0, _x1, _y1, _fgColPressed);
displaymodule 0:3713b01e72fe 47 tft->setTextColor(_bgColPressed, _fgColPressed);
displaymodule 0:3713b01e72fe 48 tft->drawStringCentered(_x0, _y0, _x1-_x0, _y1-_y0, _caption);
displaymodule 0:3713b01e72fe 49 } else {
displaymodule 0:3713b01e72fe 50 tft->fillRectangle(_x0+1, _y0+1, _x1-1, _y1-1, _bgCol);
displaymodule 0:3713b01e72fe 51 tft->drawRectangle(_x0, _y0, _x1, _y1, _fgCol);
displaymodule 0:3713b01e72fe 52 tft->setTextColor(_bgCol, _fgCol);
displaymodule 0:3713b01e72fe 53 tft->drawStringCentered(_x0, _y0, _x1-_x0, _y1-_y0, _caption);
displaymodule 0:3713b01e72fe 54 }
displaymodule 0:3713b01e72fe 55 }
displaymodule 0:3713b01e72fe 56