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