BurstSPI support for improved performance

Fork of RA8875 by David Smart

Committer:
mooreaa
Date:
Sat Sep 09 09:09:57 2017 +0000
Revision:
150:db4c4a5438fc
Parent:
149:c62c4b2d6a15
Initial

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