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:
Thu Aug 08 11:29:48 2019 +0000
Revision:
183:808f272e481e
Parent:
181:0032d1b8f5d4
Child:
184:464cc376265d
Minor code-cleanup

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