KSM edits to RA8875

Dependents:   Liz_Test_Code

Committer:
WiredHome
Date:
Fri Aug 17 01:29:06 2018 +0000
Revision:
154:ad2450fc3dc3
Parent:
150:35a4db3081c1
Child:
155:b3f225ae572c
Documentation updates for using resistive touch.

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