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

Dependents:   FRDM_RA8875_mPaint RA8875_Demo RA8875_KeyPadDemo SignalGenerator ... more

Fork of SPI_TFT by Peter Drescher

See Components - RA8875 Based Display

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

Offline Help Manual (Windows chm)

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

Committer:
WiredHome
Date:
Sun Jul 31 20:59:01 2016 +0000
Revision:
124:1690a7ae871c
Parent:
123:2f45e80fec5f
Child:
127:db7f2c704693
Incorporated the Capacitive touch panel APIs in to the base RA8875 driver in a manner that integrated smoothly with the Resistive touch driver.

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 83:7bad0068cca0 38 touchTicker.attach_us(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 124:1690a7ae871c 70 touchTicker.attach_us(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 123:2f45e80fec5f 122 if (external_abort == (*idle_callback)(touchcal_wait)) {
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 123:2f45e80fec5f 146 if (external_abort == (*idle_callback)(touchcal_wait)) {
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 123:2f45e80fec5f 160 if (external_abort == (*idle_callback)(touchcal_wait)) {
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 123:2f45e80fec5f 168 if (external_abort == (*idle_callback)(touchcal_wait)) {
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 124:1690a7ae871c 251
WiredHome 103:7e0464ca6c5c 252 /* Operation order is important since we are doing integer */
WiredHome 103:7e0464ca6c5c 253 /* math. Make sure you add all terms together before */
WiredHome 103:7e0464ca6c5c 254 /* dividing, so that the remainder is not rounded off */
WiredHome 103:7e0464ca6c5c 255 /* prematurely. */
WiredHome 124:1690a7ae871c 256 touchInfo[0].coordinates.x = ( (tpMatrix.An * a2dX) +
WiredHome 103:7e0464ca6c5c 257 (tpMatrix.Bn * a2dY) +
WiredHome 103:7e0464ca6c5c 258 tpMatrix.Cn
WiredHome 103:7e0464ca6c5c 259 ) / tpMatrix.Divider ;
WiredHome 81:01da2e34283d 260
WiredHome 124:1690a7ae871c 261 touchInfo[0].coordinates.y = ( (tpMatrix.Dn * a2dX) +
WiredHome 103:7e0464ca6c5c 262 (tpMatrix.En * a2dY) +
WiredHome 103:7e0464ca6c5c 263 tpMatrix.Fn
WiredHome 103:7e0464ca6c5c 264 ) / tpMatrix.Divider ;
WiredHome 124:1690a7ae871c 265 } else {
WiredHome 124:1690a7ae871c 266 ts = no_cal;
WiredHome 103:7e0464ca6c5c 267 }
WiredHome 81:01da2e34283d 268 } else {
WiredHome 124:1690a7ae871c 269 numberOfTouchPoints = 0;
WiredHome 81:01da2e34283d 270 }
WiredHome 124:1690a7ae871c 271 touchInfo[0].touchCode = ts;
WiredHome 124:1690a7ae871c 272 }
WiredHome 124:1690a7ae871c 273 if (panelTouched == true) {
WiredHome 124:1690a7ae871c 274 panelTouched = false;
WiredHome 124:1690a7ae871c 275 if (TouchPoint) {
WiredHome 124:1690a7ae871c 276 *TouchPoint = touchInfo[0].coordinates;
WiredHome 124:1690a7ae871c 277 ts = touchInfo[0].touchCode;
WiredHome 124:1690a7ae871c 278 }
WiredHome 124:1690a7ae871c 279 ts = touch;
WiredHome 81:01da2e34283d 280 }
WiredHome 83:7bad0068cca0 281 return ts;
WiredHome 81:01da2e34283d 282 }
WiredHome 81:01da2e34283d 283
WiredHome 81:01da2e34283d 284
WiredHome 85:022bba13c5c4 285 TouchCode_t RA8875::TouchPanelGet(point_t * TouchPoint)
WiredHome 85:022bba13c5c4 286 {
WiredHome 123:2f45e80fec5f 287 TouchCode_t t = no_touch;
WiredHome 124:1690a7ae871c 288
WiredHome 123:2f45e80fec5f 289 while (true) {
WiredHome 85:022bba13c5c4 290 t = TouchPanelReadable(TouchPoint);
WiredHome 123:2f45e80fec5f 291 if (t != no_touch)
WiredHome 123:2f45e80fec5f 292 break;
WiredHome 123:2f45e80fec5f 293 if (idle_callback) {
WiredHome 123:2f45e80fec5f 294 if (external_abort == (*idle_callback)(touch_wait)) {
WiredHome 123:2f45e80fec5f 295 return no_touch;
WiredHome 123:2f45e80fec5f 296 }
WiredHome 123:2f45e80fec5f 297 }
WiredHome 123:2f45e80fec5f 298 }
WiredHome 85:022bba13c5c4 299 return t;
WiredHome 85:022bba13c5c4 300 }
WiredHome 85:022bba13c5c4 301
WiredHome 123:2f45e80fec5f 302 // Below here are primarily "helper" functions. While many are accessible
WiredHome 123:2f45e80fec5f 303 // to the user code, they usually don't need to be called.
WiredHome 123:2f45e80fec5f 304
WiredHome 81:01da2e34283d 305 RetCode_t RA8875::TouchPanelSetMatrix(tpMatrix_t * matrixPtr)
WiredHome 81:01da2e34283d 306 {
WiredHome 81:01da2e34283d 307 if (matrixPtr == NULL || matrixPtr->Divider == 0)
WiredHome 81:01da2e34283d 308 return bad_parameter;
WiredHome 81:01da2e34283d 309 memcpy(&tpMatrix, matrixPtr, sizeof(tpMatrix_t));
WiredHome 83:7bad0068cca0 310 touchState = no_touch;
WiredHome 81:01da2e34283d 311 return noerror;
WiredHome 81:01da2e34283d 312 }
WiredHome 81:01da2e34283d 313
WiredHome 83:7bad0068cca0 314 static void InsertionSort(int * buf, int bufsize)
WiredHome 83:7bad0068cca0 315 {
WiredHome 83:7bad0068cca0 316 int i, j;
WiredHome 83:7bad0068cca0 317 int temp;
WiredHome 124:1690a7ae871c 318
WiredHome 108:7415c405ee08 319 for(i = 1; i < bufsize; i++) {
WiredHome 83:7bad0068cca0 320 temp = buf[i];
WiredHome 83:7bad0068cca0 321 j = i;
WiredHome 83:7bad0068cca0 322 while( j && (buf[j-1] > temp) ) {
WiredHome 83:7bad0068cca0 323 buf[j] = buf[j-1];
WiredHome 83:7bad0068cca0 324 j = j-1;
WiredHome 83:7bad0068cca0 325 }
WiredHome 83:7bad0068cca0 326 buf[j] = temp;
WiredHome 83:7bad0068cca0 327 } // End of sort
WiredHome 83:7bad0068cca0 328 }
WiredHome 83:7bad0068cca0 329
WiredHome 83:7bad0068cca0 330
WiredHome 83:7bad0068cca0 331 void RA8875::_TouchTicker(void)
WiredHome 78:faf49c381591 332 {
WiredHome 83:7bad0068cca0 333 if (touchTimer.read_us() > NOTOUCH_TIMEOUT_uS) {
WiredHome 83:7bad0068cca0 334 touchSample = 0;
WiredHome 83:7bad0068cca0 335 if (touchState == held)
WiredHome 83:7bad0068cca0 336 touchState = release;
WiredHome 83:7bad0068cca0 337 else
WiredHome 83:7bad0068cca0 338 touchState = no_touch;
WiredHome 83:7bad0068cca0 339 touchTimer.reset();
WiredHome 83:7bad0068cca0 340 }
WiredHome 83:7bad0068cca0 341 }
WiredHome 83:7bad0068cca0 342
WiredHome 83:7bad0068cca0 343 TouchCode_t RA8875::TouchPanelA2DRaw(int *x, int *y)
WiredHome 83:7bad0068cca0 344 {
WiredHome 83:7bad0068cca0 345 if( (ReadCommand(INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register INTC2
WiredHome 83:7bad0068cca0 346 touchTimer.reset();
WiredHome 83:7bad0068cca0 347 *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 348 *x = ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3) ); // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0]
WiredHome 83:7bad0068cca0 349 WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear that TP interrupt flag
WiredHome 83:7bad0068cca0 350 touchState = touch;
WiredHome 83:7bad0068cca0 351 } else {
WiredHome 83:7bad0068cca0 352 touchState = no_touch;
WiredHome 83:7bad0068cca0 353 }
WiredHome 83:7bad0068cca0 354 return touchState;
WiredHome 83:7bad0068cca0 355 }
WiredHome 83:7bad0068cca0 356
WiredHome 83:7bad0068cca0 357 TouchCode_t RA8875::TouchPanelA2DFiltered(int *x, int *y)
WiredHome 83:7bad0068cca0 358 {
WiredHome 83:7bad0068cca0 359 static int xbuf[TPBUFSIZE], ybuf[TPBUFSIZE];
WiredHome 83:7bad0068cca0 360 static int lastX, lastY;
WiredHome 83:7bad0068cca0 361 int i, j;
WiredHome 83:7bad0068cca0 362 TouchCode_t ret = touchState;
WiredHome 78:faf49c381591 363
WiredHome 78:faf49c381591 364 if( (ReadCommand(INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register INTC2
WiredHome 83:7bad0068cca0 365 touchTimer.reset();
WiredHome 78:faf49c381591 366 // Get the next data samples
WiredHome 83:7bad0068cca0 367 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 368 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 369 // Check for a complete set
WiredHome 83:7bad0068cca0 370 if(++touchSample == TPBUFSIZE) {
WiredHome 78:faf49c381591 371 // Buffers are full, so process them using Finn's method described in Analog Dialogue No. 44, Feb 2010
WiredHome 78:faf49c381591 372 // This requires sorting the samples in order of size, then discarding the top 25% and
WiredHome 78:faf49c381591 373 // bottom 25% as noise spikes. Finally, the middle 50% of the values are averaged to
WiredHome 78:faf49c381591 374 // reduce Gaussian noise.
WiredHome 83:7bad0068cca0 375 #if 1
WiredHome 83:7bad0068cca0 376 InsertionSort(ybuf, TPBUFSIZE);
WiredHome 83:7bad0068cca0 377 InsertionSort(xbuf, TPBUFSIZE);
WiredHome 83:7bad0068cca0 378 #else
WiredHome 78:faf49c381591 379 // Sort the Y buffer using an Insertion Sort
WiredHome 78:faf49c381591 380 for(i = 1; i <= TPBUFSIZE; i++) {
WiredHome 78:faf49c381591 381 temp = ybuf[i];
WiredHome 78:faf49c381591 382 j = i;
WiredHome 78:faf49c381591 383 while( j && (ybuf[j-1] > temp) ) {
WiredHome 78:faf49c381591 384 ybuf[j] = ybuf[j-1];
WiredHome 78:faf49c381591 385 j = j-1;
WiredHome 78:faf49c381591 386 }
WiredHome 78:faf49c381591 387 ybuf[j] = temp;
WiredHome 78:faf49c381591 388 } // End of Y sort
WiredHome 78:faf49c381591 389 // Sort the X buffer the same way
WiredHome 78:faf49c381591 390 for(i = 1; i <= TPBUFSIZE; i++) {
WiredHome 78:faf49c381591 391 temp = xbuf[i];
WiredHome 78:faf49c381591 392 j = i;
WiredHome 78:faf49c381591 393 while( j && (xbuf[j-1] > temp) ) {
WiredHome 78:faf49c381591 394 xbuf[j] = xbuf[j-1];
WiredHome 78:faf49c381591 395 j = j-1;
WiredHome 78:faf49c381591 396 }
WiredHome 78:faf49c381591 397 xbuf[j] = temp;
WiredHome 78:faf49c381591 398 } // End of X sort
WiredHome 83:7bad0068cca0 399 #endif
WiredHome 78:faf49c381591 400 // Average the middle half of the Y values and report them
WiredHome 78:faf49c381591 401 j = 0;
WiredHome 78:faf49c381591 402 for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
WiredHome 78:faf49c381591 403 j += ybuf[i];
WiredHome 78:faf49c381591 404 }
WiredHome 83:7bad0068cca0 405 *y = lastY = j * (float)2/TPBUFSIZE; // This is the average
WiredHome 78:faf49c381591 406 // Average the middle half of the X values and report them
WiredHome 78:faf49c381591 407 j = 0;
WiredHome 78:faf49c381591 408 for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
WiredHome 78:faf49c381591 409 j += xbuf[i];
WiredHome 78:faf49c381591 410 }
WiredHome 83:7bad0068cca0 411 *x = lastX = j * (float)2/TPBUFSIZE; // This is the average
WiredHome 78:faf49c381591 412 // Tidy up and return
WiredHome 83:7bad0068cca0 413 if (touchState == touch || touchState == held)
WiredHome 83:7bad0068cca0 414 touchState = held;
WiredHome 83:7bad0068cca0 415 else
WiredHome 83:7bad0068cca0 416 touchState = touch;
WiredHome 83:7bad0068cca0 417 ret = touchState;
WiredHome 83:7bad0068cca0 418 touchSample = 0; // Ready to start on the next set of data samples
WiredHome 78:faf49c381591 419 } else {
WiredHome 78:faf49c381591 420 // Buffer not yet full, so do not return any results yet
WiredHome 83:7bad0068cca0 421 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 422 *x = lastX;
WiredHome 83:7bad0068cca0 423 *y = lastY;
WiredHome 83:7bad0068cca0 424 ret = touchState = held;
WiredHome 83:7bad0068cca0 425 }
WiredHome 78:faf49c381591 426 }
WiredHome 78:faf49c381591 427 WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear that TP interrupt flag
WiredHome 78:faf49c381591 428 } // End of initial if -- data has been read and processed
WiredHome 83:7bad0068cca0 429 else {
WiredHome 83:7bad0068cca0 430 if (touchState == touch || touchState == held) {
WiredHome 83:7bad0068cca0 431 *x = lastX;
WiredHome 83:7bad0068cca0 432 *y = lastY;
WiredHome 83:7bad0068cca0 433 ret = touchState = held;
WiredHome 83:7bad0068cca0 434 } else if (touchState == release) {
WiredHome 83:7bad0068cca0 435 *x = lastX;
WiredHome 83:7bad0068cca0 436 *y = lastY;
WiredHome 83:7bad0068cca0 437 ret = release;
WiredHome 83:7bad0068cca0 438 touchState = no_touch;
WiredHome 83:7bad0068cca0 439 }
WiredHome 83:7bad0068cca0 440 }
WiredHome 83:7bad0068cca0 441 return ret;
WiredHome 78:faf49c381591 442 }
WiredHome 78:faf49c381591 443
WiredHome 78:faf49c381591 444 /* The following section is derived from Carlos E. Vidales.
WiredHome 78:faf49c381591 445 *
WiredHome 78:faf49c381591 446 * Copyright (c) 2001, Carlos E. Vidales. All rights reserved.
WiredHome 78:faf49c381591 447 *
WiredHome 124:1690a7ae871c 448 * This sample program was written and put in the public domain
WiredHome 124:1690a7ae871c 449 * by Carlos E. Vidales. The program is provided "as is"
WiredHome 78:faf49c381591 450 * without warranty of any kind, either expressed or implied.
WiredHome 78:faf49c381591 451 * If you choose to use the program within your own products
WiredHome 78:faf49c381591 452 * you do so at your own risk, and assume the responsibility
WiredHome 78:faf49c381591 453 * for servicing, repairing or correcting the program should
WiredHome 78:faf49c381591 454 * it prove defective in any manner.
WiredHome 124:1690a7ae871c 455 * You may copy and distribute the program's source code in any
WiredHome 78:faf49c381591 456 * medium, provided that you also include in each copy an
WiredHome 78:faf49c381591 457 * appropriate copyright notice and disclaimer of warranty.
WiredHome 78:faf49c381591 458 * You may also modify this program and distribute copies of
WiredHome 124:1690a7ae871c 459 * it provided that you include prominent notices stating
WiredHome 78:faf49c381591 460 * that you changed the file(s) and the date of any change,
WiredHome 124:1690a7ae871c 461 * and that you do not charge any royalties or licenses for
WiredHome 78:faf49c381591 462 * its use.
WiredHome 124:1690a7ae871c 463 *
WiredHome 124:1690a7ae871c 464 * This file contains functions that implement calculations
WiredHome 78:faf49c381591 465 * necessary to obtain calibration factors for a touch screen
WiredHome 124:1690a7ae871c 466 * that suffers from multiple distortion effects: namely,
WiredHome 78:faf49c381591 467 * translation, scaling and rotation.
WiredHome 78:faf49c381591 468 *
WiredHome 124:1690a7ae871c 469 * The following set of equations represent a valid display
WiredHome 78:faf49c381591 470 * point given a corresponding set of touch screen points:
WiredHome 78:faf49c381591 471 *
WiredHome 78:faf49c381591 472 * /- -\
WiredHome 78:faf49c381591 473 * /- -\ /- -\ | |
WiredHome 78:faf49c381591 474 * | | | | | Xs |
WiredHome 78:faf49c381591 475 * | Xd | | A B C | | |
WiredHome 78:faf49c381591 476 * | | = | | * | Ys |
WiredHome 78:faf49c381591 477 * | Yd | | D E F | | |
WiredHome 78:faf49c381591 478 * | | | | | 1 |
WiredHome 78:faf49c381591 479 * \- -/ \- -/ | |
WiredHome 78:faf49c381591 480 * \- -/
WiredHome 78:faf49c381591 481 * where:
WiredHome 124:1690a7ae871c 482 * (Xd,Yd) represents the desired display point
WiredHome 78:faf49c381591 483 * coordinates,
WiredHome 78:faf49c381591 484 * (Xs,Ys) represents the available touch screen
WiredHome 78:faf49c381591 485 * coordinates, and the matrix
WiredHome 78:faf49c381591 486 * /- -\
WiredHome 78:faf49c381591 487 * |A,B,C|
WiredHome 78:faf49c381591 488 * |D,E,F| represents the factors used to translate
WiredHome 78:faf49c381591 489 * \- -/ the available touch screen point values
WiredHome 124:1690a7ae871c 490 * into the corresponding display
WiredHome 78:faf49c381591 491 * coordinates.
WiredHome 124:1690a7ae871c 492 * Note that for practical considerations, the utilities
WiredHome 78:faf49c381591 493 * within this file do not use the matrix coefficients as
WiredHome 124:1690a7ae871c 494 * defined above, but instead use the following
WiredHome 78:faf49c381591 495 * equivalents, since floating point math is not used:
WiredHome 124:1690a7ae871c 496 * A = An/Divider
WiredHome 124:1690a7ae871c 497 * B = Bn/Divider
WiredHome 124:1690a7ae871c 498 * C = Cn/Divider
WiredHome 124:1690a7ae871c 499 * D = Dn/Divider
WiredHome 124:1690a7ae871c 500 * E = En/Divider
WiredHome 124:1690a7ae871c 501 * F = Fn/Divider
WiredHome 78:faf49c381591 502 * The functions provided within this file are:
WiredHome 78:faf49c381591 503 * setCalibrationMatrix() - calculates the set of factors
WiredHome 78:faf49c381591 504 * in the above equation, given
WiredHome 78:faf49c381591 505 * three sets of test points.
WiredHome 78:faf49c381591 506 * getDisplayPoint() - returns the actual display
WiredHome 78:faf49c381591 507 * coordinates, given a set of
WiredHome 78:faf49c381591 508 * touch screen coordinates.
WiredHome 78:faf49c381591 509 * translateRawScreenCoordinates() - helper function to transform
WiredHome 78:faf49c381591 510 * raw screen points into values
WiredHome 78:faf49c381591 511 * scaled to the desired display
WiredHome 78:faf49c381591 512 * resolution.
WiredHome 78:faf49c381591 513 */
WiredHome 78:faf49c381591 514
WiredHome 78:faf49c381591 515 /**********************************************************************
WiredHome 78:faf49c381591 516 *
WiredHome 78:faf49c381591 517 * Function: setCalibrationMatrix()
WiredHome 78:faf49c381591 518 *
WiredHome 78:faf49c381591 519 * Description: Calling this function with valid input data
WiredHome 124:1690a7ae871c 520 * in the display and screen input arguments
WiredHome 78:faf49c381591 521 * causes the calibration factors between the
WiredHome 78:faf49c381591 522 * screen and display points to be calculated,
WiredHome 124:1690a7ae871c 523 * and the output argument - matrixPtr - to be
WiredHome 78:faf49c381591 524 * populated.
WiredHome 78:faf49c381591 525 *
WiredHome 78:faf49c381591 526 * This function needs to be called only when new
WiredHome 78:faf49c381591 527 * calibration factors are desired.
WiredHome 124:1690a7ae871c 528 *
WiredHome 124:1690a7ae871c 529 *
WiredHome 124:1690a7ae871c 530 * Argument(s): displayPtr (input) - Pointer to an array of three
WiredHome 78:faf49c381591 531 * sample, reference points.
WiredHome 124:1690a7ae871c 532 * screenPtr (input) - Pointer to the array of touch
WiredHome 124:1690a7ae871c 533 * screen points corresponding
WiredHome 78:faf49c381591 534 * to the reference display points.
WiredHome 124:1690a7ae871c 535 * matrixPtr (output) - Pointer to the calibration
WiredHome 124:1690a7ae871c 536 * matrix computed for the set
WiredHome 78:faf49c381591 537 * of points being provided.
WiredHome 78:faf49c381591 538 *
WiredHome 78:faf49c381591 539 *
WiredHome 78:faf49c381591 540 * From the article text, recall that the matrix coefficients are
WiredHome 78:faf49c381591 541 * resolved to be the following:
WiredHome 78:faf49c381591 542 *
WiredHome 78:faf49c381591 543 *
WiredHome 78:faf49c381591 544 * Divider = (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2)
WiredHome 78:faf49c381591 545 *
WiredHome 78:faf49c381591 546 *
WiredHome 78:faf49c381591 547 *
WiredHome 78:faf49c381591 548 * (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2)
WiredHome 78:faf49c381591 549 * A = ---------------------------------------------------
WiredHome 78:faf49c381591 550 * Divider
WiredHome 78:faf49c381591 551 *
WiredHome 78:faf49c381591 552 *
WiredHome 78:faf49c381591 553 * (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2)
WiredHome 78:faf49c381591 554 * B = ---------------------------------------------------
WiredHome 78:faf49c381591 555 * Divider
WiredHome 78:faf49c381591 556 *
WiredHome 78:faf49c381591 557 *
WiredHome 124:1690a7ae871c 558 * Ys0*(Xs2*Xd1 - Xs1*Xd2) +
WiredHome 124:1690a7ae871c 559 * Ys1*(Xs0*Xd2 - Xs2*Xd0) +
WiredHome 78:faf49c381591 560 * Ys2*(Xs1*Xd0 - Xs0*Xd1)
WiredHome 78:faf49c381591 561 * C = ---------------------------------------------------
WiredHome 78:faf49c381591 562 * Divider
WiredHome 78:faf49c381591 563 *
WiredHome 78:faf49c381591 564 *
WiredHome 78:faf49c381591 565 * (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2)
WiredHome 78:faf49c381591 566 * D = ---------------------------------------------------
WiredHome 78:faf49c381591 567 * Divider
WiredHome 78:faf49c381591 568 *
WiredHome 78:faf49c381591 569 *
WiredHome 78:faf49c381591 570 * (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2)
WiredHome 78:faf49c381591 571 * E = ---------------------------------------------------
WiredHome 78:faf49c381591 572 * Divider
WiredHome 78:faf49c381591 573 *
WiredHome 78:faf49c381591 574 *
WiredHome 124:1690a7ae871c 575 * Ys0*(Xs2*Yd1 - Xs1*Yd2) +
WiredHome 124:1690a7ae871c 576 * Ys1*(Xs0*Yd2 - Xs2*Yd0) +
WiredHome 78:faf49c381591 577 * Ys2*(Xs1*Yd0 - Xs0*Yd1)
WiredHome 78:faf49c381591 578 * F = ---------------------------------------------------
WiredHome 78:faf49c381591 579 * Divider
WiredHome 78:faf49c381591 580 *
WiredHome 78:faf49c381591 581 *
WiredHome 124:1690a7ae871c 582 * Return: OK - the calibration matrix was correctly
WiredHome 124:1690a7ae871c 583 * calculated and its value is in the
WiredHome 78:faf49c381591 584 * output argument.
WiredHome 124:1690a7ae871c 585 * NOT_OK - an error was detected and the
WiredHome 78:faf49c381591 586 * function failed to return a valid
WiredHome 78:faf49c381591 587 * set of matrix values.
WiredHome 78:faf49c381591 588 * The only time this sample code returns
WiredHome 78:faf49c381591 589 * NOT_OK is when Divider == 0
WiredHome 78:faf49c381591 590 *
WiredHome 78:faf49c381591 591 *
WiredHome 78:faf49c381591 592 *
WiredHome 78:faf49c381591 593 * NOTE! NOTE! NOTE!
WiredHome 78:faf49c381591 594 *
WiredHome 78:faf49c381591 595 * setCalibrationMatrix() and getDisplayPoint() will do fine
WiredHome 124:1690a7ae871c 596 * for you as they are, provided that your digitizer
WiredHome 78:faf49c381591 597 * resolution does not exceed 10 bits (1024 values). Higher
WiredHome 78:faf49c381591 598 * resolutions may cause the integer operations to overflow
WiredHome 124:1690a7ae871c 599 * and return incorrect values. If you wish to use these
WiredHome 124:1690a7ae871c 600 * functions with digitizer resolutions of 12 bits (4096
WiredHome 124:1690a7ae871c 601 * values) you will either have to a) use 64-bit signed
WiredHome 124:1690a7ae871c 602 * integer variables and math, or b) judiciously modify the
WiredHome 124:1690a7ae871c 603 * operations to scale results by a factor of 2 or even 4.
WiredHome 78:faf49c381591 604 *
WiredHome 78:faf49c381591 605 */
WiredHome 81:01da2e34283d 606 RetCode_t RA8875::TouchPanelComputeCalibration(point_t * displayPtr, point_t * screenPtr, tpMatrix_t * matrixPtr)
WiredHome 78:faf49c381591 607 {
WiredHome 78:faf49c381591 608 RetCode_t retValue = noerror;
WiredHome 78:faf49c381591 609
WiredHome 78:faf49c381591 610 tpMatrix.Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 611 ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 612
WiredHome 78:faf49c381591 613 if( tpMatrix.Divider == 0 ) {
WiredHome 78:faf49c381591 614 retValue = bad_parameter;
WiredHome 78:faf49c381591 615 } else {
WiredHome 78:faf49c381591 616 tpMatrix.An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 617 ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 618
WiredHome 78:faf49c381591 619 tpMatrix.Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
WiredHome 78:faf49c381591 620 ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
WiredHome 78:faf49c381591 621
WiredHome 78:faf49c381591 622 tpMatrix.Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
WiredHome 78:faf49c381591 623 (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
WiredHome 78:faf49c381591 624 (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
WiredHome 78:faf49c381591 625
WiredHome 78:faf49c381591 626 tpMatrix.Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
WiredHome 78:faf49c381591 627 ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
WiredHome 78:faf49c381591 628
WiredHome 78:faf49c381591 629 tpMatrix.En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
WiredHome 78:faf49c381591 630 ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
WiredHome 78:faf49c381591 631
WiredHome 78:faf49c381591 632 tpMatrix.Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
WiredHome 78:faf49c381591 633 (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
WiredHome 78:faf49c381591 634 (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
WiredHome 83:7bad0068cca0 635 touchState = no_touch;
WiredHome 78:faf49c381591 636 if (matrixPtr)
WiredHome 78:faf49c381591 637 memcpy(matrixPtr, &tpMatrix, sizeof(tpMatrix_t));
WiredHome 78:faf49c381591 638 }
WiredHome 78:faf49c381591 639 return( retValue ) ;
WiredHome 78:faf49c381591 640 }
WiredHome 78:faf49c381591 641
WiredHome 124:1690a7ae871c 642 ////////////////// Capacitive Touch Panel
WiredHome 124:1690a7ae871c 643
WiredHome 124:1690a7ae871c 644 uint8_t RA8875::readRegister8(uint8_t reg) {
WiredHome 124:1690a7ae871c 645 char val;
WiredHome 124:1690a7ae871c 646
WiredHome 124:1690a7ae871c 647 m_i2c->write(m_addr, (const char *)&reg, 1);
WiredHome 124:1690a7ae871c 648 m_i2c->read(m_addr, &val, 1);
WiredHome 124:1690a7ae871c 649 return (uint8_t)val;
WiredHome 124:1690a7ae871c 650 }
WiredHome 124:1690a7ae871c 651
WiredHome 124:1690a7ae871c 652 void RA8875::writeRegister8(uint8_t reg, uint8_t val) {
WiredHome 124:1690a7ae871c 653 char data[2];
WiredHome 124:1690a7ae871c 654
WiredHome 124:1690a7ae871c 655 data[0] = (char)reg;
WiredHome 124:1690a7ae871c 656 data[1] = (char)val;
WiredHome 124:1690a7ae871c 657 m_i2c->write((int)FT5206_I2C_ADDRESS, data, 2);
WiredHome 124:1690a7ae871c 658 }
WiredHome 124:1690a7ae871c 659
WiredHome 124:1690a7ae871c 660
WiredHome 124:1690a7ae871c 661 // Interrupt for touch detection
WiredHome 124:1690a7ae871c 662 void RA8875::TouchPanelISR(void)
WiredHome 124:1690a7ae871c 663 {
WiredHome 124:1690a7ae871c 664 getTouchPositions();
WiredHome 124:1690a7ae871c 665 panelTouched = true;
WiredHome 124:1690a7ae871c 666 }
WiredHome 124:1690a7ae871c 667
WiredHome 124:1690a7ae871c 668 uint8_t RA8875::getTouchPositions(void) {
WiredHome 124:1690a7ae871c 669 uint8_t valXH;
WiredHome 124:1690a7ae871c 670 uint8_t valYH;
WiredHome 124:1690a7ae871c 671
WiredHome 124:1690a7ae871c 672 numberOfTouchPoints = readRegister8(FT5206_TD_STATUS) & 0xF;
WiredHome 124:1690a7ae871c 673 gesture = readRegister8(FT5206_GEST_ID);
WiredHome 124:1690a7ae871c 674
WiredHome 124:1690a7ae871c 675 // If the switch statement was based only on numberOfTouchPoints, it would not
WiredHome 124:1690a7ae871c 676 // be able to generate notification for 'release' events (as it is no longer touched).
WiredHome 124:1690a7ae871c 677 // Therefore, forcing a 5, and it intentially falls through each lower case.
WiredHome 124:1690a7ae871c 678 switch (5) { // numberOfTouchPoints
WiredHome 124:1690a7ae871c 679 case 5:
WiredHome 124:1690a7ae871c 680 valXH = readRegister8(FT5206_TOUCH5_XH);
WiredHome 124:1690a7ae871c 681 valYH = readRegister8(FT5206_TOUCH5_YH);
WiredHome 124:1690a7ae871c 682 touchInfo[4].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 683 touchInfo[4].touchID = (valYH >> 4);
WiredHome 124:1690a7ae871c 684 touchInfo[4].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH5_XL);
WiredHome 124:1690a7ae871c 685 touchInfo[4].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH5_YL);
WiredHome 124:1690a7ae871c 686 case 4:
WiredHome 124:1690a7ae871c 687 valXH = readRegister8(FT5206_TOUCH4_XH);
WiredHome 124:1690a7ae871c 688 valYH = readRegister8(FT5206_TOUCH4_YH);
WiredHome 124:1690a7ae871c 689 touchInfo[3].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 690 touchInfo[3].touchID = (valYH >> 4);
WiredHome 124:1690a7ae871c 691 touchInfo[3].coordinates.x = (readRegister8(FT5206_TOUCH4_XH) & 0x0f)*256 + readRegister8(FT5206_TOUCH4_XL);
WiredHome 124:1690a7ae871c 692 touchInfo[3].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH4_YL);
WiredHome 124:1690a7ae871c 693 case 3:
WiredHome 124:1690a7ae871c 694 valXH = readRegister8(FT5206_TOUCH3_XH);
WiredHome 124:1690a7ae871c 695 valYH = readRegister8(FT5206_TOUCH3_YH);
WiredHome 124:1690a7ae871c 696 touchInfo[2].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 697 touchInfo[2].touchID = (valYH >> 4);
WiredHome 124:1690a7ae871c 698 touchInfo[2].coordinates.x = (readRegister8(FT5206_TOUCH3_XH) & 0x0f)*256 + readRegister8(FT5206_TOUCH3_XL);
WiredHome 124:1690a7ae871c 699 touchInfo[2].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH3_YL);
WiredHome 124:1690a7ae871c 700 case 2:
WiredHome 124:1690a7ae871c 701 valXH = readRegister8(FT5206_TOUCH2_XH);
WiredHome 124:1690a7ae871c 702 valYH = readRegister8(FT5206_TOUCH2_YH);
WiredHome 124:1690a7ae871c 703 touchInfo[1].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 704 touchInfo[1].touchID = (valYH >> 4);
WiredHome 124:1690a7ae871c 705 touchInfo[1].coordinates.x = (readRegister8(FT5206_TOUCH2_XH) & 0x0f)*256 + readRegister8(FT5206_TOUCH2_XL);
WiredHome 124:1690a7ae871c 706 touchInfo[1].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH2_YL);
WiredHome 124:1690a7ae871c 707 case 1:
WiredHome 124:1690a7ae871c 708 valXH = readRegister8(FT5206_TOUCH1_XH);
WiredHome 124:1690a7ae871c 709 valYH = readRegister8(FT5206_TOUCH1_YH);
WiredHome 124:1690a7ae871c 710 touchInfo[0].touchCode = EventFlagToTouchCode[valXH >> 6];
WiredHome 124:1690a7ae871c 711 touchInfo[0].touchID = (valYH >> 4);
WiredHome 124:1690a7ae871c 712 touchInfo[0].coordinates.x = (readRegister8(FT5206_TOUCH1_XH) & 0x0f)*256 + readRegister8(FT5206_TOUCH1_XL);
WiredHome 124:1690a7ae871c 713 touchInfo[0].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH1_YL);
WiredHome 124:1690a7ae871c 714 break;
WiredHome 124:1690a7ae871c 715 default:
WiredHome 124:1690a7ae871c 716 break;
WiredHome 124:1690a7ae871c 717 }
WiredHome 124:1690a7ae871c 718 return numberOfTouchPoints;
WiredHome 124:1690a7ae871c 719 }
WiredHome 124:1690a7ae871c 720
WiredHome 78:faf49c381591 721 // #### end of touch panel code additions