Chris Womack / DMBasicGUI

Fork of DMBasicGUI by Embedded Artists

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     
00104     return (err == SlideShow::Ok);
00105 }
00106 
00107 void AppSlideShow::runToCompletion()
00108 {
00109     // Save existing frame buffer
00110     void* oldFB = _disp->swapFramebuffer(_fb);
00111     
00112     bool done = false;
00113     bool repeat = false;
00114     _btnDone->setAction(buttonClicked, (uint32_t)&done);
00115     _btnRepeat->setAction(buttonClicked, (uint32_t)&repeat);
00116     
00117     
00118     // Alternative 1: use the calling thread's context to run in
00119     Thread tr(tRender, _rend, osPriorityHigh);
00120     
00121     // Generate the millisecond ticks for the slideshow
00122     RtosTimer rtosTimer(ticker, osTimerPeriodic);
00123     rtosTimer.start(TICKER_RESOLUTION_IN_MS);    
00124 
00125     // Wait for touches
00126     TouchPanel* touch = DMBoard::instance().touchPanel();
00127     touch_coordinate_t coord;
00128 
00129     do {
00130     // Wait for slideshow to complete
00131     _show->run();
00132       
00133       // Temporary ugly way to get current framebuffer
00134       _btnDone->draw((COLOR_T*)LPC_LCD->UPBASE);
00135       _btnRepeat->draw((COLOR_T*)LPC_LCD->UPBASE);
00136        
00137       done = repeat = false;
00138       while (!done && !repeat) {
00139         Thread::signal_wait(0x1);
00140         if (touch->read(coord) == TouchPanel::TouchError_Ok) {
00141           if (_btnDone->handle(coord.x, coord.y, coord.z > 0)) {
00142             _btnDone->draw();
00143           }
00144           if (_btnRepeat->handle(coord.x, coord.y, coord.z > 0)) {
00145             _btnRepeat->draw();
00146           }
00147         }
00148       }
00149         
00150     } while(!done);
00151     
00152     tr.terminate();
00153     
00154     // Restore the original FB
00155     _disp->swapFramebuffer(oldFB);
00156 }
00157 
00158 bool AppSlideShow::teardown()
00159 {
00160     if (_show != NULL) {
00161         free(_show);
00162         _show = NULL;
00163     }
00164     if (_rend != NULL) {
00165         free(_rend);
00166         _rend = NULL;
00167     }
00168     if (_fb != NULL) {
00169         free(_fb);
00170         _fb = NULL;
00171     }
00172     if (_script != NULL) {
00173         free(_script);
00174         _script = NULL;
00175     }
00176     if (_path != NULL) {
00177         free(_path);
00178         _path = NULL;
00179     }
00180     return true;
00181 }
00182 
00183 
00184 void AppSlideShow::addResource(Resources id, Resource* res)
00185 {
00186     if (id == Resource_Ok_button) {
00187         _resOk = res;
00188     } else {
00189         _resRepeat = res;
00190     }
00191 }
00192