Small changes

Dependencies:   FATFileSystem

Dependents:   main_src

Fork of EALib by EmbeddedArtists AB

Committer:
embeddedartists
Date:
Fri Dec 19 15:22:22 2014 +0000
Revision:
20:74540582e639
Parent:
12:15597e45eea0
Fixed bug in AR1021 driver causing problems when aborting calibration

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 12:15597e45eea0 1 /*
embeddedartists 12:15597e45eea0 2 * Copyright 2013 Embedded Artists AB
embeddedartists 12:15597e45eea0 3 *
embeddedartists 12:15597e45eea0 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 12:15597e45eea0 5 * you may not use this file except in compliance with the License.
embeddedartists 12:15597e45eea0 6 * You may obtain a copy of the License at
embeddedartists 12:15597e45eea0 7 *
embeddedartists 12:15597e45eea0 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 12:15597e45eea0 9 *
embeddedartists 12:15597e45eea0 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 12:15597e45eea0 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 12:15597e45eea0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 12:15597e45eea0 13 * See the License for the specific language governing permissions and
embeddedartists 12:15597e45eea0 14 * limitations under the License.
embeddedartists 12:15597e45eea0 15 */
embeddedartists 0:0fdadbc3d852 16
embeddedartists 0:0fdadbc3d852 17 #include "mbed.h"
embeddedartists 4:b32cf4ef45c5 18 #include "mbed_debug.h"
embeddedartists 0:0fdadbc3d852 19 #include "TSC2046.h"
embeddedartists 0:0fdadbc3d852 20
embeddedartists 0:0fdadbc3d852 21 #ifndef ABS
embeddedartists 0:0fdadbc3d852 22 #define ABS(x) ( ((int32_t)(x)) < 0 ? (-(x)) : (x))
embeddedartists 0:0fdadbc3d852 23 #endif
embeddedartists 0:0fdadbc3d852 24
embeddedartists 0:0fdadbc3d852 25 #define ADS_START (1 << 7)
embeddedartists 0:0fdadbc3d852 26 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
embeddedartists 0:0fdadbc3d852 27 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
embeddedartists 0:0fdadbc3d852 28 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
embeddedartists 0:0fdadbc3d852 29 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
embeddedartists 0:0fdadbc3d852 30 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
embeddedartists 0:0fdadbc3d852 31 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
embeddedartists 0:0fdadbc3d852 32 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
embeddedartists 0:0fdadbc3d852 33 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
embeddedartists 0:0fdadbc3d852 34 #define ADS_8_BIT (1 << 3)
embeddedartists 0:0fdadbc3d852 35 #define ADS_12_BIT (0 << 3)
embeddedartists 0:0fdadbc3d852 36 #define ADS_SER (1 << 2) /* non-differential */
embeddedartists 0:0fdadbc3d852 37 #define ADS_DFR (0 << 2) /* differential */
embeddedartists 0:0fdadbc3d852 38 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
embeddedartists 0:0fdadbc3d852 39 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
embeddedartists 0:0fdadbc3d852 40 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
embeddedartists 0:0fdadbc3d852 41 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
embeddedartists 0:0fdadbc3d852 42
embeddedartists 0:0fdadbc3d852 43
embeddedartists 0:0fdadbc3d852 44 #define READ_12BIT_DFR(d, adc, vref) (ADS_START | d \
embeddedartists 0:0fdadbc3d852 45 | ADS_12_BIT | ADS_DFR | \
embeddedartists 0:0fdadbc3d852 46 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
embeddedartists 0:0fdadbc3d852 47
embeddedartists 0:0fdadbc3d852 48 #define READ_Y(vref) (READ_12BIT_DFR(ADS_A2A1A0_d_y, 1, vref))
embeddedartists 0:0fdadbc3d852 49 #define READ_Z1(vref) (READ_12BIT_DFR(ADS_A2A1A0_d_z1, 1, vref))
embeddedartists 0:0fdadbc3d852 50 #define READ_Z2(vref) (READ_12BIT_DFR(ADS_A2A1A0_d_z2, 1, vref))
embeddedartists 0:0fdadbc3d852 51 #define READ_X(vref) (READ_12BIT_DFR(ADS_A2A1A0_d_x, 1, vref))
embeddedartists 0:0fdadbc3d852 52 #define PWRDOWN (READ_12BIT_DFR(ADS_A2A1A0_d_y, 0, 0)) /* LAST */
embeddedartists 0:0fdadbc3d852 53
embeddedartists 0:0fdadbc3d852 54 /* single-ended samples need to first power up reference voltage;
embeddedartists 0:0fdadbc3d852 55 * we leave both ADC and VREF powered
embeddedartists 0:0fdadbc3d852 56 */
embeddedartists 0:0fdadbc3d852 57 #define READ_12BIT_SER(x) (ADS_START | x \
embeddedartists 0:0fdadbc3d852 58 | ADS_12_BIT | ADS_SER)
embeddedartists 0:0fdadbc3d852 59
embeddedartists 0:0fdadbc3d852 60 #define REF_ON (READ_12BIT_DFR(ADS_A2A1A0_d_x, 1, 1))
embeddedartists 0:0fdadbc3d852 61 #define REF_OFF (READ_12BIT_DFR(ADS_A2A1A0_d_y, 0, 0))
embeddedartists 0:0fdadbc3d852 62
embeddedartists 0:0fdadbc3d852 63 #define DEBOUNCE_MAX 10
embeddedartists 0:0fdadbc3d852 64 #define DEBOUNCE_TOL 3
embeddedartists 0:0fdadbc3d852 65
embeddedartists 4:b32cf4ef45c5 66
embeddedartists 0:0fdadbc3d852 67 TSC2046::TSC2046(PinName mosi, PinName miso, PinName sck, PinName cs) :
embeddedartists 0:0fdadbc3d852 68 _spi(mosi, miso, sck), _cs(cs)
embeddedartists 0:0fdadbc3d852 69 {
embeddedartists 0:0fdadbc3d852 70 _cs = 1; // active low
embeddedartists 0:0fdadbc3d852 71
embeddedartists 0:0fdadbc3d852 72 _spi.format(8, 3);
embeddedartists 6:405c6e5a4eaf 73
embeddedartists 6:405c6e5a4eaf 74 // We are limiting the clock rate to 500000 since
embeddedartists 6:405c6e5a4eaf 75 // we have experienced a lot of noise when running with
embeddedartists 6:405c6e5a4eaf 76 // higher rate. It has not been examined why there is a
embeddedartists 6:405c6e5a4eaf 77 // lot of noise with higher rate.
embeddedartists 6:405c6e5a4eaf 78 _spi.frequency(500000);
embeddedartists 0:0fdadbc3d852 79 _calibrated = false;
embeddedartists 0:0fdadbc3d852 80 _initialized = false;
embeddedartists 4:b32cf4ef45c5 81
embeddedartists 4:b32cf4ef45c5 82 _calibPoint = TSC2046_NUM_CALIB_POINTS+1;
embeddedartists 4:b32cf4ef45c5 83
embeddedartists 4:b32cf4ef45c5 84 _insetPx = 15;
embeddedartists 4:b32cf4ef45c5 85 }
embeddedartists 4:b32cf4ef45c5 86
embeddedartists 4:b32cf4ef45c5 87 bool TSC2046::init(uint16_t width, uint16_t height) {
embeddedartists 4:b32cf4ef45c5 88 _width = width;
embeddedartists 4:b32cf4ef45c5 89 _height = height;
embeddedartists 4:b32cf4ef45c5 90
embeddedartists 4:b32cf4ef45c5 91 _cs = 0;
embeddedartists 4:b32cf4ef45c5 92
embeddedartists 4:b32cf4ef45c5 93 _spi.write(REF_ON);
embeddedartists 4:b32cf4ef45c5 94 _spi.write((READ_12BIT_SER(ADS_A2A1A0_vaux) | ADS_PD10_ALL_ON));
embeddedartists 4:b32cf4ef45c5 95 _spi.write(PWRDOWN);
embeddedartists 4:b32cf4ef45c5 96
embeddedartists 4:b32cf4ef45c5 97 _cs = 1;
embeddedartists 4:b32cf4ef45c5 98
embeddedartists 4:b32cf4ef45c5 99
embeddedartists 4:b32cf4ef45c5 100 _initialized = true;
embeddedartists 4:b32cf4ef45c5 101
embeddedartists 4:b32cf4ef45c5 102 return true;
embeddedartists 0:0fdadbc3d852 103 }
embeddedartists 0:0fdadbc3d852 104
embeddedartists 0:0fdadbc3d852 105
embeddedartists 4:b32cf4ef45c5 106 bool TSC2046::read(touchCoordinate_t &coord) {
embeddedartists 0:0fdadbc3d852 107
embeddedartists 0:0fdadbc3d852 108 touchCoordinate_t tmpCoord;
embeddedartists 0:0fdadbc3d852 109 calibPoint_t displayPoint;
embeddedartists 0:0fdadbc3d852 110 calibPoint_t screenSample;
embeddedartists 0:0fdadbc3d852 111
embeddedartists 4:b32cf4ef45c5 112 if (!_initialized) return false;
embeddedartists 4:b32cf4ef45c5 113
embeddedartists 0:0fdadbc3d852 114
embeddedartists 0:0fdadbc3d852 115 readAndFilter(tmpCoord);
embeddedartists 0:0fdadbc3d852 116
embeddedartists 0:0fdadbc3d852 117 _cs = 0;
embeddedartists 0:0fdadbc3d852 118 _spi.write(PWRDOWN);
embeddedartists 0:0fdadbc3d852 119 _cs = 1;
embeddedartists 0:0fdadbc3d852 120
embeddedartists 0:0fdadbc3d852 121 coord.z = tmpCoord.z;
embeddedartists 0:0fdadbc3d852 122
embeddedartists 0:0fdadbc3d852 123 if (_calibrated) {
embeddedartists 0:0fdadbc3d852 124 screenSample.x = tmpCoord.x;
embeddedartists 0:0fdadbc3d852 125 screenSample.y = tmpCoord.y;
embeddedartists 0:0fdadbc3d852 126
embeddedartists 0:0fdadbc3d852 127 getDisplayPoint(&displayPoint, &screenSample, &_calibMatrix);
embeddedartists 0:0fdadbc3d852 128
embeddedartists 0:0fdadbc3d852 129 coord.x = displayPoint.x;
embeddedartists 0:0fdadbc3d852 130 coord.y = displayPoint.y;
embeddedartists 0:0fdadbc3d852 131 }
embeddedartists 0:0fdadbc3d852 132 else {
embeddedartists 0:0fdadbc3d852 133 coord.x = tmpCoord.x;
embeddedartists 0:0fdadbc3d852 134 coord.y = tmpCoord.y;
embeddedartists 0:0fdadbc3d852 135 }
embeddedartists 0:0fdadbc3d852 136
embeddedartists 4:b32cf4ef45c5 137 return true;
embeddedartists 0:0fdadbc3d852 138
embeddedartists 0:0fdadbc3d852 139 }
embeddedartists 0:0fdadbc3d852 140
embeddedartists 4:b32cf4ef45c5 141 bool TSC2046::calibrateStart() {
embeddedartists 4:b32cf4ef45c5 142 if (!_initialized) return false;
embeddedartists 4:b32cf4ef45c5 143
embeddedartists 4:b32cf4ef45c5 144 _calibPoint = 0;
embeddedartists 4:b32cf4ef45c5 145
embeddedartists 4:b32cf4ef45c5 146 return true;
embeddedartists 4:b32cf4ef45c5 147 }
embeddedartists 4:b32cf4ef45c5 148
embeddedartists 4:b32cf4ef45c5 149 bool TSC2046::getNextCalibratePoint(uint16_t* x, uint16_t* y) {
embeddedartists 4:b32cf4ef45c5 150 touchCoordinate_t coord;
embeddedartists 4:b32cf4ef45c5 151
embeddedartists 4:b32cf4ef45c5 152 if (!_initialized) return false;
embeddedartists 4:b32cf4ef45c5 153
embeddedartists 4:b32cf4ef45c5 154 if (x == NULL || y == NULL) return false;
embeddedartists 4:b32cf4ef45c5 155
embeddedartists 4:b32cf4ef45c5 156 if (_calibPoint >= TSC2046_NUM_CALIB_POINTS) return false;
embeddedartists 4:b32cf4ef45c5 157
embeddedartists 4:b32cf4ef45c5 158 getCalibratePoint(_calibPoint, &coord.x, &coord.y);
embeddedartists 4:b32cf4ef45c5 159
embeddedartists 4:b32cf4ef45c5 160 *x = (uint16_t)coord.x;
embeddedartists 4:b32cf4ef45c5 161 *y = (uint16_t)coord.y;
embeddedartists 4:b32cf4ef45c5 162 _calibrateValues[_calibPoint][0] = coord;
embeddedartists 4:b32cf4ef45c5 163
embeddedartists 4:b32cf4ef45c5 164 return true;
embeddedartists 4:b32cf4ef45c5 165 }
embeddedartists 4:b32cf4ef45c5 166
embeddedartists 4:b32cf4ef45c5 167 bool TSC2046::waitForCalibratePoint(bool* morePoints, uint32_t timeout) {
embeddedartists 4:b32cf4ef45c5 168 int result = 0;
embeddedartists 4:b32cf4ef45c5 169 bool ret = false;
embeddedartists 4:b32cf4ef45c5 170 int32_t x = 0;
embeddedartists 4:b32cf4ef45c5 171 int32_t y = 0;
embeddedartists 4:b32cf4ef45c5 172 touchCoordinate_t coord;
embeddedartists 4:b32cf4ef45c5 173
embeddedartists 4:b32cf4ef45c5 174 if (!_initialized) return false;
embeddedartists 4:b32cf4ef45c5 175
embeddedartists 4:b32cf4ef45c5 176 do {
embeddedartists 4:b32cf4ef45c5 177 if (morePoints == NULL || _calibPoint >= TSC2046_NUM_CALIB_POINTS) {
embeddedartists 4:b32cf4ef45c5 178 break;
embeddedartists 4:b32cf4ef45c5 179 }
embeddedartists 4:b32cf4ef45c5 180
embeddedartists 4:b32cf4ef45c5 181 result = waitForTouch(&x, &y, timeout);
embeddedartists 4:b32cf4ef45c5 182 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 183 debug("wait for touch response failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 184 break;
embeddedartists 4:b32cf4ef45c5 185 }
embeddedartists 4:b32cf4ef45c5 186
embeddedartists 4:b32cf4ef45c5 187 coord.x = x;
embeddedartists 4:b32cf4ef45c5 188 coord.y = y;
embeddedartists 4:b32cf4ef45c5 189 _calibrateValues[_calibPoint][1] = coord;
embeddedartists 4:b32cf4ef45c5 190
embeddedartists 4:b32cf4ef45c5 191 _calibPoint++;
embeddedartists 4:b32cf4ef45c5 192 *morePoints = (_calibPoint < TSC2046_NUM_CALIB_POINTS);
embeddedartists 4:b32cf4ef45c5 193
embeddedartists 4:b32cf4ef45c5 194 if (!(*morePoints)) {
embeddedartists 4:b32cf4ef45c5 195
embeddedartists 4:b32cf4ef45c5 196 calibrate(
embeddedartists 4:b32cf4ef45c5 197 _calibrateValues[0][0],
embeddedartists 4:b32cf4ef45c5 198 _calibrateValues[1][0],
embeddedartists 4:b32cf4ef45c5 199 _calibrateValues[2][0],
embeddedartists 4:b32cf4ef45c5 200 _calibrateValues[0][1],
embeddedartists 4:b32cf4ef45c5 201 _calibrateValues[1][1],
embeddedartists 4:b32cf4ef45c5 202 _calibrateValues[2][1]);
embeddedartists 4:b32cf4ef45c5 203 }
embeddedartists 4:b32cf4ef45c5 204
embeddedartists 4:b32cf4ef45c5 205
embeddedartists 4:b32cf4ef45c5 206 ret = true;
embeddedartists 4:b32cf4ef45c5 207
embeddedartists 4:b32cf4ef45c5 208 } while (0);
embeddedartists 4:b32cf4ef45c5 209
embeddedartists 4:b32cf4ef45c5 210
embeddedartists 4:b32cf4ef45c5 211
embeddedartists 4:b32cf4ef45c5 212 if (!ret) {
embeddedartists 4:b32cf4ef45c5 213 // calibration must restart if an error occurred
embeddedartists 4:b32cf4ef45c5 214 _calibPoint = TSC2046_NUM_CALIB_POINTS+1;
embeddedartists 4:b32cf4ef45c5 215 }
embeddedartists 4:b32cf4ef45c5 216
embeddedartists 4:b32cf4ef45c5 217
embeddedartists 4:b32cf4ef45c5 218
embeddedartists 4:b32cf4ef45c5 219 return ret;
embeddedartists 4:b32cf4ef45c5 220
embeddedartists 0:0fdadbc3d852 221 }
embeddedartists 0:0fdadbc3d852 222
embeddedartists 0:0fdadbc3d852 223
embeddedartists 4:b32cf4ef45c5 224 bool TSC2046::calibrate(touchCoordinate_t* values, int numValues) {
embeddedartists 4:b32cf4ef45c5 225 if (values == NULL || numValues < TSC2046_NUM_CALIB_POINTS) return false;
embeddedartists 4:b32cf4ef45c5 226
embeddedartists 4:b32cf4ef45c5 227 touchCoordinate_t ref[TSC2046_NUM_CALIB_POINTS];
embeddedartists 4:b32cf4ef45c5 228 touchCoordinate_t scr[TSC2046_NUM_CALIB_POINTS];
embeddedartists 0:0fdadbc3d852 229
embeddedartists 4:b32cf4ef45c5 230 for (int i = 0; i < TSC2046_NUM_CALIB_POINTS; i++) {
embeddedartists 4:b32cf4ef45c5 231 getCalibratePoint(i, &(ref[i].x), &(ref[i].y));
embeddedartists 4:b32cf4ef45c5 232 scr[i] = values[i];
embeddedartists 4:b32cf4ef45c5 233 }
embeddedartists 4:b32cf4ef45c5 234
embeddedartists 4:b32cf4ef45c5 235 calibrate(ref[0], ref[1], ref[2], scr[0], scr[1], scr[2]);
embeddedartists 0:0fdadbc3d852 236
embeddedartists 4:b32cf4ef45c5 237 return true;
embeddedartists 4:b32cf4ef45c5 238 }
embeddedartists 4:b32cf4ef45c5 239
embeddedartists 4:b32cf4ef45c5 240
embeddedartists 4:b32cf4ef45c5 241 bool TSC2046::getCalibrationValues(touchCoordinate_t* values, int numValues) {
embeddedartists 4:b32cf4ef45c5 242 if (values == NULL || numValues < TSC2046_NUM_CALIB_POINTS) return false;
embeddedartists 4:b32cf4ef45c5 243 if (!_calibrated) return false;
embeddedartists 0:0fdadbc3d852 244
embeddedartists 4:b32cf4ef45c5 245 for (int i = 0; i < TSC2046_NUM_CALIB_POINTS; i++) {
embeddedartists 4:b32cf4ef45c5 246 values[i] = _calibrateValues[i][1];
embeddedartists 4:b32cf4ef45c5 247 }
embeddedartists 4:b32cf4ef45c5 248
embeddedartists 4:b32cf4ef45c5 249 return true;
embeddedartists 0:0fdadbc3d852 250 }
embeddedartists 0:0fdadbc3d852 251
embeddedartists 4:b32cf4ef45c5 252
embeddedartists 0:0fdadbc3d852 253 void TSC2046::readAndFilter(touchCoordinate_t &coord)
embeddedartists 0:0fdadbc3d852 254 {
embeddedartists 0:0fdadbc3d852 255 int32_t ix, iy, iz1, iz2 = 0;
embeddedartists 0:0fdadbc3d852 256 int32_t lastx, lasty, lastz1, lastz2 = 0;
embeddedartists 0:0fdadbc3d852 257 int i = 0;
embeddedartists 0:0fdadbc3d852 258
embeddedartists 0:0fdadbc3d852 259 coord.x = 0;
embeddedartists 0:0fdadbc3d852 260 coord.y = 0;
embeddedartists 0:0fdadbc3d852 261 coord.z = 0;
embeddedartists 0:0fdadbc3d852 262
embeddedartists 0:0fdadbc3d852 263 lasty = getFilteredValue(READ_Y(0));
embeddedartists 0:0fdadbc3d852 264 lasty >>= 3;
embeddedartists 0:0fdadbc3d852 265 if (lasty >= 4095) {
embeddedartists 0:0fdadbc3d852 266 lasty = 0;
embeddedartists 0:0fdadbc3d852 267 }
embeddedartists 0:0fdadbc3d852 268
embeddedartists 0:0fdadbc3d852 269 lastx = getFilteredValue(READ_X(0));
embeddedartists 0:0fdadbc3d852 270 lastx >>= 3;
embeddedartists 0:0fdadbc3d852 271 if (lastx >= 4095) {
embeddedartists 0:0fdadbc3d852 272 lastx = 0;
embeddedartists 0:0fdadbc3d852 273 }
embeddedartists 0:0fdadbc3d852 274
embeddedartists 0:0fdadbc3d852 275 lastz1 = getFilteredValue(READ_Z1(0));
embeddedartists 0:0fdadbc3d852 276 lastz1 >>= 3;
embeddedartists 0:0fdadbc3d852 277
embeddedartists 0:0fdadbc3d852 278 lastz2 = getFilteredValue(READ_Z2(0));
embeddedartists 0:0fdadbc3d852 279 lastz2 >>= 3;
embeddedartists 0:0fdadbc3d852 280
embeddedartists 0:0fdadbc3d852 281
embeddedartists 0:0fdadbc3d852 282 if (lastx && lastz1) {
embeddedartists 0:0fdadbc3d852 283 coord.z = (lastx * ABS(lastz2 - lastz1)) / lastz1;
embeddedartists 0:0fdadbc3d852 284 }
embeddedartists 0:0fdadbc3d852 285 else {
embeddedartists 0:0fdadbc3d852 286 coord.z = 0;
embeddedartists 0:0fdadbc3d852 287 }
embeddedartists 0:0fdadbc3d852 288
embeddedartists 0:0fdadbc3d852 289 if (coord.z > 20000) {
embeddedartists 0:0fdadbc3d852 290 coord.z = 0;
embeddedartists 0:0fdadbc3d852 291 }
embeddedartists 0:0fdadbc3d852 292
embeddedartists 0:0fdadbc3d852 293 if (coord.z == 0) {
embeddedartists 0:0fdadbc3d852 294 return;
embeddedartists 0:0fdadbc3d852 295 }
embeddedartists 0:0fdadbc3d852 296
embeddedartists 0:0fdadbc3d852 297 for (i = 0; i < DEBOUNCE_MAX; i++) {
embeddedartists 0:0fdadbc3d852 298 iy = getFilteredValue(READ_Y(0));
embeddedartists 0:0fdadbc3d852 299 iy >>= 3;
embeddedartists 0:0fdadbc3d852 300
embeddedartists 0:0fdadbc3d852 301 if (ABS (lasty - iy) <= DEBOUNCE_TOL) {
embeddedartists 0:0fdadbc3d852 302 break;
embeddedartists 0:0fdadbc3d852 303 }
embeddedartists 0:0fdadbc3d852 304
embeddedartists 0:0fdadbc3d852 305 lasty = iy;
embeddedartists 0:0fdadbc3d852 306 }
embeddedartists 0:0fdadbc3d852 307
embeddedartists 0:0fdadbc3d852 308 for (i = 0; i < DEBOUNCE_MAX; i++) {
embeddedartists 0:0fdadbc3d852 309 ix = getFilteredValue(READ_X(0));
embeddedartists 0:0fdadbc3d852 310 ix >>= 3;
embeddedartists 0:0fdadbc3d852 311 if (ix > 4095) {
embeddedartists 0:0fdadbc3d852 312 ix = 0;
embeddedartists 0:0fdadbc3d852 313 }
embeddedartists 0:0fdadbc3d852 314
embeddedartists 0:0fdadbc3d852 315 if (ABS (lastx - ix) <= DEBOUNCE_TOL) {
embeddedartists 0:0fdadbc3d852 316 break;
embeddedartists 0:0fdadbc3d852 317 }
embeddedartists 0:0fdadbc3d852 318
embeddedartists 0:0fdadbc3d852 319 lastx = ix;
embeddedartists 0:0fdadbc3d852 320 }
embeddedartists 0:0fdadbc3d852 321
embeddedartists 0:0fdadbc3d852 322 for (i = 0; i < DEBOUNCE_MAX; i++) {
embeddedartists 0:0fdadbc3d852 323 iz1 = getFilteredValue(READ_Z1(0));
embeddedartists 0:0fdadbc3d852 324 iz1 >>= 3;
embeddedartists 0:0fdadbc3d852 325
embeddedartists 0:0fdadbc3d852 326 if (ABS (lastz1 - iz1) <= DEBOUNCE_TOL) {
embeddedartists 0:0fdadbc3d852 327 break;
embeddedartists 0:0fdadbc3d852 328 }
embeddedartists 0:0fdadbc3d852 329
embeddedartists 0:0fdadbc3d852 330 lastz1 = iz1;
embeddedartists 0:0fdadbc3d852 331 }
embeddedartists 0:0fdadbc3d852 332
embeddedartists 0:0fdadbc3d852 333 for (i = 0; i < DEBOUNCE_MAX; i++) {
embeddedartists 0:0fdadbc3d852 334 iz2 = getFilteredValue(READ_Z2(0));
embeddedartists 0:0fdadbc3d852 335 iz2 >>= 3;
embeddedartists 0:0fdadbc3d852 336
embeddedartists 0:0fdadbc3d852 337 if (ABS (lastz2 - iz2) <= DEBOUNCE_TOL) {
embeddedartists 0:0fdadbc3d852 338 break;
embeddedartists 0:0fdadbc3d852 339 }
embeddedartists 0:0fdadbc3d852 340
embeddedartists 0:0fdadbc3d852 341 lastz2 = iz2;
embeddedartists 0:0fdadbc3d852 342 }
embeddedartists 0:0fdadbc3d852 343
embeddedartists 0:0fdadbc3d852 344 coord.x = ix;
embeddedartists 0:0fdadbc3d852 345 coord.y = iy;
embeddedartists 0:0fdadbc3d852 346
embeddedartists 0:0fdadbc3d852 347 if (ix && iz1) {
embeddedartists 0:0fdadbc3d852 348 coord.z = (ix * ABS(iz2 - iz1)) / iz1;
embeddedartists 0:0fdadbc3d852 349 }
embeddedartists 0:0fdadbc3d852 350 else {
embeddedartists 0:0fdadbc3d852 351 coord.z = 0;
embeddedartists 0:0fdadbc3d852 352 }
embeddedartists 0:0fdadbc3d852 353
embeddedartists 0:0fdadbc3d852 354 if (coord.z > 20000) {
embeddedartists 0:0fdadbc3d852 355 coord.z = 0;
embeddedartists 0:0fdadbc3d852 356 }
embeddedartists 0:0fdadbc3d852 357
embeddedartists 0:0fdadbc3d852 358 }
embeddedartists 0:0fdadbc3d852 359
embeddedartists 0:0fdadbc3d852 360 int32_t TSC2046::getFilteredValue(int cmd)
embeddedartists 0:0fdadbc3d852 361 {
embeddedartists 0:0fdadbc3d852 362 int32_t a[7];
embeddedartists 0:0fdadbc3d852 363 int32_t tmp = 0;
embeddedartists 0:0fdadbc3d852 364 int i = 0, j = 0;
embeddedartists 0:0fdadbc3d852 365
embeddedartists 0:0fdadbc3d852 366 /*
embeddedartists 0:0fdadbc3d852 367 * Median and averaging filter
embeddedartists 0:0fdadbc3d852 368 *
embeddedartists 0:0fdadbc3d852 369 * 1. Get 7 values
embeddedartists 0:0fdadbc3d852 370 * 2. Sort these values
embeddedartists 0:0fdadbc3d852 371 * 3. Take average of the 3 values in the middle
embeddedartists 0:0fdadbc3d852 372 */
embeddedartists 0:0fdadbc3d852 373
embeddedartists 0:0fdadbc3d852 374 for (i = 0; i < 7; i++) {
embeddedartists 0:0fdadbc3d852 375 a[i] = spiTransfer(cmd);
embeddedartists 0:0fdadbc3d852 376 }
embeddedartists 0:0fdadbc3d852 377
embeddedartists 0:0fdadbc3d852 378 // bubble sort
embeddedartists 0:0fdadbc3d852 379 for (i = 0; i < 7; i++) {
embeddedartists 0:0fdadbc3d852 380 for (j = 0; j < (7-(i+1)); j++) {
embeddedartists 0:0fdadbc3d852 381 if (a[j] > a[j+1]) {
embeddedartists 0:0fdadbc3d852 382 // swap
embeddedartists 0:0fdadbc3d852 383 tmp = a[j];
embeddedartists 0:0fdadbc3d852 384 a[j] = a[j+1];
embeddedartists 0:0fdadbc3d852 385 a[j+1] = tmp;
embeddedartists 0:0fdadbc3d852 386 }
embeddedartists 0:0fdadbc3d852 387 }
embeddedartists 0:0fdadbc3d852 388 }
embeddedartists 0:0fdadbc3d852 389
embeddedartists 0:0fdadbc3d852 390 // average of 3 values in the middle
embeddedartists 0:0fdadbc3d852 391 return ((a[2]+a[3]+a[4])/3);
embeddedartists 0:0fdadbc3d852 392 }
embeddedartists 0:0fdadbc3d852 393
embeddedartists 0:0fdadbc3d852 394 uint16_t TSC2046::spiTransfer(uint8_t cmd)
embeddedartists 0:0fdadbc3d852 395 {
embeddedartists 0:0fdadbc3d852 396 uint8_t data[3];
embeddedartists 0:0fdadbc3d852 397
embeddedartists 0:0fdadbc3d852 398 _cs = 0;
embeddedartists 0:0fdadbc3d852 399
embeddedartists 0:0fdadbc3d852 400 /*data[0] = */_spi.write(cmd);
embeddedartists 0:0fdadbc3d852 401 data[0] = _spi.write(0xff);
embeddedartists 0:0fdadbc3d852 402 data[1] = _spi.write(0xff);
embeddedartists 0:0fdadbc3d852 403
embeddedartists 0:0fdadbc3d852 404 _cs = 1;
embeddedartists 0:0fdadbc3d852 405
embeddedartists 0:0fdadbc3d852 406 return ((data[0] << 8) | data[1]);
embeddedartists 0:0fdadbc3d852 407 }
embeddedartists 0:0fdadbc3d852 408
embeddedartists 4:b32cf4ef45c5 409 void TSC2046::calibrate(touchCoordinate_t &ref1,
embeddedartists 4:b32cf4ef45c5 410 touchCoordinate_t &ref2,
embeddedartists 4:b32cf4ef45c5 411 touchCoordinate_t &ref3,
embeddedartists 4:b32cf4ef45c5 412 touchCoordinate_t &scr1,
embeddedartists 4:b32cf4ef45c5 413 touchCoordinate_t &scr2,
embeddedartists 4:b32cf4ef45c5 414 touchCoordinate_t &scr3) {
embeddedartists 4:b32cf4ef45c5 415
embeddedartists 4:b32cf4ef45c5 416 calibPoint_t disp[3];
embeddedartists 4:b32cf4ef45c5 417 calibPoint_t scr[3];
embeddedartists 4:b32cf4ef45c5 418
embeddedartists 4:b32cf4ef45c5 419 disp[0].x = ref1.x;
embeddedartists 4:b32cf4ef45c5 420 disp[0].y = ref1.y;
embeddedartists 4:b32cf4ef45c5 421 disp[1].x = ref2.x;
embeddedartists 4:b32cf4ef45c5 422 disp[1].y = ref2.y;
embeddedartists 4:b32cf4ef45c5 423 disp[2].x = ref3.x;
embeddedartists 4:b32cf4ef45c5 424 disp[2].y = ref3.y;
embeddedartists 4:b32cf4ef45c5 425
embeddedartists 4:b32cf4ef45c5 426 scr[0].x = scr1.x;
embeddedartists 4:b32cf4ef45c5 427 scr[0].y = scr1.y;
embeddedartists 4:b32cf4ef45c5 428 scr[1].x = scr2.x;
embeddedartists 4:b32cf4ef45c5 429 scr[1].y = scr2.y;
embeddedartists 4:b32cf4ef45c5 430 scr[2].x = scr3.x;
embeddedartists 4:b32cf4ef45c5 431 scr[2].y = scr3.y;
embeddedartists 4:b32cf4ef45c5 432
embeddedartists 4:b32cf4ef45c5 433 setCalibrationMatrix(disp, scr, &_calibMatrix);
embeddedartists 4:b32cf4ef45c5 434
embeddedartists 4:b32cf4ef45c5 435 _calibrated = true;
embeddedartists 4:b32cf4ef45c5 436
embeddedartists 4:b32cf4ef45c5 437 }
embeddedartists 4:b32cf4ef45c5 438
embeddedartists 4:b32cf4ef45c5 439 void TSC2046::getCalibratePoint(int pointNum, int32_t* x, int32_t *y) {
embeddedartists 4:b32cf4ef45c5 440 switch(pointNum) {
embeddedartists 4:b32cf4ef45c5 441 case 0:
embeddedartists 4:b32cf4ef45c5 442 *x = _insetPx;
embeddedartists 4:b32cf4ef45c5 443 *y = _height - _insetPx;
embeddedartists 4:b32cf4ef45c5 444 break;
embeddedartists 4:b32cf4ef45c5 445 case 1:
embeddedartists 4:b32cf4ef45c5 446 *x = _width/2;
embeddedartists 4:b32cf4ef45c5 447 *y = _insetPx;
embeddedartists 4:b32cf4ef45c5 448 break;
embeddedartists 4:b32cf4ef45c5 449 case 2:
embeddedartists 4:b32cf4ef45c5 450 *x = _width - _insetPx;
embeddedartists 4:b32cf4ef45c5 451 *y = _height - _insetPx;
embeddedartists 4:b32cf4ef45c5 452 break;
embeddedartists 4:b32cf4ef45c5 453 }
embeddedartists 4:b32cf4ef45c5 454 }
embeddedartists 4:b32cf4ef45c5 455
embeddedartists 4:b32cf4ef45c5 456 int TSC2046::waitForTouch(int32_t* x, int32_t* y, uint32_t timeout) {
embeddedartists 4:b32cf4ef45c5 457 Timer t;
embeddedartists 4:b32cf4ef45c5 458 touchCoordinate_t coord;
embeddedartists 4:b32cf4ef45c5 459 bool waitForRelease = false;
embeddedartists 4:b32cf4ef45c5 460 int32_t tx = 0;
embeddedartists 4:b32cf4ef45c5 461 int32_t ty = 0;
embeddedartists 4:b32cf4ef45c5 462
embeddedartists 4:b32cf4ef45c5 463
embeddedartists 4:b32cf4ef45c5 464 t.start();
embeddedartists 4:b32cf4ef45c5 465 while (timeout == 0 || ((uint32_t)t.read_ms() < timeout)) {
embeddedartists 4:b32cf4ef45c5 466
embeddedartists 4:b32cf4ef45c5 467 read(coord);
embeddedartists 4:b32cf4ef45c5 468
embeddedartists 4:b32cf4ef45c5 469 if (coord.z == 0 && waitForRelease) {
embeddedartists 4:b32cf4ef45c5 470 *x = tx;
embeddedartists 4:b32cf4ef45c5 471 *y = ty;
embeddedartists 4:b32cf4ef45c5 472 break;
embeddedartists 4:b32cf4ef45c5 473 }
embeddedartists 4:b32cf4ef45c5 474
embeddedartists 4:b32cf4ef45c5 475 if (coord.z > 0) {
embeddedartists 4:b32cf4ef45c5 476 tx = coord.x;
embeddedartists 4:b32cf4ef45c5 477 ty = coord.y;
embeddedartists 4:b32cf4ef45c5 478 waitForRelease = true;
embeddedartists 4:b32cf4ef45c5 479 }
embeddedartists 4:b32cf4ef45c5 480
embeddedartists 4:b32cf4ef45c5 481 wait_ms(10);
embeddedartists 4:b32cf4ef45c5 482 }
embeddedartists 4:b32cf4ef45c5 483
embeddedartists 4:b32cf4ef45c5 484 if (timeout > 0 && (uint32_t)t.read_ms() > timeout) {
embeddedartists 4:b32cf4ef45c5 485 return -1;
embeddedartists 4:b32cf4ef45c5 486 }
embeddedartists 4:b32cf4ef45c5 487
embeddedartists 4:b32cf4ef45c5 488 return 0;
embeddedartists 4:b32cf4ef45c5 489
embeddedartists 4:b32cf4ef45c5 490 }
embeddedartists 4:b32cf4ef45c5 491
embeddedartists 4:b32cf4ef45c5 492
embeddedartists 0:0fdadbc3d852 493
embeddedartists 0:0fdadbc3d852 494 // ############################################################################
embeddedartists 0:0fdadbc3d852 495 // >>>>>>>> Calibrate code >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
embeddedartists 0:0fdadbc3d852 496 // ############################################################################
embeddedartists 0:0fdadbc3d852 497
embeddedartists 0:0fdadbc3d852 498
embeddedartists 0:0fdadbc3d852 499 /*
embeddedartists 0:0fdadbc3d852 500 *
embeddedartists 0:0fdadbc3d852 501 * Copyright (c) 2001, Carlos E. Vidales. All rights reserved.
embeddedartists 0:0fdadbc3d852 502 *
embeddedartists 0:0fdadbc3d852 503 * This sample program was written and put in the public domain
embeddedartists 0:0fdadbc3d852 504 * by Carlos E. Vidales. The program is provided "as is"
embeddedartists 0:0fdadbc3d852 505 * without warranty of any kind, either expressed or implied.
embeddedartists 0:0fdadbc3d852 506 * If you choose to use the program within your own products
embeddedartists 0:0fdadbc3d852 507 * you do so at your own risk, and assume the responsibility
embeddedartists 0:0fdadbc3d852 508 * for servicing, repairing or correcting the program should
embeddedartists 0:0fdadbc3d852 509 * it prove defective in any manner.
embeddedartists 0:0fdadbc3d852 510 * You may copy and distribute the program's source code in any
embeddedartists 0:0fdadbc3d852 511 * medium, provided that you also include in each copy an
embeddedartists 0:0fdadbc3d852 512 * appropriate copyright notice and disclaimer of warranty.
embeddedartists 0:0fdadbc3d852 513 * You may also modify this program and distribute copies of
embeddedartists 0:0fdadbc3d852 514 * it provided that you include prominent notices stating
embeddedartists 0:0fdadbc3d852 515 * that you changed the file(s) and the date of any change,
embeddedartists 0:0fdadbc3d852 516 * and that you do not charge any royalties or licenses for
embeddedartists 0:0fdadbc3d852 517 * its use.
embeddedartists 0:0fdadbc3d852 518 *
embeddedartists 0:0fdadbc3d852 519 *
embeddedartists 0:0fdadbc3d852 520 *
embeddedartists 0:0fdadbc3d852 521 * File Name: calibrate.c
embeddedartists 0:0fdadbc3d852 522 *
embeddedartists 0:0fdadbc3d852 523 *
embeddedartists 0:0fdadbc3d852 524 * This file contains functions that implement calculations
embeddedartists 0:0fdadbc3d852 525 * necessary to obtain calibration factors for a touch screen
embeddedartists 0:0fdadbc3d852 526 * that suffers from multiple distortion effects: namely,
embeddedartists 0:0fdadbc3d852 527 * translation, scaling and rotation.
embeddedartists 0:0fdadbc3d852 528 *
embeddedartists 0:0fdadbc3d852 529 * The following set of equations represent a valid display
embeddedartists 0:0fdadbc3d852 530 * point given a corresponding set of touch screen points:
embeddedartists 0:0fdadbc3d852 531 *
embeddedartists 0:0fdadbc3d852 532 *
embeddedartists 0:0fdadbc3d852 533 * /- -\
embeddedartists 0:0fdadbc3d852 534 * /- -\ /- -\ | |
embeddedartists 0:0fdadbc3d852 535 * | | | | | Xs |
embeddedartists 0:0fdadbc3d852 536 * | Xd | | A B C | | |
embeddedartists 0:0fdadbc3d852 537 * | | = | | * | Ys |
embeddedartists 0:0fdadbc3d852 538 * | Yd | | D E F | | |
embeddedartists 0:0fdadbc3d852 539 * | | | | | 1 |
embeddedartists 0:0fdadbc3d852 540 * \- -/ \- -/ | |
embeddedartists 0:0fdadbc3d852 541 * \- -/
embeddedartists 0:0fdadbc3d852 542 *
embeddedartists 0:0fdadbc3d852 543 *
embeddedartists 0:0fdadbc3d852 544 * where:
embeddedartists 0:0fdadbc3d852 545 *
embeddedartists 0:0fdadbc3d852 546 * (Xd,Yd) represents the desired display point
embeddedartists 0:0fdadbc3d852 547 * coordinates,
embeddedartists 0:0fdadbc3d852 548 *
embeddedartists 0:0fdadbc3d852 549 * (Xs,Ys) represents the available touch screen
embeddedartists 0:0fdadbc3d852 550 * coordinates, and the matrix
embeddedartists 0:0fdadbc3d852 551 *
embeddedartists 0:0fdadbc3d852 552 * /- -\
embeddedartists 0:0fdadbc3d852 553 * |A,B,C|
embeddedartists 0:0fdadbc3d852 554 * |D,E,F| represents the factors used to translate
embeddedartists 0:0fdadbc3d852 555 * \- -/ the available touch screen point values
embeddedartists 0:0fdadbc3d852 556 * into the corresponding display
embeddedartists 0:0fdadbc3d852 557 * coordinates.
embeddedartists 0:0fdadbc3d852 558 *
embeddedartists 0:0fdadbc3d852 559 *
embeddedartists 0:0fdadbc3d852 560 * Note that for practical considerations, the utilitities
embeddedartists 0:0fdadbc3d852 561 * within this file do not use the matrix coefficients as
embeddedartists 0:0fdadbc3d852 562 * defined above, but instead use the following
embeddedartists 0:0fdadbc3d852 563 * equivalents, since floating point math is not used:
embeddedartists 0:0fdadbc3d852 564 *
embeddedartists 0:0fdadbc3d852 565 * A = An/Divider
embeddedartists 0:0fdadbc3d852 566 * B = Bn/Divider
embeddedartists 0:0fdadbc3d852 567 * C = Cn/Divider
embeddedartists 0:0fdadbc3d852 568 * D = Dn/Divider
embeddedartists 0:0fdadbc3d852 569 * E = En/Divider
embeddedartists 0:0fdadbc3d852 570 * F = Fn/Divider
embeddedartists 0:0fdadbc3d852 571 *
embeddedartists 0:0fdadbc3d852 572 *
embeddedartists 0:0fdadbc3d852 573 *
embeddedartists 0:0fdadbc3d852 574 * The functions provided within this file are:
embeddedartists 0:0fdadbc3d852 575 *
embeddedartists 0:0fdadbc3d852 576 * setCalibrationMatrix() - calculates the set of factors
embeddedartists 0:0fdadbc3d852 577 * in the above equation, given
embeddedartists 0:0fdadbc3d852 578 * three sets of test points.
embeddedartists 0:0fdadbc3d852 579 * getDisplayPoint() - returns the actual display
embeddedartists 0:0fdadbc3d852 580 * coordinates, given a set of
embeddedartists 0:0fdadbc3d852 581 * touch screen coordinates.
embeddedartists 0:0fdadbc3d852 582 * translateRawScreenCoordinates() - helper function to transform
embeddedartists 0:0fdadbc3d852 583 * raw screen points into values
embeddedartists 0:0fdadbc3d852 584 * scaled to the desired display
embeddedartists 0:0fdadbc3d852 585 * resolution.
embeddedartists 0:0fdadbc3d852 586 *
embeddedartists 0:0fdadbc3d852 587 *
embeddedartists 0:0fdadbc3d852 588 */
embeddedartists 0:0fdadbc3d852 589
embeddedartists 0:0fdadbc3d852 590
embeddedartists 0:0fdadbc3d852 591 /**********************************************************************
embeddedartists 0:0fdadbc3d852 592 *
embeddedartists 0:0fdadbc3d852 593 * Function: setCalibrationMatrix()
embeddedartists 0:0fdadbc3d852 594 *
embeddedartists 0:0fdadbc3d852 595 * Description: Calling this function with valid input data
embeddedartists 0:0fdadbc3d852 596 * in the display and screen input arguments
embeddedartists 0:0fdadbc3d852 597 * causes the calibration factors between the
embeddedartists 0:0fdadbc3d852 598 * screen and display points to be calculated,
embeddedartists 0:0fdadbc3d852 599 * and the output argument - matrixPtr - to be
embeddedartists 0:0fdadbc3d852 600 * populated.
embeddedartists 0:0fdadbc3d852 601 *
embeddedartists 0:0fdadbc3d852 602 * This function needs to be called only when new
embeddedartists 0:0fdadbc3d852 603 * calibration factors are desired.
embeddedartists 0:0fdadbc3d852 604 *
embeddedartists 0:0fdadbc3d852 605 *
embeddedartists 0:0fdadbc3d852 606 * Argument(s): displayPtr (input) - Pointer to an array of three
embeddedartists 0:0fdadbc3d852 607 * sample, reference points.
embeddedartists 0:0fdadbc3d852 608 * screenPtr (input) - Pointer to the array of touch
embeddedartists 0:0fdadbc3d852 609 * screen points corresponding
embeddedartists 0:0fdadbc3d852 610 * to the reference display points.
embeddedartists 0:0fdadbc3d852 611 * matrixPtr (output) - Pointer to the calibration
embeddedartists 0:0fdadbc3d852 612 * matrix computed for the set
embeddedartists 0:0fdadbc3d852 613 * of points being provided.
embeddedartists 0:0fdadbc3d852 614 *
embeddedartists 0:0fdadbc3d852 615 *
embeddedartists 0:0fdadbc3d852 616 * From the article text, recall that the matrix coefficients are
embeddedartists 0:0fdadbc3d852 617 * resolved to be the following:
embeddedartists 0:0fdadbc3d852 618 *
embeddedartists 0:0fdadbc3d852 619 *
embeddedartists 0:0fdadbc3d852 620 * Divider = (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2)
embeddedartists 0:0fdadbc3d852 621 *
embeddedartists 0:0fdadbc3d852 622 *
embeddedartists 0:0fdadbc3d852 623 *
embeddedartists 0:0fdadbc3d852 624 * (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2)
embeddedartists 0:0fdadbc3d852 625 * A = ---------------------------------------------------
embeddedartists 0:0fdadbc3d852 626 * Divider
embeddedartists 0:0fdadbc3d852 627 *
embeddedartists 0:0fdadbc3d852 628 *
embeddedartists 0:0fdadbc3d852 629 * (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2)
embeddedartists 0:0fdadbc3d852 630 * B = ---------------------------------------------------
embeddedartists 0:0fdadbc3d852 631 * Divider
embeddedartists 0:0fdadbc3d852 632 *
embeddedartists 0:0fdadbc3d852 633 *
embeddedartists 0:0fdadbc3d852 634 * Ys0*(Xs2*Xd1 - Xs1*Xd2) +
embeddedartists 0:0fdadbc3d852 635 * Ys1*(Xs0*Xd2 - Xs2*Xd0) +
embeddedartists 0:0fdadbc3d852 636 * Ys2*(Xs1*Xd0 - Xs0*Xd1)
embeddedartists 0:0fdadbc3d852 637 * C = ---------------------------------------------------
embeddedartists 0:0fdadbc3d852 638 * Divider
embeddedartists 0:0fdadbc3d852 639 *
embeddedartists 0:0fdadbc3d852 640 *
embeddedartists 0:0fdadbc3d852 641 * (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2)
embeddedartists 0:0fdadbc3d852 642 * D = ---------------------------------------------------
embeddedartists 0:0fdadbc3d852 643 * Divider
embeddedartists 0:0fdadbc3d852 644 *
embeddedartists 0:0fdadbc3d852 645 *
embeddedartists 0:0fdadbc3d852 646 * (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2)
embeddedartists 0:0fdadbc3d852 647 * E = ---------------------------------------------------
embeddedartists 0:0fdadbc3d852 648 * Divider
embeddedartists 0:0fdadbc3d852 649 *
embeddedartists 0:0fdadbc3d852 650 *
embeddedartists 0:0fdadbc3d852 651 * Ys0*(Xs2*Yd1 - Xs1*Yd2) +
embeddedartists 0:0fdadbc3d852 652 * Ys1*(Xs0*Yd2 - Xs2*Yd0) +
embeddedartists 0:0fdadbc3d852 653 * Ys2*(Xs1*Yd0 - Xs0*Yd1)
embeddedartists 0:0fdadbc3d852 654 * F = ---------------------------------------------------
embeddedartists 0:0fdadbc3d852 655 * Divider
embeddedartists 0:0fdadbc3d852 656 *
embeddedartists 0:0fdadbc3d852 657 *
embeddedartists 0:0fdadbc3d852 658 * Return: OK - the calibration matrix was correctly
embeddedartists 0:0fdadbc3d852 659 * calculated and its value is in the
embeddedartists 0:0fdadbc3d852 660 * output argument.
embeddedartists 0:0fdadbc3d852 661 * NOT_OK - an error was detected and the
embeddedartists 0:0fdadbc3d852 662 * function failed to return a valid
embeddedartists 0:0fdadbc3d852 663 * set of matrix values.
embeddedartists 0:0fdadbc3d852 664 * The only time this sample code returns
embeddedartists 0:0fdadbc3d852 665 * NOT_OK is when Divider == 0
embeddedartists 0:0fdadbc3d852 666 *
embeddedartists 0:0fdadbc3d852 667 *
embeddedartists 0:0fdadbc3d852 668 *
embeddedartists 0:0fdadbc3d852 669 * NOTE! NOTE! NOTE!
embeddedartists 0:0fdadbc3d852 670 *
embeddedartists 0:0fdadbc3d852 671 * setCalibrationMatrix() and getDisplayPoint() will do fine
embeddedartists 0:0fdadbc3d852 672 * for you as they are, provided that your digitizer
embeddedartists 0:0fdadbc3d852 673 * resolution does not exceed 10 bits (1024 values). Higher
embeddedartists 0:0fdadbc3d852 674 * resolutions may cause the integer operations to overflow
embeddedartists 0:0fdadbc3d852 675 * and return incorrect values. If you wish to use these
embeddedartists 0:0fdadbc3d852 676 * functions with digitizer resolutions of 12 bits (4096
embeddedartists 0:0fdadbc3d852 677 * values) you will either have to a) use 64-bit signed
embeddedartists 0:0fdadbc3d852 678 * integer variables and math, or b) judiciously modify the
embeddedartists 0:0fdadbc3d852 679 * operations to scale results by a factor of 2 or even 4.
embeddedartists 0:0fdadbc3d852 680 *
embeddedartists 0:0fdadbc3d852 681 *
embeddedartists 0:0fdadbc3d852 682 */
embeddedartists 0:0fdadbc3d852 683 int TSC2046::setCalibrationMatrix( calibPoint_t * displayPtr,
embeddedartists 0:0fdadbc3d852 684 calibPoint_t * screenPtr,
embeddedartists 0:0fdadbc3d852 685 calibMatrix_t * matrixPtr)
embeddedartists 0:0fdadbc3d852 686 {
embeddedartists 0:0fdadbc3d852 687 int retValue = 0 ;
embeddedartists 0:0fdadbc3d852 688
embeddedartists 0:0fdadbc3d852 689
embeddedartists 0:0fdadbc3d852 690 matrixPtr->Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
embeddedartists 0:0fdadbc3d852 691 ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
embeddedartists 0:0fdadbc3d852 692 if( matrixPtr->Divider == 0 )
embeddedartists 0:0fdadbc3d852 693 {
embeddedartists 0:0fdadbc3d852 694 retValue = 1 ;
embeddedartists 0:0fdadbc3d852 695 }
embeddedartists 0:0fdadbc3d852 696 else
embeddedartists 0:0fdadbc3d852 697 {
embeddedartists 0:0fdadbc3d852 698 matrixPtr->An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
embeddedartists 0:0fdadbc3d852 699 ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
embeddedartists 0:0fdadbc3d852 700 matrixPtr->Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
embeddedartists 0:0fdadbc3d852 701 ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
embeddedartists 0:0fdadbc3d852 702 matrixPtr->Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
embeddedartists 0:0fdadbc3d852 703 (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
embeddedartists 0:0fdadbc3d852 704 (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
embeddedartists 0:0fdadbc3d852 705 matrixPtr->Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
embeddedartists 0:0fdadbc3d852 706 ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
embeddedartists 0:0fdadbc3d852 707
embeddedartists 0:0fdadbc3d852 708 matrixPtr->En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
embeddedartists 0:0fdadbc3d852 709 ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
embeddedartists 0:0fdadbc3d852 710 matrixPtr->Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
embeddedartists 0:0fdadbc3d852 711 (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
embeddedartists 0:0fdadbc3d852 712 (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
embeddedartists 0:0fdadbc3d852 713 }
embeddedartists 0:0fdadbc3d852 714
embeddedartists 0:0fdadbc3d852 715 return( retValue ) ;
embeddedartists 0:0fdadbc3d852 716 }
embeddedartists 0:0fdadbc3d852 717
embeddedartists 0:0fdadbc3d852 718 /**********************************************************************
embeddedartists 0:0fdadbc3d852 719 *
embeddedartists 0:0fdadbc3d852 720 * Function: getDisplayPoint()
embeddedartists 0:0fdadbc3d852 721 *
embeddedartists 0:0fdadbc3d852 722 * Description: Given a valid set of calibration factors and a point
embeddedartists 0:0fdadbc3d852 723 * value reported by the touch screen, this function
embeddedartists 0:0fdadbc3d852 724 * calculates and returns the true (or closest to true)
embeddedartists 0:0fdadbc3d852 725 * display point below the spot where the touch screen
embeddedartists 0:0fdadbc3d852 726 * was touched.
embeddedartists 0:0fdadbc3d852 727 *
embeddedartists 0:0fdadbc3d852 728 *
embeddedartists 0:0fdadbc3d852 729 *
embeddedartists 0:0fdadbc3d852 730 * Argument(s): displayPtr (output) - Pointer to the calculated
embeddedartists 0:0fdadbc3d852 731 * (true) display point.
embeddedartists 0:0fdadbc3d852 732 * screenPtr (input) - Pointer to the reported touch
embeddedartists 0:0fdadbc3d852 733 * screen point.
embeddedartists 0:0fdadbc3d852 734 * matrixPtr (input) - Pointer to calibration factors
embeddedartists 0:0fdadbc3d852 735 * matrix previously calculated
embeddedartists 0:0fdadbc3d852 736 * from a call to
embeddedartists 0:0fdadbc3d852 737 * setCalibrationMatrix()
embeddedartists 0:0fdadbc3d852 738 *
embeddedartists 0:0fdadbc3d852 739 *
embeddedartists 0:0fdadbc3d852 740 * The function simply solves for Xd and Yd by implementing the
embeddedartists 0:0fdadbc3d852 741 * computations required by the translation matrix.
embeddedartists 0:0fdadbc3d852 742 *
embeddedartists 0:0fdadbc3d852 743 * /- -\
embeddedartists 0:0fdadbc3d852 744 * /- -\ /- -\ | |
embeddedartists 0:0fdadbc3d852 745 * | | | | | Xs |
embeddedartists 0:0fdadbc3d852 746 * | Xd | | A B C | | |
embeddedartists 0:0fdadbc3d852 747 * | | = | | * | Ys |
embeddedartists 0:0fdadbc3d852 748 * | Yd | | D E F | | |
embeddedartists 0:0fdadbc3d852 749 * | | | | | 1 |
embeddedartists 0:0fdadbc3d852 750 * \- -/ \- -/ | |
embeddedartists 0:0fdadbc3d852 751 * \- -/
embeddedartists 0:0fdadbc3d852 752 *
embeddedartists 0:0fdadbc3d852 753 * It must be kept brief to avoid consuming CPU cycles.
embeddedartists 0:0fdadbc3d852 754 *
embeddedartists 0:0fdadbc3d852 755 *
embeddedartists 0:0fdadbc3d852 756 * Return: OK - the display point was correctly calculated
embeddedartists 0:0fdadbc3d852 757 * and its value is in the output argument.
embeddedartists 0:0fdadbc3d852 758 * NOT_OK - an error was detected and the function
embeddedartists 0:0fdadbc3d852 759 * failed to return a valid point.
embeddedartists 0:0fdadbc3d852 760 *
embeddedartists 0:0fdadbc3d852 761 *
embeddedartists 0:0fdadbc3d852 762 *
embeddedartists 0:0fdadbc3d852 763 * NOTE! NOTE! NOTE!
embeddedartists 0:0fdadbc3d852 764 *
embeddedartists 0:0fdadbc3d852 765 * setCalibrationMatrix() and getDisplayPoint() will do fine
embeddedartists 0:0fdadbc3d852 766 * for you as they are, provided that your digitizer
embeddedartists 0:0fdadbc3d852 767 * resolution does not exceed 10 bits (1024 values). Higher
embeddedartists 0:0fdadbc3d852 768 * resolutions may cause the integer operations to overflow
embeddedartists 0:0fdadbc3d852 769 * and return incorrect values. If you wish to use these
embeddedartists 0:0fdadbc3d852 770 * functions with digitizer resolutions of 12 bits (4096
embeddedartists 0:0fdadbc3d852 771 * values) you will either have to a) use 64-bit signed
embeddedartists 0:0fdadbc3d852 772 * integer variables and math, or b) judiciously modify the
embeddedartists 0:0fdadbc3d852 773 * operations to scale results by a factor of 2 or even 4.
embeddedartists 0:0fdadbc3d852 774 *
embeddedartists 0:0fdadbc3d852 775 *
embeddedartists 0:0fdadbc3d852 776 */
embeddedartists 0:0fdadbc3d852 777 int TSC2046::getDisplayPoint( calibPoint_t * displayPtr,
embeddedartists 0:0fdadbc3d852 778 calibPoint_t * screenPtr,
embeddedartists 0:0fdadbc3d852 779 calibMatrix_t * matrixPtr )
embeddedartists 0:0fdadbc3d852 780 {
embeddedartists 0:0fdadbc3d852 781 int retValue = 0 ;
embeddedartists 0:0fdadbc3d852 782
embeddedartists 0:0fdadbc3d852 783 if( matrixPtr->Divider != 0 )
embeddedartists 0:0fdadbc3d852 784 {
embeddedartists 0:0fdadbc3d852 785 /* Operation order is important since we are doing integer */
embeddedartists 0:0fdadbc3d852 786 /* math. Make sure you add all terms together before */
embeddedartists 0:0fdadbc3d852 787 /* dividing, so that the remainder is not rounded off */
embeddedartists 0:0fdadbc3d852 788 /* prematurely. */
embeddedartists 0:0fdadbc3d852 789 displayPtr->x = ( (matrixPtr->An * screenPtr->x) +
embeddedartists 0:0fdadbc3d852 790 (matrixPtr->Bn * screenPtr->y) +
embeddedartists 0:0fdadbc3d852 791 matrixPtr->Cn
embeddedartists 0:0fdadbc3d852 792 ) / matrixPtr->Divider ;
embeddedartists 0:0fdadbc3d852 793 displayPtr->y = ( (matrixPtr->Dn * screenPtr->x) +
embeddedartists 0:0fdadbc3d852 794 (matrixPtr->En * screenPtr->y) +
embeddedartists 0:0fdadbc3d852 795 matrixPtr->Fn
embeddedartists 0:0fdadbc3d852 796 ) / matrixPtr->Divider ;
embeddedartists 0:0fdadbc3d852 797 }
embeddedartists 0:0fdadbc3d852 798 else
embeddedartists 0:0fdadbc3d852 799 {
embeddedartists 0:0fdadbc3d852 800 retValue = 1 ;
embeddedartists 0:0fdadbc3d852 801 }
embeddedartists 0:0fdadbc3d852 802 return( retValue ) ;
embeddedartists 0:0fdadbc3d852 803 }
embeddedartists 0:0fdadbc3d852 804
embeddedartists 0:0fdadbc3d852 805
embeddedartists 4:b32cf4ef45c5 806
embeddedartists 0:0fdadbc3d852 807 // ############################################################################
embeddedartists 0:0fdadbc3d852 808 // <<<<<<<< Calibrate code <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
embeddedartists 0:0fdadbc3d852 809 // ############################################################################
embeddedartists 0:0fdadbc3d852 810
embeddedartists 0:0fdadbc3d852 811
embeddedartists 0:0fdadbc3d852 812