Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This demo shows a number of bubbles bouncing around.

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Fri Oct 03 13:09:11 2014 +0000
Revision:
0:4839ec6c350d
First version

Who changed what in which revision?

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