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

Dependents:   FRDM_RA8875_mPaint RA8875_Demo RA8875_KeyPadDemo SignalGenerator ... more

Fork of SPI_TFT by Peter Drescher

See Components - RA8875 Based Display

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

Offline Help Manual (Windows chm)

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

Committer:
WiredHome
Date:
Sun Jan 13 19:02:36 2019 +0000
Revision:
161:0215d0eec1a4
Parent:
157:1565f38ca44b
Child:
165:695c24cc5197
Readjusted font parameters - ; <space> width is 1/4 the character height,; '0' - '9' by default as sized to the width of the widest digit.; command line can still override digit width.

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