Basic swim GUI for LPC4088
Fork of DMBasicGUI by
SlideShow/AppSlideShow.cpp@17:6e2abf107800, 2015-03-20 (annotated)
- Committer:
- embeddedartists
- Date:
- Fri Mar 20 14:25:46 2015 +0100
- Revision:
- 17:6e2abf107800
- Parent:
- 15:a68bb30ab95e
- Added a Resource concept to allow runtime selection of image source. The
resource can be loaded from an array in flash or from a file on any of the
available file systems. Using a resource makes it possible to have different
resources depending on display resolution
- Removed the basic_image data files
- Added support for the 5" display (800x480 resolution)
- Added possibility to change the default font in SWIM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 5:f4de114c31c3 | 1 | /* |
embeddedartists | 5:f4de114c31c3 | 2 | * Copyright 2014 Embedded Artists AB |
embeddedartists | 5:f4de114c31c3 | 3 | * |
embeddedartists | 5:f4de114c31c3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
embeddedartists | 5:f4de114c31c3 | 5 | * you may not use this file except in compliance with the License. |
embeddedartists | 5:f4de114c31c3 | 6 | * You may obtain a copy of the License at |
embeddedartists | 5:f4de114c31c3 | 7 | * |
embeddedartists | 5:f4de114c31c3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
embeddedartists | 5:f4de114c31c3 | 9 | * |
embeddedartists | 5:f4de114c31c3 | 10 | * Unless required by applicable law or agreed to in writing, software |
embeddedartists | 5:f4de114c31c3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
embeddedartists | 5:f4de114c31c3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
embeddedartists | 5:f4de114c31c3 | 13 | * See the License for the specific language governing permissions and |
embeddedartists | 5:f4de114c31c3 | 14 | * limitations under the License. |
embeddedartists | 5:f4de114c31c3 | 15 | */ |
embeddedartists | 5:f4de114c31c3 | 16 | |
embeddedartists | 5:f4de114c31c3 | 17 | |
embeddedartists | 5:f4de114c31c3 | 18 | #include "mbed.h" |
embeddedartists | 5:f4de114c31c3 | 19 | #include "AppSlideShow.h" |
embeddedartists | 17:6e2abf107800 | 20 | |
embeddedartists | 5:f4de114c31c3 | 21 | |
embeddedartists | 5:f4de114c31c3 | 22 | /****************************************************************************** |
embeddedartists | 5:f4de114c31c3 | 23 | * Defines and typedefs |
embeddedartists | 5:f4de114c31c3 | 24 | *****************************************************************************/ |
embeddedartists | 5:f4de114c31c3 | 25 | |
embeddedartists | 5:f4de114c31c3 | 26 | #define TICKER_RESOLUTION_IN_MS 10 |
embeddedartists | 5:f4de114c31c3 | 27 | |
embeddedartists | 11:265884fa7fdd | 28 | #define BTN_OFF 20 |
embeddedartists | 11:265884fa7fdd | 29 | |
embeddedartists | 5:f4de114c31c3 | 30 | /****************************************************************************** |
embeddedartists | 5:f4de114c31c3 | 31 | * Global variables |
embeddedartists | 5:f4de114c31c3 | 32 | *****************************************************************************/ |
embeddedartists | 5:f4de114c31c3 | 33 | |
embeddedartists | 5:f4de114c31c3 | 34 | volatile uint32_t msTicks = 0; |
embeddedartists | 5:f4de114c31c3 | 35 | |
embeddedartists | 5:f4de114c31c3 | 36 | /****************************************************************************** |
embeddedartists | 5:f4de114c31c3 | 37 | * Private functions |
embeddedartists | 5:f4de114c31c3 | 38 | *****************************************************************************/ |
embeddedartists | 5:f4de114c31c3 | 39 | |
embeddedartists | 5:f4de114c31c3 | 40 | static void tRender(void const *args) |
embeddedartists | 5:f4de114c31c3 | 41 | { |
embeddedartists | 5:f4de114c31c3 | 42 | Renderer* s = (Renderer*)args; |
embeddedartists | 5:f4de114c31c3 | 43 | s->render(); |
embeddedartists | 5:f4de114c31c3 | 44 | } |
embeddedartists | 5:f4de114c31c3 | 45 | |
embeddedartists | 5:f4de114c31c3 | 46 | static void ticker(void const *n) { |
embeddedartists | 5:f4de114c31c3 | 47 | msTicks += TICKER_RESOLUTION_IN_MS; |
embeddedartists | 5:f4de114c31c3 | 48 | } |
embeddedartists | 5:f4de114c31c3 | 49 | |
embeddedartists | 11:265884fa7fdd | 50 | static void buttonClicked(uint32_t x) |
embeddedartists | 11:265884fa7fdd | 51 | { |
embeddedartists | 11:265884fa7fdd | 52 | bool* done = (bool*)x; |
embeddedartists | 11:265884fa7fdd | 53 | *done = true; |
embeddedartists | 11:265884fa7fdd | 54 | } |
embeddedartists | 11:265884fa7fdd | 55 | |
embeddedartists | 5:f4de114c31c3 | 56 | /****************************************************************************** |
embeddedartists | 5:f4de114c31c3 | 57 | * Public functions |
embeddedartists | 5:f4de114c31c3 | 58 | *****************************************************************************/ |
embeddedartists | 5:f4de114c31c3 | 59 | |
embeddedartists | 11:265884fa7fdd | 60 | AppSlideShow::AppSlideShow(const char* scriptFile, const char* pathPrefix, int xoff, int yoff) : |
embeddedartists | 11:265884fa7fdd | 61 | _fb(NULL), _disp(NULL), _show(NULL), _rend(NULL), |
embeddedartists | 11:265884fa7fdd | 62 | _fileMutex(), _btnDone(NULL), _btnRepeat(NULL), _script(NULL), _path(NULL) |
embeddedartists | 5:f4de114c31c3 | 63 | { |
embeddedartists | 11:265884fa7fdd | 64 | _script = (char*)malloc(strlen(scriptFile)+1); |
embeddedartists | 11:265884fa7fdd | 65 | if (_script != NULL) { |
embeddedartists | 11:265884fa7fdd | 66 | strcpy(_script, scriptFile); |
embeddedartists | 11:265884fa7fdd | 67 | } |
embeddedartists | 11:265884fa7fdd | 68 | if (pathPrefix != NULL) { |
embeddedartists | 11:265884fa7fdd | 69 | _path = (char*)malloc(strlen(pathPrefix)+1); |
embeddedartists | 11:265884fa7fdd | 70 | if (_path != NULL) { |
embeddedartists | 11:265884fa7fdd | 71 | strcpy(_path, pathPrefix); |
embeddedartists | 11:265884fa7fdd | 72 | } |
embeddedartists | 11:265884fa7fdd | 73 | } |
embeddedartists | 11:265884fa7fdd | 74 | _xoff = xoff; |
embeddedartists | 11:265884fa7fdd | 75 | _yoff = yoff; |
embeddedartists | 5:f4de114c31c3 | 76 | } |
embeddedartists | 5:f4de114c31c3 | 77 | |
embeddedartists | 5:f4de114c31c3 | 78 | AppSlideShow::~AppSlideShow() |
embeddedartists | 5:f4de114c31c3 | 79 | { |
embeddedartists | 5:f4de114c31c3 | 80 | teardown(); |
embeddedartists | 5:f4de114c31c3 | 81 | } |
embeddedartists | 5:f4de114c31c3 | 82 | |
embeddedartists | 5:f4de114c31c3 | 83 | bool AppSlideShow::setup() |
embeddedartists | 5:f4de114c31c3 | 84 | { |
embeddedartists | 5:f4de114c31c3 | 85 | SlideShow::SlideShowError err; |
embeddedartists | 5:f4de114c31c3 | 86 | |
embeddedartists | 5:f4de114c31c3 | 87 | _disp = DMBoard::instance().display(); |
embeddedartists | 5:f4de114c31c3 | 88 | |
embeddedartists | 5:f4de114c31c3 | 89 | _fb = _disp->allocateFramebuffer(); |
embeddedartists | 5:f4de114c31c3 | 90 | if (_fb == NULL) { |
embeddedartists | 5:f4de114c31c3 | 91 | err = SlideShow::OutOfMemory; |
embeddedartists | 5:f4de114c31c3 | 92 | } else { |
embeddedartists | 5:f4de114c31c3 | 93 | memset(_fb, 0xff, _disp->fbSize()); |
embeddedartists | 5:f4de114c31c3 | 94 | _rend = new Renderer(); |
embeddedartists | 13:bff2288c2c61 | 95 | _show = new SlideShow(_rend, _path, _xoff, _yoff, 1, &_fileMutex); |
embeddedartists | 11:265884fa7fdd | 96 | err = _show->prepare(_script); |
embeddedartists | 5:f4de114c31c3 | 97 | } |
embeddedartists | 5:f4de114c31c3 | 98 | |
embeddedartists | 17:6e2abf107800 | 99 | _btnRepeat = new ImageButton((COLOR_T*)_fb, _disp->width() - 2*BTN_OFF - _resOk->width() - _resRepeat->width(), _disp->height() - BTN_OFF - _resRepeat->height(), _resRepeat->width(), _resRepeat->height()); |
embeddedartists | 17:6e2abf107800 | 100 | _btnRepeat->loadImages(_resRepeat); |
embeddedartists | 17:6e2abf107800 | 101 | _btnDone = new ImageButton((COLOR_T*)_fb, _disp->width() - BTN_OFF - _resOk->width(), _disp->height() - BTN_OFF - _resOk->height(), _resOk->width(), _resOk->height()); |
embeddedartists | 17:6e2abf107800 | 102 | _btnDone->loadImages(_resOk); |
embeddedartists | 11:265884fa7fdd | 103 | |
embeddedartists | 5:f4de114c31c3 | 104 | return (err == SlideShow::Ok); |
embeddedartists | 5:f4de114c31c3 | 105 | } |
embeddedartists | 5:f4de114c31c3 | 106 | |
embeddedartists | 5:f4de114c31c3 | 107 | void AppSlideShow::runToCompletion() |
embeddedartists | 5:f4de114c31c3 | 108 | { |
embeddedartists | 5:f4de114c31c3 | 109 | // Save existing frame buffer |
embeddedartists | 5:f4de114c31c3 | 110 | void* oldFB = _disp->swapFramebuffer(_fb); |
embeddedartists | 5:f4de114c31c3 | 111 | |
embeddedartists | 11:265884fa7fdd | 112 | bool done = false; |
embeddedartists | 11:265884fa7fdd | 113 | bool repeat = false; |
embeddedartists | 11:265884fa7fdd | 114 | _btnDone->setAction(buttonClicked, (uint32_t)&done); |
embeddedartists | 11:265884fa7fdd | 115 | _btnRepeat->setAction(buttonClicked, (uint32_t)&repeat); |
embeddedartists | 11:265884fa7fdd | 116 | |
embeddedartists | 11:265884fa7fdd | 117 | |
embeddedartists | 5:f4de114c31c3 | 118 | // Alternative 1: use the calling thread's context to run in |
embeddedartists | 5:f4de114c31c3 | 119 | Thread tr(tRender, _rend, osPriorityHigh); |
embeddedartists | 5:f4de114c31c3 | 120 | |
embeddedartists | 5:f4de114c31c3 | 121 | // Generate the millisecond ticks for the slideshow |
embeddedartists | 5:f4de114c31c3 | 122 | RtosTimer rtosTimer(ticker, osTimerPeriodic); |
embeddedartists | 5:f4de114c31c3 | 123 | rtosTimer.start(TICKER_RESOLUTION_IN_MS); |
embeddedartists | 5:f4de114c31c3 | 124 | |
embeddedartists | 11:265884fa7fdd | 125 | // Wait for touches |
embeddedartists | 11:265884fa7fdd | 126 | TouchPanel* touch = DMBoard::instance().touchPanel(); |
embeddedartists | 11:265884fa7fdd | 127 | touch_coordinate_t coord; |
embeddedartists | 11:265884fa7fdd | 128 | |
embeddedartists | 11:265884fa7fdd | 129 | do { |
embeddedartists | 5:f4de114c31c3 | 130 | // Wait for slideshow to complete |
embeddedartists | 5:f4de114c31c3 | 131 | _show->run(); |
embeddedartists | 11:265884fa7fdd | 132 | |
embeddedartists | 11:265884fa7fdd | 133 | // Temporary ugly way to get current framebuffer |
embeddedartists | 11:265884fa7fdd | 134 | _btnDone->draw((COLOR_T*)LPC_LCD->UPBASE); |
embeddedartists | 11:265884fa7fdd | 135 | _btnRepeat->draw((COLOR_T*)LPC_LCD->UPBASE); |
embeddedartists | 11:265884fa7fdd | 136 | |
embeddedartists | 11:265884fa7fdd | 137 | done = repeat = false; |
embeddedartists | 11:265884fa7fdd | 138 | while (!done && !repeat) { |
embeddedartists | 11:265884fa7fdd | 139 | Thread::signal_wait(0x1); |
embeddedartists | 11:265884fa7fdd | 140 | if (touch->read(coord) == TouchPanel::TouchError_Ok) { |
embeddedartists | 11:265884fa7fdd | 141 | if (_btnDone->handle(coord.x, coord.y, coord.z > 0)) { |
embeddedartists | 11:265884fa7fdd | 142 | _btnDone->draw(); |
embeddedartists | 11:265884fa7fdd | 143 | } |
embeddedartists | 11:265884fa7fdd | 144 | if (_btnRepeat->handle(coord.x, coord.y, coord.z > 0)) { |
embeddedartists | 11:265884fa7fdd | 145 | _btnRepeat->draw(); |
embeddedartists | 11:265884fa7fdd | 146 | } |
embeddedartists | 11:265884fa7fdd | 147 | } |
embeddedartists | 11:265884fa7fdd | 148 | } |
embeddedartists | 11:265884fa7fdd | 149 | |
embeddedartists | 11:265884fa7fdd | 150 | } while(!done); |
embeddedartists | 5:f4de114c31c3 | 151 | |
embeddedartists | 5:f4de114c31c3 | 152 | tr.terminate(); |
embeddedartists | 5:f4de114c31c3 | 153 | |
embeddedartists | 5:f4de114c31c3 | 154 | // Restore the original FB |
embeddedartists | 5:f4de114c31c3 | 155 | _disp->swapFramebuffer(oldFB); |
embeddedartists | 5:f4de114c31c3 | 156 | } |
embeddedartists | 5:f4de114c31c3 | 157 | |
embeddedartists | 5:f4de114c31c3 | 158 | bool AppSlideShow::teardown() |
embeddedartists | 5:f4de114c31c3 | 159 | { |
embeddedartists | 5:f4de114c31c3 | 160 | if (_show != NULL) { |
embeddedartists | 5:f4de114c31c3 | 161 | free(_show); |
embeddedartists | 5:f4de114c31c3 | 162 | _show = NULL; |
embeddedartists | 5:f4de114c31c3 | 163 | } |
embeddedartists | 5:f4de114c31c3 | 164 | if (_rend != NULL) { |
embeddedartists | 5:f4de114c31c3 | 165 | free(_rend); |
embeddedartists | 5:f4de114c31c3 | 166 | _rend = NULL; |
embeddedartists | 5:f4de114c31c3 | 167 | } |
embeddedartists | 5:f4de114c31c3 | 168 | if (_fb != NULL) { |
embeddedartists | 5:f4de114c31c3 | 169 | free(_fb); |
embeddedartists | 5:f4de114c31c3 | 170 | _fb = NULL; |
embeddedartists | 5:f4de114c31c3 | 171 | } |
embeddedartists | 11:265884fa7fdd | 172 | if (_script != NULL) { |
embeddedartists | 11:265884fa7fdd | 173 | free(_script); |
embeddedartists | 11:265884fa7fdd | 174 | _script = NULL; |
embeddedartists | 11:265884fa7fdd | 175 | } |
embeddedartists | 11:265884fa7fdd | 176 | if (_path != NULL) { |
embeddedartists | 11:265884fa7fdd | 177 | free(_path); |
embeddedartists | 11:265884fa7fdd | 178 | _path = NULL; |
embeddedartists | 11:265884fa7fdd | 179 | } |
embeddedartists | 5:f4de114c31c3 | 180 | return true; |
embeddedartists | 5:f4de114c31c3 | 181 | } |
embeddedartists | 5:f4de114c31c3 | 182 | |
embeddedartists | 5:f4de114c31c3 | 183 | |
embeddedartists | 17:6e2abf107800 | 184 | void AppSlideShow::addResource(Resources id, Resource* res) |
embeddedartists | 17:6e2abf107800 | 185 | { |
embeddedartists | 17:6e2abf107800 | 186 | if (id == Resource_Ok_button) { |
embeddedartists | 17:6e2abf107800 | 187 | _resOk = res; |
embeddedartists | 17:6e2abf107800 | 188 | } else { |
embeddedartists | 17:6e2abf107800 | 189 | _resRepeat = res; |
embeddedartists | 17:6e2abf107800 | 190 | } |
embeddedartists | 17:6e2abf107800 | 191 | } |
embeddedartists | 17:6e2abf107800 | 192 |