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:
Sat May 23 15:59:11 2020 +0000
Revision:
205:f215dc32736b
Parent:
203:704df2dbd3e6
Child:
206:83edda283d90
Defect correction - disable irq while configuring the FT5206 and GSL1680 touch controllers to avoid unexpected ISRs.

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 197:853d08e2fb53 18 #define INFO(x, ...) std::printf("[INF %s %4d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 197:853d08e2fb53 19 #define WARN(x, ...) std::printf("[WRN %s %4d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 197:853d08e2fb53 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 190:3132b7dfad82 52
WiredHome 205:f215dc32736b 53 m_irq->disable_irq(); // ensure disabled while configuring it
WiredHome 124:1690a7ae871c 54 panelTouched = false;
WiredHome 165:695c24cc5197 55 if (useTouchPanel == TP_GSL1680) {
WiredHome 165:695c24cc5197 56 INFO("TouchPanelInit: TP_GSL1680");
WiredHome 169:1c1a495983bc 57 r = GSL1680_Init();
WiredHome 165:695c24cc5197 58 } else if (useTouchPanel == TP_FT5206) {
WiredHome 124:1690a7ae871c 59 // Set to normal mode
WiredHome 165:695c24cc5197 60 INFO("TouchPanelInit: TP_FT5206");
WiredHome 165:695c24cc5197 61 r = FT5206_Init();
WiredHome 124:1690a7ae871c 62 } else {
WiredHome 155:b3f225ae572c 63 INFO("TouchPanelInit: TP_RES");
WiredHome 197:853d08e2fb53 64 //RA8875_TPCR0: Set enable bit, default sample time, wakeup, and ADC clock
WiredHome 193:74f80834d59d 65 WriteCommand(RA8875_TPCR0, TP_ENABLE | TP_ADC_SAMPLE_DEFAULT_CLKS | TP_ADC_CLKDIV_DEFAULT);
WiredHome 197:853d08e2fb53 66 // RA8875_TPCR1: Set auto/manual, Ref voltage, debounce, manual mode params
WiredHome 193:74f80834d59d 67 WriteCommand(RA8875_TPCR1, TP_MODE_DEFAULT | TP_DEBOUNCE_DEFAULT);
WiredHome 193:74f80834d59d 68 WriteCommand(RA8875_INTC1, ReadCommand(RA8875_INTC1) | RA8875_INT_TP); // reg RA8875_INTC1: Enable Touch Panel Interrupts (D2 = 1)
WiredHome 193:74f80834d59d 69 WriteCommand(RA8875_INTC2, RA8875_INT_TP); // reg RA8875_INTC2: Clear any TP interrupt flag
WiredHome 124:1690a7ae871c 70 touchSample = 0;
WiredHome 124:1690a7ae871c 71 touchState = no_cal;
WiredHome 193:74f80834d59d 72 #if 0
WiredHome 181:0032d1b8f5d4 73 #if (MBED_MAJOR_VERSION >= 5) || (MBED_LIBRARY_VERSION > 127)
WiredHome 145:5eb2492acdda 74 touchTicker.attach_us(callback(this, &RA8875::_TouchTicker), TOUCH_TICKER_uS);
WiredHome 150:35a4db3081c1 75 #else
WiredHome 150:35a4db3081c1 76 touchTicker.attach_us(this, &RA8875::_TouchTicker, TOUCH_TICKER_uS);
WiredHome 150:35a4db3081c1 77 #endif
WiredHome 193:74f80834d59d 78 #endif
WiredHome 165:695c24cc5197 79 timeSinceTouch.start();
WiredHome 165:695c24cc5197 80 timeSinceTouch.reset();
WiredHome 157:1565f38ca44b 81 r = _internal_ts_cal();
WiredHome 83:7bad0068cca0 82 }
WiredHome 205:f215dc32736b 83 m_irq->enable_irq();
WiredHome 157:1565f38ca44b 84 return r;
WiredHome 78:faf49c381591 85 }
WiredHome 78:faf49c381591 86
WiredHome 124:1690a7ae871c 87
WiredHome 124:1690a7ae871c 88 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 89 {
WiredHome 165:695c24cc5197 90 if (useTouchPanel == TP_GSL1680) {
WiredHome 165:695c24cc5197 91 INFO("TouchPanelInit: TP_GSL1680");
WiredHome 165:695c24cc5197 92 } else if (useTouchPanel == TP_FT5206) {
WiredHome 165:695c24cc5197 93 INFO("TouchPanelInit: TP_FT5206");
WiredHome 124:1690a7ae871c 94 TouchPanelInit();
WiredHome 124:1690a7ae871c 95 } else {
WiredHome 155:b3f225ae572c 96 INFO("TouchPanelInit: TP_RES");
WiredHome 205:f215dc32736b 97 m_irq->disable_irq(); // ensure disabled while configuring it
WiredHome 124:1690a7ae871c 98 // Parameter bounds check
WiredHome 124:1690a7ae871c 99 if( \
WiredHome 124:1690a7ae871c 100 !(bTpEnable == TP_ENABLE || bTpEnable == TP_ENABLE) || \
WiredHome 124:1690a7ae871c 101 !(bTpAutoManual == TP_MODE_AUTO || bTpAutoManual == TP_MODE_MANUAL) || \
WiredHome 124:1690a7ae871c 102 !(bTpDebounce == TP_DEBOUNCE_OFF || bTpDebounce == TP_DEBOUNCE_ON) || \
WiredHome 124:1690a7ae871c 103 !(bTpManualMode <= TP_MANUAL_LATCH_Y) || \
WiredHome 124:1690a7ae871c 104 !(bTpAdcClkDiv <= TP_ADC_CLKDIV_128) || \
WiredHome 124:1690a7ae871c 105 !(bTpAdcSampleTime <= TP_ADC_SAMPLE_65536_CLKS) \
WiredHome 124:1690a7ae871c 106 ) return bad_parameter;
WiredHome 197:853d08e2fb53 107 // Construct the config byte for RA8875_TPCR0 and write them
WiredHome 193:74f80834d59d 108 WriteCommand(RA8875_TPCR0, bTpEnable | bTpAdcClkDiv | bTpAdcSampleTime); // Note: Wakeup is never enabled
WiredHome 197:853d08e2fb53 109 // Construct the config byte for RA8875_TPCR1 and write them
WiredHome 193:74f80834d59d 110 WriteCommand(RA8875_TPCR1, bTpDebounce | bTpManualMode); // Note: Always uses internal Vref.
WiredHome 124:1690a7ae871c 111 // Set up the interrupt flag and enable bits
WiredHome 193:74f80834d59d 112 WriteCommand(RA8875_INTC1, ReadCommand(RA8875_INTC1) | RA8875_INT_TP); // reg RA8875_INTC1: Enable Touch Panel Interrupts (D2 = 1)
WiredHome 193:74f80834d59d 113 WriteCommand(RA8875_INTC2, RA8875_INT_TP); // reg RA8875_INTC2: Clear any TP interrupt flag
WiredHome 124:1690a7ae871c 114 touchSample = 0;
WiredHome 124:1690a7ae871c 115 touchState = no_cal;
WiredHome 124:1690a7ae871c 116 if (bTpEnable == TP_ENABLE) {
WiredHome 150:35a4db3081c1 117 //touchTicker.attach_us(callback(this, &RA8875::_TouchTicker), TOUCH_TICKER_uS);
WiredHome 181:0032d1b8f5d4 118 #if (MBED_MAJOR_VERSION >= 5) || (MBED_LIBRARY_VERSION > 127)
WiredHome 145:5eb2492acdda 119 touchTicker.attach_us(callback(this, &RA8875::_TouchTicker), TOUCH_TICKER_uS);
WiredHome 150:35a4db3081c1 120 #else
WiredHome 150:35a4db3081c1 121 touchTicker.attach_us(this, &RA8875::_TouchTicker, TOUCH_TICKER_uS);
WiredHome 150:35a4db3081c1 122 #endif
WiredHome 150:35a4db3081c1 123
WiredHome 165:695c24cc5197 124 timeSinceTouch.start();
WiredHome 165:695c24cc5197 125 timeSinceTouch.reset();
WiredHome 124:1690a7ae871c 126 } else {
WiredHome 124:1690a7ae871c 127 touchTicker.detach();
WiredHome 165:695c24cc5197 128 timeSinceTouch.stop();
WiredHome 124:1690a7ae871c 129 }
WiredHome 157:1565f38ca44b 130 _internal_ts_cal();
WiredHome 205:f215dc32736b 131 m_irq->enable_irq();
WiredHome 124:1690a7ae871c 132 }
WiredHome 124:1690a7ae871c 133 return noerror;
WiredHome 124:1690a7ae871c 134 }
WiredHome 124:1690a7ae871c 135
WiredHome 124:1690a7ae871c 136
WiredHome 124:1690a7ae871c 137 int RA8875::TouchChannels(void)
WiredHome 124:1690a7ae871c 138 {
WiredHome 165:695c24cc5197 139 if (useTouchPanel == TP_GSL1680) {
WiredHome 176:4ab96d33a8ec 140 return GSL1680_TOUCH_POINTS; // based on TP_GSL1680 firmware
WiredHome 165:695c24cc5197 141 } else if (useTouchPanel == TP_FT5206) {
WiredHome 176:4ab96d33a8ec 142 return FT5206_TOUCH_POINTS; // based on the FT5206 hardware
WiredHome 124:1690a7ae871c 143 } else if (useTouchPanel == TP_RES) {
WiredHome 176:4ab96d33a8ec 144 return RESISTIVE_TOUCH_POINTS; // based on the RA8875 resistive touch driver
WiredHome 124:1690a7ae871c 145 } else {
WiredHome 124:1690a7ae871c 146 return 0; // it isn't enabled, so there are none.
WiredHome 124:1690a7ae871c 147 }
WiredHome 124:1690a7ae871c 148 }
WiredHome 124:1690a7ae871c 149
WiredHome 124:1690a7ae871c 150
WiredHome 124:1690a7ae871c 151 // +----------------------------------------------------+
WiredHome 124:1690a7ae871c 152 // | |
WiredHome 124:1690a7ae871c 153 // | 1 |
WiredHome 124:1690a7ae871c 154 // | |
WiredHome 124:1690a7ae871c 155 // | |
WiredHome 124:1690a7ae871c 156 // | 2 |
WiredHome 124:1690a7ae871c 157 // | |
WiredHome 124:1690a7ae871c 158 // | |
WiredHome 124:1690a7ae871c 159 // | 3 |
WiredHome 124:1690a7ae871c 160 // | |
WiredHome 124:1690a7ae871c 161 // +----------------------------------------------------+
WiredHome 81:01da2e34283d 162
WiredHome 81:01da2e34283d 163 RetCode_t RA8875::TouchPanelCalibrate(tpMatrix_t * matrix)
WiredHome 81:01da2e34283d 164 {
WiredHome 81:01da2e34283d 165 return TouchPanelCalibrate(NULL, matrix);
WiredHome 81:01da2e34283d 166 }
WiredHome 81:01da2e34283d 167
WiredHome 88:bfddef6ec836 168 RetCode_t RA8875::TouchPanelCalibrate(const char * msg, tpMatrix_t * matrix, int maxwait_s)
WiredHome 81:01da2e34283d 169 {
WiredHome 81:01da2e34283d 170 point_t pTest[3];
WiredHome 81:01da2e34283d 171 point_t pSample[3];
WiredHome 83:7bad0068cca0 172 int x,y;
WiredHome 88:bfddef6ec836 173 Timer timeout; // timeout guards for not-installed, stuck, user not present...
WiredHome 124:1690a7ae871c 174
WiredHome 88:bfddef6ec836 175 timeout.start();
WiredHome 123:2f45e80fec5f 176 while (TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
WiredHome 197:853d08e2fb53 177 wait_us(20000);
WiredHome 123:2f45e80fec5f 178 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 179 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
WiredHome 123:2f45e80fec5f 180 return external_abort;
WiredHome 123:2f45e80fec5f 181 }
WiredHome 123:2f45e80fec5f 182 }
WiredHome 123:2f45e80fec5f 183 }
WiredHome 81:01da2e34283d 184 cls();
WiredHome 157:1565f38ca44b 185 if (msg) { // User defines the message
WiredHome 157:1565f38ca44b 186 if (*msg) puts(msg); // If any
WiredHome 157:1565f38ca44b 187 } else { // Default message
WiredHome 157:1565f38ca44b 188 puts("Touch '+' to calibrate the touch panel");
WiredHome 157:1565f38ca44b 189 }
WiredHome 81:01da2e34283d 190 SetTextCursor(0,height()/2);
WiredHome 124:1690a7ae871c 191 pTest[0].x = 50;
WiredHome 124:1690a7ae871c 192 pTest[0].y = 50;
WiredHome 124:1690a7ae871c 193 pTest[1].x = width() - 50;
WiredHome 124:1690a7ae871c 194 pTest[1].y = height()/2;
WiredHome 124:1690a7ae871c 195 pTest[2].x = width()/2;
WiredHome 124:1690a7ae871c 196 pTest[2].y = height() - 50;
WiredHome 88:bfddef6ec836 197
WiredHome 81:01da2e34283d 198 for (int i=0; i<3; i++) {
WiredHome 81:01da2e34283d 199 foreground(Blue);
WiredHome 81:01da2e34283d 200 printf(" (%3d,%3d) => ", pTest[i].x, pTest[i].y);
WiredHome 157:1565f38ca44b 201 fillcircle(pTest[i].x,pTest[i].y, 20, White);
WiredHome 157:1565f38ca44b 202 line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, Blue);
WiredHome 157:1565f38ca44b 203 line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, Blue);
WiredHome 123:2f45e80fec5f 204 while (!TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
WiredHome 197:853d08e2fb53 205 wait_us(20000);
WiredHome 123:2f45e80fec5f 206 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 207 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
WiredHome 123:2f45e80fec5f 208 return external_abort;
WiredHome 123:2f45e80fec5f 209 }
WiredHome 123:2f45e80fec5f 210 }
WiredHome 123:2f45e80fec5f 211 }
WiredHome 81:01da2e34283d 212 pSample[i].x = x;
WiredHome 81:01da2e34283d 213 pSample[i].y = y;
WiredHome 157:1565f38ca44b 214 fillcircle(pTest[i].x,pTest[i].y, 20, Black); // Erase the target
WiredHome 157:1565f38ca44b 215 //line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, Black);
WiredHome 157:1565f38ca44b 216 //line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, Black);
WiredHome 81:01da2e34283d 217 foreground(Blue);
WiredHome 81:01da2e34283d 218 printf(" (%4d,%4d)\r\n", x,y);
WiredHome 123:2f45e80fec5f 219 while (TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
WiredHome 197:853d08e2fb53 220 wait_us(20000);
WiredHome 123:2f45e80fec5f 221 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 222 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
WiredHome 123:2f45e80fec5f 223 return external_abort;
WiredHome 123:2f45e80fec5f 224 }
WiredHome 123:2f45e80fec5f 225 }
WiredHome 123:2f45e80fec5f 226 }
WiredHome 123:2f45e80fec5f 227 for (int t=0; t<100; t++) {
WiredHome 197:853d08e2fb53 228 wait_us(20000);
WiredHome 123:2f45e80fec5f 229 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 230 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
WiredHome 123:2f45e80fec5f 231 return external_abort;
WiredHome 123:2f45e80fec5f 232 }
WiredHome 123:2f45e80fec5f 233 }
WiredHome 123:2f45e80fec5f 234 }
WiredHome 81:01da2e34283d 235 }
WiredHome 88:bfddef6ec836 236 if (timeout.read() >= maxwait_s)
WiredHome 88:bfddef6ec836 237 return touch_cal_timeout;
WiredHome 88:bfddef6ec836 238 else
WiredHome 88:bfddef6ec836 239 return TouchPanelComputeCalibration(pTest, pSample, matrix);
WiredHome 81:01da2e34283d 240 }
WiredHome 81:01da2e34283d 241
WiredHome 81:01da2e34283d 242
WiredHome 81:01da2e34283d 243 /**********************************************************************
WiredHome 81:01da2e34283d 244 *
WiredHome 123:2f45e80fec5f 245 * Function: TouchPanelReadable()
WiredHome 81:01da2e34283d 246 *
WiredHome 81:01da2e34283d 247 */
WiredHome 83:7bad0068cca0 248 TouchCode_t RA8875::TouchPanelReadable(point_t * TouchPoint)
WiredHome 81:01da2e34283d 249 {
WiredHome 124:1690a7ae871c 250 TouchCode_t ts = no_touch;
WiredHome 124:1690a7ae871c 251
WiredHome 165:695c24cc5197 252 if (useTouchPanel == TP_FT5206) {
WiredHome 198:9b6851107426 253 //INFO("TP_FT5206");
WiredHome 165:695c24cc5197 254 } else if (useTouchPanel == TP_GSL1680) {
WiredHome 198:9b6851107426 255 //INFO("TP_GSL1680");
WiredHome 165:695c24cc5197 256 } else if (useTouchPanel == TP_RES) {
WiredHome 198:9b6851107426 257 ReadResistive();
WiredHome 124:1690a7ae871c 258 }
WiredHome 165:695c24cc5197 259 // For Resistive touch, panelTouched is computed above.
WiredHome 165:695c24cc5197 260 // For Cap Sense, panelTouched is set in another process
WiredHome 124:1690a7ae871c 261 if (panelTouched == true) {
WiredHome 124:1690a7ae871c 262 panelTouched = false;
WiredHome 124:1690a7ae871c 263 if (TouchPoint) {
WiredHome 202:a22cbc04f332 264 *TouchPoint = touchInfo[0].coordinates;
WiredHome 124:1690a7ae871c 265 ts = touchInfo[0].touchCode;
WiredHome 193:74f80834d59d 266 #ifdef DEBUG
WiredHome 193:74f80834d59d 267 static TouchCode_t lastTS = no_touch;
WiredHome 193:74f80834d59d 268 if (lastTS != ts)
WiredHome 197:853d08e2fb53 269 INFO("Touch[0] %2d (%4d,%4d)", touchInfo[0].touchCode,
WiredHome 197:853d08e2fb53 270 touchInfo[0].coordinates.x, touchInfo[0].coordinates.y);
WiredHome 193:74f80834d59d 271 lastTS = ts;
WiredHome 193:74f80834d59d 272 #endif
WiredHome 130:d633e80840e0 273 } else {
WiredHome 130:d633e80840e0 274 ts = touch;
WiredHome 124:1690a7ae871c 275 }
WiredHome 81:01da2e34283d 276 }
WiredHome 83:7bad0068cca0 277 return ts;
WiredHome 81:01da2e34283d 278 }
WiredHome 81:01da2e34283d 279
WiredHome 190:3132b7dfad82 280 uint8_t RA8875::TouchID(uint8_t channel)
WiredHome 166:53fd4a876dac 281 {
WiredHome 166:53fd4a876dac 282 if (channel >= TouchChannels())
WiredHome 166:53fd4a876dac 283 channel = 0;
WiredHome 190:3132b7dfad82 284 return touchInfo[channel].touchID;
WiredHome 166:53fd4a876dac 285 }
WiredHome 166:53fd4a876dac 286
WiredHome 190:3132b7dfad82 287 TouchCode_t RA8875::TouchCode(uint8_t channel)
WiredHome 190:3132b7dfad82 288 {
WiredHome 166:53fd4a876dac 289 if (channel >= TouchChannels())
WiredHome 166:53fd4a876dac 290 channel = 0;
WiredHome 181:0032d1b8f5d4 291 INFO("TouchCode[%d] = %d", channel, touchInfo[channel].touchCode);
WiredHome 181:0032d1b8f5d4 292 return touchInfo[channel].touchCode;
WiredHome 166:53fd4a876dac 293 }
WiredHome 166:53fd4a876dac 294
WiredHome 166:53fd4a876dac 295 point_t RA8875::TouchCoordinates(uint8_t channel)
WiredHome 166:53fd4a876dac 296 {
WiredHome 166:53fd4a876dac 297 if (channel >= TouchChannels())
WiredHome 166:53fd4a876dac 298 channel = 0;
WiredHome 203:704df2dbd3e6 299 return touchInfo[channel].coordinates;
WiredHome 166:53fd4a876dac 300 }
WiredHome 81:01da2e34283d 301
WiredHome 85:022bba13c5c4 302 TouchCode_t RA8875::TouchPanelGet(point_t * TouchPoint)
WiredHome 85:022bba13c5c4 303 {
WiredHome 123:2f45e80fec5f 304 TouchCode_t t = no_touch;
WiredHome 124:1690a7ae871c 305
WiredHome 123:2f45e80fec5f 306 while (true) {
WiredHome 85:022bba13c5c4 307 t = TouchPanelReadable(TouchPoint);
WiredHome 123:2f45e80fec5f 308 if (t != no_touch)
WiredHome 123:2f45e80fec5f 309 break;
WiredHome 123:2f45e80fec5f 310 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 311 if (external_abort == (*idle_callback)(touch_wait, 0)) {
WiredHome 123:2f45e80fec5f 312 return no_touch;
WiredHome 123:2f45e80fec5f 313 }
WiredHome 123:2f45e80fec5f 314 }
WiredHome 123:2f45e80fec5f 315 }
WiredHome 85:022bba13c5c4 316 return t;
WiredHome 85:022bba13c5c4 317 }
WiredHome 85:022bba13c5c4 318
WiredHome 123:2f45e80fec5f 319 // Below here are primarily "helper" functions. While many are accessible
WiredHome 123:2f45e80fec5f 320 // to the user code, they usually don't need to be called.
WiredHome 123:2f45e80fec5f 321
WiredHome 81:01da2e34283d 322 RetCode_t RA8875::TouchPanelSetMatrix(tpMatrix_t * matrixPtr)
WiredHome 81:01da2e34283d 323 {
WiredHome 81:01da2e34283d 324 if (matrixPtr == NULL || matrixPtr->Divider == 0)
WiredHome 81:01da2e34283d 325 return bad_parameter;
WiredHome 81:01da2e34283d 326 memcpy(&tpMatrix, matrixPtr, sizeof(tpMatrix_t));
WiredHome 83:7bad0068cca0 327 touchState = no_touch;
WiredHome 81:01da2e34283d 328 return noerror;
WiredHome 81:01da2e34283d 329 }
WiredHome 81:01da2e34283d 330
WiredHome 157:1565f38ca44b 331 const tpMatrix_t * RA8875::TouchPanelGetMatrix()
WiredHome 157:1565f38ca44b 332 {
WiredHome 157:1565f38ca44b 333 return &tpMatrix;
WiredHome 157:1565f38ca44b 334 }
WiredHome 157:1565f38ca44b 335
WiredHome 157:1565f38ca44b 336
WiredHome 83:7bad0068cca0 337 static void InsertionSort(int * buf, int bufsize)
WiredHome 83:7bad0068cca0 338 {
WiredHome 83:7bad0068cca0 339 int i, j;
WiredHome 83:7bad0068cca0 340 int temp;
WiredHome 124:1690a7ae871c 341
WiredHome 108:7415c405ee08 342 for(i = 1; i < bufsize; i++) {
WiredHome 83:7bad0068cca0 343 temp = buf[i];
WiredHome 83:7bad0068cca0 344 j = i;
WiredHome 83:7bad0068cca0 345 while( j && (buf[j-1] > temp) ) {
WiredHome 83:7bad0068cca0 346 buf[j] = buf[j-1];
WiredHome 83:7bad0068cca0 347 j = j-1;
WiredHome 83:7bad0068cca0 348 }
WiredHome 83:7bad0068cca0 349 buf[j] = temp;
WiredHome 83:7bad0068cca0 350 } // End of sort
WiredHome 83:7bad0068cca0 351 }
WiredHome 83:7bad0068cca0 352
WiredHome 83:7bad0068cca0 353
WiredHome 83:7bad0068cca0 354 void RA8875::_TouchTicker(void)
WiredHome 78:faf49c381591 355 {
WiredHome 197:853d08e2fb53 356 //INFO("_TouchTicker()");
WiredHome 165:695c24cc5197 357 if (timeSinceTouch.read_us() > NOTOUCH_TIMEOUT_uS) {
WiredHome 83:7bad0068cca0 358 touchSample = 0;
WiredHome 193:74f80834d59d 359 if (touchState == held) {
WiredHome 83:7bad0068cca0 360 touchState = release;
WiredHome 193:74f80834d59d 361 INFO("release %d", touchState);
WiredHome 193:74f80834d59d 362 } else {
WiredHome 193:74f80834d59d 363 INFO("!= held %d", touchState);
WiredHome 83:7bad0068cca0 364 touchState = no_touch;
WiredHome 193:74f80834d59d 365 }
WiredHome 165:695c24cc5197 366 timeSinceTouch.reset();
WiredHome 83:7bad0068cca0 367 }
WiredHome 83:7bad0068cca0 368 }
WiredHome 83:7bad0068cca0 369
WiredHome 198:9b6851107426 370
WiredHome 198:9b6851107426 371 // Description: Given a valid set of calibration factors and a point
WiredHome 198:9b6851107426 372 // value reported by the touch screen, this function
WiredHome 198:9b6851107426 373 // calculates and returns the true (or closest to true)
WiredHome 198:9b6851107426 374 // display point below the spot where the touch screen
WiredHome 198:9b6851107426 375 // was touched.
WiredHome 198:9b6851107426 376 //
WiredHome 198:9b6851107426 377 // Argument(s): displayPtr (output) - Pointer to the calculated
WiredHome 198:9b6851107426 378 // (true) display point.
WiredHome 198:9b6851107426 379 // screenPtr (input) - Pointer to the reported touch
WiredHome 198:9b6851107426 380 // screen point.
WiredHome 198:9b6851107426 381 // matrixPtr (input) - Pointer to calibration factors
WiredHome 198:9b6851107426 382 // matrix previously calculated
WiredHome 198:9b6851107426 383 // from a call to
WiredHome 198:9b6851107426 384 // setCalibrationMatrix()
WiredHome 198:9b6851107426 385 //
WiredHome 198:9b6851107426 386 // The function simply solves for Xd and Yd by implementing the
WiredHome 198:9b6851107426 387 // computations required by the translation matrix.
WiredHome 198:9b6851107426 388 //
WiredHome 198:9b6851107426 389 // /- -\ //
WiredHome 198:9b6851107426 390 // /- -\ /- -\ | | //
WiredHome 198:9b6851107426 391 // | | | | | Xs | //
WiredHome 198:9b6851107426 392 // | Xd | | A B C | | | //
WiredHome 198:9b6851107426 393 // | | = | | * | Ys | //
WiredHome 198:9b6851107426 394 // | Yd | | D E F | | | //
WiredHome 198:9b6851107426 395 // | | | | | 1 | //
WiredHome 198:9b6851107426 396 // \- -/ \- -/ | | //
WiredHome 198:9b6851107426 397 // \- -/ //
WiredHome 198:9b6851107426 398 //
WiredHome 198:9b6851107426 399 // It must be kept brief to avoid consuming CPU cycles.
WiredHome 198:9b6851107426 400 //
WiredHome 198:9b6851107426 401 // Return: OK - the display point was correctly calculated
WiredHome 198:9b6851107426 402 // and its value is in the output argument.
WiredHome 198:9b6851107426 403 // NOT_OK - an error was detected and the function
WiredHome 198:9b6851107426 404 // failed to return a valid point.
WiredHome 198:9b6851107426 405 //
WiredHome 198:9b6851107426 406 // NOTE! NOTE! NOTE!
WiredHome 198:9b6851107426 407 //
WiredHome 198:9b6851107426 408 // setCalibrationMatrix() and getDisplayPoint() will do fine
WiredHome 198:9b6851107426 409 // for you as they are, provided that your digitizer
WiredHome 198:9b6851107426 410 // resolution does not exceed 10 bits (1024 values). Higher
WiredHome 198:9b6851107426 411 // resolutions may cause the integer operations to overflow
WiredHome 198:9b6851107426 412 // and return incorrect values. If you wish to use these
WiredHome 198:9b6851107426 413 // functions with digitizer resolutions of 12 bits (4096
WiredHome 198:9b6851107426 414 // values) you will either have to a) use 64-bit signed
WiredHome 198:9b6851107426 415 // integer variables and math, or b) judiciously modify the
WiredHome 198:9b6851107426 416 // operations to scale results by a factor of 2 or even 4.
WiredHome 198:9b6851107426 417 //
WiredHome 198:9b6851107426 418 void RA8875::ReadResistive() {
WiredHome 198:9b6851107426 419 TouchCode_t ts = no_touch;
WiredHome 198:9b6851107426 420 int a2dX = 0;
WiredHome 198:9b6851107426 421 int a2dY = 0;
WiredHome 198:9b6851107426 422
WiredHome 198:9b6851107426 423 touchInfo[0].touchID = 0;
WiredHome 198:9b6851107426 424 ts = TouchPanelA2DFiltered(&a2dX, &a2dY);
WiredHome 198:9b6851107426 425 if (ts != no_touch) {
WiredHome 198:9b6851107426 426 panelTouched = true;
WiredHome 198:9b6851107426 427 numberOfTouchPoints = 1;
WiredHome 198:9b6851107426 428
WiredHome 198:9b6851107426 429 if (tpMatrix.Divider != 0) {
WiredHome 198:9b6851107426 430 /* Operation order is important since we are doing integer */
WiredHome 198:9b6851107426 431 /* math. Make sure you add all terms together before */
WiredHome 198:9b6851107426 432 /* dividing, so that the remainder is not rounded off */
WiredHome 198:9b6851107426 433 /* prematurely. */
WiredHome 202:a22cbc04f332 434 touchInfo[0].coordinates = TranslateOrientation(
WiredHome 202:a22cbc04f332 435 ((tpMatrix.An * a2dX) + (tpMatrix.Bn * a2dY) + tpMatrix.Cn) / tpMatrix.Divider,
WiredHome 202:a22cbc04f332 436 ((tpMatrix.Dn * a2dX) + (tpMatrix.En * a2dY) + tpMatrix.Fn) / tpMatrix.Divider );
WiredHome 198:9b6851107426 437 } else {
WiredHome 198:9b6851107426 438 ts = no_cal;
WiredHome 198:9b6851107426 439 }
WiredHome 198:9b6851107426 440 } else {
WiredHome 198:9b6851107426 441 numberOfTouchPoints = 0;
WiredHome 198:9b6851107426 442 }
WiredHome 198:9b6851107426 443 touchInfo[0].touchCode = ts;
WiredHome 198:9b6851107426 444 }
WiredHome 198:9b6851107426 445
WiredHome 198:9b6851107426 446
WiredHome 83:7bad0068cca0 447 TouchCode_t RA8875::TouchPanelA2DRaw(int *x, int *y)
WiredHome 83:7bad0068cca0 448 {
WiredHome 154:ad2450fc3dc3 449 INFO("A2Raw");
WiredHome 193:74f80834d59d 450 if( (ReadCommand(RA8875_INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register RA8875_INTC2
WiredHome 154:ad2450fc3dc3 451 INFO("Int pending");
WiredHome 165:695c24cc5197 452 timeSinceTouch.reset();
WiredHome 193:74f80834d59d 453 *y = ReadCommand(RA8875_TPYH) << 2 | ( (ReadCommand(RA8875_TPXYL) & 0xC) >> 2 ); // D[9:2] from reg RA8875_TPYH, D[1:0] from reg RA8875_TPXYL[3:2]
WiredHome 193:74f80834d59d 454 *x = ReadCommand(RA8875_TPXH) << 2 | ( (ReadCommand(RA8875_TPXYL) & 0x3) ); // D[9:2] from reg RA8875_TPXH, D[1:0] from reg RA8875_TPXYL[1:0]
WiredHome 193:74f80834d59d 455 INFO("(x,y) = (%d,%d)", *x, *y);
WiredHome 193:74f80834d59d 456 WriteCommand(RA8875_INTC2, RA8875_INT_TP); // reg RA8875_INTC2: Clear that TP interrupt flag
WiredHome 83:7bad0068cca0 457 touchState = touch;
WiredHome 193:74f80834d59d 458 } else if (ReadCommand(RA8875_TPXYL) & RA8875_TPXYL_ADET) {
WiredHome 193:74f80834d59d 459 INFO("held");
WiredHome 193:74f80834d59d 460 touchState = held;
WiredHome 83:7bad0068cca0 461 } else {
WiredHome 154:ad2450fc3dc3 462 INFO("no touch");
WiredHome 83:7bad0068cca0 463 touchState = no_touch;
WiredHome 83:7bad0068cca0 464 }
WiredHome 83:7bad0068cca0 465 return touchState;
WiredHome 83:7bad0068cca0 466 }
WiredHome 83:7bad0068cca0 467
WiredHome 83:7bad0068cca0 468 TouchCode_t RA8875::TouchPanelA2DFiltered(int *x, int *y)
WiredHome 83:7bad0068cca0 469 {
WiredHome 83:7bad0068cca0 470 static int xbuf[TPBUFSIZE], ybuf[TPBUFSIZE];
WiredHome 83:7bad0068cca0 471 static int lastX, lastY;
WiredHome 83:7bad0068cca0 472 int i, j;
WiredHome 83:7bad0068cca0 473 TouchCode_t ret = touchState;
WiredHome 78:faf49c381591 474
WiredHome 193:74f80834d59d 475 if ( (ReadCommand(RA8875_INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register RA8875_INTC2
WiredHome 165:695c24cc5197 476 timeSinceTouch.reset();
WiredHome 78:faf49c381591 477 // Get the next data samples
WiredHome 193:74f80834d59d 478 ybuf[touchSample] = ReadCommand(RA8875_TPYH) << 2 | ( (ReadCommand(RA8875_TPXYL) & 0xC) >> 2 ); // D[9:2] from reg RA8875_TPYH, D[1:0] from reg RA8875_TPXYL[3:2]
WiredHome 193:74f80834d59d 479 xbuf[touchSample] = ReadCommand(RA8875_TPXH) << 2 | ( (ReadCommand(RA8875_TPXYL) & 0x3) ); // D[9:2] from reg RA8875_TPXH, D[1:0] from reg RA8875_TPXYL[1:0]
WiredHome 78:faf49c381591 480 // Check for a complete set
WiredHome 197:853d08e2fb53 481 if (++touchSample == TPBUFSIZE) {
WiredHome 78:faf49c381591 482 // Buffers are full, so process them using Finn's method described in Analog Dialogue No. 44, Feb 2010
WiredHome 78:faf49c381591 483 // This requires sorting the samples in order of size, then discarding the top 25% and
WiredHome 78:faf49c381591 484 // bottom 25% as noise spikes. Finally, the middle 50% of the values are averaged to
WiredHome 78:faf49c381591 485 // reduce Gaussian noise.
WiredHome 83:7bad0068cca0 486 InsertionSort(ybuf, TPBUFSIZE);
WiredHome 83:7bad0068cca0 487 InsertionSort(xbuf, TPBUFSIZE);
WiredHome 78:faf49c381591 488 // Average the middle half of the Y values and report them
WiredHome 78:faf49c381591 489 j = 0;
WiredHome 197:853d08e2fb53 490 for (i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
WiredHome 78:faf49c381591 491 j += ybuf[i];
WiredHome 78:faf49c381591 492 }
WiredHome 83:7bad0068cca0 493 *y = lastY = j * (float)2/TPBUFSIZE; // This is the average
WiredHome 78:faf49c381591 494 // Average the middle half of the X values and report them
WiredHome 78:faf49c381591 495 j = 0;
WiredHome 78:faf49c381591 496 for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
WiredHome 78:faf49c381591 497 j += xbuf[i];
WiredHome 78:faf49c381591 498 }
WiredHome 83:7bad0068cca0 499 *x = lastX = j * (float)2/TPBUFSIZE; // This is the average
WiredHome 78:faf49c381591 500 // Tidy up and return
WiredHome 193:74f80834d59d 501 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 502 touchState = held;
WiredHome 197:853d08e2fb53 503 //INFO("held");
WiredHome 193:74f80834d59d 504 } else {
WiredHome 83:7bad0068cca0 505 touchState = touch;
WiredHome 197:853d08e2fb53 506 //INFO("touch");
WiredHome 193:74f80834d59d 507 }
WiredHome 83:7bad0068cca0 508 ret = touchState;
WiredHome 83:7bad0068cca0 509 touchSample = 0; // Ready to start on the next set of data samples
WiredHome 78:faf49c381591 510 } else {
WiredHome 78:faf49c381591 511 // Buffer not yet full, so do not return any results yet
WiredHome 83:7bad0068cca0 512 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 513 *x = lastX;
WiredHome 83:7bad0068cca0 514 *y = lastY;
WiredHome 197:853d08e2fb53 515 #ifdef DEBUG
WiredHome 193:74f80834d59d 516 {
WiredHome 193:74f80834d59d 517 static TouchCode_t lastTC = no_touch;
WiredHome 193:74f80834d59d 518 if (lastTC != touchState)
WiredHome 197:853d08e2fb53 519 //INFO("held");
WiredHome 193:74f80834d59d 520 lastTC = touchState;
WiredHome 193:74f80834d59d 521 }
WiredHome 197:853d08e2fb53 522 #endif
WiredHome 83:7bad0068cca0 523 ret = touchState = held;
WiredHome 193:74f80834d59d 524 } else {
WiredHome 193:74f80834d59d 525 INFO("else %d, %d", touchState, touchSample);
WiredHome 83:7bad0068cca0 526 }
WiredHome 78:faf49c381591 527 }
WiredHome 193:74f80834d59d 528 WriteCommand(RA8875_INTC2, RA8875_INT_TP); // reg RA8875_INTC2: Clear that TP interrupt flag
WiredHome 193:74f80834d59d 529 } else if (0 == (ReadCommand(RA8875_TPXYL) & RA8875_TPXYL_ADET)) {
WiredHome 193:74f80834d59d 530 INFO("held");
WiredHome 193:74f80834d59d 531 *x = lastX;
WiredHome 193:74f80834d59d 532 *y = lastY;
WiredHome 193:74f80834d59d 533 ret = touchState = held;
WiredHome 193:74f80834d59d 534 } else if (timeSinceTouch.read_ms() < 20) {
WiredHome 193:74f80834d59d 535 *x = lastX;
WiredHome 193:74f80834d59d 536 *y = lastY;
WiredHome 193:74f80834d59d 537 //ret = touchState;
WiredHome 193:74f80834d59d 538 } else {
WiredHome 193:74f80834d59d 539 //INFO("touchState %d", touchState);
WiredHome 193:74f80834d59d 540 //touchSample = 0;
WiredHome 193:74f80834d59d 541 *x = lastX;
WiredHome 193:74f80834d59d 542 *y = lastY;
WiredHome 193:74f80834d59d 543 if (touchState == no_touch || touchState == release) {
WiredHome 193:74f80834d59d 544 ret = touchState = no_touch;
WiredHome 193:74f80834d59d 545 } else {
WiredHome 193:74f80834d59d 546 INFO("release");
WiredHome 193:74f80834d59d 547 touchSample = 0;
WiredHome 193:74f80834d59d 548 touchState = no_touch;
WiredHome 193:74f80834d59d 549 ret = release;
WiredHome 193:74f80834d59d 550 }
WiredHome 193:74f80834d59d 551 #if 0
WiredHome 83:7bad0068cca0 552 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 553 *x = lastX;
WiredHome 83:7bad0068cca0 554 *y = lastY;
WiredHome 83:7bad0068cca0 555 ret = touchState = held;
WiredHome 193:74f80834d59d 556 INFO("held");
WiredHome 83:7bad0068cca0 557 } else if (touchState == release) {
WiredHome 83:7bad0068cca0 558 *x = lastX;
WiredHome 83:7bad0068cca0 559 *y = lastY;
WiredHome 83:7bad0068cca0 560 ret = release;
WiredHome 83:7bad0068cca0 561 touchState = no_touch;
WiredHome 193:74f80834d59d 562 INFO("release");
WiredHome 83:7bad0068cca0 563 }
WiredHome 193:74f80834d59d 564 #endif
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 190:3132b7dfad82 598 /// /- -\ //
WiredHome 190:3132b7dfad82 599 /// /- -\ /- -\ | | //
WiredHome 190:3132b7dfad82 600 /// | | | | | Xs | //
WiredHome 190:3132b7dfad82 601 /// | Xd | | A B C | | | //
WiredHome 190:3132b7dfad82 602 /// | | = | | * | Ys | //
WiredHome 190:3132b7dfad82 603 /// | Yd | | D E F | | | //
WiredHome 190:3132b7dfad82 604 /// | | | | | 1 | //
WiredHome 190:3132b7dfad82 605 /// \- -/ \- -/ | | //
WiredHome 190:3132b7dfad82 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 190:3132b7dfad82 612 /// /- -\ represents the factors used to translate
WiredHome 190:3132b7dfad82 613 /// |A,B,C| the available touch screen point values
WiredHome 190:3132b7dfad82 614 /// |D,E,F| into the corresponding display
WiredHome 190:3132b7dfad82 615 /// \- -/ coordinates.
WiredHome 190:3132b7dfad82 616 ///
WiredHome 167:8aa3fb2a5a31 617 /// Note that for practical considerations, the utilities
WiredHome 167:8aa3fb2a5a31 618 /// within this file do not use the matrix coefficients as
WiredHome 167:8aa3fb2a5a31 619 /// defined above, but instead use the following
WiredHome 167:8aa3fb2a5a31 620 /// equivalents, since floating point math is not used:
WiredHome 167:8aa3fb2a5a31 621 /// A = An/Divider
WiredHome 167:8aa3fb2a5a31 622 /// B = Bn/Divider
WiredHome 167:8aa3fb2a5a31 623 /// C = Cn/Divider
WiredHome 167:8aa3fb2a5a31 624 /// D = Dn/Divider
WiredHome 167:8aa3fb2a5a31 625 /// E = En/Divider
WiredHome 167:8aa3fb2a5a31 626 /// F = Fn/Divider
WiredHome 167:8aa3fb2a5a31 627 /// The functions provided within this file are:
WiredHome 167:8aa3fb2a5a31 628 /// setCalibrationMatrix() - calculates the set of factors
WiredHome 167:8aa3fb2a5a31 629 /// in the above equation, given
WiredHome 167:8aa3fb2a5a31 630 /// three sets of test points.
WiredHome 167:8aa3fb2a5a31 631 /// getDisplayPoint() - returns the actual display
WiredHome 167:8aa3fb2a5a31 632 /// coordinates, given a set of
WiredHome 167:8aa3fb2a5a31 633 /// touch screen coordinates.
WiredHome 167:8aa3fb2a5a31 634 /// translateRawScreenCoordinates() - helper function to transform
WiredHome 167:8aa3fb2a5a31 635 /// raw screen points into values
WiredHome 167:8aa3fb2a5a31 636 /// scaled to the desired display
WiredHome 167:8aa3fb2a5a31 637 /// resolution.
WiredHome 167:8aa3fb2a5a31 638 ///
WiredHome 167:8aa3fb2a5a31 639 ///
WiredHome 167:8aa3fb2a5a31 640 /// Function: setCalibrationMatrix()
WiredHome 167:8aa3fb2a5a31 641 ///
WiredHome 167:8aa3fb2a5a31 642 /// Description: Calling this function with valid input data
WiredHome 167:8aa3fb2a5a31 643 /// in the display and screen input arguments
WiredHome 167:8aa3fb2a5a31 644 /// causes the calibration factors between the
WiredHome 167:8aa3fb2a5a31 645 /// screen and display points to be calculated,
WiredHome 167:8aa3fb2a5a31 646 /// and the output argument - matrixPtr - to be
WiredHome 167:8aa3fb2a5a31 647 /// populated.
WiredHome 167:8aa3fb2a5a31 648 ///
WiredHome 167:8aa3fb2a5a31 649 /// This function needs to be called only when new
WiredHome 167:8aa3fb2a5a31 650 /// calibration factors are desired.
WiredHome 167:8aa3fb2a5a31 651 ///
WiredHome 167:8aa3fb2a5a31 652 ///
WiredHome 167:8aa3fb2a5a31 653 /// Argument(s): displayPtr (input) - Pointer to an array of three
WiredHome 167:8aa3fb2a5a31 654 /// sample, reference points.
WiredHome 167:8aa3fb2a5a31 655 /// screenPtr (input) - Pointer to the array of touch
WiredHome 167:8aa3fb2a5a31 656 /// screen points corresponding
WiredHome 167:8aa3fb2a5a31 657 /// to the reference display points.
WiredHome 167:8aa3fb2a5a31 658 /// matrixPtr (output) - Pointer to the calibration
WiredHome 167:8aa3fb2a5a31 659 /// matrix computed for the set
WiredHome 167:8aa3fb2a5a31 660 /// of points being provided.
WiredHome 167:8aa3fb2a5a31 661 ///
WiredHome 167:8aa3fb2a5a31 662 ///
WiredHome 167:8aa3fb2a5a31 663 /// From the article text, recall that the matrix coefficients are
WiredHome 167:8aa3fb2a5a31 664 /// resolved to be the following:
WiredHome 167:8aa3fb2a5a31 665 ///
WiredHome 167:8aa3fb2a5a31 666 ///
WiredHome 167:8aa3fb2a5a31 667 /// Divider = (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2)
WiredHome 167:8aa3fb2a5a31 668 ///
WiredHome 167:8aa3fb2a5a31 669 ///
WiredHome 167:8aa3fb2a5a31 670 ///
WiredHome 167:8aa3fb2a5a31 671 /// (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2)
WiredHome 167:8aa3fb2a5a31 672 /// A = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 673 /// Divider
WiredHome 167:8aa3fb2a5a31 674 ///
WiredHome 167:8aa3fb2a5a31 675 ///
WiredHome 167:8aa3fb2a5a31 676 /// (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2)
WiredHome 167:8aa3fb2a5a31 677 /// B = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 678 /// Divider
WiredHome 167:8aa3fb2a5a31 679 ///
WiredHome 167:8aa3fb2a5a31 680 ///
WiredHome 167:8aa3fb2a5a31 681 /// Ys0*(Xs2*Xd1 - Xs1*Xd2) +
WiredHome 167:8aa3fb2a5a31 682 /// Ys1*(Xs0*Xd2 - Xs2*Xd0) +
WiredHome 167:8aa3fb2a5a31 683 /// Ys2*(Xs1*Xd0 - Xs0*Xd1)
WiredHome 167:8aa3fb2a5a31 684 /// C = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 685 /// Divider
WiredHome 167:8aa3fb2a5a31 686 ///
WiredHome 167:8aa3fb2a5a31 687 ///
WiredHome 167:8aa3fb2a5a31 688 /// (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2)
WiredHome 167:8aa3fb2a5a31 689 /// D = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 690 /// Divider
WiredHome 167:8aa3fb2a5a31 691 ///
WiredHome 167:8aa3fb2a5a31 692 ///
WiredHome 167:8aa3fb2a5a31 693 /// (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2)
WiredHome 167:8aa3fb2a5a31 694 /// E = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 695 /// Divider
WiredHome 167:8aa3fb2a5a31 696 ///
WiredHome 167:8aa3fb2a5a31 697 ///
WiredHome 167:8aa3fb2a5a31 698 /// Ys0*(Xs2*Yd1 - Xs1*Yd2) +
WiredHome 167:8aa3fb2a5a31 699 /// Ys1*(Xs0*Yd2 - Xs2*Yd0) +
WiredHome 167:8aa3fb2a5a31 700 /// Ys2*(Xs1*Yd0 - Xs0*Yd1)
WiredHome 167:8aa3fb2a5a31 701 /// F = ---------------------------------------------------
WiredHome 167:8aa3fb2a5a31 702 /// Divider
WiredHome 167:8aa3fb2a5a31 703 ///
WiredHome 167:8aa3fb2a5a31 704 ///
WiredHome 167:8aa3fb2a5a31 705 /// Return: OK - the calibration matrix was correctly
WiredHome 167:8aa3fb2a5a31 706 /// calculated and its value is in the
WiredHome 167:8aa3fb2a5a31 707 /// output argument.
WiredHome 167:8aa3fb2a5a31 708 /// NOT_OK - an error was detected and the
WiredHome 167:8aa3fb2a5a31 709 /// function failed to return a valid
WiredHome 167:8aa3fb2a5a31 710 /// set of matrix values.
WiredHome 167:8aa3fb2a5a31 711 /// The only time this sample code returns
WiredHome 167:8aa3fb2a5a31 712 /// NOT_OK is when Divider == 0
WiredHome 167:8aa3fb2a5a31 713 ///
WiredHome 167:8aa3fb2a5a31 714 ///
WiredHome 167:8aa3fb2a5a31 715 ///
WiredHome 167:8aa3fb2a5a31 716 /// NOTE! NOTE! NOTE!
WiredHome 167:8aa3fb2a5a31 717 ///
WiredHome 167:8aa3fb2a5a31 718 /// setCalibrationMatrix() and getDisplayPoint() will do fine
WiredHome 167:8aa3fb2a5a31 719 /// for you as they are, provided that your digitizer
WiredHome 167:8aa3fb2a5a31 720 /// resolution does not exceed 10 bits (1024 values). Higher
WiredHome 167:8aa3fb2a5a31 721 /// resolutions may cause the integer operations to overflow
WiredHome 167:8aa3fb2a5a31 722 /// and return incorrect values. If you wish to use these
WiredHome 167:8aa3fb2a5a31 723 /// functions with digitizer resolutions of 12 bits (4096
WiredHome 167:8aa3fb2a5a31 724 /// values) you will either have to a) use 64-bit signed
WiredHome 167:8aa3fb2a5a31 725 /// integer variables and math, or b) judiciously modify the
WiredHome 167:8aa3fb2a5a31 726 /// operations to scale results by a factor of 2 or even 4.
WiredHome 167:8aa3fb2a5a31 727 ///
WiredHome 167:8aa3fb2a5a31 728 /// </pre>
WiredHome 167:8aa3fb2a5a31 729 ///
WiredHome 81:01da2e34283d 730 RetCode_t RA8875::TouchPanelComputeCalibration(point_t * displayPtr, point_t * screenPtr, tpMatrix_t * matrixPtr)
WiredHome 78:faf49c381591 731 {
WiredHome 78:faf49c381591 732 RetCode_t retValue = noerror;
WiredHome 78:faf49c381591 733
WiredHome 78:faf49c381591 734 tpMatrix.Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 735 ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 736
WiredHome 78:faf49c381591 737 if( tpMatrix.Divider == 0 ) {
WiredHome 78:faf49c381591 738 retValue = bad_parameter;
WiredHome 78:faf49c381591 739 } else {
WiredHome 78:faf49c381591 740 tpMatrix.An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 741 ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 742
WiredHome 78:faf49c381591 743 tpMatrix.Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
WiredHome 78:faf49c381591 744 ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
WiredHome 78:faf49c381591 745
WiredHome 78:faf49c381591 746 tpMatrix.Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
WiredHome 78:faf49c381591 747 (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
WiredHome 78:faf49c381591 748 (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
WiredHome 78:faf49c381591 749
WiredHome 78:faf49c381591 750 tpMatrix.Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 751 ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 752
WiredHome 78:faf49c381591 753 tpMatrix.En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
WiredHome 78:faf49c381591 754 ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
WiredHome 78:faf49c381591 755
WiredHome 78:faf49c381591 756 tpMatrix.Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
WiredHome 78:faf49c381591 757 (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
WiredHome 78:faf49c381591 758 (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
WiredHome 83:7bad0068cca0 759 touchState = no_touch;
WiredHome 78:faf49c381591 760 if (matrixPtr)
WiredHome 78:faf49c381591 761 memcpy(matrixPtr, &tpMatrix, sizeof(tpMatrix_t));
WiredHome 78:faf49c381591 762 }
WiredHome 78:faf49c381591 763 return( retValue ) ;
WiredHome 78:faf49c381591 764 }
WiredHome 78:faf49c381591 765
WiredHome 157:1565f38ca44b 766 void RA8875::ResTouchPanelCfg(const char * _tpFQFN, const char * _tpCalMessage)
WiredHome 157:1565f38ca44b 767 {
WiredHome 157:1565f38ca44b 768 tpFQFN = _tpFQFN;
WiredHome 157:1565f38ca44b 769 tpCalMessage = _tpCalMessage;
WiredHome 157:1565f38ca44b 770 }
WiredHome 157:1565f38ca44b 771
WiredHome 157:1565f38ca44b 772
WiredHome 157:1565f38ca44b 773 RetCode_t RA8875::_internal_ts_cal()
WiredHome 157:1565f38ca44b 774 {
WiredHome 157:1565f38ca44b 775 FILE * fh;
WiredHome 157:1565f38ca44b 776 tpMatrix_t matrix;
WiredHome 157:1565f38ca44b 777 RetCode_t r = noerror;
WiredHome 190:3132b7dfad82 778
WiredHome 157:1565f38ca44b 779 if (tpFQFN) {
WiredHome 157:1565f38ca44b 780 fh = fopen(tpFQFN, "rb");
WiredHome 157:1565f38ca44b 781 if (fh) {
WiredHome 157:1565f38ca44b 782 fread(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 157:1565f38ca44b 783 fclose(fh);
WiredHome 157:1565f38ca44b 784 TouchPanelSetMatrix(&matrix);
WiredHome 157:1565f38ca44b 785 } else {
WiredHome 157:1565f38ca44b 786 r = TouchPanelCalibrate(tpCalMessage, &matrix);
WiredHome 157:1565f38ca44b 787 if (r == noerror) {
WiredHome 157:1565f38ca44b 788 fh = fopen(tpFQFN, "wb");
WiredHome 157:1565f38ca44b 789 if (fh) {
WiredHome 157:1565f38ca44b 790 fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 157:1565f38ca44b 791 fclose(fh);
WiredHome 157:1565f38ca44b 792 INFO(" tp cal written to '%s'.", tpFQFN);
WiredHome 157:1565f38ca44b 793 } else {
WiredHome 157:1565f38ca44b 794 ERR(" couldn't open tpcal file '%s'.", tpFQFN);
WiredHome 157:1565f38ca44b 795 r = file_not_found;
WiredHome 157:1565f38ca44b 796 }
WiredHome 157:1565f38ca44b 797 } else {
WiredHome 157:1565f38ca44b 798 ERR("error return: %d", r);
WiredHome 157:1565f38ca44b 799 }
WiredHome 157:1565f38ca44b 800 HexDump("TPCal", (const uint8_t *)&matrix, sizeof(tpMatrix_t));
WiredHome 157:1565f38ca44b 801 cls();
WiredHome 157:1565f38ca44b 802 }
WiredHome 157:1565f38ca44b 803 }
WiredHome 157:1565f38ca44b 804 return r;
WiredHome 157:1565f38ca44b 805 }
WiredHome 157:1565f38ca44b 806
WiredHome 157:1565f38ca44b 807
WiredHome 157:1565f38ca44b 808
WiredHome 124:1690a7ae871c 809 ////////////////// Capacitive Touch Panel
WiredHome 124:1690a7ae871c 810
WiredHome 165:695c24cc5197 811 uint8_t RA8875::FT5206_ReadRegU8(uint8_t reg) {
WiredHome 124:1690a7ae871c 812 char val;
WiredHome 190:3132b7dfad82 813
WiredHome 124:1690a7ae871c 814 m_i2c->write(m_addr, (const char *)&reg, 1);
WiredHome 124:1690a7ae871c 815 m_i2c->read(m_addr, &val, 1);
WiredHome 181:0032d1b8f5d4 816 //INFO("ReadReg %02X, Val %02X", reg, val);
WiredHome 124:1690a7ae871c 817 return (uint8_t)val;
WiredHome 124:1690a7ae871c 818 }
WiredHome 124:1690a7ae871c 819
WiredHome 124:1690a7ae871c 820 // Interrupt for touch detection
WiredHome 124:1690a7ae871c 821 void RA8875::TouchPanelISR(void)
WiredHome 124:1690a7ae871c 822 {
WiredHome 165:695c24cc5197 823 if (useTouchPanel == TP_FT5206) {
WiredHome 198:9b6851107426 824 if (FT5206_TouchPositions()) {
WiredHome 165:695c24cc5197 825 panelTouched = true;
WiredHome 198:9b6851107426 826 timeSinceTouch.reset();
WiredHome 198:9b6851107426 827 }
WiredHome 165:695c24cc5197 828 } else if (useTouchPanel == TP_GSL1680) {
WiredHome 198:9b6851107426 829 if (GSL1680_TouchPositions()) {
WiredHome 165:695c24cc5197 830 panelTouched = true;
WiredHome 198:9b6851107426 831 timeSinceTouch.reset();
WiredHome 198:9b6851107426 832 }
WiredHome 165:695c24cc5197 833 }
WiredHome 124:1690a7ae871c 834 }
WiredHome 124:1690a7ae871c 835
WiredHome 78:faf49c381591 836 // #### end of touch panel code additions