Library to control a Graphics TFT connected to 4-wire SPI - revised for the Raio RA8875 Display Controller.

Dependents:   FRDM_RA8875_mPaint RA8875_Demo RA8875_KeyPadDemo SignalGenerator ... more

Fork of SPI_TFT by Peter Drescher

See Components - RA8875 Based Display

Enhanced touch-screen support - where it previous supported both the Resistive Touch and Capacitive Touch based on the FT5206 Touch Controller, now it also has support for the GSL1680 Touch Controller.

Offline Help Manual (Windows chm)

/media/uploads/WiredHome/ra8875.zip.bin (download, rename to .zip and unzip)

Committer:
WiredHome
Date:
Sun Feb 24 00:40:00 2019 +0000
Revision:
165:695c24cc5197
Parent:
157:1565f38ca44b
Child:
166:53fd4a876dac
Initial refactoring to add support for another Cap Sense touch controller - based on the GSL1680.

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