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
Diff: Application/AppLauncher.cpp
- Revision:
- 0:4977187e90c7
- Child:
- 3:3fabfe3339b8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Application/AppLauncher.cpp Thu Dec 11 11:03:57 2014 +0000 @@ -0,0 +1,250 @@ +/* + * Copyright 2014 Embedded Artists AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "mbed.h" +#include "AppLauncher.h" +#include "AppSettings.h" +#include "AppTouchCalibration.h" +#include "AppColorPicker.h" +#include "AppImageViewer.h" +#include "lpc_swim_font.h" +#include "Button.h" + +/****************************************************************************** + * Defines and typedefs + *****************************************************************************/ + +#define APP_PREFIX "[Launcher] " + + +typedef enum { + NoApplication = -1, + SettingsApp = 0, + ColorPicker, + TouchTestApp, + ImageViewerApp, + //SlideshowApp, + //TouchGFXApp, + //EmWinApp, + CalibrationApp = 100, +} AppID_t; + +/****************************************************************************** + * Private variables + *****************************************************************************/ + +static AppID_t appToLaunch = NoApplication; + +/****************************************************************************** + * Private functions + *****************************************************************************/ + +static void buttonClicked(uint32_t x) +{ + if (appToLaunch == NoApplication) { + appToLaunch = (AppID_t)x; + } +} + +void AppLauncher::addButton(uint32_t buttonID, const char* caption) +{ + int idx = _usedButtons++; + int xspace = ((_disp->width() - ButtonColumns * ButtonWidth) / (ButtonColumns + 1)); + int yspace = ((_disp->height() - TitleHeight - ButtonRows * ButtonHeight) / (ButtonRows + 1)); + + _buttons[idx] = new Button(caption, (COLOR_T*)_fb, + xspace + (ButtonWidth + xspace)*(idx%ButtonColumns), + TitleHeight + yspace + (ButtonHeight + yspace)*(idx/ButtonColumns), + ButtonWidth, ButtonHeight); + _buttons[idx]->setAction(buttonClicked, buttonID); + _buttons[idx]->draw(); +} + +void AppLauncher::draw() +{ + // Prepare fullscreen + swim_window_open(_win, + _disp->width(), _disp->height(), // full size + (COLOR_T*)_fb, + 0,0,_disp->width()-1, _disp->height()-1, // window position and size + 1, // border + WHITE, RED, BLACK); // colors: pen, backgr, forgr + swim_set_title(_win, "Demo Program", BLACK); + + // Add many buttons + addButton(SettingsApp, "Settings"); + addButton(TouchTestApp, "Test Touch"); + //addButton(SlideshowApp, "Slideshow"); + //addButton(TouchGFXApp, "TouchGFX"); + //addButton(EmWinApp, "emWin"); + addButton(ColorPicker, "Color Picker"); + addButton(ImageViewerApp, "Image Viewer"); + //addButton(5, "Button 5"); + //addButton(6, "Button 6"); + //addButton(7, "Button 7"); + //addButton(8, "Button 8"); + //addButton(9, "Button 9"); + + const char* msg = "(Press physical UserButton >2s to calibrate touch)"; + int w, h; + swim_get_string_bounds(_win, msg, &w, &h); + swim_put_text_xy(_win, msg, (_disp->width()-w)/2, _disp->height()-h*4); +} + +/****************************************************************************** + * Public functions + *****************************************************************************/ + +AppLauncher::AppLauncher() : _disp(NULL), _win(NULL), _fb(NULL), _usedButtons(0) +{ + for (int i = 0; i < NumberOfButtons; i++) { + _buttons[i] = NULL; + } +} + +AppLauncher::~AppLauncher() +{ + teardown(); +} + +bool AppLauncher::setup() +{ + RtosLog* log = DMBoard::instance().logger(); + + _disp = DMBoard::instance().display(); + _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T)); + _fb = _disp->allocateFramebuffer(); + + if (_win == NULL || _fb == NULL) { + log->printf(APP_PREFIX"Failed to allocate memory for framebuffer\r\n"); + return false; + } + + return true; +} + +void AppLauncher::runToCompletion() +{ + DMBoard* board = &DMBoard::instance(); + RtosLog* log = board->logger(); + + // Draw something on the framebuffer before using it so that it doesn't look garbled + draw(); + + // Start display in default mode (16-bit) + Display::DisplayError disperr = _disp->powerUp(_fb); + if (disperr != Display::Ok) { + log->printf(APP_PREFIX"Failed to initialize the display, got error %d\r\n", disperr); + return; + } + + // To keep track of the button pushes + Timer buttonTimer; + bool buttonPressed = false; + + // Wait for touches + TouchPanel* touch = board->touchPanel(); + TouchPanel::touchCoordinate_t coord; + while(touch->read(coord)) { + + // Process the touch coordinate for each button + for (int i = 0; i < NumberOfButtons; i++) { + if (_buttons[i] != NULL) { + if (_buttons[i]->handle(coord.x, coord.y, coord.z > 0)) { + _buttons[i]->draw(); + } + } + } + + // Check if the physical USER button on the board has been pressed + if (appToLaunch == NoApplication) { + if (board->buttonPressed()) { + if (buttonPressed) { + if (buttonTimer.read_ms() > 2000) { + // User has pressed the button more than two seconds. + // Start calibration application + appToLaunch = CalibrationApp; + buttonTimer.stop(); + buttonPressed = false; + } + } else { + buttonTimer.reset(); + buttonTimer.start(); + buttonPressed = true; + } + } else if (buttonPressed) { + buttonTimer.stop(); + buttonPressed = false; + } + } else { + // pressing the display buttons take precedence so disregard the + // USER button + buttonTimer.stop(); + buttonPressed = false; + } + + if (appToLaunch != NoApplication) { + App* a = NULL; + switch (appToLaunch) { + case SettingsApp: + a = new AppSettings(); + break; + case CalibrationApp: + a = new AppTouchCalibration(); + break; + case ColorPicker: + a = new AppColorPicker(); + break; + case ImageViewerApp: + a = new AppImageViewer(); + break; + default: + break; + } + if (a != NULL) { + if (a->setup()) { + a->runToCompletion(); + a->teardown(); + } + delete a; + } + appToLaunch = NoApplication; + } + } +} + +bool AppLauncher::teardown() +{ + if (_win != NULL) { + free(_win); + _win = NULL; + } + if (_fb != NULL) { + free(_fb); + _fb = NULL; + } + for (int i = 0; i < NumberOfButtons; i++) { + _buttons[i] = NULL; + if (_buttons[i] != NULL) { + delete _buttons[i]; + _buttons[i] = NULL; + } + } + return true; +} + +