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
Application/AppLauncher.cpp@0:4977187e90c7, 2014-12-11 (annotated)
- Committer:
- embeddedartists
- Date:
- Thu Dec 11 11:03:57 2014 +0000
- Revision:
- 0:4977187e90c7
- Child:
- 3:3fabfe3339b8
First version
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 "AppSettings.h" |
embeddedartists | 0:4977187e90c7 | 21 | #include "AppTouchCalibration.h" |
embeddedartists | 0:4977187e90c7 | 22 | #include "AppColorPicker.h" |
embeddedartists | 0:4977187e90c7 | 23 | #include "AppImageViewer.h" |
embeddedartists | 0:4977187e90c7 | 24 | #include "lpc_swim_font.h" |
embeddedartists | 0:4977187e90c7 | 25 | #include "Button.h" |
embeddedartists | 0:4977187e90c7 | 26 | |
embeddedartists | 0:4977187e90c7 | 27 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 28 | * Defines and typedefs |
embeddedartists | 0:4977187e90c7 | 29 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 30 | |
embeddedartists | 0:4977187e90c7 | 31 | #define APP_PREFIX "[Launcher] " |
embeddedartists | 0:4977187e90c7 | 32 | |
embeddedartists | 0:4977187e90c7 | 33 | |
embeddedartists | 0:4977187e90c7 | 34 | typedef enum { |
embeddedartists | 0:4977187e90c7 | 35 | NoApplication = -1, |
embeddedartists | 0:4977187e90c7 | 36 | SettingsApp = 0, |
embeddedartists | 0:4977187e90c7 | 37 | ColorPicker, |
embeddedartists | 0:4977187e90c7 | 38 | TouchTestApp, |
embeddedartists | 0:4977187e90c7 | 39 | ImageViewerApp, |
embeddedartists | 0:4977187e90c7 | 40 | //SlideshowApp, |
embeddedartists | 0:4977187e90c7 | 41 | //TouchGFXApp, |
embeddedartists | 0:4977187e90c7 | 42 | //EmWinApp, |
embeddedartists | 0:4977187e90c7 | 43 | CalibrationApp = 100, |
embeddedartists | 0:4977187e90c7 | 44 | } AppID_t; |
embeddedartists | 0:4977187e90c7 | 45 | |
embeddedartists | 0:4977187e90c7 | 46 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 47 | * Private variables |
embeddedartists | 0:4977187e90c7 | 48 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 49 | |
embeddedartists | 0:4977187e90c7 | 50 | static AppID_t appToLaunch = NoApplication; |
embeddedartists | 0:4977187e90c7 | 51 | |
embeddedartists | 0:4977187e90c7 | 52 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 53 | * Private functions |
embeddedartists | 0:4977187e90c7 | 54 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 55 | |
embeddedartists | 0:4977187e90c7 | 56 | static void buttonClicked(uint32_t x) |
embeddedartists | 0:4977187e90c7 | 57 | { |
embeddedartists | 0:4977187e90c7 | 58 | if (appToLaunch == NoApplication) { |
embeddedartists | 0:4977187e90c7 | 59 | appToLaunch = (AppID_t)x; |
embeddedartists | 0:4977187e90c7 | 60 | } |
embeddedartists | 0:4977187e90c7 | 61 | } |
embeddedartists | 0:4977187e90c7 | 62 | |
embeddedartists | 0:4977187e90c7 | 63 | void AppLauncher::addButton(uint32_t buttonID, const char* caption) |
embeddedartists | 0:4977187e90c7 | 64 | { |
embeddedartists | 0:4977187e90c7 | 65 | int idx = _usedButtons++; |
embeddedartists | 0:4977187e90c7 | 66 | int xspace = ((_disp->width() - ButtonColumns * ButtonWidth) / (ButtonColumns + 1)); |
embeddedartists | 0:4977187e90c7 | 67 | int yspace = ((_disp->height() - TitleHeight - ButtonRows * ButtonHeight) / (ButtonRows + 1)); |
embeddedartists | 0:4977187e90c7 | 68 | |
embeddedartists | 0:4977187e90c7 | 69 | _buttons[idx] = new Button(caption, (COLOR_T*)_fb, |
embeddedartists | 0:4977187e90c7 | 70 | xspace + (ButtonWidth + xspace)*(idx%ButtonColumns), |
embeddedartists | 0:4977187e90c7 | 71 | TitleHeight + yspace + (ButtonHeight + yspace)*(idx/ButtonColumns), |
embeddedartists | 0:4977187e90c7 | 72 | ButtonWidth, ButtonHeight); |
embeddedartists | 0:4977187e90c7 | 73 | _buttons[idx]->setAction(buttonClicked, buttonID); |
embeddedartists | 0:4977187e90c7 | 74 | _buttons[idx]->draw(); |
embeddedartists | 0:4977187e90c7 | 75 | } |
embeddedartists | 0:4977187e90c7 | 76 | |
embeddedartists | 0:4977187e90c7 | 77 | void AppLauncher::draw() |
embeddedartists | 0:4977187e90c7 | 78 | { |
embeddedartists | 0:4977187e90c7 | 79 | // Prepare fullscreen |
embeddedartists | 0:4977187e90c7 | 80 | swim_window_open(_win, |
embeddedartists | 0:4977187e90c7 | 81 | _disp->width(), _disp->height(), // full size |
embeddedartists | 0:4977187e90c7 | 82 | (COLOR_T*)_fb, |
embeddedartists | 0:4977187e90c7 | 83 | 0,0,_disp->width()-1, _disp->height()-1, // window position and size |
embeddedartists | 0:4977187e90c7 | 84 | 1, // border |
embeddedartists | 0:4977187e90c7 | 85 | WHITE, RED, BLACK); // colors: pen, backgr, forgr |
embeddedartists | 0:4977187e90c7 | 86 | swim_set_title(_win, "Demo Program", BLACK); |
embeddedartists | 0:4977187e90c7 | 87 | |
embeddedartists | 0:4977187e90c7 | 88 | // Add many buttons |
embeddedartists | 0:4977187e90c7 | 89 | addButton(SettingsApp, "Settings"); |
embeddedartists | 0:4977187e90c7 | 90 | addButton(TouchTestApp, "Test Touch"); |
embeddedartists | 0:4977187e90c7 | 91 | //addButton(SlideshowApp, "Slideshow"); |
embeddedartists | 0:4977187e90c7 | 92 | //addButton(TouchGFXApp, "TouchGFX"); |
embeddedartists | 0:4977187e90c7 | 93 | //addButton(EmWinApp, "emWin"); |
embeddedartists | 0:4977187e90c7 | 94 | addButton(ColorPicker, "Color Picker"); |
embeddedartists | 0:4977187e90c7 | 95 | addButton(ImageViewerApp, "Image Viewer"); |
embeddedartists | 0:4977187e90c7 | 96 | //addButton(5, "Button 5"); |
embeddedartists | 0:4977187e90c7 | 97 | //addButton(6, "Button 6"); |
embeddedartists | 0:4977187e90c7 | 98 | //addButton(7, "Button 7"); |
embeddedartists | 0:4977187e90c7 | 99 | //addButton(8, "Button 8"); |
embeddedartists | 0:4977187e90c7 | 100 | //addButton(9, "Button 9"); |
embeddedartists | 0:4977187e90c7 | 101 | |
embeddedartists | 0:4977187e90c7 | 102 | const char* msg = "(Press physical UserButton >2s to calibrate touch)"; |
embeddedartists | 0:4977187e90c7 | 103 | int w, h; |
embeddedartists | 0:4977187e90c7 | 104 | swim_get_string_bounds(_win, msg, &w, &h); |
embeddedartists | 0:4977187e90c7 | 105 | swim_put_text_xy(_win, msg, (_disp->width()-w)/2, _disp->height()-h*4); |
embeddedartists | 0:4977187e90c7 | 106 | } |
embeddedartists | 0:4977187e90c7 | 107 | |
embeddedartists | 0:4977187e90c7 | 108 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 109 | * Public functions |
embeddedartists | 0:4977187e90c7 | 110 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 111 | |
embeddedartists | 0:4977187e90c7 | 112 | AppLauncher::AppLauncher() : _disp(NULL), _win(NULL), _fb(NULL), _usedButtons(0) |
embeddedartists | 0:4977187e90c7 | 113 | { |
embeddedartists | 0:4977187e90c7 | 114 | for (int i = 0; i < NumberOfButtons; i++) { |
embeddedartists | 0:4977187e90c7 | 115 | _buttons[i] = NULL; |
embeddedartists | 0:4977187e90c7 | 116 | } |
embeddedartists | 0:4977187e90c7 | 117 | } |
embeddedartists | 0:4977187e90c7 | 118 | |
embeddedartists | 0:4977187e90c7 | 119 | AppLauncher::~AppLauncher() |
embeddedartists | 0:4977187e90c7 | 120 | { |
embeddedartists | 0:4977187e90c7 | 121 | teardown(); |
embeddedartists | 0:4977187e90c7 | 122 | } |
embeddedartists | 0:4977187e90c7 | 123 | |
embeddedartists | 0:4977187e90c7 | 124 | bool AppLauncher::setup() |
embeddedartists | 0:4977187e90c7 | 125 | { |
embeddedartists | 0:4977187e90c7 | 126 | RtosLog* log = DMBoard::instance().logger(); |
embeddedartists | 0:4977187e90c7 | 127 | |
embeddedartists | 0:4977187e90c7 | 128 | _disp = DMBoard::instance().display(); |
embeddedartists | 0:4977187e90c7 | 129 | _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T)); |
embeddedartists | 0:4977187e90c7 | 130 | _fb = _disp->allocateFramebuffer(); |
embeddedartists | 0:4977187e90c7 | 131 | |
embeddedartists | 0:4977187e90c7 | 132 | if (_win == NULL || _fb == NULL) { |
embeddedartists | 0:4977187e90c7 | 133 | log->printf(APP_PREFIX"Failed to allocate memory for framebuffer\r\n"); |
embeddedartists | 0:4977187e90c7 | 134 | return false; |
embeddedartists | 0:4977187e90c7 | 135 | } |
embeddedartists | 0:4977187e90c7 | 136 | |
embeddedartists | 0:4977187e90c7 | 137 | return true; |
embeddedartists | 0:4977187e90c7 | 138 | } |
embeddedartists | 0:4977187e90c7 | 139 | |
embeddedartists | 0:4977187e90c7 | 140 | void AppLauncher::runToCompletion() |
embeddedartists | 0:4977187e90c7 | 141 | { |
embeddedartists | 0:4977187e90c7 | 142 | DMBoard* board = &DMBoard::instance(); |
embeddedartists | 0:4977187e90c7 | 143 | RtosLog* log = board->logger(); |
embeddedartists | 0:4977187e90c7 | 144 | |
embeddedartists | 0:4977187e90c7 | 145 | // Draw something on the framebuffer before using it so that it doesn't look garbled |
embeddedartists | 0:4977187e90c7 | 146 | draw(); |
embeddedartists | 0:4977187e90c7 | 147 | |
embeddedartists | 0:4977187e90c7 | 148 | // Start display in default mode (16-bit) |
embeddedartists | 0:4977187e90c7 | 149 | Display::DisplayError disperr = _disp->powerUp(_fb); |
embeddedartists | 0:4977187e90c7 | 150 | if (disperr != Display::Ok) { |
embeddedartists | 0:4977187e90c7 | 151 | log->printf(APP_PREFIX"Failed to initialize the display, got error %d\r\n", disperr); |
embeddedartists | 0:4977187e90c7 | 152 | return; |
embeddedartists | 0:4977187e90c7 | 153 | } |
embeddedartists | 0:4977187e90c7 | 154 | |
embeddedartists | 0:4977187e90c7 | 155 | // To keep track of the button pushes |
embeddedartists | 0:4977187e90c7 | 156 | Timer buttonTimer; |
embeddedartists | 0:4977187e90c7 | 157 | bool buttonPressed = false; |
embeddedartists | 0:4977187e90c7 | 158 | |
embeddedartists | 0:4977187e90c7 | 159 | // Wait for touches |
embeddedartists | 0:4977187e90c7 | 160 | TouchPanel* touch = board->touchPanel(); |
embeddedartists | 0:4977187e90c7 | 161 | TouchPanel::touchCoordinate_t coord; |
embeddedartists | 0:4977187e90c7 | 162 | while(touch->read(coord)) { |
embeddedartists | 0:4977187e90c7 | 163 | |
embeddedartists | 0:4977187e90c7 | 164 | // Process the touch coordinate for each button |
embeddedartists | 0:4977187e90c7 | 165 | for (int i = 0; i < NumberOfButtons; i++) { |
embeddedartists | 0:4977187e90c7 | 166 | if (_buttons[i] != NULL) { |
embeddedartists | 0:4977187e90c7 | 167 | if (_buttons[i]->handle(coord.x, coord.y, coord.z > 0)) { |
embeddedartists | 0:4977187e90c7 | 168 | _buttons[i]->draw(); |
embeddedartists | 0:4977187e90c7 | 169 | } |
embeddedartists | 0:4977187e90c7 | 170 | } |
embeddedartists | 0:4977187e90c7 | 171 | } |
embeddedartists | 0:4977187e90c7 | 172 | |
embeddedartists | 0:4977187e90c7 | 173 | // Check if the physical USER button on the board has been pressed |
embeddedartists | 0:4977187e90c7 | 174 | if (appToLaunch == NoApplication) { |
embeddedartists | 0:4977187e90c7 | 175 | if (board->buttonPressed()) { |
embeddedartists | 0:4977187e90c7 | 176 | if (buttonPressed) { |
embeddedartists | 0:4977187e90c7 | 177 | if (buttonTimer.read_ms() > 2000) { |
embeddedartists | 0:4977187e90c7 | 178 | // User has pressed the button more than two seconds. |
embeddedartists | 0:4977187e90c7 | 179 | // Start calibration application |
embeddedartists | 0:4977187e90c7 | 180 | appToLaunch = CalibrationApp; |
embeddedartists | 0:4977187e90c7 | 181 | buttonTimer.stop(); |
embeddedartists | 0:4977187e90c7 | 182 | buttonPressed = false; |
embeddedartists | 0:4977187e90c7 | 183 | } |
embeddedartists | 0:4977187e90c7 | 184 | } else { |
embeddedartists | 0:4977187e90c7 | 185 | buttonTimer.reset(); |
embeddedartists | 0:4977187e90c7 | 186 | buttonTimer.start(); |
embeddedartists | 0:4977187e90c7 | 187 | buttonPressed = true; |
embeddedartists | 0:4977187e90c7 | 188 | } |
embeddedartists | 0:4977187e90c7 | 189 | } else if (buttonPressed) { |
embeddedartists | 0:4977187e90c7 | 190 | buttonTimer.stop(); |
embeddedartists | 0:4977187e90c7 | 191 | buttonPressed = false; |
embeddedartists | 0:4977187e90c7 | 192 | } |
embeddedartists | 0:4977187e90c7 | 193 | } else { |
embeddedartists | 0:4977187e90c7 | 194 | // pressing the display buttons take precedence so disregard the |
embeddedartists | 0:4977187e90c7 | 195 | // USER button |
embeddedartists | 0:4977187e90c7 | 196 | buttonTimer.stop(); |
embeddedartists | 0:4977187e90c7 | 197 | buttonPressed = false; |
embeddedartists | 0:4977187e90c7 | 198 | } |
embeddedartists | 0:4977187e90c7 | 199 | |
embeddedartists | 0:4977187e90c7 | 200 | if (appToLaunch != NoApplication) { |
embeddedartists | 0:4977187e90c7 | 201 | App* a = NULL; |
embeddedartists | 0:4977187e90c7 | 202 | switch (appToLaunch) { |
embeddedartists | 0:4977187e90c7 | 203 | case SettingsApp: |
embeddedartists | 0:4977187e90c7 | 204 | a = new AppSettings(); |
embeddedartists | 0:4977187e90c7 | 205 | break; |
embeddedartists | 0:4977187e90c7 | 206 | case CalibrationApp: |
embeddedartists | 0:4977187e90c7 | 207 | a = new AppTouchCalibration(); |
embeddedartists | 0:4977187e90c7 | 208 | break; |
embeddedartists | 0:4977187e90c7 | 209 | case ColorPicker: |
embeddedartists | 0:4977187e90c7 | 210 | a = new AppColorPicker(); |
embeddedartists | 0:4977187e90c7 | 211 | break; |
embeddedartists | 0:4977187e90c7 | 212 | case ImageViewerApp: |
embeddedartists | 0:4977187e90c7 | 213 | a = new AppImageViewer(); |
embeddedartists | 0:4977187e90c7 | 214 | break; |
embeddedartists | 0:4977187e90c7 | 215 | default: |
embeddedartists | 0:4977187e90c7 | 216 | break; |
embeddedartists | 0:4977187e90c7 | 217 | } |
embeddedartists | 0:4977187e90c7 | 218 | if (a != NULL) { |
embeddedartists | 0:4977187e90c7 | 219 | if (a->setup()) { |
embeddedartists | 0:4977187e90c7 | 220 | a->runToCompletion(); |
embeddedartists | 0:4977187e90c7 | 221 | a->teardown(); |
embeddedartists | 0:4977187e90c7 | 222 | } |
embeddedartists | 0:4977187e90c7 | 223 | delete a; |
embeddedartists | 0:4977187e90c7 | 224 | } |
embeddedartists | 0:4977187e90c7 | 225 | appToLaunch = NoApplication; |
embeddedartists | 0:4977187e90c7 | 226 | } |
embeddedartists | 0:4977187e90c7 | 227 | } |
embeddedartists | 0:4977187e90c7 | 228 | } |
embeddedartists | 0:4977187e90c7 | 229 | |
embeddedartists | 0:4977187e90c7 | 230 | bool AppLauncher::teardown() |
embeddedartists | 0:4977187e90c7 | 231 | { |
embeddedartists | 0:4977187e90c7 | 232 | if (_win != NULL) { |
embeddedartists | 0:4977187e90c7 | 233 | free(_win); |
embeddedartists | 0:4977187e90c7 | 234 | _win = NULL; |
embeddedartists | 0:4977187e90c7 | 235 | } |
embeddedartists | 0:4977187e90c7 | 236 | if (_fb != NULL) { |
embeddedartists | 0:4977187e90c7 | 237 | free(_fb); |
embeddedartists | 0:4977187e90c7 | 238 | _fb = NULL; |
embeddedartists | 0:4977187e90c7 | 239 | } |
embeddedartists | 0:4977187e90c7 | 240 | for (int i = 0; i < NumberOfButtons; i++) { |
embeddedartists | 0:4977187e90c7 | 241 | _buttons[i] = NULL; |
embeddedartists | 0:4977187e90c7 | 242 | if (_buttons[i] != NULL) { |
embeddedartists | 0:4977187e90c7 | 243 | delete _buttons[i]; |
embeddedartists | 0:4977187e90c7 | 244 | _buttons[i] = NULL; |
embeddedartists | 0:4977187e90c7 | 245 | } |
embeddedartists | 0:4977187e90c7 | 246 | } |
embeddedartists | 0:4977187e90c7 | 247 | return true; |
embeddedartists | 0:4977187e90c7 | 248 | } |
embeddedartists | 0:4977187e90c7 | 249 | |
embeddedartists | 0:4977187e90c7 | 250 |