Demo for Embedded World 2015.

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