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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DigitButton.cpp Source File

DigitButton.cpp

00001 /*
00002  *  Copyright 2014 Embedded Artists AB
00003  *
00004  *  Licensed under the Apache License, Version 2.0 (the "License");
00005  *  you may not use this file except in compliance with the License.
00006  *  You may obtain a copy of the License at
00007  *
00008  *    http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  *  Unless required by applicable law or agreed to in writing, software
00011  *  distributed under the License is distributed on an "AS IS" BASIS,
00012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *  See the License for the specific language governing permissions and
00014  *  limitations under the License.
00015  */
00016 
00017 #include "DigitButton.h"
00018 #include "mbed.h"
00019 #include "DMBoard.h"
00020 
00021 #include "lpc_swim_image.h"
00022 
00023 DigitButton::DigitButton(COLOR_T* fb, uint16_t x, uint16_t y, uint16_t width, uint16_t height) :
00024   Clickable(fb, x, y, width, height), _value(0), _digits(1), _img_y_offset(0),
00025   _img_digit_height(0), _img_digit_width(0), _img_digit_size(0)
00026 {
00027   _imgUp.pointerToFree = NULL;
00028   _imgUp.pixels = NULL;
00029   _imgDown.pointerToFree = NULL;
00030   _imgDown.pixels = NULL;
00031   _win.fill = WHITE;
00032   swim_clear_screen(&_win, _win.fill);
00033 }
00034 
00035 DigitButton::~DigitButton()
00036 {
00037   if (_imgUp.pointerToFree != NULL) {
00038     free(_imgUp.pointerToFree);
00039     _imgUp.pointerToFree = NULL;
00040   }
00041   if (_imgDown.pointerToFree != NULL) {
00042     free(_imgDown.pointerToFree);
00043     _imgDown.pointerToFree = NULL;
00044   }
00045 }
00046 
00047 bool DigitButton::loadImages(const char* imgUp, const char* imgDown)
00048 {
00049   if (_imgUp.pointerToFree != NULL) {
00050     free(_imgUp.pointerToFree);
00051     _imgUp.pointerToFree = NULL;
00052   }
00053   if (_imgDown.pointerToFree != NULL) {
00054     free(_imgDown.pointerToFree);
00055     _imgDown.pointerToFree = NULL;
00056   }
00057   if (Image::decode(imgUp, Image::RES_16BIT, &_imgUp) != 0) {
00058     DMBoard::instance().logger()->printf("Failed to load %s\n", imgUp);
00059     return false;
00060   }
00061   if (imgDown != NULL) {
00062     if (Image::decode(imgDown, Image::RES_16BIT, &_imgDown) == 0) {
00063       DMBoard::instance().logger()->printf("Failed to load %s\n", imgDown);
00064       return false;
00065     }
00066   }
00067   _img_digit_width = _imgUp.width;
00068   _img_digit_height = _imgUp.height/10;
00069   _img_y_offset = _imgUp.height - 10*_img_digit_height;
00070   _img_digit_size = ((_imgUp.res == Image::RES_16BIT)?1:2) * _img_digit_height * _img_digit_width;
00071   return true;
00072 }
00073 
00074 bool DigitButton::loadImages(const Image::ImageData_t* imgUp, const Image::ImageData_t* imgDown)
00075 {
00076   if (_imgUp.pointerToFree != NULL) {
00077     free(_imgUp.pointerToFree);
00078     _imgUp.pointerToFree = NULL;
00079   }
00080   if (_imgDown.pointerToFree != NULL) {
00081     free(_imgDown.pointerToFree);
00082     _imgDown.pointerToFree = NULL;
00083   }
00084   memcpy(&_imgUp, imgUp, sizeof(Image::ImageData_t));
00085   if (imgDown != NULL) {
00086     memcpy(&_imgDown, imgDown, sizeof(Image::ImageData_t));
00087   }
00088   _img_digit_width = _imgUp.width;
00089   _img_digit_height = _imgUp.height/10;
00090   _img_y_offset = _imgUp.height - 10*_img_digit_height;
00091   _img_digit_size = ((_imgUp.res == Image::RES_16BIT)?1:2) * _img_digit_height * _img_digit_width;
00092   return true;
00093 }
00094 
00095 bool DigitButton::loadImages(const unsigned char* imgUp, unsigned int imgUpSize, 
00096                              const unsigned char* imgDown, unsigned int imgDownSize)
00097 {
00098   if (_imgUp.pointerToFree != NULL) {
00099     free(_imgUp.pointerToFree);
00100     _imgUp.pointerToFree = NULL;
00101   }
00102   if (_imgDown.pointerToFree != NULL) {
00103     free(_imgDown.pointerToFree);
00104     _imgDown.pointerToFree = NULL;
00105   }
00106   if (Image::decode(imgUp, imgUpSize, Image::RES_16BIT, &_imgUp) != 0) {
00107     DMBoard::instance().logger()->printf("Failed to load %s\n", imgUp);
00108     return false;
00109   }
00110   if (imgDown != NULL) {
00111     if (Image::decode(imgDown, imgDownSize, Image::RES_16BIT, &_imgDown) == 0) {
00112       DMBoard::instance().logger()->printf("Failed to load %s\n", imgDown);
00113       return false;
00114     }
00115   }
00116   _img_digit_width = _imgUp.width;
00117   _img_digit_height = _imgUp.height/10;
00118   _img_y_offset = _imgUp.height - 10*_img_digit_height;
00119   _img_digit_size = ((_imgUp.res == Image::RES_16BIT)?1:2) * _img_digit_height * _img_digit_width;
00120   return true;
00121 }
00122 
00123 void DigitButton::setNumDigits(unsigned int num)
00124 {
00125   _digits = num;
00126   draw();
00127 }
00128 
00129 void DigitButton::setValue(unsigned int val)
00130 {
00131   _value = val;
00132   draw();
00133 }
00134 
00135 void DigitButton::draw(COLOR_T* fb)
00136 {
00137   if (fb != NULL) {
00138     _win.fb = fb;
00139   }
00140 //  if (_pressed) {
00141 //    if (_imgDown.pixels != NULL) {
00142 //      swim_put_image(&_win, _imgDown.pixels, _imgDown.width, _imgDown.height);
00143 //    }
00144 //  } else {
00145 //    if (_imgUp.pixels != NULL) {
00146 //      swim_put_image(&_win, _imgUp.pixels, _imgUp.width, _imgUp.height);
00147 //    }
00148 //  }
00149   drawDigits(_pressed ? _imgDown : _imgUp);
00150 }
00151 
00152 void DigitButton::drawDigits(Image::ImageData_t& img)
00153 {
00154   if (img.pixels != NULL) {
00155     uint32_t v = _value;
00156     int x = (_win.xvsize - (_digits*_img_digit_width))/2;
00157     if (x < 0) {
00158       x = 0;
00159     }
00160     int y = (_win.yvsize - _img_digit_height)/2;
00161     for (unsigned int i = 0; i < _digits; i++) {
00162       uint32_t off = (v % 10)*_img_digit_size + _img_y_offset*_img_digit_width;
00163       swim_put_image_xy(&_win, _imgUp.pixels+off, _img_digit_width, _img_digit_height, x+(_digits-i-1)*_img_digit_width, y);
00164       v = v/10;
00165     }
00166   }
00167 }