Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DMBasicGUI by
Application/DigitButton.cpp@10:651861441108, 2015-01-26 (annotated)
- Committer:
- embeddedartists
- Date:
- Mon Jan 26 10:06:58 2015 +0100
- Revision:
- 10:651861441108
- Parent:
- 9:ce69a7adfe9c
- Added virtual destructors to avoid undefined behaviour
- Fixed compiler warnings
Who changed what in which revision?
| User | Revision | Line number | New 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 | 9:ce69a7adfe9c | 27 | _imgUp.pointerToFree = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 28 | _imgUp.pixels = NULL; |
| embeddedartists | 9:ce69a7adfe9c | 29 | _imgDown.pointerToFree = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 30 | _imgDown.pixels = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 31 | _win.fill = WHITE; |
| embeddedartists | 7:4ba7bd9d32ef | 32 | swim_clear_screen(&_win, _win.fill); |
| embeddedartists | 7:4ba7bd9d32ef | 33 | } |
| embeddedartists | 7:4ba7bd9d32ef | 34 | |
| embeddedartists | 7:4ba7bd9d32ef | 35 | DigitButton::~DigitButton() |
| embeddedartists | 7:4ba7bd9d32ef | 36 | { |
| embeddedartists | 9:ce69a7adfe9c | 37 | if (_imgUp.pointerToFree != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 38 | free(_imgUp.pointerToFree); |
| embeddedartists | 9:ce69a7adfe9c | 39 | _imgUp.pointerToFree = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 40 | } |
| embeddedartists | 9:ce69a7adfe9c | 41 | if (_imgDown.pointerToFree != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 42 | free(_imgDown.pointerToFree); |
| embeddedartists | 9:ce69a7adfe9c | 43 | _imgDown.pointerToFree = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 44 | } |
| embeddedartists | 7:4ba7bd9d32ef | 45 | } |
| embeddedartists | 7:4ba7bd9d32ef | 46 | |
| embeddedartists | 7:4ba7bd9d32ef | 47 | bool DigitButton::loadImages(const char* imgUp, const char* imgDown) |
| embeddedartists | 7:4ba7bd9d32ef | 48 | { |
| embeddedartists | 9:ce69a7adfe9c | 49 | if (_imgUp.pointerToFree != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 50 | free(_imgUp.pointerToFree); |
| embeddedartists | 9:ce69a7adfe9c | 51 | _imgUp.pointerToFree = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 52 | } |
| embeddedartists | 9:ce69a7adfe9c | 53 | if (_imgDown.pointerToFree != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 54 | free(_imgDown.pointerToFree); |
| embeddedartists | 9:ce69a7adfe9c | 55 | _imgDown.pointerToFree = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 56 | } |
| embeddedartists | 7:4ba7bd9d32ef | 57 | if (Image::decode(imgUp, Image::RES_16BIT, &_imgUp) != 0) { |
| embeddedartists | 7:4ba7bd9d32ef | 58 | DMBoard::instance().logger()->printf("Failed to load %s\n", imgUp); |
| embeddedartists | 7:4ba7bd9d32ef | 59 | return false; |
| embeddedartists | 7:4ba7bd9d32ef | 60 | } |
| embeddedartists | 7:4ba7bd9d32ef | 61 | if (imgDown != NULL) { |
| embeddedartists | 7:4ba7bd9d32ef | 62 | if (Image::decode(imgDown, Image::RES_16BIT, &_imgDown) == 0) { |
| embeddedartists | 7:4ba7bd9d32ef | 63 | DMBoard::instance().logger()->printf("Failed to load %s\n", imgDown); |
| embeddedartists | 7:4ba7bd9d32ef | 64 | return false; |
| embeddedartists | 7:4ba7bd9d32ef | 65 | } |
| embeddedartists | 7:4ba7bd9d32ef | 66 | } |
| embeddedartists | 7:4ba7bd9d32ef | 67 | _img_digit_width = _imgUp.width; |
| embeddedartists | 7:4ba7bd9d32ef | 68 | _img_digit_height = _imgUp.height/10; |
| embeddedartists | 7:4ba7bd9d32ef | 69 | _img_y_offset = _imgUp.height - 10*_img_digit_height; |
| embeddedartists | 7:4ba7bd9d32ef | 70 | _img_digit_size = ((_imgUp.res == Image::RES_16BIT)?1:2) * _img_digit_height * _img_digit_width; |
| embeddedartists | 7:4ba7bd9d32ef | 71 | return true; |
| embeddedartists | 7:4ba7bd9d32ef | 72 | } |
| embeddedartists | 7:4ba7bd9d32ef | 73 | |
| embeddedartists | 9:ce69a7adfe9c | 74 | bool DigitButton::loadImages(const Image::ImageData_t* imgUp, const Image::ImageData_t* imgDown) |
| embeddedartists | 9:ce69a7adfe9c | 75 | { |
| embeddedartists | 9:ce69a7adfe9c | 76 | if (_imgUp.pointerToFree != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 77 | free(_imgUp.pointerToFree); |
| embeddedartists | 9:ce69a7adfe9c | 78 | _imgUp.pointerToFree = NULL; |
| embeddedartists | 9:ce69a7adfe9c | 79 | } |
| embeddedartists | 9:ce69a7adfe9c | 80 | if (_imgDown.pointerToFree != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 81 | free(_imgDown.pointerToFree); |
| embeddedartists | 9:ce69a7adfe9c | 82 | _imgDown.pointerToFree = NULL; |
| embeddedartists | 9:ce69a7adfe9c | 83 | } |
| embeddedartists | 9:ce69a7adfe9c | 84 | memcpy(&_imgUp, imgUp, sizeof(Image::ImageData_t)); |
| embeddedartists | 9:ce69a7adfe9c | 85 | if (imgDown != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 86 | memcpy(&_imgDown, imgDown, sizeof(Image::ImageData_t)); |
| embeddedartists | 9:ce69a7adfe9c | 87 | } |
| embeddedartists | 9:ce69a7adfe9c | 88 | _img_digit_width = _imgUp.width; |
| embeddedartists | 9:ce69a7adfe9c | 89 | _img_digit_height = _imgUp.height/10; |
| embeddedartists | 9:ce69a7adfe9c | 90 | _img_y_offset = _imgUp.height - 10*_img_digit_height; |
| embeddedartists | 9:ce69a7adfe9c | 91 | _img_digit_size = ((_imgUp.res == Image::RES_16BIT)?1:2) * _img_digit_height * _img_digit_width; |
| embeddedartists | 9:ce69a7adfe9c | 92 | return true; |
| embeddedartists | 9:ce69a7adfe9c | 93 | } |
| embeddedartists | 9:ce69a7adfe9c | 94 | |
| embeddedartists | 7:4ba7bd9d32ef | 95 | bool DigitButton::loadImages(const unsigned char* imgUp, unsigned int imgUpSize, |
| embeddedartists | 7:4ba7bd9d32ef | 96 | const unsigned char* imgDown, unsigned int imgDownSize) |
| embeddedartists | 7:4ba7bd9d32ef | 97 | { |
| embeddedartists | 9:ce69a7adfe9c | 98 | if (_imgUp.pointerToFree != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 99 | free(_imgUp.pointerToFree); |
| embeddedartists | 9:ce69a7adfe9c | 100 | _imgUp.pointerToFree = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 101 | } |
| embeddedartists | 9:ce69a7adfe9c | 102 | if (_imgDown.pointerToFree != NULL) { |
| embeddedartists | 9:ce69a7adfe9c | 103 | free(_imgDown.pointerToFree); |
| embeddedartists | 9:ce69a7adfe9c | 104 | _imgDown.pointerToFree = NULL; |
| embeddedartists | 7:4ba7bd9d32ef | 105 | } |
| embeddedartists | 7:4ba7bd9d32ef | 106 | if (Image::decode(imgUp, imgUpSize, Image::RES_16BIT, &_imgUp) != 0) { |
| embeddedartists | 7:4ba7bd9d32ef | 107 | DMBoard::instance().logger()->printf("Failed to load %s\n", imgUp); |
| embeddedartists | 7:4ba7bd9d32ef | 108 | return false; |
| embeddedartists | 7:4ba7bd9d32ef | 109 | } |
| embeddedartists | 7:4ba7bd9d32ef | 110 | if (imgDown != NULL) { |
| embeddedartists | 7:4ba7bd9d32ef | 111 | if (Image::decode(imgDown, imgDownSize, Image::RES_16BIT, &_imgDown) == 0) { |
| embeddedartists | 7:4ba7bd9d32ef | 112 | DMBoard::instance().logger()->printf("Failed to load %s\n", imgDown); |
| embeddedartists | 7:4ba7bd9d32ef | 113 | return false; |
| embeddedartists | 7:4ba7bd9d32ef | 114 | } |
| embeddedartists | 7:4ba7bd9d32ef | 115 | } |
| embeddedartists | 7:4ba7bd9d32ef | 116 | _img_digit_width = _imgUp.width; |
| embeddedartists | 7:4ba7bd9d32ef | 117 | _img_digit_height = _imgUp.height/10; |
| embeddedartists | 7:4ba7bd9d32ef | 118 | _img_y_offset = _imgUp.height - 10*_img_digit_height; |
| embeddedartists | 7:4ba7bd9d32ef | 119 | _img_digit_size = ((_imgUp.res == Image::RES_16BIT)?1:2) * _img_digit_height * _img_digit_width; |
| embeddedartists | 7:4ba7bd9d32ef | 120 | return true; |
| embeddedartists | 7:4ba7bd9d32ef | 121 | } |
| embeddedartists | 7:4ba7bd9d32ef | 122 | |
| embeddedartists | 7:4ba7bd9d32ef | 123 | void DigitButton::setNumDigits(unsigned int num) |
| embeddedartists | 7:4ba7bd9d32ef | 124 | { |
| embeddedartists | 7:4ba7bd9d32ef | 125 | _digits = num; |
| embeddedartists | 7:4ba7bd9d32ef | 126 | draw(); |
| embeddedartists | 7:4ba7bd9d32ef | 127 | } |
| embeddedartists | 7:4ba7bd9d32ef | 128 | |
| embeddedartists | 7:4ba7bd9d32ef | 129 | void DigitButton::setValue(unsigned int val) |
| embeddedartists | 7:4ba7bd9d32ef | 130 | { |
| embeddedartists | 7:4ba7bd9d32ef | 131 | _value = val; |
| embeddedartists | 7:4ba7bd9d32ef | 132 | draw(); |
| embeddedartists | 7:4ba7bd9d32ef | 133 | } |
| embeddedartists | 7:4ba7bd9d32ef | 134 | |
| embeddedartists | 7:4ba7bd9d32ef | 135 | void DigitButton::draw(COLOR_T* fb) |
| embeddedartists | 7:4ba7bd9d32ef | 136 | { |
| embeddedartists | 7:4ba7bd9d32ef | 137 | if (fb != NULL) { |
| embeddedartists | 7:4ba7bd9d32ef | 138 | _win.fb = fb; |
| embeddedartists | 7:4ba7bd9d32ef | 139 | } |
| embeddedartists | 7:4ba7bd9d32ef | 140 | // if (_pressed) { |
| embeddedartists | 7:4ba7bd9d32ef | 141 | // if (_imgDown.pixels != NULL) { |
| embeddedartists | 7:4ba7bd9d32ef | 142 | // swim_put_image(&_win, _imgDown.pixels, _imgDown.width, _imgDown.height); |
| embeddedartists | 7:4ba7bd9d32ef | 143 | // } |
| embeddedartists | 7:4ba7bd9d32ef | 144 | // } else { |
| embeddedartists | 7:4ba7bd9d32ef | 145 | // if (_imgUp.pixels != NULL) { |
| embeddedartists | 7:4ba7bd9d32ef | 146 | // swim_put_image(&_win, _imgUp.pixels, _imgUp.width, _imgUp.height); |
| embeddedartists | 7:4ba7bd9d32ef | 147 | // } |
| embeddedartists | 7:4ba7bd9d32ef | 148 | // } |
| embeddedartists | 7:4ba7bd9d32ef | 149 | drawDigits(_pressed ? _imgDown : _imgUp); |
| embeddedartists | 7:4ba7bd9d32ef | 150 | } |
| embeddedartists | 7:4ba7bd9d32ef | 151 | |
| embeddedartists | 7:4ba7bd9d32ef | 152 | void DigitButton::drawDigits(Image::ImageData_t& img) |
| embeddedartists | 7:4ba7bd9d32ef | 153 | { |
| embeddedartists | 7:4ba7bd9d32ef | 154 | if (img.pixels != NULL) { |
| embeddedartists | 7:4ba7bd9d32ef | 155 | uint32_t v = _value; |
| embeddedartists | 7:4ba7bd9d32ef | 156 | int x = (_win.xvsize - (_digits*_img_digit_width))/2; |
| embeddedartists | 7:4ba7bd9d32ef | 157 | if (x < 0) { |
| embeddedartists | 7:4ba7bd9d32ef | 158 | x = 0; |
| embeddedartists | 7:4ba7bd9d32ef | 159 | } |
| embeddedartists | 7:4ba7bd9d32ef | 160 | int y = (_win.yvsize - _img_digit_height)/2; |
| embeddedartists | 10:651861441108 | 161 | for (unsigned int i = 0; i < _digits; i++) { |
| embeddedartists | 7:4ba7bd9d32ef | 162 | uint32_t off = (v % 10)*_img_digit_size + _img_y_offset*_img_digit_width; |
| embeddedartists | 7:4ba7bd9d32ef | 163 | 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 | 164 | v = v/10; |
| embeddedartists | 7:4ba7bd9d32ef | 165 | } |
| embeddedartists | 7:4ba7bd9d32ef | 166 | } |
| embeddedartists | 7:4ba7bd9d32ef | 167 | } |
