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:
Tue Feb 11 21:26:59 2020 +0000
Revision:
196:56820026701b
Parent:
193:74f80834d59d
Child:
197:853d08e2fb53
Added puts w/point_t parameter.

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