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