Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DMBasicGUI by
Application/AppTouchCalibration.cpp@3:3fabfe3339b8, 2014-12-19 (annotated)
- Committer:
- embeddedartists
- Date:
- Fri Dec 19 09:12:51 2014 +0100
- Revision:
- 3:3fabfe3339b8
- Parent:
- 1:46c8df4608c8
- Child:
- 4:a73760d09423
- Fixed bug in Clickable
- Fixed missing include in Image
- Updated to match the new TouchPanel interface
Who changed what in which revision?
| User | Revision | Line number | New 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->calibrateStart()) { |
| embeddedartists | 0:4977187e90c7 | 94 | showStatus("Failed to start calibration\n"); |
| embeddedartists | 0:4977187e90c7 | 95 | break; |
| embeddedartists | 0:4977187e90c7 | 96 | } |
| embeddedartists | 0:4977187e90c7 | 97 | while (morePoints) { |
| embeddedartists | 0:4977187e90c7 | 98 | if (point++ > 0) { |
| embeddedartists | 0:4977187e90c7 | 99 | // erase old location |
| embeddedartists | 0:4977187e90c7 | 100 | drawMarker(x, y, true); |
| embeddedartists | 0:4977187e90c7 | 101 | } |
| embeddedartists | 0:4977187e90c7 | 102 | if (!_touch->getNextCalibratePoint(&x, &y, &lastPoint)) { |
| embeddedartists | 0:4977187e90c7 | 103 | showStatus("Failed to get calibration point\n"); |
| embeddedartists | 0:4977187e90c7 | 104 | break; |
| embeddedartists | 0:4977187e90c7 | 105 | } |
| embeddedartists | 0:4977187e90c7 | 106 | drawMarker(x, y, false); |
| embeddedartists | 0:4977187e90c7 | 107 | if (lastPoint) { |
| embeddedartists | 0:4977187e90c7 | 108 | showStatus("Last calibration point. After this the data will"); |
| embeddedartists | 0:4977187e90c7 | 109 | showStatus("be saved, which can take a couple of seconds!", 1); |
| embeddedartists | 0:4977187e90c7 | 110 | } |
| embeddedartists | 0:4977187e90c7 | 111 | if (!_touch->waitForCalibratePoint(&morePoints, 0)) { |
| embeddedartists | 0:4977187e90c7 | 112 | showStatus("Failed to get user click\n"); |
| embeddedartists | 0:4977187e90c7 | 113 | break; |
| embeddedartists | 0:4977187e90c7 | 114 | } |
| embeddedartists | 0:4977187e90c7 | 115 | } |
| embeddedartists | 0:4977187e90c7 | 116 | if (morePoints) { |
| embeddedartists | 0:4977187e90c7 | 117 | // aborted calibration due to error(s) |
| embeddedartists | 0:4977187e90c7 | 118 | break; |
| embeddedartists | 0:4977187e90c7 | 119 | } |
| embeddedartists | 0:4977187e90c7 | 120 | |
| embeddedartists | 0:4977187e90c7 | 121 | // erase old location |
| embeddedartists | 0:4977187e90c7 | 122 | drawMarker(x, y, true); |
| embeddedartists | 0:4977187e90c7 | 123 | } while(0); |
| embeddedartists | 0:4977187e90c7 | 124 | |
| embeddedartists | 0:4977187e90c7 | 125 | return !morePoints; |
| embeddedartists | 0:4977187e90c7 | 126 | } |
| embeddedartists | 0:4977187e90c7 | 127 | |
| embeddedartists | 0:4977187e90c7 | 128 | void AppTouchCalibration::showStatus(const char* message, int line) |
| embeddedartists | 0:4977187e90c7 | 129 | { |
| embeddedartists | 0:4977187e90c7 | 130 | if (line == 0) { |
| embeddedartists | 0:4977187e90c7 | 131 | swim_clear_screen(_msg, _msg->bkg); |
| embeddedartists | 0:4977187e90c7 | 132 | swim_put_text_centered_win(_msg, message, 0); |
| embeddedartists | 0:4977187e90c7 | 133 | } else { |
| embeddedartists | 0:4977187e90c7 | 134 | swim_put_text_centered_win(_msg, message, line*swim_get_font_height(_msg)); |
| embeddedartists | 0:4977187e90c7 | 135 | } |
| embeddedartists | 0:4977187e90c7 | 136 | DMBoard::instance().logger()->printf("[CALIB] %s\n", message); |
| embeddedartists | 0:4977187e90c7 | 137 | } |
| embeddedartists | 0:4977187e90c7 | 138 | |
| embeddedartists | 0:4977187e90c7 | 139 | /****************************************************************************** |
| embeddedartists | 0:4977187e90c7 | 140 | * Public functions |
| embeddedartists | 0:4977187e90c7 | 141 | *****************************************************************************/ |
| embeddedartists | 0:4977187e90c7 | 142 | |
| embeddedartists | 0:4977187e90c7 | 143 | AppTouchCalibration::AppTouchCalibration() : _disp(NULL), _touch(NULL), _win(NULL), _fb(NULL), _btn(NULL) |
| embeddedartists | 0:4977187e90c7 | 144 | { |
| embeddedartists | 0:4977187e90c7 | 145 | } |
| embeddedartists | 0:4977187e90c7 | 146 | |
| embeddedartists | 0:4977187e90c7 | 147 | AppTouchCalibration::~AppTouchCalibration() |
| embeddedartists | 0:4977187e90c7 | 148 | { |
| embeddedartists | 0:4977187e90c7 | 149 | teardown(); |
| embeddedartists | 0:4977187e90c7 | 150 | } |
| embeddedartists | 0:4977187e90c7 | 151 | |
| embeddedartists | 0:4977187e90c7 | 152 | bool AppTouchCalibration::setup() |
| embeddedartists | 0:4977187e90c7 | 153 | { |
| embeddedartists | 0:4977187e90c7 | 154 | _disp = DMBoard::instance().display(); |
| embeddedartists | 0:4977187e90c7 | 155 | _touch = DMBoard::instance().touchPanel(); |
| embeddedartists | 0:4977187e90c7 | 156 | _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T)); |
| embeddedartists | 0:4977187e90c7 | 157 | _msg = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T)); |
| embeddedartists | 0:4977187e90c7 | 158 | _fb = _disp->allocateFramebuffer(); |
| embeddedartists | 0:4977187e90c7 | 159 | |
| embeddedartists | 0:4977187e90c7 | 160 | return (_win != NULL && _msg != NULL && _fb != NULL); |
| embeddedartists | 0:4977187e90c7 | 161 | } |
| embeddedartists | 0:4977187e90c7 | 162 | |
| embeddedartists | 0:4977187e90c7 | 163 | void AppTouchCalibration::runToCompletion() |
| embeddedartists | 0:4977187e90c7 | 164 | { |
| embeddedartists | 0:4977187e90c7 | 165 | // Alternative 1: use the calling thread's context to run in |
| embeddedartists | 0:4977187e90c7 | 166 | bool done = false; |
| embeddedartists | 0:4977187e90c7 | 167 | draw(); |
| embeddedartists | 0:4977187e90c7 | 168 | _btn->setAction(buttonClicked, (uint32_t)&done); |
| embeddedartists | 0:4977187e90c7 | 169 | void* oldFB = _disp->swapFramebuffer(_fb); |
| embeddedartists | 0:4977187e90c7 | 170 | |
| embeddedartists | 0:4977187e90c7 | 171 | // Run calibration |
| embeddedartists | 0:4977187e90c7 | 172 | if (calibrate()) { |
| embeddedartists | 0:4977187e90c7 | 173 | showStatus("Calibration Completed. Test to draw!"); |
| embeddedartists | 0:4977187e90c7 | 174 | _btn->draw(); |
| embeddedartists | 0:4977187e90c7 | 175 | } else { |
| embeddedartists | 0:4977187e90c7 | 176 | // Something went wrong. The error is already shown! |
| embeddedartists | 0:4977187e90c7 | 177 | // Without touch there is no point in continuing, ask |
| embeddedartists | 0:4977187e90c7 | 178 | // user to reset. |
| embeddedartists | 0:4977187e90c7 | 179 | |
| embeddedartists | 0:4977187e90c7 | 180 | swim_put_text_centered_win(_win, "Without touch there is nothing to do. Press RESET !", 2*_disp->height()/3); |
| embeddedartists | 0:4977187e90c7 | 181 | while(1) { |
| embeddedartists | 0:4977187e90c7 | 182 | Thread::wait(5000); |
| embeddedartists | 0:4977187e90c7 | 183 | } |
| embeddedartists | 0:4977187e90c7 | 184 | } |
| embeddedartists | 0:4977187e90c7 | 185 | |
| embeddedartists | 0:4977187e90c7 | 186 | // Allow user to draw. Exit by pressing button |
| embeddedartists | 0:4977187e90c7 | 187 | swim_set_pen_color(_win, BLACK); |
| embeddedartists | 0:4977187e90c7 | 188 | TouchPanel::touchCoordinate_t coord; |
| embeddedartists | 0:4977187e90c7 | 189 | while(!done) { |
| embeddedartists | 3:3fabfe3339b8 | 190 | if (_touch->read(coord) == TouchPanel::TouchError_Ok) { |
| embeddedartists | 3:3fabfe3339b8 | 191 | if (coord.z > 0) { |
| embeddedartists | 3:3fabfe3339b8 | 192 | //swim_put_pixel(_win, coord.x, coord.y); |
| embeddedartists | 3:3fabfe3339b8 | 193 | swim_put_box(_win, coord.x-1, coord.y-1, coord.x+1, coord.y+1); |
| embeddedartists | 3:3fabfe3339b8 | 194 | } |
| embeddedartists | 3:3fabfe3339b8 | 195 | if (_btn->handle(coord.x, coord.y, coord.z > 0)) { |
| embeddedartists | 3:3fabfe3339b8 | 196 | _btn->draw(); |
| embeddedartists | 3:3fabfe3339b8 | 197 | } |
| embeddedartists | 0:4977187e90c7 | 198 | } |
| embeddedartists | 0:4977187e90c7 | 199 | } |
| embeddedartists | 0:4977187e90c7 | 200 | |
| embeddedartists | 0:4977187e90c7 | 201 | // User has clicked the button, restore the original FB |
| embeddedartists | 0:4977187e90c7 | 202 | _disp->swapFramebuffer(oldFB); |
| embeddedartists | 0:4977187e90c7 | 203 | swim_window_close(_win); |
| embeddedartists | 0:4977187e90c7 | 204 | swim_window_close(_msg); |
| embeddedartists | 0:4977187e90c7 | 205 | } |
| embeddedartists | 0:4977187e90c7 | 206 | |
| embeddedartists | 0:4977187e90c7 | 207 | bool AppTouchCalibration::teardown() |
| embeddedartists | 0:4977187e90c7 | 208 | { |
| embeddedartists | 0:4977187e90c7 | 209 | if (_win != NULL) { |
| embeddedartists | 0:4977187e90c7 | 210 | free(_win); |
| embeddedartists | 0:4977187e90c7 | 211 | _win = NULL; |
| embeddedartists | 0:4977187e90c7 | 212 | } |
| embeddedartists | 0:4977187e90c7 | 213 | if (_msg != NULL) { |
| embeddedartists | 0:4977187e90c7 | 214 | free(_msg); |
| embeddedartists | 0:4977187e90c7 | 215 | _msg = NULL; |
| embeddedartists | 0:4977187e90c7 | 216 | } |
| embeddedartists | 0:4977187e90c7 | 217 | if (_fb != NULL) { |
| embeddedartists | 0:4977187e90c7 | 218 | free(_fb); |
| embeddedartists | 0:4977187e90c7 | 219 | _fb = NULL; |
| embeddedartists | 0:4977187e90c7 | 220 | } |
| embeddedartists | 0:4977187e90c7 | 221 | if (_btn != NULL) { |
| embeddedartists | 0:4977187e90c7 | 222 | delete _btn; |
| embeddedartists | 0:4977187e90c7 | 223 | _btn = NULL; |
| embeddedartists | 0:4977187e90c7 | 224 | } |
| embeddedartists | 0:4977187e90c7 | 225 | return true; |
| embeddedartists | 0:4977187e90c7 | 226 | } |
