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:
Sat Aug 06 22:21:41 2016 +0000
Revision:
129:3f2d8b84144d
Parent:
128:3c74ba4533dc
Child:
130:d633e80840e0
Remove dead-code.

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