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

Committer:
embeddedartists
Date:
Thu Dec 11 11:03:57 2014 +0000
Revision:
0:4977187e90c7
Child:
1:46c8df4608c8
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:4977187e90c7 1 /*
embeddedartists 0:4977187e90c7 2 * Copyright 2014 Embedded Artists AB
embeddedartists 0:4977187e90c7 3 *
embeddedartists 0:4977187e90c7 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 0:4977187e90c7 5 * you may not use this file except in compliance with the License.
embeddedartists 0:4977187e90c7 6 * You may obtain a copy of the License at
embeddedartists 0:4977187e90c7 7 *
embeddedartists 0:4977187e90c7 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 0:4977187e90c7 9 *
embeddedartists 0:4977187e90c7 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 0:4977187e90c7 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 0:4977187e90c7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 0:4977187e90c7 13 * See the License for the specific language governing permissions and
embeddedartists 0:4977187e90c7 14 * limitations under the License.
embeddedartists 0:4977187e90c7 15 */
embeddedartists 0:4977187e90c7 16
embeddedartists 0:4977187e90c7 17
embeddedartists 0:4977187e90c7 18 #include "mbed.h"
embeddedartists 0:4977187e90c7 19 #include "AppTouchCalibration.h"
embeddedartists 0:4977187e90c7 20 #include "lpc_swim_font.h"
embeddedartists 0:4977187e90c7 21
embeddedartists 0:4977187e90c7 22 /******************************************************************************
embeddedartists 0:4977187e90c7 23 * Defines and typedefs
embeddedartists 0:4977187e90c7 24 *****************************************************************************/
embeddedartists 0:4977187e90c7 25
embeddedartists 0:4977187e90c7 26 #define BTN_WIDTH 65
embeddedartists 0:4977187e90c7 27 #define BTN_HEIGHT 40
embeddedartists 0:4977187e90c7 28 #define BTN_OFF 20
embeddedartists 0:4977187e90c7 29
embeddedartists 0:4977187e90c7 30 /******************************************************************************
embeddedartists 0:4977187e90c7 31 * Global variables
embeddedartists 0:4977187e90c7 32 *****************************************************************************/
embeddedartists 0:4977187e90c7 33
embeddedartists 0:4977187e90c7 34
embeddedartists 0:4977187e90c7 35 /******************************************************************************
embeddedartists 0:4977187e90c7 36 * Private functions
embeddedartists 0:4977187e90c7 37 *****************************************************************************/
embeddedartists 0:4977187e90c7 38
embeddedartists 0:4977187e90c7 39 static void buttonClicked(uint32_t x)
embeddedartists 0:4977187e90c7 40 {
embeddedartists 0:4977187e90c7 41 bool* done = (bool*)x;
embeddedartists 0:4977187e90c7 42 *done = true;
embeddedartists 0:4977187e90c7 43 }
embeddedartists 0:4977187e90c7 44
embeddedartists 0:4977187e90c7 45 void AppTouchCalibration::draw()
embeddedartists 0:4977187e90c7 46 {
embeddedartists 0:4977187e90c7 47 // Prepare fullscreen
embeddedartists 0:4977187e90c7 48 swim_window_open(_win,
embeddedartists 0:4977187e90c7 49 _disp->width(), _disp->height(), // full size
embeddedartists 0:4977187e90c7 50 (COLOR_T*)_fb,
embeddedartists 0:4977187e90c7 51 0,0,_disp->width()-1, _disp->height()-1, // window position and size
embeddedartists 0:4977187e90c7 52 1, // border
embeddedartists 0:4977187e90c7 53 WHITE, WHITE, BLACK); // colors: pen, backgr, forgr
embeddedartists 0:4977187e90c7 54 swim_set_title(_win, "Touch Calibration", BLACK);
embeddedartists 0:4977187e90c7 55
embeddedartists 0:4977187e90c7 56 // Prepare status area in the middle
embeddedartists 0:4977187e90c7 57 swim_window_open(_msg,
embeddedartists 0:4977187e90c7 58 _disp->width(), _disp->height(), // full size
embeddedartists 0:4977187e90c7 59 (COLOR_T*)_fb,
embeddedartists 0:4977187e90c7 60 50,_disp->height()/2-15,_disp->width()-50, _disp->height()/2+15, // window position and size
embeddedartists 0:4977187e90c7 61 0, // border
embeddedartists 0:4977187e90c7 62 BLACK, WHITE, BLACK); // colors: pen, backgr, forgr
embeddedartists 0:4977187e90c7 63
embeddedartists 0:4977187e90c7 64 showStatus("Press the crosshairs in each of the corners");
embeddedartists 0:4977187e90c7 65
embeddedartists 0:4977187e90c7 66 // Create (but don't show) the button
embeddedartists 0:4977187e90c7 67 _btn = new Button("Done", _win->fb, _win->xpmax - BTN_OFF - BTN_WIDTH, _win->ypmax - BTN_OFF - BTN_HEIGHT, BTN_WIDTH, BTN_HEIGHT);
embeddedartists 0:4977187e90c7 68 }
embeddedartists 0:4977187e90c7 69
embeddedartists 0:4977187e90c7 70 void AppTouchCalibration::drawMarker(uint16_t x, uint16_t y, bool erase)
embeddedartists 0:4977187e90c7 71 {
embeddedartists 0:4977187e90c7 72 // The markers must be drawn at exact locations to get a good calibration.
embeddedartists 0:4977187e90c7 73 // However the lpc_swim functions take the window's title bar into
embeddedartists 0:4977187e90c7 74 // consideration which will move them. To combat this the marker's
embeddedartists 0:4977187e90c7 75 // coordinates will have to be moved
embeddedartists 0:4977187e90c7 76 x -= _win->xpvmin;
embeddedartists 0:4977187e90c7 77 y -= _win->ypvmin;
embeddedartists 0:4977187e90c7 78
embeddedartists 0:4977187e90c7 79 swim_set_pen_color(_win, (erase ? _win->bkg : BLACK));
embeddedartists 0:4977187e90c7 80 swim_put_line(_win, x-15, y, x+15, y);
embeddedartists 0:4977187e90c7 81 swim_put_line(_win, x, y-15, x, y+15);
embeddedartists 0:4977187e90c7 82 swim_put_circle(_win, x, y, 10, false);
embeddedartists 0:4977187e90c7 83 }
embeddedartists 0:4977187e90c7 84
embeddedartists 0:4977187e90c7 85 bool AppTouchCalibration::calibrate()
embeddedartists 0:4977187e90c7 86 {
embeddedartists 0:4977187e90c7 87 bool morePoints = true;
embeddedartists 0:4977187e90c7 88 bool lastPoint = false;
embeddedartists 0:4977187e90c7 89 uint16_t x, y;
embeddedartists 0:4977187e90c7 90 int point = 0;
embeddedartists 0:4977187e90c7 91
embeddedartists 0:4977187e90c7 92 do {
embeddedartists 0:4977187e90c7 93 //if (!_touch->init(_disp->width(), _disp->height())) {
embeddedartists 0:4977187e90c7 94 // showStatus("Failed to initialize touch controller\n");
embeddedartists 0:4977187e90c7 95 // break;
embeddedartists 0:4977187e90c7 96 //}
embeddedartists 0:4977187e90c7 97 if (!_touch->calibrateStart()) {
embeddedartists 0:4977187e90c7 98 showStatus("Failed to start calibration\n");
embeddedartists 0:4977187e90c7 99 break;
embeddedartists 0:4977187e90c7 100 }
embeddedartists 0:4977187e90c7 101 while (morePoints) {
embeddedartists 0:4977187e90c7 102 if (point++ > 0) {
embeddedartists 0:4977187e90c7 103 // erase old location
embeddedartists 0:4977187e90c7 104 drawMarker(x, y, true);
embeddedartists 0:4977187e90c7 105 }
embeddedartists 0:4977187e90c7 106 if (!_touch->getNextCalibratePoint(&x, &y, &lastPoint)) {
embeddedartists 0:4977187e90c7 107 showStatus("Failed to get calibration point\n");
embeddedartists 0:4977187e90c7 108 break;
embeddedartists 0:4977187e90c7 109 }
embeddedartists 0:4977187e90c7 110 drawMarker(x, y, false);
embeddedartists 0:4977187e90c7 111 if (lastPoint) {
embeddedartists 0:4977187e90c7 112 showStatus("Last calibration point. After this the data will");
embeddedartists 0:4977187e90c7 113 showStatus("be saved, which can take a couple of seconds!", 1);
embeddedartists 0:4977187e90c7 114 }
embeddedartists 0:4977187e90c7 115 if (!_touch->waitForCalibratePoint(&morePoints, 0)) {
embeddedartists 0:4977187e90c7 116 showStatus("Failed to get user click\n");
embeddedartists 0:4977187e90c7 117 break;
embeddedartists 0:4977187e90c7 118 }
embeddedartists 0:4977187e90c7 119 }
embeddedartists 0:4977187e90c7 120 if (morePoints) {
embeddedartists 0:4977187e90c7 121 // aborted calibration due to error(s)
embeddedartists 0:4977187e90c7 122 break;
embeddedartists 0:4977187e90c7 123 }
embeddedartists 0:4977187e90c7 124
embeddedartists 0:4977187e90c7 125 // erase old location
embeddedartists 0:4977187e90c7 126 drawMarker(x, y, true);
embeddedartists 0:4977187e90c7 127 } while(0);
embeddedartists 0:4977187e90c7 128
embeddedartists 0:4977187e90c7 129 return !morePoints;
embeddedartists 0:4977187e90c7 130 }
embeddedartists 0:4977187e90c7 131
embeddedartists 0:4977187e90c7 132 void AppTouchCalibration::showStatus(const char* message, int line)
embeddedartists 0:4977187e90c7 133 {
embeddedartists 0:4977187e90c7 134 if (line == 0) {
embeddedartists 0:4977187e90c7 135 swim_clear_screen(_msg, _msg->bkg);
embeddedartists 0:4977187e90c7 136 swim_put_text_centered_win(_msg, message, 0);
embeddedartists 0:4977187e90c7 137 } else {
embeddedartists 0:4977187e90c7 138 swim_put_text_centered_win(_msg, message, line*swim_get_font_height(_msg));
embeddedartists 0:4977187e90c7 139 }
embeddedartists 0:4977187e90c7 140 DMBoard::instance().logger()->printf("[CALIB] %s\n", message);
embeddedartists 0:4977187e90c7 141 }
embeddedartists 0:4977187e90c7 142
embeddedartists 0:4977187e90c7 143 /******************************************************************************
embeddedartists 0:4977187e90c7 144 * Public functions
embeddedartists 0:4977187e90c7 145 *****************************************************************************/
embeddedartists 0:4977187e90c7 146
embeddedartists 0:4977187e90c7 147 AppTouchCalibration::AppTouchCalibration() : _disp(NULL), _touch(NULL), _win(NULL), _fb(NULL), _btn(NULL)
embeddedartists 0:4977187e90c7 148 {
embeddedartists 0:4977187e90c7 149 }
embeddedartists 0:4977187e90c7 150
embeddedartists 0:4977187e90c7 151 AppTouchCalibration::~AppTouchCalibration()
embeddedartists 0:4977187e90c7 152 {
embeddedartists 0:4977187e90c7 153 teardown();
embeddedartists 0:4977187e90c7 154 }
embeddedartists 0:4977187e90c7 155
embeddedartists 0:4977187e90c7 156 bool AppTouchCalibration::setup()
embeddedartists 0:4977187e90c7 157 {
embeddedartists 0:4977187e90c7 158 _disp = DMBoard::instance().display();
embeddedartists 0:4977187e90c7 159 _touch = DMBoard::instance().touchPanel();
embeddedartists 0:4977187e90c7 160 _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T));
embeddedartists 0:4977187e90c7 161 _msg = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T));
embeddedartists 0:4977187e90c7 162 _fb = _disp->allocateFramebuffer();
embeddedartists 0:4977187e90c7 163
embeddedartists 0:4977187e90c7 164 return (_win != NULL && _msg != NULL && _fb != NULL);
embeddedartists 0:4977187e90c7 165 }
embeddedartists 0:4977187e90c7 166
embeddedartists 0:4977187e90c7 167 void AppTouchCalibration::runToCompletion()
embeddedartists 0:4977187e90c7 168 {
embeddedartists 0:4977187e90c7 169 // Alternative 1: use the calling thread's context to run in
embeddedartists 0:4977187e90c7 170 bool done = false;
embeddedartists 0:4977187e90c7 171 draw();
embeddedartists 0:4977187e90c7 172 _btn->setAction(buttonClicked, (uint32_t)&done);
embeddedartists 0:4977187e90c7 173 void* oldFB = _disp->swapFramebuffer(_fb);
embeddedartists 0:4977187e90c7 174
embeddedartists 0:4977187e90c7 175 // Run calibration
embeddedartists 0:4977187e90c7 176 if (calibrate()) {
embeddedartists 0:4977187e90c7 177 showStatus("Calibration Completed. Test to draw!");
embeddedartists 0:4977187e90c7 178 _btn->draw();
embeddedartists 0:4977187e90c7 179 } else {
embeddedartists 0:4977187e90c7 180 // Something went wrong. The error is already shown!
embeddedartists 0:4977187e90c7 181 // Without touch there is no point in continuing, ask
embeddedartists 0:4977187e90c7 182 // user to reset.
embeddedartists 0:4977187e90c7 183
embeddedartists 0:4977187e90c7 184 swim_put_text_centered_win(_win, "Without touch there is nothing to do. Press RESET !", 2*_disp->height()/3);
embeddedartists 0:4977187e90c7 185 while(1) {
embeddedartists 0:4977187e90c7 186 Thread::wait(5000);
embeddedartists 0:4977187e90c7 187 }
embeddedartists 0:4977187e90c7 188 }
embeddedartists 0:4977187e90c7 189
embeddedartists 0:4977187e90c7 190 // Allow user to draw. Exit by pressing button
embeddedartists 0:4977187e90c7 191 swim_set_pen_color(_win, BLACK);
embeddedartists 0:4977187e90c7 192 TouchPanel::touchCoordinate_t coord;
embeddedartists 0:4977187e90c7 193 while(!done) {
embeddedartists 0:4977187e90c7 194 _touch->read(coord);
embeddedartists 0:4977187e90c7 195 if (coord.z > 0) {
embeddedartists 0:4977187e90c7 196 //swim_put_pixel(_win, coord.x, coord.y);
embeddedartists 0:4977187e90c7 197 swim_put_box(_win, coord.x-1, coord.y-1, coord.x+1, coord.y+1);
embeddedartists 0:4977187e90c7 198 }
embeddedartists 0:4977187e90c7 199 if (_btn->handle(coord.x, coord.y, coord.z > 0)) {
embeddedartists 0:4977187e90c7 200 _btn->draw();
embeddedartists 0:4977187e90c7 201 }
embeddedartists 0:4977187e90c7 202 }
embeddedartists 0:4977187e90c7 203
embeddedartists 0:4977187e90c7 204 // User has clicked the button, restore the original FB
embeddedartists 0:4977187e90c7 205 _disp->swapFramebuffer(oldFB);
embeddedartists 0:4977187e90c7 206 swim_window_close(_win);
embeddedartists 0:4977187e90c7 207 swim_window_close(_msg);
embeddedartists 0:4977187e90c7 208 }
embeddedartists 0:4977187e90c7 209
embeddedartists 0:4977187e90c7 210 bool AppTouchCalibration::teardown()
embeddedartists 0:4977187e90c7 211 {
embeddedartists 0:4977187e90c7 212 if (_win != NULL) {
embeddedartists 0:4977187e90c7 213 free(_win);
embeddedartists 0:4977187e90c7 214 _win = NULL;
embeddedartists 0:4977187e90c7 215 }
embeddedartists 0:4977187e90c7 216 if (_msg != NULL) {
embeddedartists 0:4977187e90c7 217 free(_msg);
embeddedartists 0:4977187e90c7 218 _msg = NULL;
embeddedartists 0:4977187e90c7 219 }
embeddedartists 0:4977187e90c7 220 if (_fb != NULL) {
embeddedartists 0:4977187e90c7 221 free(_fb);
embeddedartists 0:4977187e90c7 222 _fb = NULL;
embeddedartists 0:4977187e90c7 223 }
embeddedartists 0:4977187e90c7 224 if (_btn != NULL) {
embeddedartists 0:4977187e90c7 225 delete _btn;
embeddedartists 0:4977187e90c7 226 _btn = NULL;
embeddedartists 0:4977187e90c7 227 }
embeddedartists 0:4977187e90c7 228 return true;
embeddedartists 0:4977187e90c7 229 }
embeddedartists 0:4977187e90c7 230
embeddedartists 0:4977187e90c7 231