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 EmbeddedArtists AB

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AppSlideShow.cpp Source File

AppSlideShow.cpp

00001 /*
00002  *  Copyright 2014 Embedded Artists AB
00003  *
00004  *  Licensed under the Apache License, Version 2.0 (the "License");
00005  *  you may not use this file except in compliance with the License.
00006  *  You may obtain a copy of the License at
00007  *
00008  *    http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  *  Unless required by applicable law or agreed to in writing, software
00011  *  distributed under the License is distributed on an "AS IS" BASIS,
00012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *  See the License for the specific language governing permissions and
00014  *  limitations under the License.
00015  */
00016 
00017 
00018 #include "mbed.h"
00019 #include "AppSlideShow.h"
00020 
00021 
00022 /******************************************************************************
00023  * Defines and typedefs
00024  *****************************************************************************/
00025 
00026 #define TICKER_RESOLUTION_IN_MS  10
00027 
00028 #define BTN_OFF    20
00029 
00030 /******************************************************************************
00031  * Global variables
00032  *****************************************************************************/
00033 
00034 volatile uint32_t msTicks = 0;
00035 
00036 /******************************************************************************
00037  * Private functions
00038  *****************************************************************************/
00039 
00040 static void tRender(void const *args)
00041 {
00042   Renderer* s = (Renderer*)args;
00043   s->render();
00044 }
00045 
00046 static void ticker(void const *n) {
00047   msTicks += TICKER_RESOLUTION_IN_MS;
00048 }
00049 
00050 static void buttonClicked(uint32_t x)
00051 {
00052   bool* done = (bool*)x;
00053   *done = true;
00054 }
00055 
00056 /******************************************************************************
00057  * Public functions
00058  *****************************************************************************/
00059 
00060 AppSlideShow::AppSlideShow(const char* scriptFile, const char* pathPrefix, int xoff, int yoff) :
00061     _fb(NULL), _disp(NULL), _show(NULL), _rend(NULL), 
00062    _fileMutex(), _btnDone(NULL), _btnRepeat(NULL), _script(NULL), _path(NULL)
00063 {
00064     _script = (char*)malloc(strlen(scriptFile)+1);
00065     if (_script != NULL) {
00066         strcpy(_script, scriptFile);
00067     }
00068     if (pathPrefix != NULL) {
00069         _path = (char*)malloc(strlen(pathPrefix)+1);
00070         if (_path != NULL) {
00071             strcpy(_path, pathPrefix);
00072         }
00073     }
00074     _xoff = xoff;
00075     _yoff = yoff;
00076 }
00077 
00078 AppSlideShow::~AppSlideShow()
00079 {
00080     teardown();
00081 }
00082 
00083 bool AppSlideShow::setup()
00084 {
00085     SlideShow::SlideShowError err;
00086     
00087     _disp = DMBoard::instance().display();
00088     
00089     _fb = _disp->allocateFramebuffer();
00090     if (_fb == NULL) {
00091         err = SlideShow::OutOfMemory;
00092     } else {
00093         memset(_fb, 0xff, _disp->fbSize());
00094         _rend = new Renderer();
00095         _show = new SlideShow(_rend, _path, _xoff, _yoff, 1, &_fileMutex);
00096         err = _show->prepare(_script);
00097     }
00098 
00099     _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());
00100     _btnRepeat->loadImages(_resRepeat);
00101     _btnDone = new ImageButton((COLOR_T*)_fb, _disp->width() - BTN_OFF - _resOk->width(), _disp->height() - BTN_OFF - _resOk->height(), _resOk->width(), _resOk->height());
00102     _btnDone->loadImages(_resOk);
00103     return (err == SlideShow::Ok);
00104 }
00105 
00106 void AppSlideShow::runToCompletion()
00107 {
00108     // Save existing frame buffer
00109     void* oldFB = _disp->swapFramebuffer(_fb);
00110     
00111     bool done = false;
00112     bool repeat = false;
00113     _btnDone->setAction(buttonClicked, (uint32_t)&done);
00114     _btnRepeat->setAction(buttonClicked, (uint32_t)&repeat);
00115     
00116     
00117     // Alternative 1: use the calling thread's context to run in
00118     //Thread tr(tRender, _rend, osPriorityHigh);
00119     Thread tr;
00120     tr.start(callback(tRender, _rend));
00121     
00122     // Generate the millisecond ticks for the slideshow
00123     //RtosTimer rtosTimer(ticker, osTimerPeriodic);
00124     //rtosTimer.start(TICKER_RESOLUTION_IN_MS);    
00125     EventQueue rtosTimer(4*EVENTS_EVENT_SIZE);
00126     rtosTimer.call_every(TICKER_RESOLUTION_IN_MS, ticker, &done);
00127     rtosTimer.dispatch(0);
00128 
00129     // Wait for touches
00130     TouchPanel* touch = DMBoard::instance().touchPanel();
00131     touch_coordinate_t coord;
00132 
00133     do {
00134     // Wait for slideshow to complete
00135     _show->run();
00136       
00137       // Temporary ugly way to get current framebuffer
00138       _btnDone->draw((COLOR_T*)LPC_LCD->UPBASE);
00139       _btnRepeat->draw((COLOR_T*)LPC_LCD->UPBASE);
00140        
00141       done = repeat = false;
00142       while (!done && !repeat) {
00143         ThisThread::flags_wait_any(0x1);
00144         if (touch->read(coord) == TouchPanel::TouchError_Ok) {
00145           if (_btnDone->handle(coord.x, coord.y, coord.z > 0)) {
00146             _btnDone->draw();
00147           }
00148           if (_btnRepeat->handle(coord.x, coord.y, coord.z > 0)) {
00149             _btnRepeat->draw();
00150           }
00151         }
00152       }
00153         
00154     } while(!done);
00155     
00156     tr.terminate();
00157     
00158     // Restore the original FB
00159     _disp->swapFramebuffer(oldFB);
00160 }
00161 
00162 bool AppSlideShow::teardown()
00163 {
00164     if (_show != NULL) {
00165         free(_show);
00166         _show = NULL;
00167     }
00168     if (_rend != NULL) {
00169         free(_rend);
00170         _rend = NULL;
00171     }
00172     if (_fb != NULL) {
00173         free(_fb);
00174         _fb = NULL;
00175     }
00176     if (_script != NULL) {
00177         free(_script);
00178         _script = NULL;
00179     }
00180     if (_path != NULL) {
00181         free(_path);
00182         _path = NULL;
00183     }
00184     return true;
00185 }
00186 
00187 
00188 void AppSlideShow::addResource(Resources id, Resource* res)
00189 {
00190     if (id == Resource_Ok_button) {
00191         _resOk = res;
00192     } else {
00193         _resRepeat = res;
00194     }
00195 }
00196