A basic graphics package for the LPC4088 Display Module.

Dependents:   lpc4088_displaymodule_demo_sphere sampleGUI sampleEmptyGUI lpc4088_displaymodule_fs_aid ... more

Fork of DMBasicGUI by EmbeddedArtists AB

Committer:
embeddedartists
Date:
Thu Jan 08 19:28:22 2015 +0100
Revision:
7:4ba7bd9d32ef
Child:
9:ce69a7adfe9c
- Added pressed() and bounds() functions to Clickable
- Added DigitButton
- Added swim_put_image_xy() to be able to tell where an image is drawn
- Moved AppImageViewer and AppSettings from this lib into the main program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 7:4ba7bd9d32ef 1 /*
embeddedartists 7:4ba7bd9d32ef 2 * Copyright 2014 Embedded Artists AB
embeddedartists 7:4ba7bd9d32ef 3 *
embeddedartists 7:4ba7bd9d32ef 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 7:4ba7bd9d32ef 5 * you may not use this file except in compliance with the License.
embeddedartists 7:4ba7bd9d32ef 6 * You may obtain a copy of the License at
embeddedartists 7:4ba7bd9d32ef 7 *
embeddedartists 7:4ba7bd9d32ef 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 7:4ba7bd9d32ef 9 *
embeddedartists 7:4ba7bd9d32ef 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 7:4ba7bd9d32ef 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 7:4ba7bd9d32ef 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 7:4ba7bd9d32ef 13 * See the License for the specific language governing permissions and
embeddedartists 7:4ba7bd9d32ef 14 * limitations under the License.
embeddedartists 7:4ba7bd9d32ef 15 */
embeddedartists 7:4ba7bd9d32ef 16
embeddedartists 7:4ba7bd9d32ef 17 #include "DigitButton.h"
embeddedartists 7:4ba7bd9d32ef 18 #include "mbed.h"
embeddedartists 7:4ba7bd9d32ef 19 #include "DMBoard.h"
embeddedartists 7:4ba7bd9d32ef 20
embeddedartists 7:4ba7bd9d32ef 21 #include "lpc_swim_image.h"
embeddedartists 7:4ba7bd9d32ef 22
embeddedartists 7:4ba7bd9d32ef 23 DigitButton::DigitButton(COLOR_T* fb, uint16_t x, uint16_t y, uint16_t width, uint16_t height) :
embeddedartists 7:4ba7bd9d32ef 24 Clickable(fb, x, y, width, height), _value(0), _digits(1), _img_y_offset(0),
embeddedartists 7:4ba7bd9d32ef 25 _img_digit_height(0), _img_digit_width(0), _img_digit_size(0)
embeddedartists 7:4ba7bd9d32ef 26 {
embeddedartists 7:4ba7bd9d32ef 27 _imgUp.pixels = NULL;
embeddedartists 7:4ba7bd9d32ef 28 _imgDown.pixels = NULL;
embeddedartists 7:4ba7bd9d32ef 29 _win.fill = WHITE;
embeddedartists 7:4ba7bd9d32ef 30 swim_clear_screen(&_win, _win.fill);
embeddedartists 7:4ba7bd9d32ef 31 }
embeddedartists 7:4ba7bd9d32ef 32
embeddedartists 7:4ba7bd9d32ef 33 DigitButton::~DigitButton()
embeddedartists 7:4ba7bd9d32ef 34 {
embeddedartists 7:4ba7bd9d32ef 35 if (_imgUp.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 36 free(_imgUp.pixels);
embeddedartists 7:4ba7bd9d32ef 37 _imgUp.pixels = NULL;
embeddedartists 7:4ba7bd9d32ef 38 }
embeddedartists 7:4ba7bd9d32ef 39 if (_imgDown.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 40 free(_imgDown.pixels);
embeddedartists 7:4ba7bd9d32ef 41 _imgDown.pixels = NULL;
embeddedartists 7:4ba7bd9d32ef 42 }
embeddedartists 7:4ba7bd9d32ef 43 }
embeddedartists 7:4ba7bd9d32ef 44
embeddedartists 7:4ba7bd9d32ef 45 bool DigitButton::loadImages(const char* imgUp, const char* imgDown)
embeddedartists 7:4ba7bd9d32ef 46 {
embeddedartists 7:4ba7bd9d32ef 47 if (_imgUp.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 48 free(_imgUp.pixels);
embeddedartists 7:4ba7bd9d32ef 49 _imgUp.pixels = NULL;
embeddedartists 7:4ba7bd9d32ef 50 }
embeddedartists 7:4ba7bd9d32ef 51 if (_imgDown.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 52 free(_imgDown.pixels);
embeddedartists 7:4ba7bd9d32ef 53 _imgDown.pixels = NULL;
embeddedartists 7:4ba7bd9d32ef 54 }
embeddedartists 7:4ba7bd9d32ef 55 if (Image::decode(imgUp, Image::RES_16BIT, &_imgUp) != 0) {
embeddedartists 7:4ba7bd9d32ef 56 DMBoard::instance().logger()->printf("Failed to load %s\n", imgUp);
embeddedartists 7:4ba7bd9d32ef 57 return false;
embeddedartists 7:4ba7bd9d32ef 58 }
embeddedartists 7:4ba7bd9d32ef 59 if (imgDown != NULL) {
embeddedartists 7:4ba7bd9d32ef 60 if (Image::decode(imgDown, Image::RES_16BIT, &_imgDown) == 0) {
embeddedartists 7:4ba7bd9d32ef 61 DMBoard::instance().logger()->printf("Failed to load %s\n", imgDown);
embeddedartists 7:4ba7bd9d32ef 62 return false;
embeddedartists 7:4ba7bd9d32ef 63 }
embeddedartists 7:4ba7bd9d32ef 64 }
embeddedartists 7:4ba7bd9d32ef 65 _img_digit_width = _imgUp.width;
embeddedartists 7:4ba7bd9d32ef 66 _img_digit_height = _imgUp.height/10;
embeddedartists 7:4ba7bd9d32ef 67 _img_y_offset = _imgUp.height - 10*_img_digit_height;
embeddedartists 7:4ba7bd9d32ef 68 _img_digit_size = ((_imgUp.res == Image::RES_16BIT)?1:2) * _img_digit_height * _img_digit_width;
embeddedartists 7:4ba7bd9d32ef 69 return true;
embeddedartists 7:4ba7bd9d32ef 70 }
embeddedartists 7:4ba7bd9d32ef 71
embeddedartists 7:4ba7bd9d32ef 72 bool DigitButton::loadImages(const unsigned char* imgUp, unsigned int imgUpSize,
embeddedartists 7:4ba7bd9d32ef 73 const unsigned char* imgDown, unsigned int imgDownSize)
embeddedartists 7:4ba7bd9d32ef 74 {
embeddedartists 7:4ba7bd9d32ef 75 if (_imgUp.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 76 free(_imgUp.pixels);
embeddedartists 7:4ba7bd9d32ef 77 _imgUp.pixels = NULL;
embeddedartists 7:4ba7bd9d32ef 78 }
embeddedartists 7:4ba7bd9d32ef 79 if (_imgDown.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 80 free(_imgDown.pixels);
embeddedartists 7:4ba7bd9d32ef 81 _imgDown.pixels = NULL;
embeddedartists 7:4ba7bd9d32ef 82 }
embeddedartists 7:4ba7bd9d32ef 83 if (Image::decode(imgUp, imgUpSize, Image::RES_16BIT, &_imgUp) != 0) {
embeddedartists 7:4ba7bd9d32ef 84 DMBoard::instance().logger()->printf("Failed to load %s\n", imgUp);
embeddedartists 7:4ba7bd9d32ef 85 return false;
embeddedartists 7:4ba7bd9d32ef 86 }
embeddedartists 7:4ba7bd9d32ef 87 if (imgDown != NULL) {
embeddedartists 7:4ba7bd9d32ef 88 if (Image::decode(imgDown, imgDownSize, Image::RES_16BIT, &_imgDown) == 0) {
embeddedartists 7:4ba7bd9d32ef 89 DMBoard::instance().logger()->printf("Failed to load %s\n", imgDown);
embeddedartists 7:4ba7bd9d32ef 90 return false;
embeddedartists 7:4ba7bd9d32ef 91 }
embeddedartists 7:4ba7bd9d32ef 92 }
embeddedartists 7:4ba7bd9d32ef 93 _img_digit_width = _imgUp.width;
embeddedartists 7:4ba7bd9d32ef 94 _img_digit_height = _imgUp.height/10;
embeddedartists 7:4ba7bd9d32ef 95 _img_y_offset = _imgUp.height - 10*_img_digit_height;
embeddedartists 7:4ba7bd9d32ef 96 _img_digit_size = ((_imgUp.res == Image::RES_16BIT)?1:2) * _img_digit_height * _img_digit_width;
embeddedartists 7:4ba7bd9d32ef 97 return true;
embeddedartists 7:4ba7bd9d32ef 98 }
embeddedartists 7:4ba7bd9d32ef 99
embeddedartists 7:4ba7bd9d32ef 100 void DigitButton::setNumDigits(unsigned int num)
embeddedartists 7:4ba7bd9d32ef 101 {
embeddedartists 7:4ba7bd9d32ef 102 _digits = num;
embeddedartists 7:4ba7bd9d32ef 103 draw();
embeddedartists 7:4ba7bd9d32ef 104 }
embeddedartists 7:4ba7bd9d32ef 105
embeddedartists 7:4ba7bd9d32ef 106 void DigitButton::setValue(unsigned int val)
embeddedartists 7:4ba7bd9d32ef 107 {
embeddedartists 7:4ba7bd9d32ef 108 _value = val;
embeddedartists 7:4ba7bd9d32ef 109 draw();
embeddedartists 7:4ba7bd9d32ef 110 }
embeddedartists 7:4ba7bd9d32ef 111
embeddedartists 7:4ba7bd9d32ef 112 void DigitButton::draw(COLOR_T* fb)
embeddedartists 7:4ba7bd9d32ef 113 {
embeddedartists 7:4ba7bd9d32ef 114 if (fb != NULL) {
embeddedartists 7:4ba7bd9d32ef 115 _win.fb = fb;
embeddedartists 7:4ba7bd9d32ef 116 }
embeddedartists 7:4ba7bd9d32ef 117 // if (_pressed) {
embeddedartists 7:4ba7bd9d32ef 118 // if (_imgDown.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 119 // swim_put_image(&_win, _imgDown.pixels, _imgDown.width, _imgDown.height);
embeddedartists 7:4ba7bd9d32ef 120 // }
embeddedartists 7:4ba7bd9d32ef 121 // } else {
embeddedartists 7:4ba7bd9d32ef 122 // if (_imgUp.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 123 // swim_put_image(&_win, _imgUp.pixels, _imgUp.width, _imgUp.height);
embeddedartists 7:4ba7bd9d32ef 124 // }
embeddedartists 7:4ba7bd9d32ef 125 // }
embeddedartists 7:4ba7bd9d32ef 126 drawDigits(_pressed ? _imgDown : _imgUp);
embeddedartists 7:4ba7bd9d32ef 127 }
embeddedartists 7:4ba7bd9d32ef 128
embeddedartists 7:4ba7bd9d32ef 129 void DigitButton::drawDigits(Image::ImageData_t& img)
embeddedartists 7:4ba7bd9d32ef 130 {
embeddedartists 7:4ba7bd9d32ef 131 if (img.pixels != NULL) {
embeddedartists 7:4ba7bd9d32ef 132 uint32_t v = _value;
embeddedartists 7:4ba7bd9d32ef 133 int x = (_win.xvsize - (_digits*_img_digit_width))/2;
embeddedartists 7:4ba7bd9d32ef 134 if (x < 0) {
embeddedartists 7:4ba7bd9d32ef 135 x = 0;
embeddedartists 7:4ba7bd9d32ef 136 }
embeddedartists 7:4ba7bd9d32ef 137 int y = (_win.yvsize - _img_digit_height)/2;
embeddedartists 7:4ba7bd9d32ef 138 for (int i = 0; i < _digits; i++) {
embeddedartists 7:4ba7bd9d32ef 139 uint32_t off = (v % 10)*_img_digit_size + _img_y_offset*_img_digit_width;
embeddedartists 7:4ba7bd9d32ef 140 swim_put_image_xy(&_win, _imgUp.pixels+off, _img_digit_width, _img_digit_height, x+(_digits-i-1)*_img_digit_width, y);
embeddedartists 7:4ba7bd9d32ef 141 v = v/10;
embeddedartists 7:4ba7bd9d32ef 142 }
embeddedartists 7:4ba7bd9d32ef 143 }
embeddedartists 7:4ba7bd9d32ef 144 }