KSM edits to RA8875

Dependents:   Liz_Test_Code

Committer:
WiredHome
Date:
Tue Nov 13 01:33:41 2018 +0000
Revision:
155:b3f225ae572c
Parent:
154:ad2450fc3dc3
Child:
157:1565f38ca44b
use eventThread for newer OS5 in place of callbacks.; touch-cal timeout increased to 30s from 15.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 78:faf49c381591 1 /// This file contains the RA8875 Touch panel methods.
WiredHome 78:faf49c381591 2 ///
WiredHome 124:1690a7ae871c 3 /// It combines both resistive and capacitive touch methods, and tries
WiredHome 124:1690a7ae871c 4 /// to make them nearly transparent alternates for each other.
WiredHome 124:1690a7ae871c 5 ///
WiredHome 78:faf49c381591 6 #include "RA8875.h"
WiredHome 78:faf49c381591 7
WiredHome 83:7bad0068cca0 8 #define NOTOUCH_TIMEOUT_uS 100000
WiredHome 83:7bad0068cca0 9 #define TOUCH_TICKER_uS 1000
WiredHome 83:7bad0068cca0 10
WiredHome 154:ad2450fc3dc3 11 //#define DEBUG "TUCH"
WiredHome 154:ad2450fc3dc3 12 // ...
WiredHome 154:ad2450fc3dc3 13 // INFO("Stuff to show %d", var); // new-line is automatically appended
WiredHome 154:ad2450fc3dc3 14 //
WiredHome 154:ad2450fc3dc3 15 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 154:ad2450fc3dc3 16 #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 154:ad2450fc3dc3 17 #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 154:ad2450fc3dc3 18 #define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 154:ad2450fc3dc3 19 static void HexDump(const char * title, const uint8_t * p, int count)
WiredHome 154:ad2450fc3dc3 20 {
WiredHome 154:ad2450fc3dc3 21 int i;
WiredHome 154:ad2450fc3dc3 22 char buf[100] = "0000: ";
WiredHome 154:ad2450fc3dc3 23
WiredHome 154:ad2450fc3dc3 24 if (*title)
WiredHome 154:ad2450fc3dc3 25 INFO("%s", title);
WiredHome 154:ad2450fc3dc3 26 for (i=0; i<count; ) {
WiredHome 154:ad2450fc3dc3 27 sprintf(buf + strlen(buf), "%02X ", *(p+i));
WiredHome 154:ad2450fc3dc3 28 if ((++i & 0x0F) == 0x00) {
WiredHome 154:ad2450fc3dc3 29 INFO("%s", buf);
WiredHome 154:ad2450fc3dc3 30 if (i < count)
WiredHome 154:ad2450fc3dc3 31 sprintf(buf, "%04X: ", i);
WiredHome 154:ad2450fc3dc3 32 else
WiredHome 154:ad2450fc3dc3 33 buf[0] = '\0';
WiredHome 154:ad2450fc3dc3 34 }
WiredHome 154:ad2450fc3dc3 35 }
WiredHome 154:ad2450fc3dc3 36 if (strlen(buf))
WiredHome 154:ad2450fc3dc3 37 INFO("%s", buf);
WiredHome 154:ad2450fc3dc3 38 }
WiredHome 154:ad2450fc3dc3 39 #else
WiredHome 154:ad2450fc3dc3 40 #define INFO(x, ...)
WiredHome 154:ad2450fc3dc3 41 #define WARN(x, ...)
WiredHome 154:ad2450fc3dc3 42 #define ERR(x, ...)
WiredHome 154:ad2450fc3dc3 43 #define HexDump(a, b, c)
WiredHome 154:ad2450fc3dc3 44 #endif
WiredHome 154:ad2450fc3dc3 45
WiredHome 124:1690a7ae871c 46
WiredHome 124:1690a7ae871c 47 // Translate from FT5206 Event Flag to Touch Code to API-match the
WiredHome 124:1690a7ae871c 48 // alternate resistive touch screen driver common in the RA8875
WiredHome 124:1690a7ae871c 49 // displays.
WiredHome 124:1690a7ae871c 50 static const TouchCode_t EventFlagToTouchCode[4] = {
WiredHome 124:1690a7ae871c 51 touch, // 00b Put Down
WiredHome 124:1690a7ae871c 52 release, // 01b Put Up
WiredHome 124:1690a7ae871c 53 held, // 10b Contact
WiredHome 124:1690a7ae871c 54 no_touch // 11b Reserved
WiredHome 124:1690a7ae871c 55 };
WiredHome 124:1690a7ae871c 56
WiredHome 124:1690a7ae871c 57
WiredHome 78:faf49c381591 58 RetCode_t RA8875::TouchPanelInit(void)
WiredHome 78:faf49c381591 59 {
WiredHome 124:1690a7ae871c 60 panelTouched = false;
WiredHome 124:1690a7ae871c 61 if (useTouchPanel == TP_CAP) {
WiredHome 124:1690a7ae871c 62 // Set to normal mode
WiredHome 155:b3f225ae572c 63 INFO("TouchPanelInit: TP_CAP");
WiredHome 124:1690a7ae871c 64 writeRegister8(FT5206_DEVICE_MODE, 0);
WiredHome 124:1690a7ae871c 65 } else {
WiredHome 155:b3f225ae572c 66 INFO("TouchPanelInit: TP_RES");
WiredHome 124:1690a7ae871c 67 //TPCR0: Set enable bit, default sample time, wakeup, and ADC clock
WiredHome 124:1690a7ae871c 68 WriteCommand(TPCR0, TP_ENABLE | TP_ADC_SAMPLE_DEFAULT_CLKS | TP_ADC_CLKDIV_DEFAULT);
WiredHome 124:1690a7ae871c 69 // TPCR1: Set auto/manual, Ref voltage, debounce, manual mode params
WiredHome 124:1690a7ae871c 70 WriteCommand(TPCR1, TP_MODE_DEFAULT | TP_DEBOUNCE_DEFAULT);
WiredHome 124:1690a7ae871c 71 WriteCommand(INTC1, ReadCommand(INTC1) | RA8875_INT_TP); // reg INTC1: Enable Touch Panel Interrupts (D2 = 1)
WiredHome 124:1690a7ae871c 72 WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear any TP interrupt flag
WiredHome 124:1690a7ae871c 73 touchSample = 0;
WiredHome 124:1690a7ae871c 74 touchState = no_cal;
WiredHome 150:35a4db3081c1 75 //touchTicker.attach_us(callback(this, &RA8875::_TouchTicker), TOUCH_TICKER_uS);
WiredHome 154:ad2450fc3dc3 76 #if (MBED_MAJOR_VERSION >= 5) || (MBED_LIBRARY_VERSION > 122) // Is this the right version?
WiredHome 145:5eb2492acdda 77 touchTicker.attach_us(callback(this, &RA8875::_TouchTicker), TOUCH_TICKER_uS);
WiredHome 150:35a4db3081c1 78 #else
WiredHome 150:35a4db3081c1 79 touchTicker.attach_us(this, &RA8875::_TouchTicker, TOUCH_TICKER_uS);
WiredHome 150:35a4db3081c1 80 #endif
WiredHome 150:35a4db3081c1 81
WiredHome 83:7bad0068cca0 82 touchTimer.start();
WiredHome 83:7bad0068cca0 83 touchTimer.reset();
WiredHome 83:7bad0068cca0 84 }
WiredHome 78:faf49c381591 85 return noerror;
WiredHome 78:faf49c381591 86 }
WiredHome 78:faf49c381591 87
WiredHome 124:1690a7ae871c 88
WiredHome 124:1690a7ae871c 89 RetCode_t RA8875::TouchPanelInit(uint8_t bTpEnable, uint8_t bTpAutoManual, uint8_t bTpDebounce, uint8_t bTpManualMode, uint8_t bTpAdcClkDiv, uint8_t bTpAdcSampleTime)
WiredHome 124:1690a7ae871c 90 {
WiredHome 124:1690a7ae871c 91 if (useTouchPanel == TP_CAP) {
WiredHome 155:b3f225ae572c 92 INFO("TouchPanelInit: TP_CAP");
WiredHome 124:1690a7ae871c 93 TouchPanelInit();
WiredHome 124:1690a7ae871c 94 } else {
WiredHome 155:b3f225ae572c 95 INFO("TouchPanelInit: TP_RES");
WiredHome 124:1690a7ae871c 96 // Parameter bounds check
WiredHome 124:1690a7ae871c 97 if( \
WiredHome 124:1690a7ae871c 98 !(bTpEnable == TP_ENABLE || bTpEnable == TP_ENABLE) || \
WiredHome 124:1690a7ae871c 99 !(bTpAutoManual == TP_MODE_AUTO || bTpAutoManual == TP_MODE_MANUAL) || \
WiredHome 124:1690a7ae871c 100 !(bTpDebounce == TP_DEBOUNCE_OFF || bTpDebounce == TP_DEBOUNCE_ON) || \
WiredHome 124:1690a7ae871c 101 !(bTpManualMode <= TP_MANUAL_LATCH_Y) || \
WiredHome 124:1690a7ae871c 102 !(bTpAdcClkDiv <= TP_ADC_CLKDIV_128) || \
WiredHome 124:1690a7ae871c 103 !(bTpAdcSampleTime <= TP_ADC_SAMPLE_65536_CLKS) \
WiredHome 124:1690a7ae871c 104 ) return bad_parameter;
WiredHome 124:1690a7ae871c 105 // Construct the config byte for TPCR0 and write them
WiredHome 124:1690a7ae871c 106 WriteCommand(TPCR0, bTpEnable | bTpAdcClkDiv | bTpAdcSampleTime); // Note: Wakeup is never enabled
WiredHome 124:1690a7ae871c 107 // Construct the config byte for TPCR1 and write them
WiredHome 124:1690a7ae871c 108 WriteCommand(TPCR1, bTpManualMode | bTpDebounce | bTpManualMode); // Note: Always uses internal Vref.
WiredHome 124:1690a7ae871c 109 // Set up the interrupt flag and enable bits
WiredHome 124:1690a7ae871c 110 WriteCommand(INTC1, ReadCommand(INTC1) | RA8875_INT_TP); // reg INTC1: Enable Touch Panel Interrupts (D2 = 1)
WiredHome 124:1690a7ae871c 111 WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear any TP interrupt flag
WiredHome 124:1690a7ae871c 112 touchSample = 0;
WiredHome 124:1690a7ae871c 113 touchState = no_cal;
WiredHome 124:1690a7ae871c 114 if (bTpEnable == TP_ENABLE) {
WiredHome 150:35a4db3081c1 115 //touchTicker.attach_us(callback(this, &RA8875::_TouchTicker), TOUCH_TICKER_uS);
WiredHome 154:ad2450fc3dc3 116 #if (MBED_MAJOR_VERSION >= 5) || (MBED_LIBRARY_VERSION > 122) // Is this the right version?
WiredHome 145:5eb2492acdda 117 touchTicker.attach_us(callback(this, &RA8875::_TouchTicker), TOUCH_TICKER_uS);
WiredHome 150:35a4db3081c1 118 #else
WiredHome 150:35a4db3081c1 119 touchTicker.attach_us(this, &RA8875::_TouchTicker, TOUCH_TICKER_uS);
WiredHome 150:35a4db3081c1 120 #endif
WiredHome 150:35a4db3081c1 121
WiredHome 124:1690a7ae871c 122 touchTimer.start();
WiredHome 124:1690a7ae871c 123 touchTimer.reset();
WiredHome 124:1690a7ae871c 124 } else {
WiredHome 124:1690a7ae871c 125 touchTicker.detach();
WiredHome 124:1690a7ae871c 126 touchTimer.stop();
WiredHome 124:1690a7ae871c 127 }
WiredHome 124:1690a7ae871c 128 }
WiredHome 124:1690a7ae871c 129 return noerror;
WiredHome 124:1690a7ae871c 130 }
WiredHome 124:1690a7ae871c 131
WiredHome 124:1690a7ae871c 132
WiredHome 124:1690a7ae871c 133 int RA8875::TouchChannels(void)
WiredHome 124:1690a7ae871c 134 {
WiredHome 124:1690a7ae871c 135 if (useTouchPanel == TP_CAP) {
WiredHome 124:1690a7ae871c 136 return 5; // based on the FT5206 hardware
WiredHome 124:1690a7ae871c 137 } else if (useTouchPanel == TP_RES) {
WiredHome 124:1690a7ae871c 138 return 1; // based on the RA8875 resistive touch driver
WiredHome 124:1690a7ae871c 139 } else {
WiredHome 124:1690a7ae871c 140 return 0; // it isn't enabled, so there are none.
WiredHome 124:1690a7ae871c 141 }
WiredHome 124:1690a7ae871c 142 }
WiredHome 124:1690a7ae871c 143
WiredHome 124:1690a7ae871c 144
WiredHome 124:1690a7ae871c 145 // +----------------------------------------------------+
WiredHome 124:1690a7ae871c 146 // | |
WiredHome 124:1690a7ae871c 147 // | 1 |
WiredHome 124:1690a7ae871c 148 // | |
WiredHome 124:1690a7ae871c 149 // | |
WiredHome 124:1690a7ae871c 150 // | 2 |
WiredHome 124:1690a7ae871c 151 // | |
WiredHome 124:1690a7ae871c 152 // | |
WiredHome 124:1690a7ae871c 153 // | 3 |
WiredHome 124:1690a7ae871c 154 // | |
WiredHome 124:1690a7ae871c 155 // +----------------------------------------------------+
WiredHome 81:01da2e34283d 156
WiredHome 81:01da2e34283d 157 RetCode_t RA8875::TouchPanelCalibrate(tpMatrix_t * matrix)
WiredHome 81:01da2e34283d 158 {
WiredHome 81:01da2e34283d 159 return TouchPanelCalibrate(NULL, matrix);
WiredHome 81:01da2e34283d 160 }
WiredHome 81:01da2e34283d 161
WiredHome 88:bfddef6ec836 162 RetCode_t RA8875::TouchPanelCalibrate(const char * msg, tpMatrix_t * matrix, int maxwait_s)
WiredHome 81:01da2e34283d 163 {
WiredHome 81:01da2e34283d 164 point_t pTest[3];
WiredHome 81:01da2e34283d 165 point_t pSample[3];
WiredHome 83:7bad0068cca0 166 int x,y;
WiredHome 88:bfddef6ec836 167 Timer timeout; // timeout guards for not-installed, stuck, user not present...
WiredHome 124:1690a7ae871c 168
WiredHome 88:bfddef6ec836 169 timeout.start();
WiredHome 123:2f45e80fec5f 170 while (TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
WiredHome 83:7bad0068cca0 171 wait_ms(20);
WiredHome 123:2f45e80fec5f 172 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 173 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
WiredHome 123:2f45e80fec5f 174 return external_abort;
WiredHome 123:2f45e80fec5f 175 }
WiredHome 123:2f45e80fec5f 176 }
WiredHome 123:2f45e80fec5f 177 }
WiredHome 81:01da2e34283d 178 cls();
WiredHome 81:01da2e34283d 179 if (msg)
WiredHome 81:01da2e34283d 180 puts(msg);
WiredHome 81:01da2e34283d 181 SetTextCursor(0,height()/2);
WiredHome 124:1690a7ae871c 182 pTest[0].x = 50;
WiredHome 124:1690a7ae871c 183 pTest[0].y = 50;
WiredHome 124:1690a7ae871c 184 pTest[1].x = width() - 50;
WiredHome 124:1690a7ae871c 185 pTest[1].y = height()/2;
WiredHome 124:1690a7ae871c 186 pTest[2].x = width()/2;
WiredHome 124:1690a7ae871c 187 pTest[2].y = height() - 50;
WiredHome 88:bfddef6ec836 188
WiredHome 81:01da2e34283d 189 for (int i=0; i<3; i++) {
WiredHome 81:01da2e34283d 190 foreground(Blue);
WiredHome 81:01da2e34283d 191 printf(" (%3d,%3d) => ", pTest[i].x, pTest[i].y);
WiredHome 81:01da2e34283d 192 line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, White);
WiredHome 81:01da2e34283d 193 line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, White);
WiredHome 123:2f45e80fec5f 194 while (!TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
WiredHome 81:01da2e34283d 195 wait_ms(20);
WiredHome 123:2f45e80fec5f 196 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 197 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
WiredHome 123:2f45e80fec5f 198 return external_abort;
WiredHome 123:2f45e80fec5f 199 }
WiredHome 123:2f45e80fec5f 200 }
WiredHome 123:2f45e80fec5f 201 }
WiredHome 81:01da2e34283d 202 pSample[i].x = x;
WiredHome 81:01da2e34283d 203 pSample[i].y = y;
WiredHome 81:01da2e34283d 204 line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, Black);
WiredHome 81:01da2e34283d 205 line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, Black);
WiredHome 81:01da2e34283d 206 foreground(Blue);
WiredHome 81:01da2e34283d 207 printf(" (%4d,%4d)\r\n", x,y);
WiredHome 123:2f45e80fec5f 208 while (TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
WiredHome 81:01da2e34283d 209 wait_ms(20);
WiredHome 123:2f45e80fec5f 210 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 211 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
WiredHome 123:2f45e80fec5f 212 return external_abort;
WiredHome 123:2f45e80fec5f 213 }
WiredHome 123:2f45e80fec5f 214 }
WiredHome 123:2f45e80fec5f 215 }
WiredHome 123:2f45e80fec5f 216 for (int t=0; t<100; t++) {
WiredHome 123:2f45e80fec5f 217 wait_ms(20);
WiredHome 123:2f45e80fec5f 218 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 219 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
WiredHome 123:2f45e80fec5f 220 return external_abort;
WiredHome 123:2f45e80fec5f 221 }
WiredHome 123:2f45e80fec5f 222 }
WiredHome 123:2f45e80fec5f 223 }
WiredHome 81:01da2e34283d 224 }
WiredHome 88:bfddef6ec836 225 if (timeout.read() >= maxwait_s)
WiredHome 88:bfddef6ec836 226 return touch_cal_timeout;
WiredHome 88:bfddef6ec836 227 else
WiredHome 88:bfddef6ec836 228 return TouchPanelComputeCalibration(pTest, pSample, matrix);
WiredHome 81:01da2e34283d 229 }
WiredHome 81:01da2e34283d 230
WiredHome 81:01da2e34283d 231
WiredHome 81:01da2e34283d 232 /**********************************************************************
WiredHome 81:01da2e34283d 233 *
WiredHome 123:2f45e80fec5f 234 * Function: TouchPanelReadable()
WiredHome 81:01da2e34283d 235 *
WiredHome 81:01da2e34283d 236 * Description: Given a valid set of calibration factors and a point
WiredHome 81:01da2e34283d 237 * value reported by the touch screen, this function
WiredHome 81:01da2e34283d 238 * calculates and returns the true (or closest to true)
WiredHome 124:1690a7ae871c 239 * display point below the spot where the touch screen
WiredHome 81:01da2e34283d 240 * was touched.
WiredHome 124:1690a7ae871c 241 *
WiredHome 81:01da2e34283d 242 *
WiredHome 124:1690a7ae871c 243 *
WiredHome 81:01da2e34283d 244 * Argument(s): displayPtr (output) - Pointer to the calculated
WiredHome 81:01da2e34283d 245 * (true) display point.
WiredHome 81:01da2e34283d 246 * screenPtr (input) - Pointer to the reported touch
WiredHome 81:01da2e34283d 247 * screen point.
WiredHome 81:01da2e34283d 248 * matrixPtr (input) - Pointer to calibration factors
WiredHome 81:01da2e34283d 249 * matrix previously calculated
WiredHome 124:1690a7ae871c 250 * from a call to
WiredHome 81:01da2e34283d 251 * setCalibrationMatrix()
WiredHome 124:1690a7ae871c 252 *
WiredHome 81:01da2e34283d 253 *
WiredHome 124:1690a7ae871c 254 * The function simply solves for Xd and Yd by implementing the
WiredHome 124:1690a7ae871c 255 * computations required by the translation matrix.
WiredHome 124:1690a7ae871c 256 *
WiredHome 81:01da2e34283d 257 * /- -\
WiredHome 81:01da2e34283d 258 * /- -\ /- -\ | |
WiredHome 81:01da2e34283d 259 * | | | | | Xs |
WiredHome 81:01da2e34283d 260 * | Xd | | A B C | | |
WiredHome 81:01da2e34283d 261 * | | = | | * | Ys |
WiredHome 81:01da2e34283d 262 * | Yd | | D E F | | |
WiredHome 81:01da2e34283d 263 * | | | | | 1 |
WiredHome 81:01da2e34283d 264 * \- -/ \- -/ | |
WiredHome 81:01da2e34283d 265 * \- -/
WiredHome 124:1690a7ae871c 266 *
WiredHome 81:01da2e34283d 267 * It must be kept brief to avoid consuming CPU cycles.
WiredHome 81:01da2e34283d 268 *
WiredHome 124:1690a7ae871c 269 * Return: OK - the display point was correctly calculated
WiredHome 81:01da2e34283d 270 * and its value is in the output argument.
WiredHome 81:01da2e34283d 271 * NOT_OK - an error was detected and the function
WiredHome 81:01da2e34283d 272 * failed to return a valid point.
WiredHome 81:01da2e34283d 273 *
WiredHome 81:01da2e34283d 274 * NOTE! NOTE! NOTE!
WiredHome 81:01da2e34283d 275 *
WiredHome 81:01da2e34283d 276 * setCalibrationMatrix() and getDisplayPoint() will do fine
WiredHome 124:1690a7ae871c 277 * for you as they are, provided that your digitizer
WiredHome 81:01da2e34283d 278 * resolution does not exceed 10 bits (1024 values). Higher
WiredHome 81:01da2e34283d 279 * resolutions may cause the integer operations to overflow
WiredHome 124:1690a7ae871c 280 * and return incorrect values. If you wish to use these
WiredHome 124:1690a7ae871c 281 * functions with digitizer resolutions of 12 bits (4096
WiredHome 124:1690a7ae871c 282 * values) you will either have to a) use 64-bit signed
WiredHome 124:1690a7ae871c 283 * integer variables and math, or b) judiciously modify the
WiredHome 124:1690a7ae871c 284 * operations to scale results by a factor of 2 or even 4.
WiredHome 81:01da2e34283d 285 *
WiredHome 81:01da2e34283d 286 */
WiredHome 83:7bad0068cca0 287 TouchCode_t RA8875::TouchPanelReadable(point_t * TouchPoint)
WiredHome 81:01da2e34283d 288 {
WiredHome 124:1690a7ae871c 289 TouchCode_t ts = no_touch;
WiredHome 124:1690a7ae871c 290
WiredHome 124:1690a7ae871c 291 if (useTouchPanel == TP_RES) {
WiredHome 124:1690a7ae871c 292 int a2dX = 0;
WiredHome 124:1690a7ae871c 293 int a2dY = 0;
WiredHome 124:1690a7ae871c 294
WiredHome 124:1690a7ae871c 295 touchInfo[0].touchID = 0;
WiredHome 124:1690a7ae871c 296 ts = TouchPanelA2DFiltered(&a2dX, &a2dY);
WiredHome 124:1690a7ae871c 297 if (ts != no_touch) {
WiredHome 124:1690a7ae871c 298 panelTouched = true;
WiredHome 124:1690a7ae871c 299 numberOfTouchPoints = 1;
WiredHome 124:1690a7ae871c 300
WiredHome 124:1690a7ae871c 301 if (tpMatrix.Divider != 0) {
WiredHome 103:7e0464ca6c5c 302 /* Operation order is important since we are doing integer */
WiredHome 103:7e0464ca6c5c 303 /* math. Make sure you add all terms together before */
WiredHome 103:7e0464ca6c5c 304 /* dividing, so that the remainder is not rounded off */
WiredHome 103:7e0464ca6c5c 305 /* prematurely. */
WiredHome 124:1690a7ae871c 306 touchInfo[0].coordinates.x = ( (tpMatrix.An * a2dX) +
WiredHome 127:db7f2c704693 307 (tpMatrix.Bn * a2dY) + tpMatrix.Cn
WiredHome 103:7e0464ca6c5c 308 ) / tpMatrix.Divider ;
WiredHome 124:1690a7ae871c 309 touchInfo[0].coordinates.y = ( (tpMatrix.Dn * a2dX) +
WiredHome 127:db7f2c704693 310 (tpMatrix.En * a2dY) + tpMatrix.Fn
WiredHome 103:7e0464ca6c5c 311 ) / tpMatrix.Divider ;
WiredHome 124:1690a7ae871c 312 } else {
WiredHome 124:1690a7ae871c 313 ts = no_cal;
WiredHome 103:7e0464ca6c5c 314 }
WiredHome 81:01da2e34283d 315 } else {
WiredHome 124:1690a7ae871c 316 numberOfTouchPoints = 0;
WiredHome 81:01da2e34283d 317 }
WiredHome 124:1690a7ae871c 318 touchInfo[0].touchCode = ts;
WiredHome 128:3c74ba4533dc 319 } else /* (useTouchPanel == TP_CAP) */ {
WiredHome 130:d633e80840e0 320 ;
WiredHome 124:1690a7ae871c 321 }
WiredHome 124:1690a7ae871c 322 if (panelTouched == true) {
WiredHome 124:1690a7ae871c 323 panelTouched = false;
WiredHome 124:1690a7ae871c 324 if (TouchPoint) {
WiredHome 124:1690a7ae871c 325 *TouchPoint = touchInfo[0].coordinates;
WiredHome 124:1690a7ae871c 326 ts = touchInfo[0].touchCode;
WiredHome 155:b3f225ae572c 327 INFO("Touch[0] %2d (%4d,%4d)", touchInfo[0].touchCode,
WiredHome 155:b3f225ae572c 328 touchInfo[0].coordinates.x, touchInfo[0].coordinates.y);
WiredHome 130:d633e80840e0 329 } else {
WiredHome 130:d633e80840e0 330 ts = touch;
WiredHome 124:1690a7ae871c 331 }
WiredHome 81:01da2e34283d 332 }
WiredHome 83:7bad0068cca0 333 return ts;
WiredHome 81:01da2e34283d 334 }
WiredHome 81:01da2e34283d 335
WiredHome 81:01da2e34283d 336
WiredHome 85:022bba13c5c4 337 TouchCode_t RA8875::TouchPanelGet(point_t * TouchPoint)
WiredHome 85:022bba13c5c4 338 {
WiredHome 123:2f45e80fec5f 339 TouchCode_t t = no_touch;
WiredHome 124:1690a7ae871c 340
WiredHome 123:2f45e80fec5f 341 while (true) {
WiredHome 85:022bba13c5c4 342 t = TouchPanelReadable(TouchPoint);
WiredHome 123:2f45e80fec5f 343 if (t != no_touch)
WiredHome 123:2f45e80fec5f 344 break;
WiredHome 123:2f45e80fec5f 345 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 346 if (external_abort == (*idle_callback)(touch_wait, 0)) {
WiredHome 123:2f45e80fec5f 347 return no_touch;
WiredHome 123:2f45e80fec5f 348 }
WiredHome 123:2f45e80fec5f 349 }
WiredHome 123:2f45e80fec5f 350 }
WiredHome 85:022bba13c5c4 351 return t;
WiredHome 85:022bba13c5c4 352 }
WiredHome 85:022bba13c5c4 353
WiredHome 123:2f45e80fec5f 354 // Below here are primarily "helper" functions. While many are accessible
WiredHome 123:2f45e80fec5f 355 // to the user code, they usually don't need to be called.
WiredHome 123:2f45e80fec5f 356
WiredHome 81:01da2e34283d 357 RetCode_t RA8875::TouchPanelSetMatrix(tpMatrix_t * matrixPtr)
WiredHome 81:01da2e34283d 358 {
WiredHome 81:01da2e34283d 359 if (matrixPtr == NULL || matrixPtr->Divider == 0)
WiredHome 81:01da2e34283d 360 return bad_parameter;
WiredHome 81:01da2e34283d 361 memcpy(&tpMatrix, matrixPtr, sizeof(tpMatrix_t));
WiredHome 83:7bad0068cca0 362 touchState = no_touch;
WiredHome 81:01da2e34283d 363 return noerror;
WiredHome 81:01da2e34283d 364 }
WiredHome 81:01da2e34283d 365
WiredHome 83:7bad0068cca0 366 static void InsertionSort(int * buf, int bufsize)
WiredHome 83:7bad0068cca0 367 {
WiredHome 83:7bad0068cca0 368 int i, j;
WiredHome 83:7bad0068cca0 369 int temp;
WiredHome 124:1690a7ae871c 370
WiredHome 108:7415c405ee08 371 for(i = 1; i < bufsize; i++) {
WiredHome 83:7bad0068cca0 372 temp = buf[i];
WiredHome 83:7bad0068cca0 373 j = i;
WiredHome 83:7bad0068cca0 374 while( j && (buf[j-1] > temp) ) {
WiredHome 83:7bad0068cca0 375 buf[j] = buf[j-1];
WiredHome 83:7bad0068cca0 376 j = j-1;
WiredHome 83:7bad0068cca0 377 }
WiredHome 83:7bad0068cca0 378 buf[j] = temp;
WiredHome 83:7bad0068cca0 379 } // End of sort
WiredHome 83:7bad0068cca0 380 }
WiredHome 83:7bad0068cca0 381
WiredHome 83:7bad0068cca0 382
WiredHome 83:7bad0068cca0 383 void RA8875::_TouchTicker(void)
WiredHome 78:faf49c381591 384 {
WiredHome 155:b3f225ae572c 385 INFO("_TouchTicker()");
WiredHome 83:7bad0068cca0 386 if (touchTimer.read_us() > NOTOUCH_TIMEOUT_uS) {
WiredHome 83:7bad0068cca0 387 touchSample = 0;
WiredHome 83:7bad0068cca0 388 if (touchState == held)
WiredHome 83:7bad0068cca0 389 touchState = release;
WiredHome 83:7bad0068cca0 390 else
WiredHome 83:7bad0068cca0 391 touchState = no_touch;
WiredHome 83:7bad0068cca0 392 touchTimer.reset();
WiredHome 83:7bad0068cca0 393 }
WiredHome 83:7bad0068cca0 394 }
WiredHome 83:7bad0068cca0 395
WiredHome 83:7bad0068cca0 396 TouchCode_t RA8875::TouchPanelA2DRaw(int *x, int *y)
WiredHome 83:7bad0068cca0 397 {
WiredHome 154:ad2450fc3dc3 398 INFO("A2Raw");
WiredHome 83:7bad0068cca0 399 if( (ReadCommand(INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register INTC2
WiredHome 154:ad2450fc3dc3 400 INFO("Int pending");
WiredHome 83:7bad0068cca0 401 touchTimer.reset();
WiredHome 83:7bad0068cca0 402 *y = ReadCommand(TPYH) << 2 | ( (ReadCommand(TPXYL) & 0xC) >> 2 ); // D[9:2] from reg TPYH, D[1:0] from reg TPXYL[3:2]
WiredHome 83:7bad0068cca0 403 *x = ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3) ); // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0]
WiredHome 154:ad2450fc3dc3 404 INFO("(x,y) = (%d,%d)", x, y);
WiredHome 83:7bad0068cca0 405 WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear that TP interrupt flag
WiredHome 83:7bad0068cca0 406 touchState = touch;
WiredHome 83:7bad0068cca0 407 } else {
WiredHome 154:ad2450fc3dc3 408 INFO("no touch");
WiredHome 83:7bad0068cca0 409 touchState = no_touch;
WiredHome 83:7bad0068cca0 410 }
WiredHome 83:7bad0068cca0 411 return touchState;
WiredHome 83:7bad0068cca0 412 }
WiredHome 83:7bad0068cca0 413
WiredHome 83:7bad0068cca0 414 TouchCode_t RA8875::TouchPanelA2DFiltered(int *x, int *y)
WiredHome 83:7bad0068cca0 415 {
WiredHome 83:7bad0068cca0 416 static int xbuf[TPBUFSIZE], ybuf[TPBUFSIZE];
WiredHome 83:7bad0068cca0 417 static int lastX, lastY;
WiredHome 83:7bad0068cca0 418 int i, j;
WiredHome 83:7bad0068cca0 419 TouchCode_t ret = touchState;
WiredHome 78:faf49c381591 420
WiredHome 78:faf49c381591 421 if( (ReadCommand(INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register INTC2
WiredHome 83:7bad0068cca0 422 touchTimer.reset();
WiredHome 78:faf49c381591 423 // Get the next data samples
WiredHome 83:7bad0068cca0 424 ybuf[touchSample] = ReadCommand(TPYH) << 2 | ( (ReadCommand(TPXYL) & 0xC) >> 2 ); // D[9:2] from reg TPYH, D[1:0] from reg TPXYL[3:2]
WiredHome 83:7bad0068cca0 425 xbuf[touchSample] = ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3) ); // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0]
WiredHome 78:faf49c381591 426 // Check for a complete set
WiredHome 83:7bad0068cca0 427 if(++touchSample == TPBUFSIZE) {
WiredHome 78:faf49c381591 428 // Buffers are full, so process them using Finn's method described in Analog Dialogue No. 44, Feb 2010
WiredHome 78:faf49c381591 429 // This requires sorting the samples in order of size, then discarding the top 25% and
WiredHome 78:faf49c381591 430 // bottom 25% as noise spikes. Finally, the middle 50% of the values are averaged to
WiredHome 78:faf49c381591 431 // reduce Gaussian noise.
WiredHome 83:7bad0068cca0 432 #if 1
WiredHome 83:7bad0068cca0 433 InsertionSort(ybuf, TPBUFSIZE);
WiredHome 83:7bad0068cca0 434 InsertionSort(xbuf, TPBUFSIZE);
WiredHome 83:7bad0068cca0 435 #else
WiredHome 78:faf49c381591 436 // Sort the Y buffer using an Insertion Sort
WiredHome 78:faf49c381591 437 for(i = 1; i <= TPBUFSIZE; i++) {
WiredHome 78:faf49c381591 438 temp = ybuf[i];
WiredHome 78:faf49c381591 439 j = i;
WiredHome 78:faf49c381591 440 while( j && (ybuf[j-1] > temp) ) {
WiredHome 78:faf49c381591 441 ybuf[j] = ybuf[j-1];
WiredHome 78:faf49c381591 442 j = j-1;
WiredHome 78:faf49c381591 443 }
WiredHome 78:faf49c381591 444 ybuf[j] = temp;
WiredHome 78:faf49c381591 445 } // End of Y sort
WiredHome 78:faf49c381591 446 // Sort the X buffer the same way
WiredHome 78:faf49c381591 447 for(i = 1; i <= TPBUFSIZE; i++) {
WiredHome 78:faf49c381591 448 temp = xbuf[i];
WiredHome 78:faf49c381591 449 j = i;
WiredHome 78:faf49c381591 450 while( j && (xbuf[j-1] > temp) ) {
WiredHome 78:faf49c381591 451 xbuf[j] = xbuf[j-1];
WiredHome 78:faf49c381591 452 j = j-1;
WiredHome 78:faf49c381591 453 }
WiredHome 78:faf49c381591 454 xbuf[j] = temp;
WiredHome 78:faf49c381591 455 } // End of X sort
WiredHome 83:7bad0068cca0 456 #endif
WiredHome 78:faf49c381591 457 // Average the middle half of the Y values and report them
WiredHome 78:faf49c381591 458 j = 0;
WiredHome 78:faf49c381591 459 for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
WiredHome 78:faf49c381591 460 j += ybuf[i];
WiredHome 78:faf49c381591 461 }
WiredHome 83:7bad0068cca0 462 *y = lastY = j * (float)2/TPBUFSIZE; // This is the average
WiredHome 78:faf49c381591 463 // Average the middle half of the X values and report them
WiredHome 78:faf49c381591 464 j = 0;
WiredHome 78:faf49c381591 465 for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
WiredHome 78:faf49c381591 466 j += xbuf[i];
WiredHome 78:faf49c381591 467 }
WiredHome 83:7bad0068cca0 468 *x = lastX = j * (float)2/TPBUFSIZE; // This is the average
WiredHome 78:faf49c381591 469 // Tidy up and return
WiredHome 83:7bad0068cca0 470 if (touchState == touch || touchState == held)
WiredHome 83:7bad0068cca0 471 touchState = held;
WiredHome 83:7bad0068cca0 472 else
WiredHome 83:7bad0068cca0 473 touchState = touch;
WiredHome 83:7bad0068cca0 474 ret = touchState;
WiredHome 83:7bad0068cca0 475 touchSample = 0; // Ready to start on the next set of data samples
WiredHome 78:faf49c381591 476 } else {
WiredHome 78:faf49c381591 477 // Buffer not yet full, so do not return any results yet
WiredHome 83:7bad0068cca0 478 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 479 *x = lastX;
WiredHome 83:7bad0068cca0 480 *y = lastY;
WiredHome 83:7bad0068cca0 481 ret = touchState = held;
WiredHome 83:7bad0068cca0 482 }
WiredHome 78:faf49c381591 483 }
WiredHome 78:faf49c381591 484 WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear that TP interrupt flag
WiredHome 78:faf49c381591 485 } // End of initial if -- data has been read and processed
WiredHome 83:7bad0068cca0 486 else {
WiredHome 83:7bad0068cca0 487 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 488 *x = lastX;
WiredHome 83:7bad0068cca0 489 *y = lastY;
WiredHome 83:7bad0068cca0 490 ret = touchState = held;
WiredHome 83:7bad0068cca0 491 } else if (touchState == release) {
WiredHome 83:7bad0068cca0 492 *x = lastX;
WiredHome 83:7bad0068cca0 493 *y = lastY;
WiredHome 83:7bad0068cca0 494 ret = release;
WiredHome 83:7bad0068cca0 495 touchState = no_touch;
WiredHome 83:7bad0068cca0 496 }
WiredHome 83:7bad0068cca0 497 }
WiredHome 83:7bad0068cca0 498 return ret;
WiredHome 78:faf49c381591 499 }
WiredHome 78:faf49c381591 500
WiredHome 78:faf49c381591 501 /* The following section is derived from Carlos E. Vidales.
WiredHome 78:faf49c381591 502 *
WiredHome 78:faf49c381591 503 * Copyright (c) 2001, Carlos E. Vidales. All rights reserved.
WiredHome 78:faf49c381591 504 *
WiredHome 124:1690a7ae871c 505 * This sample program was written and put in the public domain
WiredHome 124:1690a7ae871c 506 * by Carlos E. Vidales. The program is provided "as is"
WiredHome 78:faf49c381591 507 * without warranty of any kind, either expressed or implied.
WiredHome 78:faf49c381591 508 * If you choose to use the program within your own products
WiredHome 78:faf49c381591 509 * you do so at your own risk, and assume the responsibility
WiredHome 78:faf49c381591 510 * for servicing, repairing or correcting the program should
WiredHome 78:faf49c381591 511 * it prove defective in any manner.
WiredHome 124:1690a7ae871c 512 * You may copy and distribute the program's source code in any
WiredHome 78:faf49c381591 513 * medium, provided that you also include in each copy an
WiredHome 78:faf49c381591 514 * appropriate copyright notice and disclaimer of warranty.
WiredHome 78:faf49c381591 515 * You may also modify this program and distribute copies of
WiredHome 124:1690a7ae871c 516 * it provided that you include prominent notices stating
WiredHome 78:faf49c381591 517 * that you changed the file(s) and the date of any change,
WiredHome 124:1690a7ae871c 518 * and that you do not charge any royalties or licenses for
WiredHome 78:faf49c381591 519 * its use.
WiredHome 124:1690a7ae871c 520 *
WiredHome 124:1690a7ae871c 521 * This file contains functions that implement calculations
WiredHome 78:faf49c381591 522 * necessary to obtain calibration factors for a touch screen
WiredHome 124:1690a7ae871c 523 * that suffers from multiple distortion effects: namely,
WiredHome 78:faf49c381591 524 * translation, scaling and rotation.
WiredHome 78:faf49c381591 525 *
WiredHome 124:1690a7ae871c 526 * The following set of equations represent a valid display
WiredHome 78:faf49c381591 527 * point given a corresponding set of touch screen points:
WiredHome 78:faf49c381591 528 *
WiredHome 78:faf49c381591 529 * /- -\
WiredHome 78:faf49c381591 530 * /- -\ /- -\ | |
WiredHome 78:faf49c381591 531 * | | | | | Xs |
WiredHome 78:faf49c381591 532 * | Xd | | A B C | | |
WiredHome 78:faf49c381591 533 * | | = | | * | Ys |
WiredHome 78:faf49c381591 534 * | Yd | | D E F | | |
WiredHome 78:faf49c381591 535 * | | | | | 1 |
WiredHome 78:faf49c381591 536 * \- -/ \- -/ | |
WiredHome 78:faf49c381591 537 * \- -/
WiredHome 78:faf49c381591 538 * where:
WiredHome 124:1690a7ae871c 539 * (Xd,Yd) represents the desired display point
WiredHome 78:faf49c381591 540 * coordinates,
WiredHome 78:faf49c381591 541 * (Xs,Ys) represents the available touch screen
WiredHome 78:faf49c381591 542 * coordinates, and the matrix
WiredHome 78:faf49c381591 543 * /- -\
WiredHome 78:faf49c381591 544 * |A,B,C|
WiredHome 78:faf49c381591 545 * |D,E,F| represents the factors used to translate
WiredHome 78:faf49c381591 546 * \- -/ the available touch screen point values
WiredHome 124:1690a7ae871c 547 * into the corresponding display
WiredHome 78:faf49c381591 548 * coordinates.
WiredHome 124:1690a7ae871c 549 * Note that for practical considerations, the utilities
WiredHome 78:faf49c381591 550 * within this file do not use the matrix coefficients as
WiredHome 124:1690a7ae871c 551 * defined above, but instead use the following
WiredHome 78:faf49c381591 552 * equivalents, since floating point math is not used:
WiredHome 124:1690a7ae871c 553 * A = An/Divider
WiredHome 124:1690a7ae871c 554 * B = Bn/Divider
WiredHome 124:1690a7ae871c 555 * C = Cn/Divider
WiredHome 124:1690a7ae871c 556 * D = Dn/Divider
WiredHome 124:1690a7ae871c 557 * E = En/Divider
WiredHome 124:1690a7ae871c 558 * F = Fn/Divider
WiredHome 78:faf49c381591 559 * The functions provided within this file are:
WiredHome 78:faf49c381591 560 * setCalibrationMatrix() - calculates the set of factors
WiredHome 78:faf49c381591 561 * in the above equation, given
WiredHome 78:faf49c381591 562 * three sets of test points.
WiredHome 78:faf49c381591 563 * getDisplayPoint() - returns the actual display
WiredHome 78:faf49c381591 564 * coordinates, given a set of
WiredHome 78:faf49c381591 565 * touch screen coordinates.
WiredHome 78:faf49c381591 566 * translateRawScreenCoordinates() - helper function to transform
WiredHome 78:faf49c381591 567 * raw screen points into values
WiredHome 78:faf49c381591 568 * scaled to the desired display
WiredHome 78:faf49c381591 569 * resolution.
WiredHome 78:faf49c381591 570 */
WiredHome 78:faf49c381591 571
WiredHome 78:faf49c381591 572 /**********************************************************************
WiredHome 78:faf49c381591 573 *
WiredHome 78:faf49c381591 574 * Function: setCalibrationMatrix()
WiredHome 78:faf49c381591 575 *
WiredHome 78:faf49c381591 576 * Description: Calling this function with valid input data
WiredHome 124:1690a7ae871c 577 * in the display and screen input arguments
WiredHome 78:faf49c381591 578 * causes the calibration factors between the
WiredHome 78:faf49c381591 579 * screen and display points to be calculated,
WiredHome 124:1690a7ae871c 580 * and the output argument - matrixPtr - to be
WiredHome 78:faf49c381591 581 * populated.
WiredHome 78:faf49c381591 582 *
WiredHome 78:faf49c381591 583 * This function needs to be called only when new
WiredHome 78:faf49c381591 584 * calibration factors are desired.
WiredHome 124:1690a7ae871c 585 *
WiredHome 124:1690a7ae871c 586 *
WiredHome 124:1690a7ae871c 587 * Argument(s): displayPtr (input) - Pointer to an array of three
WiredHome 78:faf49c381591 588 * sample, reference points.
WiredHome 124:1690a7ae871c 589 * screenPtr (input) - Pointer to the array of touch
WiredHome 124:1690a7ae871c 590 * screen points corresponding
WiredHome 78:faf49c381591 591 * to the reference display points.
WiredHome 124:1690a7ae871c 592 * matrixPtr (output) - Pointer to the calibration
WiredHome 124:1690a7ae871c 593 * matrix computed for the set
WiredHome 78:faf49c381591 594 * of points being provided.
WiredHome 78:faf49c381591 595 *
WiredHome 78:faf49c381591 596 *
WiredHome 78:faf49c381591 597 * From the article text, recall that the matrix coefficients are
WiredHome 78:faf49c381591 598 * resolved to be the following:
WiredHome 78:faf49c381591 599 *
WiredHome 78:faf49c381591 600 *
WiredHome 78:faf49c381591 601 * Divider = (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2)
WiredHome 78:faf49c381591 602 *
WiredHome 78:faf49c381591 603 *
WiredHome 78:faf49c381591 604 *
WiredHome 78:faf49c381591 605 * (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2)
WiredHome 78:faf49c381591 606 * A = ---------------------------------------------------
WiredHome 78:faf49c381591 607 * Divider
WiredHome 78:faf49c381591 608 *
WiredHome 78:faf49c381591 609 *
WiredHome 78:faf49c381591 610 * (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2)
WiredHome 78:faf49c381591 611 * B = ---------------------------------------------------
WiredHome 78:faf49c381591 612 * Divider
WiredHome 78:faf49c381591 613 *
WiredHome 78:faf49c381591 614 *
WiredHome 124:1690a7ae871c 615 * Ys0*(Xs2*Xd1 - Xs1*Xd2) +
WiredHome 124:1690a7ae871c 616 * Ys1*(Xs0*Xd2 - Xs2*Xd0) +
WiredHome 78:faf49c381591 617 * Ys2*(Xs1*Xd0 - Xs0*Xd1)
WiredHome 78:faf49c381591 618 * C = ---------------------------------------------------
WiredHome 78:faf49c381591 619 * Divider
WiredHome 78:faf49c381591 620 *
WiredHome 78:faf49c381591 621 *
WiredHome 78:faf49c381591 622 * (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2)
WiredHome 78:faf49c381591 623 * D = ---------------------------------------------------
WiredHome 78:faf49c381591 624 * Divider
WiredHome 78:faf49c381591 625 *
WiredHome 78:faf49c381591 626 *
WiredHome 78:faf49c381591 627 * (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2)
WiredHome 78:faf49c381591 628 * E = ---------------------------------------------------
WiredHome 78:faf49c381591 629 * Divider
WiredHome 78:faf49c381591 630 *
WiredHome 78:faf49c381591 631 *
WiredHome 124:1690a7ae871c 632 * Ys0*(Xs2*Yd1 - Xs1*Yd2) +
WiredHome 124:1690a7ae871c 633 * Ys1*(Xs0*Yd2 - Xs2*Yd0) +
WiredHome 78:faf49c381591 634 * Ys2*(Xs1*Yd0 - Xs0*Yd1)
WiredHome 78:faf49c381591 635 * F = ---------------------------------------------------
WiredHome 78:faf49c381591 636 * Divider
WiredHome 78:faf49c381591 637 *
WiredHome 78:faf49c381591 638 *
WiredHome 124:1690a7ae871c 639 * Return: OK - the calibration matrix was correctly
WiredHome 124:1690a7ae871c 640 * calculated and its value is in the
WiredHome 78:faf49c381591 641 * output argument.
WiredHome 124:1690a7ae871c 642 * NOT_OK - an error was detected and the
WiredHome 78:faf49c381591 643 * function failed to return a valid
WiredHome 78:faf49c381591 644 * set of matrix values.
WiredHome 78:faf49c381591 645 * The only time this sample code returns
WiredHome 78:faf49c381591 646 * NOT_OK is when Divider == 0
WiredHome 78:faf49c381591 647 *
WiredHome 78:faf49c381591 648 *
WiredHome 78:faf49c381591 649 *
WiredHome 78:faf49c381591 650 * NOTE! NOTE! NOTE!
WiredHome 78:faf49c381591 651 *
WiredHome 78:faf49c381591 652 * setCalibrationMatrix() and getDisplayPoint() will do fine
WiredHome 124:1690a7ae871c 653 * for you as they are, provided that your digitizer
WiredHome 78:faf49c381591 654 * resolution does not exceed 10 bits (1024 values). Higher
WiredHome 78:faf49c381591 655 * resolutions may cause the integer operations to overflow
WiredHome 124:1690a7ae871c 656 * and return incorrect values. If you wish to use these
WiredHome 124:1690a7ae871c 657 * functions with digitizer resolutions of 12 bits (4096
WiredHome 124:1690a7ae871c 658 * values) you will either have to a) use 64-bit signed
WiredHome 124:1690a7ae871c 659 * integer variables and math, or b) judiciously modify the
WiredHome 124:1690a7ae871c 660 * operations to scale results by a factor of 2 or even 4.
WiredHome 78:faf49c381591 661 *
WiredHome 78:faf49c381591 662 */
WiredHome 81:01da2e34283d 663 RetCode_t RA8875::TouchPanelComputeCalibration(point_t * displayPtr, point_t * screenPtr, tpMatrix_t * matrixPtr)
WiredHome 78:faf49c381591 664 {
WiredHome 78:faf49c381591 665 RetCode_t retValue = noerror;
WiredHome 78:faf49c381591 666
WiredHome 78:faf49c381591 667 tpMatrix.Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 668 ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 669
WiredHome 78:faf49c381591 670 if( tpMatrix.Divider == 0 ) {
WiredHome 78:faf49c381591 671 retValue = bad_parameter;
WiredHome 78:faf49c381591 672 } else {
WiredHome 78:faf49c381591 673 tpMatrix.An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 674 ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 675
WiredHome 78:faf49c381591 676 tpMatrix.Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
WiredHome 78:faf49c381591 677 ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
WiredHome 78:faf49c381591 678
WiredHome 78:faf49c381591 679 tpMatrix.Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
WiredHome 78:faf49c381591 680 (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
WiredHome 78:faf49c381591 681 (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
WiredHome 78:faf49c381591 682
WiredHome 78:faf49c381591 683 tpMatrix.Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 684 ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 685
WiredHome 78:faf49c381591 686 tpMatrix.En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
WiredHome 78:faf49c381591 687 ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
WiredHome 78:faf49c381591 688
WiredHome 78:faf49c381591 689 tpMatrix.Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
WiredHome 78:faf49c381591 690 (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
WiredHome 78:faf49c381591 691 (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
WiredHome 83:7bad0068cca0 692 touchState = no_touch;
WiredHome 78:faf49c381591 693 if (matrixPtr)
WiredHome 78:faf49c381591 694 memcpy(matrixPtr, &tpMatrix, sizeof(tpMatrix_t));
WiredHome 78:faf49c381591 695 }
WiredHome 78:faf49c381591 696 return( retValue ) ;
WiredHome 78:faf49c381591 697 }
WiredHome 78:faf49c381591 698
WiredHome 124:1690a7ae871c 699 ////////////////// Capacitive Touch Panel
WiredHome 124:1690a7ae871c 700
WiredHome 124:1690a7ae871c 701 uint8_t RA8875::readRegister8(uint8_t reg) {
WiredHome 124:1690a7ae871c 702 char val;
WiredHome 124:1690a7ae871c 703
WiredHome 155:b3f225ae572c 704 //INFO("readRegister8(%02X) @ %X", reg, m_i2c);
WiredHome 124:1690a7ae871c 705 m_i2c->write(m_addr, (const char *)&reg, 1);
WiredHome 155:b3f225ae572c 706 //INFO(" write(%X, ...)", m_addr);
WiredHome 124:1690a7ae871c 707 m_i2c->read(m_addr, &val, 1);
WiredHome 124:1690a7ae871c 708 return (uint8_t)val;
WiredHome 124:1690a7ae871c 709 }
WiredHome 124:1690a7ae871c 710
WiredHome 124:1690a7ae871c 711 void RA8875::writeRegister8(uint8_t reg, uint8_t val) {
WiredHome 124:1690a7ae871c 712 char data[2];
WiredHome 124:1690a7ae871c 713
WiredHome 124:1690a7ae871c 714 data[0] = (char)reg;
WiredHome 124:1690a7ae871c 715 data[1] = (char)val;
WiredHome 124:1690a7ae871c 716 m_i2c->write((int)FT5206_I2C_ADDRESS, data, 2);
WiredHome 124:1690a7ae871c 717 }
WiredHome 124:1690a7ae871c 718
WiredHome 124:1690a7ae871c 719
WiredHome 124:1690a7ae871c 720 // Interrupt for touch detection
WiredHome 124:1690a7ae871c 721 void RA8875::TouchPanelISR(void)
WiredHome 124:1690a7ae871c 722 {
WiredHome 155:b3f225ae572c 723 //INFO("TouchPanelISR()");
WiredHome 155:b3f225ae572c 724 if (getTouchPositions())
WiredHome 155:b3f225ae572c 725 panelTouched = true;
WiredHome 155:b3f225ae572c 726 //INFO(" return");
WiredHome 124:1690a7ae871c 727 }
WiredHome 124:1690a7ae871c 728
WiredHome 124:1690a7ae871c 729 uint8_t RA8875::getTouchPositions(void) {
WiredHome 124:1690a7ae871c 730 uint8_t valXH;
WiredHome 124:1690a7ae871c 731 uint8_t valYH;
WiredHome 124:1690a7ae871c 732
WiredHome 155:b3f225ae572c 733 //INFO("getTouchPositions()");
WiredHome 124:1690a7ae871c 734 numberOfTouchPoints = readRegister8(FT5206_TD_STATUS) & 0xF;
WiredHome 155:b3f225ae572c 735 //INFO(" numOfTouchPoints %d", numberOfTouchPoints);
WiredHome 124:1690a7ae871c 736 gesture = readRegister8(FT5206_GEST_ID);
WiredHome 155:b3f225ae572c 737 //INFO(" gesture %d", gesture);
WiredHome 124:1690a7ae871c 738
WiredHome 124:1690a7ae871c 739 // If the switch statement was based only on numberOfTouchPoints, it would not
WiredHome 124:1690a7ae871c 740 // be able to generate notification for 'release' events (as it is no longer touched).
WiredHome 124:1690a7ae871c 741 // Therefore, forcing a 5, and it intentially falls through each lower case.
WiredHome 124:1690a7ae871c 742 switch (5) { // numberOfTouchPoints
WiredHome 124:1690a7ae871c 743 case 5:
WiredHome 124:1690a7ae871c 744 valXH = readRegister8(FT5206_TOUCH5_XH);
WiredHome 124:1690a7ae871c 745 valYH = readRegister8(FT5206_TOUCH5_YH);
WiredHome 124:1690a7ae871c 746 touchInfo[4].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 747 touchInfo[4].touchID = (valYH >> 4);
WiredHome 124:1690a7ae871c 748 touchInfo[4].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH5_XL);
WiredHome 124:1690a7ae871c 749 touchInfo[4].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH5_YL);
WiredHome 155:b3f225ae572c 750 if (touchInfo[4].touchCode)
WiredHome 155:b3f225ae572c 751 INFO(" Touch[%d] code %d, id %2d, (%4d,%4d)",
WiredHome 155:b3f225ae572c 752 5,
WiredHome 155:b3f225ae572c 753 touchInfo[4].touchCode,
WiredHome 155:b3f225ae572c 754 touchInfo[4].touchID,
WiredHome 155:b3f225ae572c 755 touchInfo[4].coordinates.x,
WiredHome 155:b3f225ae572c 756 touchInfo[4].coordinates.y);
WiredHome 124:1690a7ae871c 757 case 4:
WiredHome 124:1690a7ae871c 758 valXH = readRegister8(FT5206_TOUCH4_XH);
WiredHome 124:1690a7ae871c 759 valYH = readRegister8(FT5206_TOUCH4_YH);
WiredHome 124:1690a7ae871c 760 touchInfo[3].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 761 touchInfo[3].touchID = (valYH >> 4);
WiredHome 155:b3f225ae572c 762 touchInfo[3].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH4_XL);
WiredHome 124:1690a7ae871c 763 touchInfo[3].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH4_YL);
WiredHome 155:b3f225ae572c 764 if (touchInfo[3].touchCode)
WiredHome 155:b3f225ae572c 765 INFO(" Touch[%d] code %d, id %2d, (%4d,%4d)",
WiredHome 155:b3f225ae572c 766 4,
WiredHome 155:b3f225ae572c 767 touchInfo[3].touchCode,
WiredHome 155:b3f225ae572c 768 touchInfo[3].touchID,
WiredHome 155:b3f225ae572c 769 touchInfo[3].coordinates.x,
WiredHome 155:b3f225ae572c 770 touchInfo[3].coordinates.y);
WiredHome 124:1690a7ae871c 771 case 3:
WiredHome 124:1690a7ae871c 772 valXH = readRegister8(FT5206_TOUCH3_XH);
WiredHome 124:1690a7ae871c 773 valYH = readRegister8(FT5206_TOUCH3_YH);
WiredHome 124:1690a7ae871c 774 touchInfo[2].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 775 touchInfo[2].touchID = (valYH >> 4);
WiredHome 155:b3f225ae572c 776 touchInfo[2].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH3_XL);
WiredHome 124:1690a7ae871c 777 touchInfo[2].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH3_YL);
WiredHome 155:b3f225ae572c 778 if (touchInfo[2].touchCode)
WiredHome 155:b3f225ae572c 779 INFO(" Touch[%d] code %d, id %2d, (%4d,%4d)",
WiredHome 155:b3f225ae572c 780 3,
WiredHome 155:b3f225ae572c 781 touchInfo[2].touchCode,
WiredHome 155:b3f225ae572c 782 touchInfo[2].touchID,
WiredHome 155:b3f225ae572c 783 touchInfo[2].coordinates.x,
WiredHome 155:b3f225ae572c 784 touchInfo[2].coordinates.y);
WiredHome 124:1690a7ae871c 785 case 2:
WiredHome 124:1690a7ae871c 786 valXH = readRegister8(FT5206_TOUCH2_XH);
WiredHome 124:1690a7ae871c 787 valYH = readRegister8(FT5206_TOUCH2_YH);
WiredHome 124:1690a7ae871c 788 touchInfo[1].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 789 touchInfo[1].touchID = (valYH >> 4);
WiredHome 155:b3f225ae572c 790 touchInfo[1].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH2_XL);
WiredHome 155:b3f225ae572c 791 touchInfo[1].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH2_YL);
WiredHome 155:b3f225ae572c 792 if (touchInfo[1].touchCode)
WiredHome 155:b3f225ae572c 793 INFO(" Touch[%d] code %d, id %2d, (%4d,%4d)",
WiredHome 155:b3f225ae572c 794 2,
WiredHome 155:b3f225ae572c 795 touchInfo[1].touchCode,
WiredHome 155:b3f225ae572c 796 touchInfo[1].touchID,
WiredHome 155:b3f225ae572c 797 touchInfo[1].coordinates.x,
WiredHome 155:b3f225ae572c 798 touchInfo[1].coordinates.y);
WiredHome 124:1690a7ae871c 799 case 1:
WiredHome 124:1690a7ae871c 800 valXH = readRegister8(FT5206_TOUCH1_XH);
WiredHome 124:1690a7ae871c 801 valYH = readRegister8(FT5206_TOUCH1_YH);
WiredHome 124:1690a7ae871c 802 touchInfo[0].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 803 touchInfo[0].touchID = (valYH >> 4);
WiredHome 155:b3f225ae572c 804 touchInfo[0].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH1_XL);
WiredHome 124:1690a7ae871c 805 touchInfo[0].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH1_YL);
WiredHome 155:b3f225ae572c 806 if (touchInfo[0].touchCode)
WiredHome 155:b3f225ae572c 807 INFO(" Touch[%d] code %d, id %2d, (%4d,%4d)",
WiredHome 155:b3f225ae572c 808 1,
WiredHome 155:b3f225ae572c 809 touchInfo[0].touchCode,
WiredHome 155:b3f225ae572c 810 touchInfo[0].touchID,
WiredHome 155:b3f225ae572c 811 touchInfo[0].coordinates.x,
WiredHome 155:b3f225ae572c 812 touchInfo[0].coordinates.y);
WiredHome 124:1690a7ae871c 813 break;
WiredHome 124:1690a7ae871c 814 default:
WiredHome 124:1690a7ae871c 815 break;
WiredHome 124:1690a7ae871c 816 }
WiredHome 124:1690a7ae871c 817 return numberOfTouchPoints;
WiredHome 124:1690a7ae871c 818 }
WiredHome 124:1690a7ae871c 819
WiredHome 78:faf49c381591 820 // #### end of touch panel code additions