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:
Fri Mar 01 19:34:32 2019 +0000
Revision:
169:1c1a495983bc
Parent:
168:37a0c4d8791c
Child:
171:f92c0f1f6db4
Additional debug and configuration for the GSL1680 Touch Controller.

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 168:37a0c4d8791c 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 169:1c1a495983bc 56 r = GSL1680_Init();
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 167:8aa3fb2a5a31 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 166:53fd4a876dac 137 return GSL1680_TOUCH_POINTS; // based on TP_GSL1680 firmware
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 166:53fd4a876dac 348 uint8_t RA8875::TouchID(uint8_t channel)
WiredHome 166:53fd4a876dac 349 {
WiredHome 166:53fd4a876dac 350 if (channel >= TouchChannels())
WiredHome 166:53fd4a876dac 351 channel = 0;
WiredHome 166:53fd4a876dac 352 return touchInfo[channel].touchID;
WiredHome 166:53fd4a876dac 353 }
WiredHome 166:53fd4a876dac 354
WiredHome 166:53fd4a876dac 355 TouchCode_t RA8875::TouchCode(uint8_t channel)
WiredHome 166:53fd4a876dac 356 {
WiredHome 166:53fd4a876dac 357 if (channel >= TouchChannels())
WiredHome 166:53fd4a876dac 358 channel = 0;
WiredHome 166:53fd4a876dac 359 return touchInfo[channel].touchCode;
WiredHome 166:53fd4a876dac 360 }
WiredHome 166:53fd4a876dac 361
WiredHome 166:53fd4a876dac 362 point_t RA8875::TouchCoordinates(uint8_t channel)
WiredHome 166:53fd4a876dac 363 {
WiredHome 166:53fd4a876dac 364 if (channel >= TouchChannels())
WiredHome 166:53fd4a876dac 365 channel = 0;
WiredHome 166:53fd4a876dac 366 return touchInfo[channel].coordinates;
WiredHome 166:53fd4a876dac 367 }
WiredHome 81:01da2e34283d 368
WiredHome 85:022bba13c5c4 369 TouchCode_t RA8875::TouchPanelGet(point_t * TouchPoint)
WiredHome 85:022bba13c5c4 370 {
WiredHome 123:2f45e80fec5f 371 TouchCode_t t = no_touch;
WiredHome 124:1690a7ae871c 372
WiredHome 123:2f45e80fec5f 373 while (true) {
WiredHome 85:022bba13c5c4 374 t = TouchPanelReadable(TouchPoint);
WiredHome 123:2f45e80fec5f 375 if (t != no_touch)
WiredHome 123:2f45e80fec5f 376 break;
WiredHome 123:2f45e80fec5f 377 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 378 if (external_abort == (*idle_callback)(touch_wait, 0)) {
WiredHome 123:2f45e80fec5f 379 return no_touch;
WiredHome 123:2f45e80fec5f 380 }
WiredHome 123:2f45e80fec5f 381 }
WiredHome 123:2f45e80fec5f 382 }
WiredHome 85:022bba13c5c4 383 return t;
WiredHome 85:022bba13c5c4 384 }
WiredHome 85:022bba13c5c4 385
WiredHome 123:2f45e80fec5f 386 // Below here are primarily "helper" functions. While many are accessible
WiredHome 123:2f45e80fec5f 387 // to the user code, they usually don't need to be called.
WiredHome 123:2f45e80fec5f 388
WiredHome 81:01da2e34283d 389 RetCode_t RA8875::TouchPanelSetMatrix(tpMatrix_t * matrixPtr)
WiredHome 81:01da2e34283d 390 {
WiredHome 81:01da2e34283d 391 if (matrixPtr == NULL || matrixPtr->Divider == 0)
WiredHome 81:01da2e34283d 392 return bad_parameter;
WiredHome 81:01da2e34283d 393 memcpy(&tpMatrix, matrixPtr, sizeof(tpMatrix_t));
WiredHome 83:7bad0068cca0 394 touchState = no_touch;
WiredHome 81:01da2e34283d 395 return noerror;
WiredHome 81:01da2e34283d 396 }
WiredHome 81:01da2e34283d 397
WiredHome 157:1565f38ca44b 398 const tpMatrix_t * RA8875::TouchPanelGetMatrix()
WiredHome 157:1565f38ca44b 399 {
WiredHome 157:1565f38ca44b 400 return &tpMatrix;
WiredHome 157:1565f38ca44b 401 }
WiredHome 157:1565f38ca44b 402
WiredHome 157:1565f38ca44b 403
WiredHome 83:7bad0068cca0 404 static void InsertionSort(int * buf, int bufsize)
WiredHome 83:7bad0068cca0 405 {
WiredHome 83:7bad0068cca0 406 int i, j;
WiredHome 83:7bad0068cca0 407 int temp;
WiredHome 124:1690a7ae871c 408
WiredHome 108:7415c405ee08 409 for(i = 1; i < bufsize; i++) {
WiredHome 83:7bad0068cca0 410 temp = buf[i];
WiredHome 83:7bad0068cca0 411 j = i;
WiredHome 83:7bad0068cca0 412 while( j && (buf[j-1] > temp) ) {
WiredHome 83:7bad0068cca0 413 buf[j] = buf[j-1];
WiredHome 83:7bad0068cca0 414 j = j-1;
WiredHome 83:7bad0068cca0 415 }
WiredHome 83:7bad0068cca0 416 buf[j] = temp;
WiredHome 83:7bad0068cca0 417 } // End of sort
WiredHome 83:7bad0068cca0 418 }
WiredHome 83:7bad0068cca0 419
WiredHome 83:7bad0068cca0 420
WiredHome 83:7bad0068cca0 421 void RA8875::_TouchTicker(void)
WiredHome 78:faf49c381591 422 {
WiredHome 155:b3f225ae572c 423 INFO("_TouchTicker()");
WiredHome 165:695c24cc5197 424 if (timeSinceTouch.read_us() > NOTOUCH_TIMEOUT_uS) {
WiredHome 83:7bad0068cca0 425 touchSample = 0;
WiredHome 83:7bad0068cca0 426 if (touchState == held)
WiredHome 83:7bad0068cca0 427 touchState = release;
WiredHome 83:7bad0068cca0 428 else
WiredHome 83:7bad0068cca0 429 touchState = no_touch;
WiredHome 165:695c24cc5197 430 timeSinceTouch.reset();
WiredHome 83:7bad0068cca0 431 }
WiredHome 83:7bad0068cca0 432 }
WiredHome 83:7bad0068cca0 433
WiredHome 83:7bad0068cca0 434 TouchCode_t RA8875::TouchPanelA2DRaw(int *x, int *y)
WiredHome 83:7bad0068cca0 435 {
WiredHome 154:ad2450fc3dc3 436 INFO("A2Raw");
WiredHome 83:7bad0068cca0 437 if( (ReadCommand(INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register INTC2
WiredHome 154:ad2450fc3dc3 438 INFO("Int pending");
WiredHome 165:695c24cc5197 439 timeSinceTouch.reset();
WiredHome 83:7bad0068cca0 440 *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 441 *x = ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3) ); // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0]
WiredHome 154:ad2450fc3dc3 442 INFO("(x,y) = (%d,%d)", x, y);
WiredHome 83:7bad0068cca0 443 WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear that TP interrupt flag
WiredHome 83:7bad0068cca0 444 touchState = touch;
WiredHome 83:7bad0068cca0 445 } else {
WiredHome 154:ad2450fc3dc3 446 INFO("no touch");
WiredHome 83:7bad0068cca0 447 touchState = no_touch;
WiredHome 83:7bad0068cca0 448 }
WiredHome 83:7bad0068cca0 449 return touchState;
WiredHome 83:7bad0068cca0 450 }
WiredHome 83:7bad0068cca0 451
WiredHome 83:7bad0068cca0 452 TouchCode_t RA8875::TouchPanelA2DFiltered(int *x, int *y)
WiredHome 83:7bad0068cca0 453 {
WiredHome 83:7bad0068cca0 454 static int xbuf[TPBUFSIZE], ybuf[TPBUFSIZE];
WiredHome 83:7bad0068cca0 455 static int lastX, lastY;
WiredHome 83:7bad0068cca0 456 int i, j;
WiredHome 83:7bad0068cca0 457 TouchCode_t ret = touchState;
WiredHome 78:faf49c381591 458
WiredHome 78:faf49c381591 459 if( (ReadCommand(INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register INTC2
WiredHome 165:695c24cc5197 460 timeSinceTouch.reset();
WiredHome 78:faf49c381591 461 // Get the next data samples
WiredHome 83:7bad0068cca0 462 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 463 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 464 // Check for a complete set
WiredHome 83:7bad0068cca0 465 if(++touchSample == TPBUFSIZE) {
WiredHome 78:faf49c381591 466 // Buffers are full, so process them using Finn's method described in Analog Dialogue No. 44, Feb 2010
WiredHome 78:faf49c381591 467 // This requires sorting the samples in order of size, then discarding the top 25% and
WiredHome 78:faf49c381591 468 // bottom 25% as noise spikes. Finally, the middle 50% of the values are averaged to
WiredHome 78:faf49c381591 469 // reduce Gaussian noise.
WiredHome 83:7bad0068cca0 470 #if 1
WiredHome 83:7bad0068cca0 471 InsertionSort(ybuf, TPBUFSIZE);
WiredHome 83:7bad0068cca0 472 InsertionSort(xbuf, TPBUFSIZE);
WiredHome 83:7bad0068cca0 473 #else
WiredHome 78:faf49c381591 474 // Sort the Y buffer using an Insertion Sort
WiredHome 78:faf49c381591 475 for(i = 1; i <= TPBUFSIZE; i++) {
WiredHome 78:faf49c381591 476 temp = ybuf[i];
WiredHome 78:faf49c381591 477 j = i;
WiredHome 78:faf49c381591 478 while( j && (ybuf[j-1] > temp) ) {
WiredHome 78:faf49c381591 479 ybuf[j] = ybuf[j-1];
WiredHome 78:faf49c381591 480 j = j-1;
WiredHome 78:faf49c381591 481 }
WiredHome 78:faf49c381591 482 ybuf[j] = temp;
WiredHome 78:faf49c381591 483 } // End of Y sort
WiredHome 78:faf49c381591 484 // Sort the X buffer the same way
WiredHome 78:faf49c381591 485 for(i = 1; i <= TPBUFSIZE; i++) {
WiredHome 78:faf49c381591 486 temp = xbuf[i];
WiredHome 78:faf49c381591 487 j = i;
WiredHome 78:faf49c381591 488 while( j && (xbuf[j-1] > temp) ) {
WiredHome 78:faf49c381591 489 xbuf[j] = xbuf[j-1];
WiredHome 78:faf49c381591 490 j = j-1;
WiredHome 78:faf49c381591 491 }
WiredHome 78:faf49c381591 492 xbuf[j] = temp;
WiredHome 78:faf49c381591 493 } // End of X sort
WiredHome 83:7bad0068cca0 494 #endif
WiredHome 78:faf49c381591 495 // Average the middle half of the Y values and report them
WiredHome 78:faf49c381591 496 j = 0;
WiredHome 78:faf49c381591 497 for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
WiredHome 78:faf49c381591 498 j += ybuf[i];
WiredHome 78:faf49c381591 499 }
WiredHome 83:7bad0068cca0 500 *y = lastY = j * (float)2/TPBUFSIZE; // This is the average
WiredHome 78:faf49c381591 501 // Average the middle half of the X values and report them
WiredHome 78:faf49c381591 502 j = 0;
WiredHome 78:faf49c381591 503 for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
WiredHome 78:faf49c381591 504 j += xbuf[i];
WiredHome 78:faf49c381591 505 }
WiredHome 83:7bad0068cca0 506 *x = lastX = j * (float)2/TPBUFSIZE; // This is the average
WiredHome 78:faf49c381591 507 // Tidy up and return
WiredHome 83:7bad0068cca0 508 if (touchState == touch || touchState == held)
WiredHome 83:7bad0068cca0 509 touchState = held;
WiredHome 83:7bad0068cca0 510 else
WiredHome 83:7bad0068cca0 511 touchState = touch;
WiredHome 83:7bad0068cca0 512 ret = touchState;
WiredHome 83:7bad0068cca0 513 touchSample = 0; // Ready to start on the next set of data samples
WiredHome 78:faf49c381591 514 } else {
WiredHome 78:faf49c381591 515 // Buffer not yet full, so do not return any results yet
WiredHome 83:7bad0068cca0 516 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 517 *x = lastX;
WiredHome 83:7bad0068cca0 518 *y = lastY;
WiredHome 83:7bad0068cca0 519 ret = touchState = held;
WiredHome 83:7bad0068cca0 520 }
WiredHome 78:faf49c381591 521 }
WiredHome 78:faf49c381591 522 WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear that TP interrupt flag
WiredHome 78:faf49c381591 523 } // End of initial if -- data has been read and processed
WiredHome 83:7bad0068cca0 524 else {
WiredHome 83:7bad0068cca0 525 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 526 *x = lastX;
WiredHome 83:7bad0068cca0 527 *y = lastY;
WiredHome 83:7bad0068cca0 528 ret = touchState = held;
WiredHome 83:7bad0068cca0 529 } else if (touchState == release) {
WiredHome 83:7bad0068cca0 530 *x = lastX;
WiredHome 83:7bad0068cca0 531 *y = lastY;
WiredHome 83:7bad0068cca0 532 ret = release;
WiredHome 83:7bad0068cca0 533 touchState = no_touch;
WiredHome 83:7bad0068cca0 534 }
WiredHome 83:7bad0068cca0 535 }
WiredHome 83:7bad0068cca0 536 return ret;
WiredHome 78:faf49c381591 537 }
WiredHome 78:faf49c381591 538
WiredHome 167:8aa3fb2a5a31 539 /// The following section is derived from Carlos E. Vidales.
WiredHome 167:8aa3fb2a5a31 540 ///
WiredHome 167:8aa3fb2a5a31 541 /// @copyright &copy; 2001, Carlos E. Vidales. All rights reserved.
WiredHome 167:8aa3fb2a5a31 542 ///
WiredHome 167:8aa3fb2a5a31 543 /// This sample program was written and put in the public domain
WiredHome 167:8aa3fb2a5a31 544 /// by Carlos E. Vidales. The program is provided "as is"
WiredHome 167:8aa3fb2a5a31 545 /// without warranty of any kind, either expressed or implied.
WiredHome 167:8aa3fb2a5a31 546 /// If you choose to use the program within your own products
WiredHome 167:8aa3fb2a5a31 547 /// you do so at your own risk, and assume the responsibility
WiredHome 167:8aa3fb2a5a31 548 /// for servicing, repairing or correcting the program should
WiredHome 167:8aa3fb2a5a31 549 /// it prove defective in any manner.
WiredHome 167:8aa3fb2a5a31 550 /// You may copy and distribute the program's source code in any
WiredHome 167:8aa3fb2a5a31 551 /// medium, provided that you also include in each copy an
WiredHome 167:8aa3fb2a5a31 552 /// appropriate copyright notice and disclaimer of warranty.
WiredHome 167:8aa3fb2a5a31 553 /// You may also modify this program and distribute copies of
WiredHome 167:8aa3fb2a5a31 554 /// it provided that you include prominent notices stating
WiredHome 167:8aa3fb2a5a31 555 /// that you changed the file(s) and the date of any change,
WiredHome 167:8aa3fb2a5a31 556 /// and that you do not charge any royalties or licenses for
WiredHome 167:8aa3fb2a5a31 557 /// its use.
WiredHome 167:8aa3fb2a5a31 558 ///
WiredHome 167:8aa3fb2a5a31 559 /// This file contains functions that implement calculations
WiredHome 167:8aa3fb2a5a31 560 /// necessary to obtain calibration factors for a touch screen
WiredHome 167:8aa3fb2a5a31 561 /// that suffers from multiple distortion effects: namely,
WiredHome 167:8aa3fb2a5a31 562 /// translation, scaling and rotation.
WiredHome 167:8aa3fb2a5a31 563 ///
WiredHome 167:8aa3fb2a5a31 564 /// The following set of equations represent a valid display
WiredHome 167:8aa3fb2a5a31 565 /// point given a corresponding set of touch screen points:
WiredHome 167:8aa3fb2a5a31 566 ///
WiredHome 167:8aa3fb2a5a31 567 /// <pre>
WiredHome 167:8aa3fb2a5a31 568 /// /- -\
WiredHome 167:8aa3fb2a5a31 569 /// /- -\ /- -\ | |
WiredHome 167:8aa3fb2a5a31 570 /// | | | | | Xs |
WiredHome 167:8aa3fb2a5a31 571 /// | Xd | | A B C | | |
WiredHome 167:8aa3fb2a5a31 572 /// | | = | | * | Ys |
WiredHome 167:8aa3fb2a5a31 573 /// | Yd | | D E F | | |
WiredHome 167:8aa3fb2a5a31 574 /// | | | | | 1 |
WiredHome 167:8aa3fb2a5a31 575 /// \- -/ \- -/ | |
WiredHome 167:8aa3fb2a5a31 576 /// \- -/
WiredHome 167:8aa3fb2a5a31 577 /// where:
WiredHome 167:8aa3fb2a5a31 578 /// (Xd,Yd) represents the desired display point
WiredHome 167:8aa3fb2a5a31 579 /// coordinates,
WiredHome 167:8aa3fb2a5a31 580 /// (Xs,Ys) represents the available touch screen
WiredHome 167:8aa3fb2a5a31 581 /// coordinates, and the matrix
WiredHome 167:8aa3fb2a5a31 582 /// /- -\
WiredHome 167:8aa3fb2a5a31 583 /// |A,B,C|
WiredHome 167:8aa3fb2a5a31 584 /// |D,E,F| represents the factors used to translate
WiredHome 167:8aa3fb2a5a31 585 /// \- -/ the available touch screen point values
WiredHome 167:8aa3fb2a5a31 586 /// into the corresponding display
WiredHome 167:8aa3fb2a5a31 587 /// coordinates.
WiredHome 167:8aa3fb2a5a31 588 /// Note that for practical considerations, the utilities
WiredHome 167:8aa3fb2a5a31 589 /// within this file do not use the matrix coefficients as
WiredHome 167:8aa3fb2a5a31 590 /// defined above, but instead use the following
WiredHome 167:8aa3fb2a5a31 591 /// equivalents, since floating point math is not used:
WiredHome 167:8aa3fb2a5a31 592 /// A = An/Divider
WiredHome 167:8aa3fb2a5a31 593 /// B = Bn/Divider
WiredHome 167:8aa3fb2a5a31 594 /// C = Cn/Divider
WiredHome 167:8aa3fb2a5a31 595 /// D = Dn/Divider
WiredHome 167:8aa3fb2a5a31 596 /// E = En/Divider
WiredHome 167:8aa3fb2a5a31 597 /// F = Fn/Divider
WiredHome 167:8aa3fb2a5a31 598 /// The functions provided within this file are:
WiredHome 167:8aa3fb2a5a31 599 /// setCalibrationMatrix() - calculates the set of factors
WiredHome 167:8aa3fb2a5a31 600 /// in the above equation, given
WiredHome 167:8aa3fb2a5a31 601 /// three sets of test points.
WiredHome 167:8aa3fb2a5a31 602 /// getDisplayPoint() - returns the actual display
WiredHome 167:8aa3fb2a5a31 603 /// coordinates, given a set of
WiredHome 167:8aa3fb2a5a31 604 /// touch screen coordinates.
WiredHome 167:8aa3fb2a5a31 605 /// translateRawScreenCoordinates() - helper function to transform
WiredHome 167:8aa3fb2a5a31 606 /// raw screen points into values
WiredHome 167:8aa3fb2a5a31 607 /// scaled to the desired display
WiredHome 167:8aa3fb2a5a31 608 /// resolution.
WiredHome 167:8aa3fb2a5a31 609 ///
WiredHome 167:8aa3fb2a5a31 610 ///
WiredHome 167:8aa3fb2a5a31 611 /// Function: setCalibrationMatrix()
WiredHome 167:8aa3fb2a5a31 612 ///
WiredHome 167:8aa3fb2a5a31 613 /// Description: Calling this function with valid input data
WiredHome 167:8aa3fb2a5a31 614 /// in the display and screen input arguments
WiredHome 167:8aa3fb2a5a31 615 /// causes the calibration factors between the
WiredHome 167:8aa3fb2a5a31 616 /// screen and display points to be calculated,
WiredHome 167:8aa3fb2a5a31 617 /// and the output argument - matrixPtr - to be
WiredHome 167:8aa3fb2a5a31 618 /// populated.
WiredHome 167:8aa3fb2a5a31 619 ///
WiredHome 167:8aa3fb2a5a31 620 /// This function needs to be called only when new
WiredHome 167:8aa3fb2a5a31 621 /// calibration factors are desired.
WiredHome 167:8aa3fb2a5a31 622 ///
WiredHome 167:8aa3fb2a5a31 623 ///
WiredHome 167:8aa3fb2a5a31 624 /// Argument(s): displayPtr (input) - Pointer to an array of three
WiredHome 167:8aa3fb2a5a31 625 /// sample, reference points.
WiredHome 167:8aa3fb2a5a31 626 /// screenPtr (input) - Pointer to the array of touch
WiredHome 167:8aa3fb2a5a31 627 /// screen points corresponding
WiredHome 167:8aa3fb2a5a31 628 /// to the reference display points.
WiredHome 167:8aa3fb2a5a31 629 /// matrixPtr (output) - Pointer to the calibration
WiredHome 167:8aa3fb2a5a31 630 /// matrix computed for the set
WiredHome 167:8aa3fb2a5a31 631 /// of points being provided.
WiredHome 167:8aa3fb2a5a31 632 ///
WiredHome 167:8aa3fb2a5a31 633 ///
WiredHome 167:8aa3fb2a5a31 634 /// From the article text, recall that the matrix coefficients are
WiredHome 167:8aa3fb2a5a31 635 /// resolved to be the following:
WiredHome 167:8aa3fb2a5a31 636 ///
WiredHome 167:8aa3fb2a5a31 637 ///
WiredHome 167:8aa3fb2a5a31 638 /// Divider = (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2)
WiredHome 167:8aa3fb2a5a31 639 ///
WiredHome 167:8aa3fb2a5a31 640 ///
WiredHome 167:8aa3fb2a5a31 641 ///
WiredHome 167:8aa3fb2a5a31 642 /// (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2)
WiredHome 167:8aa3fb2a5a31 643 /// A = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 644 /// Divider
WiredHome 167:8aa3fb2a5a31 645 ///
WiredHome 167:8aa3fb2a5a31 646 ///
WiredHome 167:8aa3fb2a5a31 647 /// (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2)
WiredHome 167:8aa3fb2a5a31 648 /// B = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 649 /// Divider
WiredHome 167:8aa3fb2a5a31 650 ///
WiredHome 167:8aa3fb2a5a31 651 ///
WiredHome 167:8aa3fb2a5a31 652 /// Ys0*(Xs2*Xd1 - Xs1*Xd2) +
WiredHome 167:8aa3fb2a5a31 653 /// Ys1*(Xs0*Xd2 - Xs2*Xd0) +
WiredHome 167:8aa3fb2a5a31 654 /// Ys2*(Xs1*Xd0 - Xs0*Xd1)
WiredHome 167:8aa3fb2a5a31 655 /// C = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 656 /// Divider
WiredHome 167:8aa3fb2a5a31 657 ///
WiredHome 167:8aa3fb2a5a31 658 ///
WiredHome 167:8aa3fb2a5a31 659 /// (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2)
WiredHome 167:8aa3fb2a5a31 660 /// D = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 661 /// Divider
WiredHome 167:8aa3fb2a5a31 662 ///
WiredHome 167:8aa3fb2a5a31 663 ///
WiredHome 167:8aa3fb2a5a31 664 /// (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2)
WiredHome 167:8aa3fb2a5a31 665 /// E = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 666 /// Divider
WiredHome 167:8aa3fb2a5a31 667 ///
WiredHome 167:8aa3fb2a5a31 668 ///
WiredHome 167:8aa3fb2a5a31 669 /// Ys0*(Xs2*Yd1 - Xs1*Yd2) +
WiredHome 167:8aa3fb2a5a31 670 /// Ys1*(Xs0*Yd2 - Xs2*Yd0) +
WiredHome 167:8aa3fb2a5a31 671 /// Ys2*(Xs1*Yd0 - Xs0*Yd1)
WiredHome 167:8aa3fb2a5a31 672 /// F = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 673 /// Divider
WiredHome 167:8aa3fb2a5a31 674 ///
WiredHome 167:8aa3fb2a5a31 675 ///
WiredHome 167:8aa3fb2a5a31 676 /// Return: OK - the calibration matrix was correctly
WiredHome 167:8aa3fb2a5a31 677 /// calculated and its value is in the
WiredHome 167:8aa3fb2a5a31 678 /// output argument.
WiredHome 167:8aa3fb2a5a31 679 /// NOT_OK - an error was detected and the
WiredHome 167:8aa3fb2a5a31 680 /// function failed to return a valid
WiredHome 167:8aa3fb2a5a31 681 /// set of matrix values.
WiredHome 167:8aa3fb2a5a31 682 /// The only time this sample code returns
WiredHome 167:8aa3fb2a5a31 683 /// NOT_OK is when Divider == 0
WiredHome 167:8aa3fb2a5a31 684 ///
WiredHome 167:8aa3fb2a5a31 685 ///
WiredHome 167:8aa3fb2a5a31 686 ///
WiredHome 167:8aa3fb2a5a31 687 /// NOTE! NOTE! NOTE!
WiredHome 167:8aa3fb2a5a31 688 ///
WiredHome 167:8aa3fb2a5a31 689 /// setCalibrationMatrix() and getDisplayPoint() will do fine
WiredHome 167:8aa3fb2a5a31 690 /// for you as they are, provided that your digitizer
WiredHome 167:8aa3fb2a5a31 691 /// resolution does not exceed 10 bits (1024 values). Higher
WiredHome 167:8aa3fb2a5a31 692 /// resolutions may cause the integer operations to overflow
WiredHome 167:8aa3fb2a5a31 693 /// and return incorrect values. If you wish to use these
WiredHome 167:8aa3fb2a5a31 694 /// functions with digitizer resolutions of 12 bits (4096
WiredHome 167:8aa3fb2a5a31 695 /// values) you will either have to a) use 64-bit signed
WiredHome 167:8aa3fb2a5a31 696 /// integer variables and math, or b) judiciously modify the
WiredHome 167:8aa3fb2a5a31 697 /// operations to scale results by a factor of 2 or even 4.
WiredHome 167:8aa3fb2a5a31 698 ///
WiredHome 167:8aa3fb2a5a31 699 /// </pre>
WiredHome 167:8aa3fb2a5a31 700 ///
WiredHome 81:01da2e34283d 701 RetCode_t RA8875::TouchPanelComputeCalibration(point_t * displayPtr, point_t * screenPtr, tpMatrix_t * matrixPtr)
WiredHome 78:faf49c381591 702 {
WiredHome 78:faf49c381591 703 RetCode_t retValue = noerror;
WiredHome 78:faf49c381591 704
WiredHome 78:faf49c381591 705 tpMatrix.Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 706 ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 707
WiredHome 78:faf49c381591 708 if( tpMatrix.Divider == 0 ) {
WiredHome 78:faf49c381591 709 retValue = bad_parameter;
WiredHome 78:faf49c381591 710 } else {
WiredHome 78:faf49c381591 711 tpMatrix.An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 712 ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 713
WiredHome 78:faf49c381591 714 tpMatrix.Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
WiredHome 78:faf49c381591 715 ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
WiredHome 78:faf49c381591 716
WiredHome 78:faf49c381591 717 tpMatrix.Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
WiredHome 78:faf49c381591 718 (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
WiredHome 78:faf49c381591 719 (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
WiredHome 78:faf49c381591 720
WiredHome 78:faf49c381591 721 tpMatrix.Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 722 ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 723
WiredHome 78:faf49c381591 724 tpMatrix.En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
WiredHome 78:faf49c381591 725 ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
WiredHome 78:faf49c381591 726
WiredHome 78:faf49c381591 727 tpMatrix.Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
WiredHome 78:faf49c381591 728 (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
WiredHome 78:faf49c381591 729 (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
WiredHome 83:7bad0068cca0 730 touchState = no_touch;
WiredHome 78:faf49c381591 731 if (matrixPtr)
WiredHome 78:faf49c381591 732 memcpy(matrixPtr, &tpMatrix, sizeof(tpMatrix_t));
WiredHome 78:faf49c381591 733 }
WiredHome 78:faf49c381591 734 return( retValue ) ;
WiredHome 78:faf49c381591 735 }
WiredHome 78:faf49c381591 736
WiredHome 157:1565f38ca44b 737 void RA8875::ResTouchPanelCfg(const char * _tpFQFN, const char * _tpCalMessage)
WiredHome 157:1565f38ca44b 738 {
WiredHome 157:1565f38ca44b 739 tpFQFN = _tpFQFN;
WiredHome 157:1565f38ca44b 740 tpCalMessage = _tpCalMessage;
WiredHome 157:1565f38ca44b 741 }
WiredHome 157:1565f38ca44b 742
WiredHome 157:1565f38ca44b 743
WiredHome 157:1565f38ca44b 744 RetCode_t RA8875::_internal_ts_cal()
WiredHome 157:1565f38ca44b 745 {
WiredHome 157:1565f38ca44b 746 FILE * fh;
WiredHome 157:1565f38ca44b 747 tpMatrix_t matrix;
WiredHome 157:1565f38ca44b 748 RetCode_t r = noerror;
WiredHome 157:1565f38ca44b 749
WiredHome 157:1565f38ca44b 750 if (tpFQFN) {
WiredHome 157:1565f38ca44b 751 fh = fopen(tpFQFN, "rb");
WiredHome 157:1565f38ca44b 752 if (fh) {
WiredHome 157:1565f38ca44b 753 fread(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 157:1565f38ca44b 754 fclose(fh);
WiredHome 157:1565f38ca44b 755 TouchPanelSetMatrix(&matrix);
WiredHome 157:1565f38ca44b 756 } else {
WiredHome 157:1565f38ca44b 757 r = TouchPanelCalibrate(tpCalMessage, &matrix);
WiredHome 157:1565f38ca44b 758 if (r == noerror) {
WiredHome 157:1565f38ca44b 759 fh = fopen(tpFQFN, "wb");
WiredHome 157:1565f38ca44b 760 if (fh) {
WiredHome 157:1565f38ca44b 761 fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 157:1565f38ca44b 762 fclose(fh);
WiredHome 157:1565f38ca44b 763 INFO(" tp cal written to '%s'.", tpFQFN);
WiredHome 157:1565f38ca44b 764 } else {
WiredHome 157:1565f38ca44b 765 ERR(" couldn't open tpcal file '%s'.", tpFQFN);
WiredHome 157:1565f38ca44b 766 r = file_not_found;
WiredHome 157:1565f38ca44b 767 }
WiredHome 157:1565f38ca44b 768 } else {
WiredHome 157:1565f38ca44b 769 ERR("error return: %d", r);
WiredHome 157:1565f38ca44b 770 }
WiredHome 157:1565f38ca44b 771 HexDump("TPCal", (const uint8_t *)&matrix, sizeof(tpMatrix_t));
WiredHome 157:1565f38ca44b 772 cls();
WiredHome 157:1565f38ca44b 773 }
WiredHome 157:1565f38ca44b 774 }
WiredHome 157:1565f38ca44b 775 return r;
WiredHome 157:1565f38ca44b 776 }
WiredHome 157:1565f38ca44b 777
WiredHome 157:1565f38ca44b 778
WiredHome 157:1565f38ca44b 779
WiredHome 124:1690a7ae871c 780 ////////////////// Capacitive Touch Panel
WiredHome 124:1690a7ae871c 781
WiredHome 165:695c24cc5197 782 uint8_t RA8875::FT5206_ReadRegU8(uint8_t reg) {
WiredHome 124:1690a7ae871c 783 char val;
WiredHome 124:1690a7ae871c 784
WiredHome 124:1690a7ae871c 785 m_i2c->write(m_addr, (const char *)&reg, 1);
WiredHome 124:1690a7ae871c 786 m_i2c->read(m_addr, &val, 1);
WiredHome 124:1690a7ae871c 787 return (uint8_t)val;
WiredHome 124:1690a7ae871c 788 }
WiredHome 124:1690a7ae871c 789
WiredHome 124:1690a7ae871c 790 // Interrupt for touch detection
WiredHome 124:1690a7ae871c 791 void RA8875::TouchPanelISR(void)
WiredHome 124:1690a7ae871c 792 {
WiredHome 165:695c24cc5197 793 if (useTouchPanel == TP_FT5206) {
WiredHome 165:695c24cc5197 794 if (FT5206_TouchPositions())
WiredHome 165:695c24cc5197 795 panelTouched = true;
WiredHome 165:695c24cc5197 796 } else if (useTouchPanel == TP_GSL1680) {
WiredHome 165:695c24cc5197 797 if (GSL1680_TouchPositions())
WiredHome 165:695c24cc5197 798 panelTouched = true;
WiredHome 165:695c24cc5197 799 }
WiredHome 124:1690a7ae871c 800 }
WiredHome 124:1690a7ae871c 801
WiredHome 124:1690a7ae871c 802
WiredHome 78:faf49c381591 803 // #### end of touch panel code additions