Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This program displays how to use the emWin library from Segger.

Dependencies:   EALib ewgui mbed

This program requires the emWin library. Instructions and more information.

Committer:
embeddedartists
Date:
Tue Jul 14 11:34:15 2015 +0000
Revision:
0:7f5765fcd048
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:7f5765fcd048 1 /*
embeddedartists 0:7f5765fcd048 2 * Copyright 2013 Embedded Artists AB
embeddedartists 0:7f5765fcd048 3 *
embeddedartists 0:7f5765fcd048 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 0:7f5765fcd048 5 * you may not use this file except in compliance with the License.
embeddedartists 0:7f5765fcd048 6 * You may obtain a copy of the License at
embeddedartists 0:7f5765fcd048 7 *
embeddedartists 0:7f5765fcd048 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 0:7f5765fcd048 9 *
embeddedartists 0:7f5765fcd048 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 0:7f5765fcd048 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 0:7f5765fcd048 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 0:7f5765fcd048 13 * See the License for the specific language governing permissions and
embeddedartists 0:7f5765fcd048 14 * limitations under the License.
embeddedartists 0:7f5765fcd048 15 */
embeddedartists 0:7f5765fcd048 16
embeddedartists 0:7f5765fcd048 17 /******************************************************************************
embeddedartists 0:7f5765fcd048 18 * Includes
embeddedartists 0:7f5765fcd048 19 *****************************************************************************/
embeddedartists 0:7f5765fcd048 20
embeddedartists 0:7f5765fcd048 21 #include "mbed.h"
embeddedartists 0:7f5765fcd048 22 #include "mbed_debug.h"
embeddedartists 0:7f5765fcd048 23
embeddedartists 0:7f5765fcd048 24 #include "AR1021I2C.h"
embeddedartists 0:7f5765fcd048 25
embeddedartists 0:7f5765fcd048 26 /******************************************************************************
embeddedartists 0:7f5765fcd048 27 * Defines and typedefs
embeddedartists 0:7f5765fcd048 28 *****************************************************************************/
embeddedartists 0:7f5765fcd048 29
embeddedartists 0:7f5765fcd048 30 #define AR1021_REG_TOUCH_THRESHOLD (0x02)
embeddedartists 0:7f5765fcd048 31 #define AR1021_REG_SENS_FILTER (0x03)
embeddedartists 0:7f5765fcd048 32 #define AR1021_REG_SAMPLING_FAST (0x04)
embeddedartists 0:7f5765fcd048 33 #define AR1021_REG_SAMPLING_SLOW (0x05)
embeddedartists 0:7f5765fcd048 34 #define AR1021_REG_ACC_FILTER_FAST (0x06)
embeddedartists 0:7f5765fcd048 35 #define AR1021_REG_ACC_FILTER_SLOW (0x07)
embeddedartists 0:7f5765fcd048 36 #define AR1021_REG_SPEED_THRESHOLD (0x08)
embeddedartists 0:7f5765fcd048 37 #define AR1021_REG_SLEEP_DELAY (0x0A)
embeddedartists 0:7f5765fcd048 38 #define AR1021_REG_PEN_UP_DELAY (0x0B)
embeddedartists 0:7f5765fcd048 39 #define AR1021_REG_TOUCH_MODE (0x0C)
embeddedartists 0:7f5765fcd048 40 #define AR1021_REG_TOUCH_OPTIONS (0x0D)
embeddedartists 0:7f5765fcd048 41 #define AR1021_REG_CALIB_INSETS (0x0E)
embeddedartists 0:7f5765fcd048 42 #define AR1021_REG_PEN_STATE_REPORT_DELAY (0x0F)
embeddedartists 0:7f5765fcd048 43 #define AR1021_REG_TOUCH_REPORT_DELAY (0x11)
embeddedartists 0:7f5765fcd048 44
embeddedartists 0:7f5765fcd048 45
embeddedartists 0:7f5765fcd048 46 #define AR1021_CMD_GET_VERSION (0x10)
embeddedartists 0:7f5765fcd048 47 #define AR1021_CMD_ENABLE_TOUCH (0x12)
embeddedartists 0:7f5765fcd048 48 #define AR1021_CMD_DISABLE_TOUCH (0x13)
embeddedartists 0:7f5765fcd048 49 #define AR1021_CMD_CALIBRATE_MODE (0x14)
embeddedartists 0:7f5765fcd048 50 #define AR1021_CMD_REGISTER_READ (0x20)
embeddedartists 0:7f5765fcd048 51 #define AR1021_CMD_REGISTER_WRITE (0x21)
embeddedartists 0:7f5765fcd048 52 #define AR1021_CMD_REGISTER_START_ADDR_REQUEST (0x22)
embeddedartists 0:7f5765fcd048 53 #define AR1021_CMD_REGISTER_WRITE_TO_EEPROM (0x23)
embeddedartists 0:7f5765fcd048 54 #define AR1021_CMD_EEPROM_READ (0x28)
embeddedartists 0:7f5765fcd048 55 #define AR1021_CMD_EEPROM_WRITE (0x29)
embeddedartists 0:7f5765fcd048 56 #define AR1021_CMD_EEPROM_WRITE_TO_REGISTERS (0x2B)
embeddedartists 0:7f5765fcd048 57
embeddedartists 0:7f5765fcd048 58 #define AR1021_RESP_STAT_OK (0x00)
embeddedartists 0:7f5765fcd048 59 #define AR1021_RESP_STAT_CMD_UNREC (0x01)
embeddedartists 0:7f5765fcd048 60 #define AR1021_RESP_STAT_HDR_UNREC (0x03)
embeddedartists 0:7f5765fcd048 61 #define AR1021_RESP_STAT_TIMEOUT (0x04)
embeddedartists 0:7f5765fcd048 62 #define AR1021_RESP_STAT_CANCEL_CALIB (0xFC)
embeddedartists 0:7f5765fcd048 63
embeddedartists 0:7f5765fcd048 64
embeddedartists 0:7f5765fcd048 65 #define AR1021_ERR_NO_HDR (-1000)
embeddedartists 0:7f5765fcd048 66 #define AR1021_ERR_INV_LEN (-1001)
embeddedartists 0:7f5765fcd048 67 #define AR1021_ERR_INV_RESP (-1002)
embeddedartists 0:7f5765fcd048 68 #define AR1021_ERR_INV_RESPLEN (-1003)
embeddedartists 0:7f5765fcd048 69 #define AR1021_ERR_TIMEOUT (-1004)
embeddedartists 0:7f5765fcd048 70
embeddedartists 0:7f5765fcd048 71 // bit 7 is always 1 and bit 0 defines pen up or down
embeddedartists 0:7f5765fcd048 72 #define AR1021_PEN_MASK (0x81)
embeddedartists 0:7f5765fcd048 73
embeddedartists 0:7f5765fcd048 74 #define AR1021_NUM_CALIB_POINTS (4)
embeddedartists 0:7f5765fcd048 75
embeddedartists 0:7f5765fcd048 76 #define AR1021_ADDR (0x4D << 1)
embeddedartists 0:7f5765fcd048 77
embeddedartists 0:7f5765fcd048 78 #define AR1021_TIMEOUT 1000 //how many ms to wait for responce
embeddedartists 0:7f5765fcd048 79 #define AR1021_RETRY 5 //how many times to retry sending command
embeddedartists 0:7f5765fcd048 80
embeddedartists 0:7f5765fcd048 81 #define AR1021_MIN(__a, __b) (((__a)<(__b))?(__a):(__b))
embeddedartists 0:7f5765fcd048 82
embeddedartists 0:7f5765fcd048 83
embeddedartists 0:7f5765fcd048 84 AR1021I2C::AR1021I2C(PinName sda, PinName scl, PinName siq) :
embeddedartists 0:7f5765fcd048 85 _i2c(sda, scl), _siq(siq), _siqIrq(siq)
embeddedartists 0:7f5765fcd048 86 {
embeddedartists 0:7f5765fcd048 87 _i2c.frequency(200000);
embeddedartists 0:7f5765fcd048 88
embeddedartists 0:7f5765fcd048 89 // default calibration inset is 25 -> (25/2 = 12.5%)
embeddedartists 0:7f5765fcd048 90 _inset = 25;
embeddedartists 0:7f5765fcd048 91
embeddedartists 0:7f5765fcd048 92 // make sure _calibPoint has an invalid value to begin with
embeddedartists 0:7f5765fcd048 93 // correct value is set in calibrateStart()
embeddedartists 0:7f5765fcd048 94 _calibPoint = AR1021_NUM_CALIB_POINTS+1;
embeddedartists 0:7f5765fcd048 95
embeddedartists 0:7f5765fcd048 96 _x = 0;
embeddedartists 0:7f5765fcd048 97 _y = 0;
embeddedartists 0:7f5765fcd048 98 _pen = 0;
embeddedartists 0:7f5765fcd048 99
embeddedartists 0:7f5765fcd048 100 _initialized = false;
embeddedartists 0:7f5765fcd048 101 }
embeddedartists 0:7f5765fcd048 102
embeddedartists 0:7f5765fcd048 103
embeddedartists 0:7f5765fcd048 104 bool AR1021I2C::read(touchCoordinate_t &coord) {
embeddedartists 0:7f5765fcd048 105
embeddedartists 0:7f5765fcd048 106 if (!_initialized) return false;
embeddedartists 0:7f5765fcd048 107
embeddedartists 0:7f5765fcd048 108 coord.x = _x * _width/4095;
embeddedartists 0:7f5765fcd048 109 coord.y = _y * _height/4095;
embeddedartists 0:7f5765fcd048 110 coord.z = _pen;
embeddedartists 0:7f5765fcd048 111
embeddedartists 0:7f5765fcd048 112 return true;
embeddedartists 0:7f5765fcd048 113 }
embeddedartists 0:7f5765fcd048 114
embeddedartists 0:7f5765fcd048 115
embeddedartists 0:7f5765fcd048 116 bool AR1021I2C::init(uint16_t width, uint16_t height) {
embeddedartists 0:7f5765fcd048 117 int result = 0;
embeddedartists 0:7f5765fcd048 118 bool ok = false;
embeddedartists 0:7f5765fcd048 119 int attempts = 0;
embeddedartists 0:7f5765fcd048 120
embeddedartists 0:7f5765fcd048 121 _width = width;
embeddedartists 0:7f5765fcd048 122 _height = height;
embeddedartists 0:7f5765fcd048 123
embeddedartists 0:7f5765fcd048 124 while (1) {
embeddedartists 0:7f5765fcd048 125
embeddedartists 0:7f5765fcd048 126 do {
embeddedartists 0:7f5765fcd048 127 // disable touch
embeddedartists 0:7f5765fcd048 128 result = cmd(AR1021_CMD_DISABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 0:7f5765fcd048 129 if (result != 0) {
embeddedartists 0:7f5765fcd048 130 debug("disable touch failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 131 break;
embeddedartists 0:7f5765fcd048 132 }
embeddedartists 0:7f5765fcd048 133
embeddedartists 0:7f5765fcd048 134 char regOffset = 0;
embeddedartists 0:7f5765fcd048 135 int regOffLen = 1;
embeddedartists 0:7f5765fcd048 136 result = cmd(AR1021_CMD_REGISTER_START_ADDR_REQUEST, NULL, 0,
embeddedartists 0:7f5765fcd048 137 &regOffset, &regOffLen);
embeddedartists 0:7f5765fcd048 138 if (result != 0) {
embeddedartists 0:7f5765fcd048 139 debug("register offset request failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 140 break;
embeddedartists 0:7f5765fcd048 141 }
embeddedartists 0:7f5765fcd048 142
embeddedartists 0:7f5765fcd048 143 // enable calibrated coordinates
embeddedartists 0:7f5765fcd048 144 // high, low address, len, value
embeddedartists 0:7f5765fcd048 145 char toptions[4] = {0x00, AR1021_REG_TOUCH_OPTIONS+regOffset, 0x01, 0x01};
embeddedartists 0:7f5765fcd048 146 result = cmd(AR1021_CMD_REGISTER_WRITE, toptions, 4, NULL, 0);
embeddedartists 0:7f5765fcd048 147 if (result != 0) {
embeddedartists 0:7f5765fcd048 148 debug("register write request failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 149 break;
embeddedartists 0:7f5765fcd048 150 }
embeddedartists 0:7f5765fcd048 151
embeddedartists 0:7f5765fcd048 152 // save registers to eeprom
embeddedartists 0:7f5765fcd048 153 result = cmd(AR1021_CMD_REGISTER_WRITE_TO_EEPROM, NULL, 0, NULL, 0);
embeddedartists 0:7f5765fcd048 154 if (result != 0) {
embeddedartists 0:7f5765fcd048 155 debug("register write to eeprom failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 156 break;
embeddedartists 0:7f5765fcd048 157 }
embeddedartists 0:7f5765fcd048 158
embeddedartists 0:7f5765fcd048 159 // enable touch
embeddedartists 0:7f5765fcd048 160 result = cmd(AR1021_CMD_ENABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 0:7f5765fcd048 161 if (result != 0) {
embeddedartists 0:7f5765fcd048 162 debug("enable touch failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 163 break;
embeddedartists 0:7f5765fcd048 164 }
embeddedartists 0:7f5765fcd048 165
embeddedartists 0:7f5765fcd048 166 _siqIrq.rise(this, &AR1021I2C::readTouchIrq);
embeddedartists 0:7f5765fcd048 167
embeddedartists 0:7f5765fcd048 168 _initialized = true;
embeddedartists 0:7f5765fcd048 169 ok = true;
embeddedartists 0:7f5765fcd048 170
embeddedartists 0:7f5765fcd048 171 } while(0);
embeddedartists 0:7f5765fcd048 172
embeddedartists 0:7f5765fcd048 173 if (ok) break;
embeddedartists 0:7f5765fcd048 174
embeddedartists 0:7f5765fcd048 175 // try to run the initialize sequence at most 2 times
embeddedartists 0:7f5765fcd048 176 if(++attempts >= 2) break;
embeddedartists 0:7f5765fcd048 177 }
embeddedartists 0:7f5765fcd048 178
embeddedartists 0:7f5765fcd048 179
embeddedartists 0:7f5765fcd048 180 return ok;
embeddedartists 0:7f5765fcd048 181 }
embeddedartists 0:7f5765fcd048 182
embeddedartists 0:7f5765fcd048 183 bool AR1021I2C::calibrateStart() {
embeddedartists 0:7f5765fcd048 184 bool ok = false;
embeddedartists 0:7f5765fcd048 185 int result = 0;
embeddedartists 0:7f5765fcd048 186 int attempts = 0;
embeddedartists 0:7f5765fcd048 187
embeddedartists 0:7f5765fcd048 188 if (!_initialized) return false;
embeddedartists 0:7f5765fcd048 189
embeddedartists 0:7f5765fcd048 190 _siqIrq.rise(NULL);
embeddedartists 0:7f5765fcd048 191
embeddedartists 0:7f5765fcd048 192 while(1) {
embeddedartists 0:7f5765fcd048 193
embeddedartists 0:7f5765fcd048 194 do {
embeddedartists 0:7f5765fcd048 195 // disable touch
embeddedartists 0:7f5765fcd048 196 result = cmd(AR1021_CMD_DISABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 0:7f5765fcd048 197 if (result != 0) {
embeddedartists 0:7f5765fcd048 198 debug("disable touch failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 199 break;
embeddedartists 0:7f5765fcd048 200 }
embeddedartists 0:7f5765fcd048 201
embeddedartists 0:7f5765fcd048 202 char regOffset = 0;
embeddedartists 0:7f5765fcd048 203 int regOffLen = 1;
embeddedartists 0:7f5765fcd048 204 result = cmd(AR1021_CMD_REGISTER_START_ADDR_REQUEST, NULL, 0,
embeddedartists 0:7f5765fcd048 205 &regOffset, &regOffLen);
embeddedartists 0:7f5765fcd048 206 if (result != 0) {
embeddedartists 0:7f5765fcd048 207 debug("register offset request failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 208 break;
embeddedartists 0:7f5765fcd048 209 }
embeddedartists 0:7f5765fcd048 210
embeddedartists 0:7f5765fcd048 211 // set insets
embeddedartists 0:7f5765fcd048 212 // enable calibrated coordinates
embeddedartists 0:7f5765fcd048 213 // high, low address, len, value
embeddedartists 0:7f5765fcd048 214 char insets[4] = {0x00, AR1021_REG_CALIB_INSETS+regOffset, 0x01, _inset};
embeddedartists 0:7f5765fcd048 215 result = cmd(AR1021_CMD_REGISTER_WRITE, insets, 4, NULL, 0);
embeddedartists 0:7f5765fcd048 216 if (result != 0) {
embeddedartists 0:7f5765fcd048 217 debug("register write request failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 218 break;
embeddedartists 0:7f5765fcd048 219 }
embeddedartists 0:7f5765fcd048 220
embeddedartists 0:7f5765fcd048 221 // calibration mode
embeddedartists 0:7f5765fcd048 222 char calibType = 4;
embeddedartists 0:7f5765fcd048 223 result = cmd(AR1021_CMD_CALIBRATE_MODE, &calibType, 1, NULL, 0, false);
embeddedartists 0:7f5765fcd048 224 if (result != 0) {
embeddedartists 0:7f5765fcd048 225 debug("calibration mode failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 226 break;
embeddedartists 0:7f5765fcd048 227 }
embeddedartists 0:7f5765fcd048 228
embeddedartists 0:7f5765fcd048 229 _calibPoint = 0;
embeddedartists 0:7f5765fcd048 230 ok = true;
embeddedartists 0:7f5765fcd048 231
embeddedartists 0:7f5765fcd048 232 } while(0);
embeddedartists 0:7f5765fcd048 233
embeddedartists 0:7f5765fcd048 234 if (ok) break;
embeddedartists 0:7f5765fcd048 235
embeddedartists 0:7f5765fcd048 236 // try to run the calibrate mode sequence at most 2 times
embeddedartists 0:7f5765fcd048 237 if (++attempts >= 2) break;
embeddedartists 0:7f5765fcd048 238 }
embeddedartists 0:7f5765fcd048 239
embeddedartists 0:7f5765fcd048 240 return ok;
embeddedartists 0:7f5765fcd048 241 }
embeddedartists 0:7f5765fcd048 242
embeddedartists 0:7f5765fcd048 243 bool AR1021I2C::getNextCalibratePoint(uint16_t* x, uint16_t* y) {
embeddedartists 0:7f5765fcd048 244
embeddedartists 0:7f5765fcd048 245 if (!_initialized) return false;
embeddedartists 0:7f5765fcd048 246 if (x == NULL || y == NULL) return false;
embeddedartists 0:7f5765fcd048 247
embeddedartists 0:7f5765fcd048 248 int xInset = (_width * _inset / 100) / 2;
embeddedartists 0:7f5765fcd048 249 int yInset = (_height * _inset / 100) / 2;
embeddedartists 0:7f5765fcd048 250
embeddedartists 0:7f5765fcd048 251 switch(_calibPoint) {
embeddedartists 0:7f5765fcd048 252 case 0:
embeddedartists 0:7f5765fcd048 253 *x = xInset;
embeddedartists 0:7f5765fcd048 254 *y = yInset;
embeddedartists 0:7f5765fcd048 255 break;
embeddedartists 0:7f5765fcd048 256 case 1:
embeddedartists 0:7f5765fcd048 257 *x = _width - xInset;
embeddedartists 0:7f5765fcd048 258 *y = yInset;
embeddedartists 0:7f5765fcd048 259 break;
embeddedartists 0:7f5765fcd048 260 case 2:
embeddedartists 0:7f5765fcd048 261 *x = _width - xInset;
embeddedartists 0:7f5765fcd048 262 *y = _height - yInset;
embeddedartists 0:7f5765fcd048 263 break;
embeddedartists 0:7f5765fcd048 264 case 3:
embeddedartists 0:7f5765fcd048 265 *x = xInset;
embeddedartists 0:7f5765fcd048 266 *y = _height - yInset;
embeddedartists 0:7f5765fcd048 267 break;
embeddedartists 0:7f5765fcd048 268 default:
embeddedartists 0:7f5765fcd048 269 return false;
embeddedartists 0:7f5765fcd048 270 }
embeddedartists 0:7f5765fcd048 271
embeddedartists 0:7f5765fcd048 272 return true;
embeddedartists 0:7f5765fcd048 273 }
embeddedartists 0:7f5765fcd048 274
embeddedartists 0:7f5765fcd048 275 bool AR1021I2C::waitForCalibratePoint(bool* morePoints, uint32_t timeout) {
embeddedartists 0:7f5765fcd048 276 int result = 0;
embeddedartists 0:7f5765fcd048 277 bool ret = false;
embeddedartists 0:7f5765fcd048 278
embeddedartists 0:7f5765fcd048 279 if (!_initialized) return false;
embeddedartists 0:7f5765fcd048 280
embeddedartists 0:7f5765fcd048 281 do {
embeddedartists 0:7f5765fcd048 282 if (morePoints == NULL || _calibPoint >= AR1021_NUM_CALIB_POINTS) {
embeddedartists 0:7f5765fcd048 283 break;
embeddedartists 0:7f5765fcd048 284 }
embeddedartists 0:7f5765fcd048 285
embeddedartists 0:7f5765fcd048 286 // wait for response
embeddedartists 0:7f5765fcd048 287 result = waitForCalibResponse(timeout);
embeddedartists 0:7f5765fcd048 288 if (result != 0) {
embeddedartists 0:7f5765fcd048 289 debug("wait for calibration response failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 290 break;
embeddedartists 0:7f5765fcd048 291 }
embeddedartists 0:7f5765fcd048 292
embeddedartists 0:7f5765fcd048 293 _calibPoint++;
embeddedartists 0:7f5765fcd048 294 *morePoints = (_calibPoint < AR1021_NUM_CALIB_POINTS);
embeddedartists 0:7f5765fcd048 295
embeddedartists 0:7f5765fcd048 296
embeddedartists 0:7f5765fcd048 297 // no more points -> enable touch
embeddedartists 0:7f5765fcd048 298 if (!(*morePoints)) {
embeddedartists 0:7f5765fcd048 299
embeddedartists 0:7f5765fcd048 300 // wait for calibration data to be written to eeprom
embeddedartists 0:7f5765fcd048 301 // before enabling touch
embeddedartists 0:7f5765fcd048 302 result = waitForCalibResponse(timeout);
embeddedartists 0:7f5765fcd048 303 if (result != 0) {
embeddedartists 0:7f5765fcd048 304 debug("wait for calibration response failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 305 break;
embeddedartists 0:7f5765fcd048 306 }
embeddedartists 0:7f5765fcd048 307
embeddedartists 0:7f5765fcd048 308
embeddedartists 0:7f5765fcd048 309 // clear chip-select since calibration is done;
embeddedartists 0:7f5765fcd048 310 // _cs = 1;
embeddedartists 0:7f5765fcd048 311
embeddedartists 0:7f5765fcd048 312 result = cmd(AR1021_CMD_ENABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 0:7f5765fcd048 313 if (result != 0) {
embeddedartists 0:7f5765fcd048 314 debug("enable touch failed (%d)\n", result);
embeddedartists 0:7f5765fcd048 315 break;
embeddedartists 0:7f5765fcd048 316 }
embeddedartists 0:7f5765fcd048 317
embeddedartists 0:7f5765fcd048 318 _siqIrq.rise(this, &AR1021I2C::readTouchIrq);
embeddedartists 0:7f5765fcd048 319 }
embeddedartists 0:7f5765fcd048 320
embeddedartists 0:7f5765fcd048 321 ret = true;
embeddedartists 0:7f5765fcd048 322
embeddedartists 0:7f5765fcd048 323 } while (0);
embeddedartists 0:7f5765fcd048 324
embeddedartists 0:7f5765fcd048 325
embeddedartists 0:7f5765fcd048 326
embeddedartists 0:7f5765fcd048 327 if (!ret) {
embeddedartists 0:7f5765fcd048 328 // make sure to set chip-select off in case of an error
embeddedartists 0:7f5765fcd048 329 // _cs = 1;
embeddedartists 0:7f5765fcd048 330 // calibration must restart if an error occurred
embeddedartists 0:7f5765fcd048 331 _calibPoint = AR1021_NUM_CALIB_POINTS+1;
embeddedartists 0:7f5765fcd048 332 }
embeddedartists 0:7f5765fcd048 333
embeddedartists 0:7f5765fcd048 334
embeddedartists 0:7f5765fcd048 335
embeddedartists 0:7f5765fcd048 336 return ret;
embeddedartists 0:7f5765fcd048 337 }
embeddedartists 0:7f5765fcd048 338
embeddedartists 0:7f5765fcd048 339 int AR1021I2C::cmd(char cmd, char* data, int len, char* respBuf, int* respLen,
embeddedartists 0:7f5765fcd048 340 bool setCsOff) {
embeddedartists 0:7f5765fcd048 341
embeddedartists 0:7f5765fcd048 342 int ret = 0;
embeddedartists 0:7f5765fcd048 343 int readLen = (respLen == NULL) ? 0 : *respLen;
embeddedartists 0:7f5765fcd048 344 for (int attempt = 1; attempt <= AR1021_RETRY; attempt++) {
embeddedartists 0:7f5765fcd048 345 if (attempt > 1) {
embeddedartists 0:7f5765fcd048 346 wait_ms(50);
embeddedartists 0:7f5765fcd048 347 }
embeddedartists 0:7f5765fcd048 348
embeddedartists 0:7f5765fcd048 349 // command request
embeddedartists 0:7f5765fcd048 350 // ---------------
embeddedartists 0:7f5765fcd048 351 // 0x00 0x55 len cmd data
embeddedartists 0:7f5765fcd048 352 // 0x00 = protocol command byte
embeddedartists 0:7f5765fcd048 353 // 0x55 = header
embeddedartists 0:7f5765fcd048 354 // len = data length + cmd (1)
embeddedartists 0:7f5765fcd048 355 // data = data to send
embeddedartists 0:7f5765fcd048 356
embeddedartists 0:7f5765fcd048 357 _i2c.start();
embeddedartists 0:7f5765fcd048 358 _i2c.write(AR1021_ADDR); //send write address
embeddedartists 0:7f5765fcd048 359 _i2c.write(0x00);
embeddedartists 0:7f5765fcd048 360 _i2c.write(0x55); //header
embeddedartists 0:7f5765fcd048 361 _i2c.write(len+1); //message length
embeddedartists 0:7f5765fcd048 362 _i2c.write(cmd);
embeddedartists 0:7f5765fcd048 363 for (int i = 0; i < len; i++) {
embeddedartists 0:7f5765fcd048 364 _i2c.write(data[i]);
embeddedartists 0:7f5765fcd048 365 }
embeddedartists 0:7f5765fcd048 366 wait_us(60);
embeddedartists 0:7f5765fcd048 367 _i2c.stop();
embeddedartists 0:7f5765fcd048 368
embeddedartists 0:7f5765fcd048 369 // wait for response (siq goes high when response is available)
embeddedartists 0:7f5765fcd048 370 Timer t;
embeddedartists 0:7f5765fcd048 371 t.start();
embeddedartists 0:7f5765fcd048 372 while(_siq.read() != 1 && t.read_ms() < AR1021_TIMEOUT);
embeddedartists 0:7f5765fcd048 373
embeddedartists 0:7f5765fcd048 374 if (t.read_ms() < AR1021_TIMEOUT) {
embeddedartists 0:7f5765fcd048 375
embeddedartists 0:7f5765fcd048 376 // command response
embeddedartists 0:7f5765fcd048 377 // ---------------
embeddedartists 0:7f5765fcd048 378 // 0x55 len status cmd data
embeddedartists 0:7f5765fcd048 379 // 0x55 = header
embeddedartists 0:7f5765fcd048 380 // len = number of bytes following the len byte (i.e. including the status&cmd)
embeddedartists 0:7f5765fcd048 381 // status = status
embeddedartists 0:7f5765fcd048 382 // cmd = command ID
embeddedartists 0:7f5765fcd048 383 // data = data to receive
embeddedartists 0:7f5765fcd048 384 _i2c.start();
embeddedartists 0:7f5765fcd048 385 _i2c.write(AR1021_ADDR + 1); //send read address
embeddedartists 0:7f5765fcd048 386 char header = _i2c.read(1); //header should always be 0x55
embeddedartists 0:7f5765fcd048 387 if (header != 0x55) {
embeddedartists 0:7f5765fcd048 388 ret = AR1021_ERR_NO_HDR;
embeddedartists 0:7f5765fcd048 389 continue;
embeddedartists 0:7f5765fcd048 390 }
embeddedartists 0:7f5765fcd048 391 char length = _i2c.read(1); //data length
embeddedartists 0:7f5765fcd048 392 if (length < 2) {
embeddedartists 0:7f5765fcd048 393 ret = AR1021_ERR_INV_LEN; //must have at least status and command bytes
embeddedartists 0:7f5765fcd048 394 continue;
embeddedartists 0:7f5765fcd048 395 }
embeddedartists 0:7f5765fcd048 396 length -= 2;
embeddedartists 0:7f5765fcd048 397 if (length > readLen) {
embeddedartists 0:7f5765fcd048 398 ret = AR1021_ERR_INV_LEN; //supplied buffer is not enough
embeddedartists 0:7f5765fcd048 399 continue;
embeddedartists 0:7f5765fcd048 400 }
embeddedartists 0:7f5765fcd048 401
embeddedartists 0:7f5765fcd048 402 char status = _i2c.read(1); //command status
embeddedartists 0:7f5765fcd048 403 char usedCmd;
embeddedartists 0:7f5765fcd048 404 if (readLen <= 0) {
embeddedartists 0:7f5765fcd048 405 usedCmd = _i2c.read(0); //no data => read command byte + NACK
embeddedartists 0:7f5765fcd048 406 } else {
embeddedartists 0:7f5765fcd048 407 usedCmd = _i2c.read(1); //which command
embeddedartists 0:7f5765fcd048 408
embeddedartists 0:7f5765fcd048 409 //we need to send a NACK on the last read.
embeddedartists 0:7f5765fcd048 410 int i;
embeddedartists 0:7f5765fcd048 411 for (i = 0; i < (length-1); i++) {
embeddedartists 0:7f5765fcd048 412 respBuf[i] = _i2c.read(1);
embeddedartists 0:7f5765fcd048 413 }
embeddedartists 0:7f5765fcd048 414 respBuf[i] = _i2c.read(0); //last returned data byte + NACK
embeddedartists 0:7f5765fcd048 415 }
embeddedartists 0:7f5765fcd048 416 _i2c.stop();
embeddedartists 0:7f5765fcd048 417
embeddedartists 0:7f5765fcd048 418
embeddedartists 0:7f5765fcd048 419 if (status != AR1021_RESP_STAT_OK) {
embeddedartists 0:7f5765fcd048 420 ret = -status;
embeddedartists 0:7f5765fcd048 421 continue;
embeddedartists 0:7f5765fcd048 422 }
embeddedartists 0:7f5765fcd048 423 if (usedCmd != cmd) {
embeddedartists 0:7f5765fcd048 424 ret = AR1021_ERR_INV_RESP;
embeddedartists 0:7f5765fcd048 425 continue;
embeddedartists 0:7f5765fcd048 426 }
embeddedartists 0:7f5765fcd048 427
embeddedartists 0:7f5765fcd048 428 // success
embeddedartists 0:7f5765fcd048 429 ret = 0;
embeddedartists 0:7f5765fcd048 430 break;
embeddedartists 0:7f5765fcd048 431
embeddedartists 0:7f5765fcd048 432 } else {
embeddedartists 0:7f5765fcd048 433 ret = AR1021_ERR_TIMEOUT;
embeddedartists 0:7f5765fcd048 434 continue;
embeddedartists 0:7f5765fcd048 435 }
embeddedartists 0:7f5765fcd048 436 }
embeddedartists 0:7f5765fcd048 437
embeddedartists 0:7f5765fcd048 438 return ret;
embeddedartists 0:7f5765fcd048 439 }
embeddedartists 0:7f5765fcd048 440
embeddedartists 0:7f5765fcd048 441 int AR1021I2C::waitForCalibResponse(uint32_t timeout) {
embeddedartists 0:7f5765fcd048 442 Timer t;
embeddedartists 0:7f5765fcd048 443 int ret = 0;
embeddedartists 0:7f5765fcd048 444
embeddedartists 0:7f5765fcd048 445 t.start();
embeddedartists 0:7f5765fcd048 446
embeddedartists 0:7f5765fcd048 447 // wait for siq
embeddedartists 0:7f5765fcd048 448 while (_siq.read() != 1 &&
embeddedartists 0:7f5765fcd048 449 (timeout == 0 || (uint32_t)t.read_ms() < (int)timeout));
embeddedartists 0:7f5765fcd048 450
embeddedartists 0:7f5765fcd048 451
embeddedartists 0:7f5765fcd048 452 do {
embeddedartists 0:7f5765fcd048 453
embeddedartists 0:7f5765fcd048 454 if (timeout > 0 && (uint32_t)t.read_ms() >= timeout) {
embeddedartists 0:7f5765fcd048 455 ret = AR1021_ERR_TIMEOUT;
embeddedartists 0:7f5765fcd048 456 break;
embeddedartists 0:7f5765fcd048 457 }
embeddedartists 0:7f5765fcd048 458
embeddedartists 0:7f5765fcd048 459 // command response
embeddedartists 0:7f5765fcd048 460 // ---------------
embeddedartists 0:7f5765fcd048 461 // 0x55 len status cmd data
embeddedartists 0:7f5765fcd048 462 // 0x55 = header
embeddedartists 0:7f5765fcd048 463 // len = number of bytes following the len byte (should be 2)
embeddedartists 0:7f5765fcd048 464 // status = status
embeddedartists 0:7f5765fcd048 465 // cmd = command ID
embeddedartists 0:7f5765fcd048 466 _i2c.start();
embeddedartists 0:7f5765fcd048 467 _i2c.write(AR1021_ADDR + 1); //send read address
embeddedartists 0:7f5765fcd048 468 char header = _i2c.read(1); //header should always be 0x55
embeddedartists 0:7f5765fcd048 469 char length = _i2c.read(1); //data length
embeddedartists 0:7f5765fcd048 470
embeddedartists 0:7f5765fcd048 471 if (header != 0x55) {
embeddedartists 0:7f5765fcd048 472 ret = AR1021_ERR_NO_HDR;
embeddedartists 0:7f5765fcd048 473 break;
embeddedartists 0:7f5765fcd048 474 }
embeddedartists 0:7f5765fcd048 475 if (length < 2) {
embeddedartists 0:7f5765fcd048 476 ret = AR1021_ERR_INV_LEN;
embeddedartists 0:7f5765fcd048 477 break;
embeddedartists 0:7f5765fcd048 478 }
embeddedartists 0:7f5765fcd048 479 char status = _i2c.read(1); //status
embeddedartists 0:7f5765fcd048 480 char cmd = _i2c.read(0); //command, should be NACK'ed
embeddedartists 0:7f5765fcd048 481 _i2c.stop();
embeddedartists 0:7f5765fcd048 482 if (status != AR1021_RESP_STAT_OK) {
embeddedartists 0:7f5765fcd048 483 ret = -status;
embeddedartists 0:7f5765fcd048 484 break;
embeddedartists 0:7f5765fcd048 485 }
embeddedartists 0:7f5765fcd048 486 if (cmd != AR1021_CMD_CALIBRATE_MODE) {
embeddedartists 0:7f5765fcd048 487 ret = AR1021_ERR_INV_RESP;
embeddedartists 0:7f5765fcd048 488 break;
embeddedartists 0:7f5765fcd048 489 }
embeddedartists 0:7f5765fcd048 490
embeddedartists 0:7f5765fcd048 491 // success
embeddedartists 0:7f5765fcd048 492 ret = 0;
embeddedartists 0:7f5765fcd048 493
embeddedartists 0:7f5765fcd048 494 } while (0);
embeddedartists 0:7f5765fcd048 495
embeddedartists 0:7f5765fcd048 496 return ret;
embeddedartists 0:7f5765fcd048 497 }
embeddedartists 0:7f5765fcd048 498
embeddedartists 0:7f5765fcd048 499
embeddedartists 0:7f5765fcd048 500 void AR1021I2C::readTouchIrq() {
embeddedartists 0:7f5765fcd048 501 while(_siq.read() == 1) {
embeddedartists 0:7f5765fcd048 502
embeddedartists 0:7f5765fcd048 503 // touch coordinates are sent in a 5-byte data packet
embeddedartists 0:7f5765fcd048 504 _i2c.start();
embeddedartists 0:7f5765fcd048 505 _i2c.write(AR1021_ADDR + 1); //send read address
embeddedartists 0:7f5765fcd048 506 int pen = _i2c.read(1);
embeddedartists 0:7f5765fcd048 507 int xlo = _i2c.read(1);
embeddedartists 0:7f5765fcd048 508 int xhi = _i2c.read(1);
embeddedartists 0:7f5765fcd048 509 int ylo = _i2c.read(1);
embeddedartists 0:7f5765fcd048 510 int yhi = _i2c.read(0);
embeddedartists 0:7f5765fcd048 511 _i2c.stop();
embeddedartists 0:7f5765fcd048 512
embeddedartists 0:7f5765fcd048 513 // pen down
embeddedartists 0:7f5765fcd048 514 if ((pen&AR1021_PEN_MASK) == (1<<7|1<<0)) {
embeddedartists 0:7f5765fcd048 515 _pen = 1;
embeddedartists 0:7f5765fcd048 516 }
embeddedartists 0:7f5765fcd048 517 // pen up
embeddedartists 0:7f5765fcd048 518 else if ((pen&AR1021_PEN_MASK) == (1<<7)){
embeddedartists 0:7f5765fcd048 519 _pen = 0;
embeddedartists 0:7f5765fcd048 520 }
embeddedartists 0:7f5765fcd048 521 // invalid value
embeddedartists 0:7f5765fcd048 522 else {
embeddedartists 0:7f5765fcd048 523 continue;
embeddedartists 0:7f5765fcd048 524 }
embeddedartists 0:7f5765fcd048 525
embeddedartists 0:7f5765fcd048 526 _x = ((xhi<<7)|xlo);
embeddedartists 0:7f5765fcd048 527 _y = ((yhi<<7)|ylo);
embeddedartists 0:7f5765fcd048 528 }
embeddedartists 0:7f5765fcd048 529 }
embeddedartists 0:7f5765fcd048 530
embeddedartists 0:7f5765fcd048 531 bool AR1021I2C::info(int* verHigh, int* verLow, int* resBits, int* type)
embeddedartists 0:7f5765fcd048 532 {
embeddedartists 0:7f5765fcd048 533 char buff[3] = {0,0,0};
embeddedartists 0:7f5765fcd048 534 int read = 3;
embeddedartists 0:7f5765fcd048 535 int res = cmd(AR1021_CMD_GET_VERSION, NULL, 0, buff, &read);
embeddedartists 0:7f5765fcd048 536 if (res == 0) {
embeddedartists 0:7f5765fcd048 537 *verHigh = buff[0];
embeddedartists 0:7f5765fcd048 538 *verLow = buff[1];
embeddedartists 0:7f5765fcd048 539 switch(buff[2] & 3) {
embeddedartists 0:7f5765fcd048 540 case 0:
embeddedartists 0:7f5765fcd048 541 *resBits = 8;
embeddedartists 0:7f5765fcd048 542 break;
embeddedartists 0:7f5765fcd048 543 case 1:
embeddedartists 0:7f5765fcd048 544 *resBits = 10;
embeddedartists 0:7f5765fcd048 545 break;
embeddedartists 0:7f5765fcd048 546 case 2:
embeddedartists 0:7f5765fcd048 547 *resBits = 12;
embeddedartists 0:7f5765fcd048 548 break;
embeddedartists 0:7f5765fcd048 549 case 3:
embeddedartists 0:7f5765fcd048 550 *resBits = 12;
embeddedartists 0:7f5765fcd048 551 break;
embeddedartists 0:7f5765fcd048 552 default:
embeddedartists 0:7f5765fcd048 553 res = 25;
embeddedartists 0:7f5765fcd048 554 printf("Invalid resolution %d\n", buff[2]&3);
embeddedartists 0:7f5765fcd048 555 break;
embeddedartists 0:7f5765fcd048 556 }
embeddedartists 0:7f5765fcd048 557 *type = buff[2]>>2;
embeddedartists 0:7f5765fcd048 558 }
embeddedartists 0:7f5765fcd048 559 return (res == 0);
embeddedartists 0:7f5765fcd048 560 }
embeddedartists 0:7f5765fcd048 561
embeddedartists 0:7f5765fcd048 562