Basic swim GUI for LPC4088
Fork of DMBasicGUI by
Application/AppLauncher.cpp@38:1b7c79a10e14, 2016-03-14 (annotated)
- Committer:
- redbird
- Date:
- Mon Mar 14 20:14:06 2016 -0600
- Revision:
- 38:1b7c79a10e14
- Parent:
- 37:4a87841ebbb7
revert programming
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:4977187e90c7 | 1 | /* |
embeddedartists | 0:4977187e90c7 | 2 | * Copyright 2014 Embedded Artists AB |
embeddedartists | 0:4977187e90c7 | 3 | * |
embeddedartists | 0:4977187e90c7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
embeddedartists | 0:4977187e90c7 | 5 | * you may not use this file except in compliance with the License. |
embeddedartists | 0:4977187e90c7 | 6 | * You may obtain a copy of the License at |
embeddedartists | 0:4977187e90c7 | 7 | * |
embeddedartists | 0:4977187e90c7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
embeddedartists | 0:4977187e90c7 | 9 | * |
embeddedartists | 0:4977187e90c7 | 10 | * Unless required by applicable law or agreed to in writing, software |
embeddedartists | 0:4977187e90c7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
embeddedartists | 0:4977187e90c7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
embeddedartists | 0:4977187e90c7 | 13 | * See the License for the specific language governing permissions and |
embeddedartists | 0:4977187e90c7 | 14 | * limitations under the License. |
embeddedartists | 0:4977187e90c7 | 15 | */ |
embeddedartists | 0:4977187e90c7 | 16 | |
embeddedartists | 0:4977187e90c7 | 17 | |
embeddedartists | 0:4977187e90c7 | 18 | #include "mbed.h" |
embeddedartists | 0:4977187e90c7 | 19 | #include "AppLauncher.h" |
embeddedartists | 0:4977187e90c7 | 20 | #include "lpc_swim_font.h" |
embeddedartists | 0:4977187e90c7 | 21 | #include "Button.h" |
embeddedartists | 3:3fabfe3339b8 | 22 | #include "ImageButton.h" |
redbird |
30:f1679daafa2e | 23 | #include "lpc_winfreesystem14x16.h" |
embeddedartists | 0:4977187e90c7 | 24 | |
embeddedartists | 0:4977187e90c7 | 25 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 26 | * Defines and typedefs |
embeddedartists | 0:4977187e90c7 | 27 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 28 | |
embeddedartists | 0:4977187e90c7 | 29 | #define APP_PREFIX "[Launcher] " |
embeddedartists | 0:4977187e90c7 | 30 | |
embeddedartists | 5:f4de114c31c3 | 31 | #define NO_APPLICATION (-1) |
embeddedartists | 0:4977187e90c7 | 32 | |
embeddedartists | 8:19a6b70d42b1 | 33 | #define APP_SIGID_TOUCH 0x1 |
embeddedartists | 0:4977187e90c7 | 34 | |
embeddedartists | 0:4977187e90c7 | 35 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 36 | * Private variables |
embeddedartists | 0:4977187e90c7 | 37 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 38 | |
embeddedartists | 5:f4de114c31c3 | 39 | static int appToLaunch = NO_APPLICATION; |
embeddedartists | 8:19a6b70d42b1 | 40 | static osThreadId appThread; |
embeddedartists | 0:4977187e90c7 | 41 | |
embeddedartists | 0:4977187e90c7 | 42 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 43 | * Private functions |
embeddedartists | 0:4977187e90c7 | 44 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 45 | |
embeddedartists | 0:4977187e90c7 | 46 | static void buttonClicked(uint32_t x) |
embeddedartists | 0:4977187e90c7 | 47 | { |
embeddedartists | 5:f4de114c31c3 | 48 | if (appToLaunch == NO_APPLICATION) { |
embeddedartists | 5:f4de114c31c3 | 49 | appToLaunch = (int)x; |
embeddedartists | 0:4977187e90c7 | 50 | } |
embeddedartists | 0:4977187e90c7 | 51 | } |
embeddedartists | 0:4977187e90c7 | 52 | |
embeddedartists | 0:4977187e90c7 | 53 | void AppLauncher::draw() |
embeddedartists | 0:4977187e90c7 | 54 | { |
embeddedartists | 0:4977187e90c7 | 55 | // Prepare fullscreen |
embeddedartists | 0:4977187e90c7 | 56 | swim_window_open(_win, |
embeddedartists | 0:4977187e90c7 | 57 | _disp->width(), _disp->height(), // full size |
embeddedartists | 0:4977187e90c7 | 58 | (COLOR_T*)_fb, |
embeddedartists | 0:4977187e90c7 | 59 | 0,0,_disp->width()-1, _disp->height()-1, // window position and size |
redbird |
28:5457bdb06bbc | 60 | 0, // border |
redbird | 36:93d7ea74dd27 | 61 | BLACK, WHITE, BLACK); // colors: pen, backgr, forgr |
redbird | 37:4a87841ebbb7 | 62 | swim_set_font(_win, (FONT_T*)&font_winfreesys14x16); |
redbird |
30:f1679daafa2e | 63 | |
embeddedartists | 8:19a6b70d42b1 | 64 | if (_supportsCalibration) { |
embeddedartists | 8:19a6b70d42b1 | 65 | const char* msg = "(Press physical UserButton >2s to calibrate touch)"; |
embeddedartists | 8:19a6b70d42b1 | 66 | int w, h; |
embeddedartists | 8:19a6b70d42b1 | 67 | swim_get_string_bounds(_win, msg, &w, &h); |
redbird |
29:462eb35c5947 | 68 | //swim_put_text_xy(_win, msg, (_disp->width()-w)/2, _disp->height()-h*4); |
embeddedartists | 8:19a6b70d42b1 | 69 | } |
embeddedartists | 5:f4de114c31c3 | 70 | |
embeddedartists | 5:f4de114c31c3 | 71 | for (int i = 0; i < _usedButtons; i++) { |
embeddedartists | 5:f4de114c31c3 | 72 | _buttons[i]->draw(); |
embeddedartists | 5:f4de114c31c3 | 73 | } |
embeddedartists | 0:4977187e90c7 | 74 | } |
embeddedartists | 0:4977187e90c7 | 75 | |
embeddedartists | 8:19a6b70d42b1 | 76 | void AppLauncher::onTouchEvent() |
embeddedartists | 8:19a6b70d42b1 | 77 | { |
embeddedartists | 8:19a6b70d42b1 | 78 | _newTouchEvent = true; |
embeddedartists | 8:19a6b70d42b1 | 79 | osSignalSet(appThread, APP_SIGID_TOUCH); |
embeddedartists | 8:19a6b70d42b1 | 80 | } |
embeddedartists | 8:19a6b70d42b1 | 81 | |
embeddedartists | 8:19a6b70d42b1 | 82 | void AppLauncher::onButtonEvent() |
embeddedartists | 8:19a6b70d42b1 | 83 | { |
embeddedartists | 8:19a6b70d42b1 | 84 | _newButtonEvent = true; |
embeddedartists | 8:19a6b70d42b1 | 85 | osSignalSet(appThread, APP_SIGID_TOUCH); |
embeddedartists | 8:19a6b70d42b1 | 86 | } |
embeddedartists | 8:19a6b70d42b1 | 87 | |
embeddedartists | 11:265884fa7fdd | 88 | static void onTimeoutEvent(void const* arg) |
embeddedartists | 8:19a6b70d42b1 | 89 | { |
embeddedartists | 8:19a6b70d42b1 | 90 | *((bool*)arg) = true; |
embeddedartists | 8:19a6b70d42b1 | 91 | osSignalSet(appThread, APP_SIGID_TOUCH); |
embeddedartists | 8:19a6b70d42b1 | 92 | } |
embeddedartists | 8:19a6b70d42b1 | 93 | |
embeddedartists | 0:4977187e90c7 | 94 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 95 | * Public functions |
embeddedartists | 0:4977187e90c7 | 96 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 97 | |
embeddedartists | 0:4977187e90c7 | 98 | AppLauncher::AppLauncher() : _disp(NULL), _win(NULL), _fb(NULL), _usedButtons(0) |
embeddedartists | 0:4977187e90c7 | 99 | { |
embeddedartists | 0:4977187e90c7 | 100 | for (int i = 0; i < NumberOfButtons; i++) { |
embeddedartists | 0:4977187e90c7 | 101 | _buttons[i] = NULL; |
embeddedartists | 0:4977187e90c7 | 102 | } |
embeddedartists | 8:19a6b70d42b1 | 103 | bool r; |
embeddedartists | 8:19a6b70d42b1 | 104 | int n; |
embeddedartists | 8:19a6b70d42b1 | 105 | if (DMBoard::instance().touchPanel()->info(&r, &n, &_supportsCalibration) != TouchPanel::TouchError_Ok) { |
embeddedartists | 8:19a6b70d42b1 | 106 | _supportsCalibration = false; |
embeddedartists | 8:19a6b70d42b1 | 107 | } |
embeddedartists | 0:4977187e90c7 | 108 | } |
embeddedartists | 0:4977187e90c7 | 109 | |
embeddedartists | 0:4977187e90c7 | 110 | AppLauncher::~AppLauncher() |
embeddedartists | 0:4977187e90c7 | 111 | { |
embeddedartists | 0:4977187e90c7 | 112 | teardown(); |
embeddedartists | 0:4977187e90c7 | 113 | } |
embeddedartists | 0:4977187e90c7 | 114 | |
embeddedartists | 0:4977187e90c7 | 115 | bool AppLauncher::setup() |
embeddedartists | 0:4977187e90c7 | 116 | { |
embeddedartists | 0:4977187e90c7 | 117 | RtosLog* log = DMBoard::instance().logger(); |
embeddedartists | 0:4977187e90c7 | 118 | |
embeddedartists | 0:4977187e90c7 | 119 | _disp = DMBoard::instance().display(); |
embeddedartists | 0:4977187e90c7 | 120 | _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T)); |
embeddedartists | 0:4977187e90c7 | 121 | _fb = _disp->allocateFramebuffer(); |
embeddedartists | 0:4977187e90c7 | 122 | |
embeddedartists | 0:4977187e90c7 | 123 | if (_win == NULL || _fb == NULL) { |
embeddedartists | 0:4977187e90c7 | 124 | log->printf(APP_PREFIX"Failed to allocate memory for framebuffer\r\n"); |
embeddedartists | 0:4977187e90c7 | 125 | return false; |
embeddedartists | 0:4977187e90c7 | 126 | } |
embeddedartists | 0:4977187e90c7 | 127 | |
embeddedartists | 0:4977187e90c7 | 128 | return true; |
embeddedartists | 0:4977187e90c7 | 129 | } |
embeddedartists | 0:4977187e90c7 | 130 | |
embeddedartists | 0:4977187e90c7 | 131 | void AppLauncher::runToCompletion() |
embeddedartists | 0:4977187e90c7 | 132 | { |
embeddedartists | 0:4977187e90c7 | 133 | DMBoard* board = &DMBoard::instance(); |
embeddedartists | 0:4977187e90c7 | 134 | RtosLog* log = board->logger(); |
embeddedartists | 8:19a6b70d42b1 | 135 | |
embeddedartists | 8:19a6b70d42b1 | 136 | appThread = osThreadGetId(); |
embeddedartists | 0:4977187e90c7 | 137 | |
embeddedartists | 0:4977187e90c7 | 138 | // Draw something on the framebuffer before using it so that it doesn't look garbled |
embeddedartists | 0:4977187e90c7 | 139 | draw(); |
embeddedartists | 0:4977187e90c7 | 140 | |
embeddedartists | 0:4977187e90c7 | 141 | // Start display in default mode (16-bit) |
embeddedartists | 0:4977187e90c7 | 142 | Display::DisplayError disperr = _disp->powerUp(_fb); |
embeddedartists | 3:3fabfe3339b8 | 143 | if (disperr != Display::DisplayError_Ok) { |
embeddedartists | 0:4977187e90c7 | 144 | log->printf(APP_PREFIX"Failed to initialize the display, got error %d\r\n", disperr); |
embeddedartists | 0:4977187e90c7 | 145 | return; |
embeddedartists | 0:4977187e90c7 | 146 | } |
embeddedartists | 8:19a6b70d42b1 | 147 | |
embeddedartists | 0:4977187e90c7 | 148 | // To keep track of the button pushes |
embeddedartists | 0:4977187e90c7 | 149 | bool buttonPressed = false; |
embeddedartists | 8:19a6b70d42b1 | 150 | bool buttonTimeout = false; |
embeddedartists | 12:53601973f7eb | 151 | InterruptIn button(P2_10); |
embeddedartists | 8:19a6b70d42b1 | 152 | button.rise(this, &AppLauncher::onButtonEvent); |
embeddedartists | 8:19a6b70d42b1 | 153 | button.fall(this, &AppLauncher::onButtonEvent); |
embeddedartists | 8:19a6b70d42b1 | 154 | RtosTimer buttonTimer(onTimeoutEvent, osTimerOnce, &buttonTimeout); |
embeddedartists | 0:4977187e90c7 | 155 | |
embeddedartists | 0:4977187e90c7 | 156 | // Wait for touches |
embeddedartists | 0:4977187e90c7 | 157 | TouchPanel* touch = board->touchPanel(); |
embeddedartists | 8:19a6b70d42b1 | 158 | FunctionPointer* fpOld = touch->setListener(new FunctionPointer(this, &AppLauncher::onTouchEvent)); |
embeddedartists | 8:19a6b70d42b1 | 159 | touch_coordinate_t coord; |
embeddedartists | 8:19a6b70d42b1 | 160 | while (true) { |
embeddedartists | 8:19a6b70d42b1 | 161 | Thread::signal_wait(APP_SIGID_TOUCH); |
embeddedartists | 8:19a6b70d42b1 | 162 | if (_newTouchEvent) { |
embeddedartists | 8:19a6b70d42b1 | 163 | if (buttonPressed) { |
embeddedartists | 8:19a6b70d42b1 | 164 | // cancel |
embeddedartists | 8:19a6b70d42b1 | 165 | buttonPressed = false; |
embeddedartists | 8:19a6b70d42b1 | 166 | buttonTimer.stop(); |
embeddedartists | 8:19a6b70d42b1 | 167 | } |
embeddedartists | 8:19a6b70d42b1 | 168 | _newTouchEvent = false; |
embeddedartists | 8:19a6b70d42b1 | 169 | if (touch->read(coord) != TouchPanel::TouchError_Ok) { |
embeddedartists | 8:19a6b70d42b1 | 170 | log->printf("Failed to read touch coordinate\n"); |
embeddedartists | 8:19a6b70d42b1 | 171 | continue; |
embeddedartists | 8:19a6b70d42b1 | 172 | } |
embeddedartists | 8:19a6b70d42b1 | 173 | // Process the touch coordinate for each button |
embeddedartists | 8:19a6b70d42b1 | 174 | for (int i = 0; i < NumberOfButtons; i++) { |
embeddedartists | 0:4977187e90c7 | 175 | if (_buttons[i] != NULL) { |
embeddedartists | 8:19a6b70d42b1 | 176 | if (_buttons[i]->handle(coord.x, coord.y, coord.z > 0)) { |
embeddedartists | 8:19a6b70d42b1 | 177 | _buttons[i]->draw(); |
embeddedartists | 8:19a6b70d42b1 | 178 | } |
embeddedartists | 0:4977187e90c7 | 179 | } |
embeddedartists | 8:19a6b70d42b1 | 180 | } |
embeddedartists | 8:19a6b70d42b1 | 181 | } else if (buttonTimeout) { |
embeddedartists | 8:19a6b70d42b1 | 182 | if (board->buttonPressed()) { |
embeddedartists | 8:19a6b70d42b1 | 183 | appToLaunch = CalibrationApp; |
embeddedartists | 8:19a6b70d42b1 | 184 | } |
embeddedartists | 8:19a6b70d42b1 | 185 | buttonPressed = false; |
embeddedartists | 8:19a6b70d42b1 | 186 | buttonTimeout = false; |
embeddedartists | 8:19a6b70d42b1 | 187 | } else if (_newButtonEvent) { |
embeddedartists | 8:19a6b70d42b1 | 188 | _newButtonEvent = false; |
embeddedartists | 8:19a6b70d42b1 | 189 | if (board->buttonPressed()) { |
embeddedartists | 8:19a6b70d42b1 | 190 | buttonPressed = true; |
embeddedartists | 8:19a6b70d42b1 | 191 | buttonTimer.start(2000); |
embeddedartists | 8:19a6b70d42b1 | 192 | } else { |
embeddedartists | 8:19a6b70d42b1 | 193 | buttonPressed = false; |
embeddedartists | 0:4977187e90c7 | 194 | buttonTimer.stop(); |
embeddedartists | 8:19a6b70d42b1 | 195 | } |
embeddedartists | 8:19a6b70d42b1 | 196 | continue; |
embeddedartists | 0:4977187e90c7 | 197 | } |
embeddedartists | 0:4977187e90c7 | 198 | |
embeddedartists | 5:f4de114c31c3 | 199 | if (appToLaunch != NO_APPLICATION) { |
embeddedartists | 0:4977187e90c7 | 200 | App* a = NULL; |
embeddedartists | 5:f4de114c31c3 | 201 | if (_callback != NULL) { |
embeddedartists | 5:f4de114c31c3 | 202 | a = _callback(appToLaunch); |
embeddedartists | 0:4977187e90c7 | 203 | } |
embeddedartists | 0:4977187e90c7 | 204 | if (a != NULL) { |
embeddedartists | 0:4977187e90c7 | 205 | if (a->setup()) { |
embeddedartists | 0:4977187e90c7 | 206 | a->runToCompletion(); |
embeddedartists | 0:4977187e90c7 | 207 | a->teardown(); |
embeddedartists | 0:4977187e90c7 | 208 | } |
embeddedartists | 0:4977187e90c7 | 209 | delete a; |
embeddedartists | 0:4977187e90c7 | 210 | } |
embeddedartists | 5:f4de114c31c3 | 211 | appToLaunch = NO_APPLICATION; |
embeddedartists | 5:f4de114c31c3 | 212 | } |
embeddedartists | 0:4977187e90c7 | 213 | } |
embeddedartists | 8:19a6b70d42b1 | 214 | |
redbird | 38:1b7c79a10e14 | 215 | restore old touch listener (get our listener in return) |
redbird | 38:1b7c79a10e14 | 216 | fpOld = touch->setListener(fpOld); |
redbird | 38:1b7c79a10e14 | 217 | delete fpOld; |
embeddedartists | 0:4977187e90c7 | 218 | } |
embeddedartists | 0:4977187e90c7 | 219 | |
embeddedartists | 0:4977187e90c7 | 220 | bool AppLauncher::teardown() |
embeddedartists | 0:4977187e90c7 | 221 | { |
embeddedartists | 0:4977187e90c7 | 222 | if (_win != NULL) { |
embeddedartists | 0:4977187e90c7 | 223 | free(_win); |
embeddedartists | 0:4977187e90c7 | 224 | _win = NULL; |
embeddedartists | 0:4977187e90c7 | 225 | } |
embeddedartists | 0:4977187e90c7 | 226 | if (_fb != NULL) { |
embeddedartists | 0:4977187e90c7 | 227 | free(_fb); |
embeddedartists | 0:4977187e90c7 | 228 | _fb = NULL; |
embeddedartists | 0:4977187e90c7 | 229 | } |
embeddedartists | 0:4977187e90c7 | 230 | for (int i = 0; i < NumberOfButtons; i++) { |
embeddedartists | 0:4977187e90c7 | 231 | _buttons[i] = NULL; |
embeddedartists | 0:4977187e90c7 | 232 | if (_buttons[i] != NULL) { |
embeddedartists | 0:4977187e90c7 | 233 | delete _buttons[i]; |
embeddedartists | 0:4977187e90c7 | 234 | _buttons[i] = NULL; |
embeddedartists | 0:4977187e90c7 | 235 | } |
embeddedartists | 0:4977187e90c7 | 236 | } |
embeddedartists | 0:4977187e90c7 | 237 | return true; |
embeddedartists | 0:4977187e90c7 | 238 | } |
embeddedartists | 0:4977187e90c7 | 239 | |
embeddedartists | 5:f4de114c31c3 | 240 | void AppLauncher::setAppCreatorFunc(App*(*callback)(uint32_t buttonID)) |
embeddedartists | 5:f4de114c31c3 | 241 | { |
embeddedartists | 5:f4de114c31c3 | 242 | _callback = callback; |
embeddedartists | 5:f4de114c31c3 | 243 | } |
embeddedartists | 0:4977187e90c7 | 244 | |
embeddedartists | 5:f4de114c31c3 | 245 | bool AppLauncher::addButton(uint32_t buttonID, const char* caption) |
embeddedartists | 5:f4de114c31c3 | 246 | { |
embeddedartists | 5:f4de114c31c3 | 247 | int idx = _usedButtons++; |
embeddedartists | 5:f4de114c31c3 | 248 | int xspace = ((_disp->width() - ButtonColumns * ButtonWidth) / (ButtonColumns + 1)); |
embeddedartists | 5:f4de114c31c3 | 249 | int yspace = ((_disp->height() - TitleHeight - ButtonRows * ButtonHeight) / (ButtonRows + 1)); |
embeddedartists | 5:f4de114c31c3 | 250 | |
embeddedartists | 5:f4de114c31c3 | 251 | _buttons[idx] = new Button(caption, (COLOR_T*)_fb, |
embeddedartists | 5:f4de114c31c3 | 252 | xspace + (ButtonWidth + xspace)*(idx%ButtonColumns), |
embeddedartists | 5:f4de114c31c3 | 253 | TitleHeight + yspace + (ButtonHeight + yspace)*(idx/ButtonColumns), |
embeddedartists | 5:f4de114c31c3 | 254 | ButtonWidth, ButtonHeight); |
embeddedartists | 5:f4de114c31c3 | 255 | _buttons[idx]->setAction(buttonClicked, buttonID); |
embeddedartists | 5:f4de114c31c3 | 256 | //_buttons[idx]->draw(); |
embeddedartists | 5:f4de114c31c3 | 257 | return true; |
embeddedartists | 5:f4de114c31c3 | 258 | } |
embeddedartists | 5:f4de114c31c3 | 259 | |
embeddedartists | 5:f4de114c31c3 | 260 | bool AppLauncher::addImageButton(uint32_t buttonID, const char* imgUp, const char* imgDown) |
embeddedartists | 5:f4de114c31c3 | 261 | { |
embeddedartists | 11:265884fa7fdd | 262 | return addImageButton(buttonID, NULL, BLACK, imgUp, imgDown); |
embeddedartists | 11:265884fa7fdd | 263 | } |
embeddedartists | 11:265884fa7fdd | 264 | |
embeddedartists | 11:265884fa7fdd | 265 | bool AppLauncher::addImageButton(uint32_t buttonID, const char* caption, COLOR_T color, const char* imgUp, const char* imgDown) |
embeddedartists | 11:265884fa7fdd | 266 | { |
embeddedartists | 5:f4de114c31c3 | 267 | int idx = _usedButtons++; |
redbird |
24:bd216e6a4628 | 268 | int xspace = ((_disp->width() - ButtonColumns * 128) / (ButtonColumns + 1)); |
redbird |
24:bd216e6a4628 | 269 | int yspace = ((_disp->height() - TitleHeight - ButtonRows * 128) / (ButtonRows + 1)); |
embeddedartists | 11:265884fa7fdd | 270 | int yoff = (caption == NULL) ? 0 : -15; // compensate for caption taking up space |
embeddedartists | 5:f4de114c31c3 | 271 | |
embeddedartists | 5:f4de114c31c3 | 272 | ImageButton* img = new ImageButton((COLOR_T*)_fb, |
redbird |
24:bd216e6a4628 | 273 | xspace + (128 + xspace)*(idx%ButtonColumns), |
redbird |
24:bd216e6a4628 | 274 | TitleHeight + yspace + (128 + yspace)*(idx/ButtonColumns) + yoff, |
redbird |
24:bd216e6a4628 | 275 | 128, 128, caption, color); |
embeddedartists | 5:f4de114c31c3 | 276 | if (img->loadImages(imgUp, imgDown)) { |
embeddedartists | 5:f4de114c31c3 | 277 | _buttons[idx] = img; |
embeddedartists | 5:f4de114c31c3 | 278 | _buttons[idx]->setAction(buttonClicked, buttonID); |
embeddedartists | 5:f4de114c31c3 | 279 | //_buttons[idx]->draw(); |
embeddedartists | 5:f4de114c31c3 | 280 | return true; |
embeddedartists | 5:f4de114c31c3 | 281 | } else { |
embeddedartists | 5:f4de114c31c3 | 282 | //DMBoard::instance().logger()->printf("Failed to load image for buttonID %u, %s[%s]\n", buttonID, imgUp, imgDown==NULL?"":imgDown); |
embeddedartists | 5:f4de114c31c3 | 283 | return false; |
embeddedartists | 5:f4de114c31c3 | 284 | } |
embeddedartists | 5:f4de114c31c3 | 285 | } |
embeddedartists | 5:f4de114c31c3 | 286 | |
embeddedartists | 5:f4de114c31c3 | 287 | bool AppLauncher::addImageButton(uint32_t buttonID, const unsigned char* imgUp, unsigned int imgUpSize, const unsigned char* imgDown, unsigned int imgDownSize) |
embeddedartists | 5:f4de114c31c3 | 288 | { |
embeddedartists | 11:265884fa7fdd | 289 | return addImageButton(buttonID, NULL, BLACK, imgUp, imgUpSize, imgDown, imgDownSize); |
embeddedartists | 11:265884fa7fdd | 290 | } |
embeddedartists | 11:265884fa7fdd | 291 | |
embeddedartists | 11:265884fa7fdd | 292 | bool AppLauncher::addImageButton(uint32_t buttonID, const char* caption, COLOR_T color, const unsigned char* imgUp, unsigned int imgUpSize, const unsigned char* imgDown, unsigned int imgDownSize) |
embeddedartists | 11:265884fa7fdd | 293 | { |
embeddedartists | 5:f4de114c31c3 | 294 | int idx = _usedButtons++; |
redbird |
24:bd216e6a4628 | 295 | int xspace = ((_disp->width() - ButtonColumns * 128) / (ButtonColumns + 1)); |
redbird |
24:bd216e6a4628 | 296 | int yspace = ((_disp->height() - TitleHeight - ButtonRows * 128) / (ButtonRows + 1)); |
embeddedartists | 11:265884fa7fdd | 297 | int yoff = (caption == NULL) ? 0 : -15; // compensate for caption taking up space |
embeddedartists | 5:f4de114c31c3 | 298 | |
embeddedartists | 5:f4de114c31c3 | 299 | ImageButton* img = new ImageButton((COLOR_T*)_fb, |
redbird |
24:bd216e6a4628 | 300 | xspace + (128 + xspace)*(idx%ButtonColumns), |
redbird |
24:bd216e6a4628 | 301 | TitleHeight + yspace + (128 + yspace)*(idx/ButtonColumns) + yoff, |
redbird |
24:bd216e6a4628 | 302 | 128, 128, caption, color); |
embeddedartists | 5:f4de114c31c3 | 303 | if (img->loadImages(imgUp, imgUpSize, imgDown, imgDownSize)) { |
embeddedartists | 5:f4de114c31c3 | 304 | _buttons[idx] = img; |
embeddedartists | 5:f4de114c31c3 | 305 | _buttons[idx]->setAction(buttonClicked, buttonID); |
embeddedartists | 5:f4de114c31c3 | 306 | //_buttons[idx]->draw(); |
embeddedartists | 5:f4de114c31c3 | 307 | return true; |
embeddedartists | 5:f4de114c31c3 | 308 | } else { |
embeddedartists | 5:f4de114c31c3 | 309 | //DMBoard::instance().logger()->printf("Failed to load image for buttonID %u, %s[%s]\n", buttonID, imgUp, imgDown==NULL?"":imgDown); |
embeddedartists | 5:f4de114c31c3 | 310 | return false; |
embeddedartists | 5:f4de114c31c3 | 311 | } |
embeddedartists | 5:f4de114c31c3 | 312 | } |