The out-of-the-box demo application flashed on all display modules before they are shipped.

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AppDraw.cpp Source File

AppDraw.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 "EthernetInterface.h"
00020 #include "AppDraw.h"
00021 #include "lpc_swim_font.h"
00022 #include "lpc_colors.h"
00023 
00024 /******************************************************************************
00025  * Defines and typedefs
00026  *****************************************************************************/
00027  
00028 #define BTN_OFF    20
00029 
00030 /******************************************************************************
00031  * Local variables
00032  *****************************************************************************/
00033 
00034 static const COLOR_T COLORS[] = {
00035   RED,
00036   GREEN,
00037   BLUE,
00038   CYAN,
00039   MAGENTA,
00040   YELLOW,
00041   BLACK,
00042 };
00043 
00044 /******************************************************************************
00045  * Private functions
00046  *****************************************************************************/
00047 
00048 static void buttonClicked(uint32_t x)
00049 {
00050   bool* done = (bool*)x;
00051   *done = true;
00052 }
00053 
00054 void AppDraw::draw()
00055 {
00056     // Prepare fullscreen
00057     swim_window_open(_win, 
00058                    _disp->width(), _disp->height(),         // full size
00059                    (COLOR_T*)_fb,
00060                    0,0,_disp->width()-1, _disp->height()-1, // window position and size
00061                    0,                                       // border
00062                    WHITE, WHITE, BLACK);                    // colors: pen, backgr, forgr
00063     
00064     _btn = new ImageButton(_win->fb, _win->xpmax - BTN_OFF - _resOk->width(), _win->ypmax - BTN_OFF - _resOk->height(), _resOk->width(), _resOk->height());
00065     _btn->loadImages(_resOk);
00066     _btn->draw();
00067 }
00068 
00069 /******************************************************************************
00070  * Public functions
00071  *****************************************************************************/
00072 
00073 AppDraw::AppDraw() : _disp(NULL), _win(NULL), _fb(NULL), _btn(NULL), _resOk(NULL)
00074 {
00075 }
00076 
00077 AppDraw::~AppDraw()
00078 {
00079     teardown();
00080 }
00081 
00082 bool AppDraw::setup()
00083 {
00084     _disp = DMBoard::instance().display();
00085     _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T));
00086     _fb = _disp->allocateFramebuffer();
00087 
00088     return (_win != NULL && _fb != NULL);
00089 }
00090 
00091 void AppDraw::runToCompletion()
00092 {
00093     // Alternative 1: use the calling thread's context to run in
00094     bool done = false;
00095     draw();
00096     _btn->setAction(buttonClicked, (uint32_t)&done);
00097     void* oldFB = _disp->swapFramebuffer(_fb);
00098     
00099     // Wait for touches
00100     TouchPanel* touch = DMBoard::instance().touchPanel();
00101     bool ignore;
00102     int fingers = 0;
00103     touch->info(&ignore, &fingers, &ignore);
00104     if (fingers > MaxSupportedFingers) {
00105         fingers = MaxSupportedFingers;
00106     }
00107     while(!done) {
00108       // wait for a new touch signal (signal is sent from AppLauncher,
00109       // which listens for touch events)
00110       ThisThread::flags_wait_all(0x1);
00111       if (touch->read(_coords, fingers) == TouchPanel::TouchError_Ok) {
00112         for (int i = 0; i < fingers; i++) {
00113           if (_coords[i].z > 0) {
00114             _win->pen = COLORS[i];
00115             swim_put_circle(_win, _coords[i].x, _coords[i].y, 2, 1);
00116           }
00117         }
00118         if (_btn->handle(_coords[0].x, _coords[0].y, _coords[0].z > 0)) {
00119             _btn->draw();
00120         }
00121       }
00122     }
00123     
00124     // User has clicked the button, restore the original FB
00125     _disp->swapFramebuffer(oldFB);
00126     swim_window_close(_win);
00127 }
00128 
00129 bool AppDraw::teardown()
00130 {
00131     if (_win != NULL) {
00132         free(_win);
00133         _win = NULL;
00134     }
00135     if (_fb != NULL) {
00136         free(_fb);
00137         _fb = NULL;
00138     }
00139     if (_btn != NULL) {
00140         delete _btn;
00141         _btn = NULL;
00142     }
00143     return true;
00144 }
00145 
00146 
00147 void AppDraw::addResource(Resources id, Resource* res)
00148 {
00149     _resOk = res;
00150 }
00151