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