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