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
Application/AppTouchCalibration.cpp@22:f0d00f29bfeb, 2019-11-04 (annotated)
- Committer:
- embeddedartists
- Date:
- Mon Nov 04 14:31:50 2019 +0000
- Revision:
- 22:f0d00f29bfeb
- Parent:
- 21:0038059e3a8f
More updates related to mbed OS 5
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_OFF 20 |
embeddedartists | 0:4977187e90c7 | 27 | |
embeddedartists | 0:4977187e90c7 | 28 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 29 | * Global variables |
embeddedartists | 0:4977187e90c7 | 30 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 31 | |
embeddedartists | 0:4977187e90c7 | 32 | |
embeddedartists | 0:4977187e90c7 | 33 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 34 | * Private functions |
embeddedartists | 0:4977187e90c7 | 35 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 36 | |
embeddedartists | 0:4977187e90c7 | 37 | static void buttonClicked(uint32_t x) |
embeddedartists | 0:4977187e90c7 | 38 | { |
embeddedartists | 0:4977187e90c7 | 39 | bool* done = (bool*)x; |
embeddedartists | 0:4977187e90c7 | 40 | *done = true; |
embeddedartists | 0:4977187e90c7 | 41 | } |
embeddedartists | 0:4977187e90c7 | 42 | |
embeddedartists | 0:4977187e90c7 | 43 | void AppTouchCalibration::draw() |
embeddedartists | 0:4977187e90c7 | 44 | { |
embeddedartists | 0:4977187e90c7 | 45 | // Prepare fullscreen |
embeddedartists | 0:4977187e90c7 | 46 | swim_window_open(_win, |
embeddedartists | 0:4977187e90c7 | 47 | _disp->width(), _disp->height(), // full size |
embeddedartists | 0:4977187e90c7 | 48 | (COLOR_T*)_fb, |
embeddedartists | 0:4977187e90c7 | 49 | 0,0,_disp->width()-1, _disp->height()-1, // window position and size |
embeddedartists | 0:4977187e90c7 | 50 | 1, // border |
embeddedartists | 0:4977187e90c7 | 51 | WHITE, WHITE, BLACK); // colors: pen, backgr, forgr |
embeddedartists | 0:4977187e90c7 | 52 | swim_set_title(_win, "Touch Calibration", BLACK); |
embeddedartists | 0:4977187e90c7 | 53 | |
embeddedartists | 0:4977187e90c7 | 54 | // Prepare status area in the middle |
embeddedartists | 0:4977187e90c7 | 55 | swim_window_open(_msg, |
embeddedartists | 0:4977187e90c7 | 56 | _disp->width(), _disp->height(), // full size |
embeddedartists | 0:4977187e90c7 | 57 | (COLOR_T*)_fb, |
embeddedartists | 0:4977187e90c7 | 58 | 50,_disp->height()/2-15,_disp->width()-50, _disp->height()/2+15, // window position and size |
embeddedartists | 0:4977187e90c7 | 59 | 0, // border |
embeddedartists | 0:4977187e90c7 | 60 | BLACK, WHITE, BLACK); // colors: pen, backgr, forgr |
embeddedartists | 0:4977187e90c7 | 61 | |
embeddedartists | 0:4977187e90c7 | 62 | showStatus("Press the crosshairs in each of the corners"); |
embeddedartists | 0:4977187e90c7 | 63 | |
embeddedartists | 0:4977187e90c7 | 64 | // Create (but don't show) the button |
embeddedartists | 17:6e2abf107800 | 65 | _btn = new ImageButton(_win->fb, _win->xpmax - BTN_OFF - _resOk->width(), _win->ypmax - BTN_OFF - _resOk->height(), _resOk->width(), _resOk->height()); |
embeddedartists | 17:6e2abf107800 | 66 | _btn->loadImages(_resOk); |
embeddedartists | 0:4977187e90c7 | 67 | } |
embeddedartists | 0:4977187e90c7 | 68 | |
embeddedartists | 0:4977187e90c7 | 69 | void AppTouchCalibration::drawMarker(uint16_t x, uint16_t y, bool erase) |
embeddedartists | 0:4977187e90c7 | 70 | { |
embeddedartists | 0:4977187e90c7 | 71 | // The markers must be drawn at exact locations to get a good calibration. |
embeddedartists | 0:4977187e90c7 | 72 | // However the lpc_swim functions take the window's title bar into |
embeddedartists | 0:4977187e90c7 | 73 | // consideration which will move them. To combat this the marker's |
embeddedartists | 0:4977187e90c7 | 74 | // coordinates will have to be moved |
embeddedartists | 0:4977187e90c7 | 75 | x -= _win->xpvmin; |
embeddedartists | 0:4977187e90c7 | 76 | y -= _win->ypvmin; |
embeddedartists | 0:4977187e90c7 | 77 | |
embeddedartists | 0:4977187e90c7 | 78 | swim_set_pen_color(_win, (erase ? _win->bkg : BLACK)); |
embeddedartists | 0:4977187e90c7 | 79 | swim_put_line(_win, x-15, y, x+15, y); |
embeddedartists | 0:4977187e90c7 | 80 | swim_put_line(_win, x, y-15, x, y+15); |
embeddedartists | 0:4977187e90c7 | 81 | swim_put_circle(_win, x, y, 10, false); |
embeddedartists | 0:4977187e90c7 | 82 | } |
embeddedartists | 0:4977187e90c7 | 83 | |
embeddedartists | 0:4977187e90c7 | 84 | bool AppTouchCalibration::calibrate() |
embeddedartists | 0:4977187e90c7 | 85 | { |
embeddedartists | 0:4977187e90c7 | 86 | bool morePoints = true; |
embeddedartists | 0:4977187e90c7 | 87 | bool lastPoint = false; |
embeddedartists | 0:4977187e90c7 | 88 | uint16_t x, y; |
embeddedartists | 0:4977187e90c7 | 89 | int point = 0; |
embeddedartists | 0:4977187e90c7 | 90 | |
embeddedartists | 0:4977187e90c7 | 91 | do { |
embeddedartists | 4:a73760d09423 | 92 | if (_touch->calibrateStart() != TouchPanel::TouchError_Ok) { |
embeddedartists | 0:4977187e90c7 | 93 | showStatus("Failed to start calibration\n"); |
embeddedartists | 0:4977187e90c7 | 94 | break; |
embeddedartists | 0:4977187e90c7 | 95 | } |
embeddedartists | 0:4977187e90c7 | 96 | while (morePoints) { |
embeddedartists | 0:4977187e90c7 | 97 | if (point++ > 0) { |
embeddedartists | 0:4977187e90c7 | 98 | // erase old location |
embeddedartists | 0:4977187e90c7 | 99 | drawMarker(x, y, true); |
embeddedartists | 0:4977187e90c7 | 100 | } |
embeddedartists | 4:a73760d09423 | 101 | if (_touch->getNextCalibratePoint(&x, &y, &lastPoint) != TouchPanel::TouchError_Ok) { |
embeddedartists | 0:4977187e90c7 | 102 | showStatus("Failed to get calibration point\n"); |
embeddedartists | 0:4977187e90c7 | 103 | break; |
embeddedartists | 0:4977187e90c7 | 104 | } |
embeddedartists | 0:4977187e90c7 | 105 | drawMarker(x, y, false); |
embeddedartists | 0:4977187e90c7 | 106 | if (lastPoint) { |
embeddedartists | 0:4977187e90c7 | 107 | showStatus("Last calibration point. After this the data will"); |
embeddedartists | 0:4977187e90c7 | 108 | showStatus("be saved, which can take a couple of seconds!", 1); |
embeddedartists | 0:4977187e90c7 | 109 | } |
embeddedartists | 4:a73760d09423 | 110 | if (_touch->waitForCalibratePoint(&morePoints, 0) != TouchPanel::TouchError_Ok) { |
embeddedartists | 0:4977187e90c7 | 111 | showStatus("Failed to get user click\n"); |
embeddedartists | 0:4977187e90c7 | 112 | break; |
embeddedartists | 0:4977187e90c7 | 113 | } |
embeddedartists | 0:4977187e90c7 | 114 | } |
embeddedartists | 0:4977187e90c7 | 115 | if (morePoints) { |
embeddedartists | 0:4977187e90c7 | 116 | // aborted calibration due to error(s) |
embeddedartists | 0:4977187e90c7 | 117 | break; |
embeddedartists | 0:4977187e90c7 | 118 | } |
embeddedartists | 0:4977187e90c7 | 119 | |
embeddedartists | 0:4977187e90c7 | 120 | // erase old location |
embeddedartists | 0:4977187e90c7 | 121 | drawMarker(x, y, true); |
embeddedartists | 0:4977187e90c7 | 122 | } while(0); |
embeddedartists | 0:4977187e90c7 | 123 | |
embeddedartists | 0:4977187e90c7 | 124 | return !morePoints; |
embeddedartists | 0:4977187e90c7 | 125 | } |
embeddedartists | 0:4977187e90c7 | 126 | |
embeddedartists | 0:4977187e90c7 | 127 | void AppTouchCalibration::showStatus(const char* message, int line) |
embeddedartists | 0:4977187e90c7 | 128 | { |
embeddedartists | 0:4977187e90c7 | 129 | if (line == 0) { |
embeddedartists | 0:4977187e90c7 | 130 | swim_clear_screen(_msg, _msg->bkg); |
embeddedartists | 0:4977187e90c7 | 131 | swim_put_text_centered_win(_msg, message, 0); |
embeddedartists | 0:4977187e90c7 | 132 | } else { |
embeddedartists | 0:4977187e90c7 | 133 | swim_put_text_centered_win(_msg, message, line*swim_get_font_height(_msg)); |
embeddedartists | 0:4977187e90c7 | 134 | } |
embeddedartists | 0:4977187e90c7 | 135 | DMBoard::instance().logger()->printf("[CALIB] %s\n", message); |
embeddedartists | 0:4977187e90c7 | 136 | } |
embeddedartists | 0:4977187e90c7 | 137 | |
embeddedartists | 0:4977187e90c7 | 138 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 139 | * Public functions |
embeddedartists | 0:4977187e90c7 | 140 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 141 | |
embeddedartists | 17:6e2abf107800 | 142 | AppTouchCalibration::AppTouchCalibration() : _disp(NULL), _touch(NULL), |
embeddedartists | 17:6e2abf107800 | 143 | _win(NULL), _fb(NULL), _btn(NULL), _resOk(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 | 21:0038059e3a8f | 182 | ThisThread::sleep_for(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 | 8:19a6b70d42b1 | 188 | touch_coordinate_t coord; |
embeddedartists | 0:4977187e90c7 | 189 | while(!done) { |
embeddedartists | 8:19a6b70d42b1 | 190 | // wait for a new touch signal (signal is sent from AppLauncher, |
embeddedartists | 8:19a6b70d42b1 | 191 | // which listens for touch events) |
embeddedartists | 21:0038059e3a8f | 192 | ThisThread::flags_wait_any(0x1); |
embeddedartists | 8:19a6b70d42b1 | 193 | |
embeddedartists | 3:3fabfe3339b8 | 194 | if (_touch->read(coord) == TouchPanel::TouchError_Ok) { |
embeddedartists | 3:3fabfe3339b8 | 195 | if (coord.z > 0) { |
embeddedartists | 3:3fabfe3339b8 | 196 | //swim_put_pixel(_win, coord.x, coord.y); |
embeddedartists | 3:3fabfe3339b8 | 197 | swim_put_box(_win, coord.x-1, coord.y-1, coord.x+1, coord.y+1); |
embeddedartists | 3:3fabfe3339b8 | 198 | } |
embeddedartists | 3:3fabfe3339b8 | 199 | if (_btn->handle(coord.x, coord.y, coord.z > 0)) { |
embeddedartists | 3:3fabfe3339b8 | 200 | _btn->draw(); |
embeddedartists | 3:3fabfe3339b8 | 201 | } |
embeddedartists | 0:4977187e90c7 | 202 | } |
embeddedartists | 0:4977187e90c7 | 203 | } |
embeddedartists | 0:4977187e90c7 | 204 | |
embeddedartists | 0:4977187e90c7 | 205 | // User has clicked the button, restore the original FB |
embeddedartists | 0:4977187e90c7 | 206 | _disp->swapFramebuffer(oldFB); |
embeddedartists | 0:4977187e90c7 | 207 | swim_window_close(_win); |
embeddedartists | 0:4977187e90c7 | 208 | swim_window_close(_msg); |
embeddedartists | 0:4977187e90c7 | 209 | } |
embeddedartists | 0:4977187e90c7 | 210 | |
embeddedartists | 0:4977187e90c7 | 211 | bool AppTouchCalibration::teardown() |
embeddedartists | 0:4977187e90c7 | 212 | { |
embeddedartists | 0:4977187e90c7 | 213 | if (_win != NULL) { |
embeddedartists | 0:4977187e90c7 | 214 | free(_win); |
embeddedartists | 0:4977187e90c7 | 215 | _win = NULL; |
embeddedartists | 0:4977187e90c7 | 216 | } |
embeddedartists | 0:4977187e90c7 | 217 | if (_msg != NULL) { |
embeddedartists | 0:4977187e90c7 | 218 | free(_msg); |
embeddedartists | 0:4977187e90c7 | 219 | _msg = NULL; |
embeddedartists | 0:4977187e90c7 | 220 | } |
embeddedartists | 0:4977187e90c7 | 221 | if (_fb != NULL) { |
embeddedartists | 0:4977187e90c7 | 222 | free(_fb); |
embeddedartists | 0:4977187e90c7 | 223 | _fb = NULL; |
embeddedartists | 0:4977187e90c7 | 224 | } |
embeddedartists | 0:4977187e90c7 | 225 | if (_btn != NULL) { |
embeddedartists | 0:4977187e90c7 | 226 | delete _btn; |
embeddedartists | 0:4977187e90c7 | 227 | _btn = NULL; |
embeddedartists | 0:4977187e90c7 | 228 | } |
embeddedartists | 0:4977187e90c7 | 229 | return true; |
embeddedartists | 0:4977187e90c7 | 230 | } |
embeddedartists | 17:6e2abf107800 | 231 | |
embeddedartists | 17:6e2abf107800 | 232 | void AppTouchCalibration::addResource(Resources id, Resource* res) |
embeddedartists | 17:6e2abf107800 | 233 | { |
embeddedartists | 17:6e2abf107800 | 234 | _resOk = res; |
embeddedartists | 17:6e2abf107800 | 235 | } |