Forked para SNOCC
Fork of RA8875 by
RA8875_Touch.cpp@79:544eb4964795, 2014-12-28 (annotated)
- Committer:
- WiredHome
- Date:
- Sun Dec 28 19:55:16 2014 +0000
- Revision:
- 79:544eb4964795
- Parent:
- 78:faf49c381591
- Child:
- 81:01da2e34283d
cleaned up touch screen support, which included renaming the APIs to better reflect their purpose.; remove dead code, added documentation for methods that were missing it.; major refactor to the constructor, to move needed code to init().
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 78:faf49c381591 | 1 | /// This file contains the RA8875 Touch panel methods. |
WiredHome | 78:faf49c381591 | 2 | /// |
WiredHome | 78:faf49c381591 | 3 | |
WiredHome | 78:faf49c381591 | 4 | #include "RA8875.h" |
WiredHome | 78:faf49c381591 | 5 | |
WiredHome | 78:faf49c381591 | 6 | // ### Touch Panel support code additions begin here |
WiredHome | 78:faf49c381591 | 7 | |
WiredHome | 78:faf49c381591 | 8 | RetCode_t RA8875::TouchPanelInit(void) |
WiredHome | 78:faf49c381591 | 9 | { |
WiredHome | 78:faf49c381591 | 10 | //TPCR0: Set enable bit, default sample time, wakeup, and ADC clock |
WiredHome | 78:faf49c381591 | 11 | WriteCommand(TPCR0, TP_ENABLE | TP_ADC_SAMPLE_DEFAULT_CLKS | TP_ADC_CLKDIV_DEFAULT); |
WiredHome | 78:faf49c381591 | 12 | // TPCR1: Set auto/manual, Ref voltage, debounce, manual mode params |
WiredHome | 78:faf49c381591 | 13 | WriteCommand(TPCR1, TP_MODE_DEFAULT | TP_DEBOUNCE_DEFAULT); |
WiredHome | 78:faf49c381591 | 14 | WriteCommand(INTC1, ReadCommand(INTC1) | RA8875_INT_TP); // reg INTC1: Enable Touch Panel Interrupts (D2 = 1) |
WiredHome | 78:faf49c381591 | 15 | WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear any TP interrupt flag |
WiredHome | 78:faf49c381591 | 16 | return noerror; |
WiredHome | 78:faf49c381591 | 17 | } |
WiredHome | 78:faf49c381591 | 18 | |
WiredHome | 78:faf49c381591 | 19 | RetCode_t RA8875::TouchPanelInit(uint8_t bTpEnable, uint8_t bTpAutoManual, uint8_t bTpDebounce, uint8_t bTpManualMode, uint8_t bTpAdcClkDiv, uint8_t bTpAdcSampleTime) |
WiredHome | 78:faf49c381591 | 20 | { |
WiredHome | 78:faf49c381591 | 21 | // Parameter bounds check |
WiredHome | 78:faf49c381591 | 22 | if( \ |
WiredHome | 78:faf49c381591 | 23 | !(bTpEnable == TP_ENABLE || bTpEnable == TP_ENABLE) || \ |
WiredHome | 78:faf49c381591 | 24 | !(bTpAutoManual == TP_MODE_AUTO || bTpAutoManual == TP_MODE_MANUAL) || \ |
WiredHome | 78:faf49c381591 | 25 | !(bTpDebounce == TP_DEBOUNCE_OFF || bTpDebounce == TP_DEBOUNCE_ON) || \ |
WiredHome | 78:faf49c381591 | 26 | !(bTpManualMode <= TP_MANUAL_LATCH_Y) || \ |
WiredHome | 78:faf49c381591 | 27 | !(bTpAdcClkDiv <= TP_ADC_CLKDIV_128) || \ |
WiredHome | 78:faf49c381591 | 28 | !(bTpAdcSampleTime <= TP_ADC_SAMPLE_65536_CLKS) \ |
WiredHome | 78:faf49c381591 | 29 | ) return bad_parameter; |
WiredHome | 78:faf49c381591 | 30 | // Construct the config byte for TPCR0 and write them |
WiredHome | 78:faf49c381591 | 31 | WriteCommand(TPCR0, bTpEnable | bTpAdcClkDiv | bTpAdcSampleTime); // Note: Wakeup is never enabled |
WiredHome | 78:faf49c381591 | 32 | // Construct the config byte for TPCR1 and write them |
WiredHome | 78:faf49c381591 | 33 | WriteCommand(TPCR1, bTpManualMode | bTpDebounce | bTpManualMode); // Note: Always uses internal Vref. |
WiredHome | 78:faf49c381591 | 34 | // Set up the interrupt flag and enable bits |
WiredHome | 78:faf49c381591 | 35 | WriteCommand(INTC1, ReadCommand(INTC1) | RA8875_INT_TP); // reg INTC1: Enable Touch Panel Interrupts (D2 = 1) |
WiredHome | 78:faf49c381591 | 36 | WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear any TP interrupt flag |
WiredHome | 78:faf49c381591 | 37 | return noerror; |
WiredHome | 78:faf49c381591 | 38 | } |
WiredHome | 78:faf49c381591 | 39 | |
WiredHome | 79:544eb4964795 | 40 | bool RA8875::TouchPanelA2DFiltered(loc_t *x, loc_t *y) |
WiredHome | 78:faf49c381591 | 41 | { |
WiredHome | 78:faf49c381591 | 42 | unsigned char touchready; |
WiredHome | 78:faf49c381591 | 43 | static int xbuf[TPBUFSIZE], ybuf[TPBUFSIZE], sample = 0; |
WiredHome | 78:faf49c381591 | 44 | int i, j, temp; |
WiredHome | 78:faf49c381591 | 45 | |
WiredHome | 78:faf49c381591 | 46 | if( (ReadCommand(INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register INTC2 |
WiredHome | 78:faf49c381591 | 47 | // Get the next data samples |
WiredHome | 78:faf49c381591 | 48 | ybuf[sample] = ReadCommand(TPYH) << 2 | ( (ReadCommand(TPXYL) & 0xC) >> 2 ); // D[9:2] from reg TPYH, D[1:0] from reg TPXYL[3:2] |
WiredHome | 78:faf49c381591 | 49 | xbuf[sample] = ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3) ); // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0] |
WiredHome | 78:faf49c381591 | 50 | // Check for a complete set |
WiredHome | 78:faf49c381591 | 51 | if(++sample == TPBUFSIZE) { |
WiredHome | 78:faf49c381591 | 52 | // Buffers are full, so process them using Finn's method described in Analog Dialogue No. 44, Feb 2010 |
WiredHome | 78:faf49c381591 | 53 | // This requires sorting the samples in order of size, then discarding the top 25% and |
WiredHome | 78:faf49c381591 | 54 | // bottom 25% as noise spikes. Finally, the middle 50% of the values are averaged to |
WiredHome | 78:faf49c381591 | 55 | // reduce Gaussian noise. |
WiredHome | 78:faf49c381591 | 56 | |
WiredHome | 78:faf49c381591 | 57 | // Sort the Y buffer using an Insertion Sort |
WiredHome | 78:faf49c381591 | 58 | for(i = 1; i <= TPBUFSIZE; i++) { |
WiredHome | 78:faf49c381591 | 59 | temp = ybuf[i]; |
WiredHome | 78:faf49c381591 | 60 | j = i; |
WiredHome | 78:faf49c381591 | 61 | while( j && (ybuf[j-1] > temp) ) { |
WiredHome | 78:faf49c381591 | 62 | ybuf[j] = ybuf[j-1]; |
WiredHome | 78:faf49c381591 | 63 | j = j-1; |
WiredHome | 78:faf49c381591 | 64 | } |
WiredHome | 78:faf49c381591 | 65 | ybuf[j] = temp; |
WiredHome | 78:faf49c381591 | 66 | } // End of Y sort |
WiredHome | 78:faf49c381591 | 67 | // Sort the X buffer the same way |
WiredHome | 78:faf49c381591 | 68 | for(i = 1; i <= TPBUFSIZE; i++) { |
WiredHome | 78:faf49c381591 | 69 | temp = xbuf[i]; |
WiredHome | 78:faf49c381591 | 70 | j = i; |
WiredHome | 78:faf49c381591 | 71 | while( j && (xbuf[j-1] > temp) ) { |
WiredHome | 78:faf49c381591 | 72 | xbuf[j] = xbuf[j-1]; |
WiredHome | 78:faf49c381591 | 73 | j = j-1; |
WiredHome | 78:faf49c381591 | 74 | } |
WiredHome | 78:faf49c381591 | 75 | xbuf[j] = temp; |
WiredHome | 78:faf49c381591 | 76 | } // End of X sort |
WiredHome | 78:faf49c381591 | 77 | // Average the middle half of the Y values and report them |
WiredHome | 78:faf49c381591 | 78 | j = 0; |
WiredHome | 78:faf49c381591 | 79 | for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) { |
WiredHome | 78:faf49c381591 | 80 | j += ybuf[i]; |
WiredHome | 78:faf49c381591 | 81 | } |
WiredHome | 78:faf49c381591 | 82 | *y = j * (float)2/TPBUFSIZE; // This is the average |
WiredHome | 78:faf49c381591 | 83 | // Average the middle half of the X values and report them |
WiredHome | 78:faf49c381591 | 84 | j = 0; |
WiredHome | 78:faf49c381591 | 85 | for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) { |
WiredHome | 78:faf49c381591 | 86 | j += xbuf[i]; |
WiredHome | 78:faf49c381591 | 87 | } |
WiredHome | 78:faf49c381591 | 88 | *x = j * (float)2/TPBUFSIZE; // This is the average |
WiredHome | 78:faf49c381591 | 89 | // Tidy up and return |
WiredHome | 78:faf49c381591 | 90 | touchready = 1; |
WiredHome | 78:faf49c381591 | 91 | sample = 0; // Ready to start on the next set of data samples |
WiredHome | 78:faf49c381591 | 92 | } else { |
WiredHome | 78:faf49c381591 | 93 | // Buffer not yet full, so do not return any results yet |
WiredHome | 78:faf49c381591 | 94 | touchready = 0; |
WiredHome | 78:faf49c381591 | 95 | } |
WiredHome | 78:faf49c381591 | 96 | WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear that TP interrupt flag |
WiredHome | 78:faf49c381591 | 97 | } // End of initial if -- data has been read and processed |
WiredHome | 78:faf49c381591 | 98 | else |
WiredHome | 78:faf49c381591 | 99 | touchready = 0; // Touch Panel "Int" was not set |
WiredHome | 78:faf49c381591 | 100 | return touchready; |
WiredHome | 78:faf49c381591 | 101 | } |
WiredHome | 78:faf49c381591 | 102 | |
WiredHome | 79:544eb4964795 | 103 | bool RA8875::TouchPanelA2DRaw(loc_t *x, loc_t *y) |
WiredHome | 78:faf49c381591 | 104 | { |
WiredHome | 78:faf49c381591 | 105 | unsigned char touchready; |
WiredHome | 78:faf49c381591 | 106 | |
WiredHome | 78:faf49c381591 | 107 | if( (ReadCommand(INTC2) & RA8875_INT_TP) ) { // Test for TP Interrupt pending in register INTC2 |
WiredHome | 78:faf49c381591 | 108 | *y = ReadCommand(TPYH) << 2 | ( (ReadCommand(TPXYL) & 0xC) >> 2 ); // D[9:2] from reg TPYH, D[1:0] from reg TPXYL[3:2] |
WiredHome | 78:faf49c381591 | 109 | *x = ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3) ); // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0] |
WiredHome | 78:faf49c381591 | 110 | WriteCommand(INTC2, RA8875_INT_TP); // reg INTC2: Clear that TP interrupt flag |
WiredHome | 78:faf49c381591 | 111 | touchready = 1; |
WiredHome | 78:faf49c381591 | 112 | } else |
WiredHome | 78:faf49c381591 | 113 | touchready = 0; |
WiredHome | 78:faf49c381591 | 114 | return touchready; |
WiredHome | 78:faf49c381591 | 115 | } |
WiredHome | 78:faf49c381591 | 116 | |
WiredHome | 78:faf49c381591 | 117 | /* The following section is derived from Carlos E. Vidales. |
WiredHome | 78:faf49c381591 | 118 | * |
WiredHome | 78:faf49c381591 | 119 | * Copyright (c) 2001, Carlos E. Vidales. All rights reserved. |
WiredHome | 78:faf49c381591 | 120 | * |
WiredHome | 78:faf49c381591 | 121 | * This sample program was written and put in the public domain |
WiredHome | 78:faf49c381591 | 122 | * by Carlos E. Vidales. The program is provided "as is" |
WiredHome | 78:faf49c381591 | 123 | * without warranty of any kind, either expressed or implied. |
WiredHome | 78:faf49c381591 | 124 | * If you choose to use the program within your own products |
WiredHome | 78:faf49c381591 | 125 | * you do so at your own risk, and assume the responsibility |
WiredHome | 78:faf49c381591 | 126 | * for servicing, repairing or correcting the program should |
WiredHome | 78:faf49c381591 | 127 | * it prove defective in any manner. |
WiredHome | 78:faf49c381591 | 128 | * You may copy and distribute the program's source code in any |
WiredHome | 78:faf49c381591 | 129 | * medium, provided that you also include in each copy an |
WiredHome | 78:faf49c381591 | 130 | * appropriate copyright notice and disclaimer of warranty. |
WiredHome | 78:faf49c381591 | 131 | * You may also modify this program and distribute copies of |
WiredHome | 78:faf49c381591 | 132 | * it provided that you include prominent notices stating |
WiredHome | 78:faf49c381591 | 133 | * that you changed the file(s) and the date of any change, |
WiredHome | 78:faf49c381591 | 134 | * and that you do not charge any royalties or licenses for |
WiredHome | 78:faf49c381591 | 135 | * its use. |
WiredHome | 78:faf49c381591 | 136 | * |
WiredHome | 78:faf49c381591 | 137 | * This file contains functions that implement calculations |
WiredHome | 78:faf49c381591 | 138 | * necessary to obtain calibration factors for a touch screen |
WiredHome | 78:faf49c381591 | 139 | * that suffers from multiple distortion effects: namely, |
WiredHome | 78:faf49c381591 | 140 | * translation, scaling and rotation. |
WiredHome | 78:faf49c381591 | 141 | * |
WiredHome | 78:faf49c381591 | 142 | * The following set of equations represent a valid display |
WiredHome | 78:faf49c381591 | 143 | * point given a corresponding set of touch screen points: |
WiredHome | 78:faf49c381591 | 144 | * |
WiredHome | 78:faf49c381591 | 145 | * /- -\ |
WiredHome | 78:faf49c381591 | 146 | * /- -\ /- -\ | | |
WiredHome | 78:faf49c381591 | 147 | * | | | | | Xs | |
WiredHome | 78:faf49c381591 | 148 | * | Xd | | A B C | | | |
WiredHome | 78:faf49c381591 | 149 | * | | = | | * | Ys | |
WiredHome | 78:faf49c381591 | 150 | * | Yd | | D E F | | | |
WiredHome | 78:faf49c381591 | 151 | * | | | | | 1 | |
WiredHome | 78:faf49c381591 | 152 | * \- -/ \- -/ | | |
WiredHome | 78:faf49c381591 | 153 | * \- -/ |
WiredHome | 78:faf49c381591 | 154 | * where: |
WiredHome | 78:faf49c381591 | 155 | * (Xd,Yd) represents the desired display point |
WiredHome | 78:faf49c381591 | 156 | * coordinates, |
WiredHome | 78:faf49c381591 | 157 | * (Xs,Ys) represents the available touch screen |
WiredHome | 78:faf49c381591 | 158 | * coordinates, and the matrix |
WiredHome | 78:faf49c381591 | 159 | * /- -\ |
WiredHome | 78:faf49c381591 | 160 | * |A,B,C| |
WiredHome | 78:faf49c381591 | 161 | * |D,E,F| represents the factors used to translate |
WiredHome | 78:faf49c381591 | 162 | * \- -/ the available touch screen point values |
WiredHome | 78:faf49c381591 | 163 | * into the corresponding display |
WiredHome | 78:faf49c381591 | 164 | * coordinates. |
WiredHome | 78:faf49c381591 | 165 | * Note that for practical considerations, the utilities |
WiredHome | 78:faf49c381591 | 166 | * within this file do not use the matrix coefficients as |
WiredHome | 78:faf49c381591 | 167 | * defined above, but instead use the following |
WiredHome | 78:faf49c381591 | 168 | * equivalents, since floating point math is not used: |
WiredHome | 78:faf49c381591 | 169 | * A = An/Divider |
WiredHome | 78:faf49c381591 | 170 | * B = Bn/Divider |
WiredHome | 78:faf49c381591 | 171 | * C = Cn/Divider |
WiredHome | 78:faf49c381591 | 172 | * D = Dn/Divider |
WiredHome | 78:faf49c381591 | 173 | * E = En/Divider |
WiredHome | 78:faf49c381591 | 174 | * F = Fn/Divider |
WiredHome | 78:faf49c381591 | 175 | * The functions provided within this file are: |
WiredHome | 78:faf49c381591 | 176 | * setCalibrationMatrix() - calculates the set of factors |
WiredHome | 78:faf49c381591 | 177 | * in the above equation, given |
WiredHome | 78:faf49c381591 | 178 | * three sets of test points. |
WiredHome | 78:faf49c381591 | 179 | * getDisplayPoint() - returns the actual display |
WiredHome | 78:faf49c381591 | 180 | * coordinates, given a set of |
WiredHome | 78:faf49c381591 | 181 | * touch screen coordinates. |
WiredHome | 78:faf49c381591 | 182 | * translateRawScreenCoordinates() - helper function to transform |
WiredHome | 78:faf49c381591 | 183 | * raw screen points into values |
WiredHome | 78:faf49c381591 | 184 | * scaled to the desired display |
WiredHome | 78:faf49c381591 | 185 | * resolution. |
WiredHome | 78:faf49c381591 | 186 | */ |
WiredHome | 78:faf49c381591 | 187 | |
WiredHome | 78:faf49c381591 | 188 | /********************************************************************** |
WiredHome | 78:faf49c381591 | 189 | * |
WiredHome | 78:faf49c381591 | 190 | * Function: setCalibrationMatrix() |
WiredHome | 78:faf49c381591 | 191 | * |
WiredHome | 78:faf49c381591 | 192 | * Description: Calling this function with valid input data |
WiredHome | 78:faf49c381591 | 193 | * in the display and screen input arguments |
WiredHome | 78:faf49c381591 | 194 | * causes the calibration factors between the |
WiredHome | 78:faf49c381591 | 195 | * screen and display points to be calculated, |
WiredHome | 78:faf49c381591 | 196 | * and the output argument - matrixPtr - to be |
WiredHome | 78:faf49c381591 | 197 | * populated. |
WiredHome | 78:faf49c381591 | 198 | * |
WiredHome | 78:faf49c381591 | 199 | * This function needs to be called only when new |
WiredHome | 78:faf49c381591 | 200 | * calibration factors are desired. |
WiredHome | 78:faf49c381591 | 201 | * |
WiredHome | 78:faf49c381591 | 202 | * |
WiredHome | 78:faf49c381591 | 203 | * Argument(s): displayPtr (input) - Pointer to an array of three |
WiredHome | 78:faf49c381591 | 204 | * sample, reference points. |
WiredHome | 78:faf49c381591 | 205 | * screenPtr (input) - Pointer to the array of touch |
WiredHome | 78:faf49c381591 | 206 | * screen points corresponding |
WiredHome | 78:faf49c381591 | 207 | * to the reference display points. |
WiredHome | 78:faf49c381591 | 208 | * matrixPtr (output) - Pointer to the calibration |
WiredHome | 78:faf49c381591 | 209 | * matrix computed for the set |
WiredHome | 78:faf49c381591 | 210 | * of points being provided. |
WiredHome | 78:faf49c381591 | 211 | * |
WiredHome | 78:faf49c381591 | 212 | * |
WiredHome | 78:faf49c381591 | 213 | * From the article text, recall that the matrix coefficients are |
WiredHome | 78:faf49c381591 | 214 | * resolved to be the following: |
WiredHome | 78:faf49c381591 | 215 | * |
WiredHome | 78:faf49c381591 | 216 | * |
WiredHome | 78:faf49c381591 | 217 | * Divider = (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2) |
WiredHome | 78:faf49c381591 | 218 | * |
WiredHome | 78:faf49c381591 | 219 | * |
WiredHome | 78:faf49c381591 | 220 | * |
WiredHome | 78:faf49c381591 | 221 | * (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2) |
WiredHome | 78:faf49c381591 | 222 | * A = --------------------------------------------------- |
WiredHome | 78:faf49c381591 | 223 | * Divider |
WiredHome | 78:faf49c381591 | 224 | * |
WiredHome | 78:faf49c381591 | 225 | * |
WiredHome | 78:faf49c381591 | 226 | * (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2) |
WiredHome | 78:faf49c381591 | 227 | * B = --------------------------------------------------- |
WiredHome | 78:faf49c381591 | 228 | * Divider |
WiredHome | 78:faf49c381591 | 229 | * |
WiredHome | 78:faf49c381591 | 230 | * |
WiredHome | 78:faf49c381591 | 231 | * Ys0*(Xs2*Xd1 - Xs1*Xd2) + |
WiredHome | 78:faf49c381591 | 232 | * Ys1*(Xs0*Xd2 - Xs2*Xd0) + |
WiredHome | 78:faf49c381591 | 233 | * Ys2*(Xs1*Xd0 - Xs0*Xd1) |
WiredHome | 78:faf49c381591 | 234 | * C = --------------------------------------------------- |
WiredHome | 78:faf49c381591 | 235 | * Divider |
WiredHome | 78:faf49c381591 | 236 | * |
WiredHome | 78:faf49c381591 | 237 | * |
WiredHome | 78:faf49c381591 | 238 | * (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2) |
WiredHome | 78:faf49c381591 | 239 | * D = --------------------------------------------------- |
WiredHome | 78:faf49c381591 | 240 | * Divider |
WiredHome | 78:faf49c381591 | 241 | * |
WiredHome | 78:faf49c381591 | 242 | * |
WiredHome | 78:faf49c381591 | 243 | * (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2) |
WiredHome | 78:faf49c381591 | 244 | * E = --------------------------------------------------- |
WiredHome | 78:faf49c381591 | 245 | * Divider |
WiredHome | 78:faf49c381591 | 246 | * |
WiredHome | 78:faf49c381591 | 247 | * |
WiredHome | 78:faf49c381591 | 248 | * Ys0*(Xs2*Yd1 - Xs1*Yd2) + |
WiredHome | 78:faf49c381591 | 249 | * Ys1*(Xs0*Yd2 - Xs2*Yd0) + |
WiredHome | 78:faf49c381591 | 250 | * Ys2*(Xs1*Yd0 - Xs0*Yd1) |
WiredHome | 78:faf49c381591 | 251 | * F = --------------------------------------------------- |
WiredHome | 78:faf49c381591 | 252 | * Divider |
WiredHome | 78:faf49c381591 | 253 | * |
WiredHome | 78:faf49c381591 | 254 | * |
WiredHome | 78:faf49c381591 | 255 | * Return: OK - the calibration matrix was correctly |
WiredHome | 78:faf49c381591 | 256 | * calculated and its value is in the |
WiredHome | 78:faf49c381591 | 257 | * output argument. |
WiredHome | 78:faf49c381591 | 258 | * NOT_OK - an error was detected and the |
WiredHome | 78:faf49c381591 | 259 | * function failed to return a valid |
WiredHome | 78:faf49c381591 | 260 | * set of matrix values. |
WiredHome | 78:faf49c381591 | 261 | * The only time this sample code returns |
WiredHome | 78:faf49c381591 | 262 | * NOT_OK is when Divider == 0 |
WiredHome | 78:faf49c381591 | 263 | * |
WiredHome | 78:faf49c381591 | 264 | * |
WiredHome | 78:faf49c381591 | 265 | * |
WiredHome | 78:faf49c381591 | 266 | * NOTE! NOTE! NOTE! |
WiredHome | 78:faf49c381591 | 267 | * |
WiredHome | 78:faf49c381591 | 268 | * setCalibrationMatrix() and getDisplayPoint() will do fine |
WiredHome | 78:faf49c381591 | 269 | * for you as they are, provided that your digitizer |
WiredHome | 78:faf49c381591 | 270 | * resolution does not exceed 10 bits (1024 values). Higher |
WiredHome | 78:faf49c381591 | 271 | * resolutions may cause the integer operations to overflow |
WiredHome | 78:faf49c381591 | 272 | * and return incorrect values. If you wish to use these |
WiredHome | 78:faf49c381591 | 273 | * functions with digitizer resolutions of 12 bits (4096 |
WiredHome | 78:faf49c381591 | 274 | * values) you will either have to a) use 64-bit signed |
WiredHome | 78:faf49c381591 | 275 | * integer variables and math, or b) judiciously modify the |
WiredHome | 78:faf49c381591 | 276 | * operations to scale results by a factor of 2 or even 4. |
WiredHome | 78:faf49c381591 | 277 | * |
WiredHome | 78:faf49c381591 | 278 | */ |
WiredHome | 78:faf49c381591 | 279 | RetCode_t RA8875::TouchPanelCalibrate(point_t * displayPtr, point_t * screenPtr, tpMatrix_t * matrixPtr) |
WiredHome | 78:faf49c381591 | 280 | { |
WiredHome | 78:faf49c381591 | 281 | RetCode_t retValue = noerror; |
WiredHome | 78:faf49c381591 | 282 | |
WiredHome | 78:faf49c381591 | 283 | tpMatrix.Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) - |
WiredHome | 78:faf49c381591 | 284 | ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ; |
WiredHome | 78:faf49c381591 | 285 | |
WiredHome | 78:faf49c381591 | 286 | if( tpMatrix.Divider == 0 ) { |
WiredHome | 78:faf49c381591 | 287 | retValue = bad_parameter; |
WiredHome | 78:faf49c381591 | 288 | } else { |
WiredHome | 78:faf49c381591 | 289 | tpMatrix.An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) - |
WiredHome | 78:faf49c381591 | 290 | ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ; |
WiredHome | 78:faf49c381591 | 291 | |
WiredHome | 78:faf49c381591 | 292 | tpMatrix.Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) - |
WiredHome | 78:faf49c381591 | 293 | ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ; |
WiredHome | 78:faf49c381591 | 294 | |
WiredHome | 78:faf49c381591 | 295 | tpMatrix.Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y + |
WiredHome | 78:faf49c381591 | 296 | (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y + |
WiredHome | 78:faf49c381591 | 297 | (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ; |
WiredHome | 78:faf49c381591 | 298 | |
WiredHome | 78:faf49c381591 | 299 | tpMatrix.Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) - |
WiredHome | 78:faf49c381591 | 300 | ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ; |
WiredHome | 78:faf49c381591 | 301 | |
WiredHome | 78:faf49c381591 | 302 | tpMatrix.En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) - |
WiredHome | 78:faf49c381591 | 303 | ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ; |
WiredHome | 78:faf49c381591 | 304 | |
WiredHome | 78:faf49c381591 | 305 | tpMatrix.Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y + |
WiredHome | 78:faf49c381591 | 306 | (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y + |
WiredHome | 78:faf49c381591 | 307 | (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ; |
WiredHome | 78:faf49c381591 | 308 | if (matrixPtr) |
WiredHome | 78:faf49c381591 | 309 | memcpy(matrixPtr, &tpMatrix, sizeof(tpMatrix_t)); |
WiredHome | 78:faf49c381591 | 310 | } |
WiredHome | 78:faf49c381591 | 311 | return( retValue ) ; |
WiredHome | 78:faf49c381591 | 312 | } |
WiredHome | 78:faf49c381591 | 313 | |
WiredHome | 78:faf49c381591 | 314 | /********************************************************************** |
WiredHome | 78:faf49c381591 | 315 | * |
WiredHome | 78:faf49c381591 | 316 | * Function: getDisplayPoint() |
WiredHome | 78:faf49c381591 | 317 | * |
WiredHome | 78:faf49c381591 | 318 | * Description: Given a valid set of calibration factors and a point |
WiredHome | 78:faf49c381591 | 319 | * value reported by the touch screen, this function |
WiredHome | 78:faf49c381591 | 320 | * calculates and returns the true (or closest to true) |
WiredHome | 78:faf49c381591 | 321 | * display point below the spot where the touch screen |
WiredHome | 78:faf49c381591 | 322 | * was touched. |
WiredHome | 78:faf49c381591 | 323 | * |
WiredHome | 78:faf49c381591 | 324 | * |
WiredHome | 78:faf49c381591 | 325 | * |
WiredHome | 78:faf49c381591 | 326 | * Argument(s): displayPtr (output) - Pointer to the calculated |
WiredHome | 78:faf49c381591 | 327 | * (true) display point. |
WiredHome | 78:faf49c381591 | 328 | * screenPtr (input) - Pointer to the reported touch |
WiredHome | 78:faf49c381591 | 329 | * screen point. |
WiredHome | 78:faf49c381591 | 330 | * matrixPtr (input) - Pointer to calibration factors |
WiredHome | 78:faf49c381591 | 331 | * matrix previously calculated |
WiredHome | 78:faf49c381591 | 332 | * from a call to |
WiredHome | 78:faf49c381591 | 333 | * setCalibrationMatrix() |
WiredHome | 78:faf49c381591 | 334 | * |
WiredHome | 78:faf49c381591 | 335 | * |
WiredHome | 78:faf49c381591 | 336 | * The function simply solves for Xd and Yd by implementing the |
WiredHome | 78:faf49c381591 | 337 | * computations required by the translation matrix. |
WiredHome | 78:faf49c381591 | 338 | * |
WiredHome | 78:faf49c381591 | 339 | * /- -\ |
WiredHome | 78:faf49c381591 | 340 | * /- -\ /- -\ | | |
WiredHome | 78:faf49c381591 | 341 | * | | | | | Xs | |
WiredHome | 78:faf49c381591 | 342 | * | Xd | | A B C | | | |
WiredHome | 78:faf49c381591 | 343 | * | | = | | * | Ys | |
WiredHome | 78:faf49c381591 | 344 | * | Yd | | D E F | | | |
WiredHome | 78:faf49c381591 | 345 | * | | | | | 1 | |
WiredHome | 78:faf49c381591 | 346 | * \- -/ \- -/ | | |
WiredHome | 78:faf49c381591 | 347 | * \- -/ |
WiredHome | 78:faf49c381591 | 348 | * |
WiredHome | 78:faf49c381591 | 349 | * It must be kept brief to avoid consuming CPU cycles. |
WiredHome | 78:faf49c381591 | 350 | * |
WiredHome | 78:faf49c381591 | 351 | * Return: OK - the display point was correctly calculated |
WiredHome | 78:faf49c381591 | 352 | * and its value is in the output argument. |
WiredHome | 78:faf49c381591 | 353 | * NOT_OK - an error was detected and the function |
WiredHome | 78:faf49c381591 | 354 | * failed to return a valid point. |
WiredHome | 78:faf49c381591 | 355 | * |
WiredHome | 78:faf49c381591 | 356 | * NOTE! NOTE! NOTE! |
WiredHome | 78:faf49c381591 | 357 | * |
WiredHome | 78:faf49c381591 | 358 | * setCalibrationMatrix() and getDisplayPoint() will do fine |
WiredHome | 78:faf49c381591 | 359 | * for you as they are, provided that your digitizer |
WiredHome | 78:faf49c381591 | 360 | * resolution does not exceed 10 bits (1024 values). Higher |
WiredHome | 78:faf49c381591 | 361 | * resolutions may cause the integer operations to overflow |
WiredHome | 78:faf49c381591 | 362 | * and return incorrect values. If you wish to use these |
WiredHome | 78:faf49c381591 | 363 | * functions with digitizer resolutions of 12 bits (4096 |
WiredHome | 78:faf49c381591 | 364 | * values) you will either have to a) use 64-bit signed |
WiredHome | 78:faf49c381591 | 365 | * integer variables and math, or b) judiciously modify the |
WiredHome | 78:faf49c381591 | 366 | * operations to scale results by a factor of 2 or even 4. |
WiredHome | 78:faf49c381591 | 367 | * |
WiredHome | 78:faf49c381591 | 368 | */ |
WiredHome | 79:544eb4964795 | 369 | bool RA8875::TouchPanelReadable(point_t * TouchPoint) |
WiredHome | 78:faf49c381591 | 370 | { |
WiredHome | 79:544eb4964795 | 371 | bool touched = false; |
WiredHome | 78:faf49c381591 | 372 | point_t screenpoint = {0, 0}; |
WiredHome | 78:faf49c381591 | 373 | |
WiredHome | 79:544eb4964795 | 374 | if (TouchPanelA2DFiltered(&screenpoint.x, &screenpoint.y)) { |
WiredHome | 79:544eb4964795 | 375 | touched = true; |
WiredHome | 79:544eb4964795 | 376 | if (tpMatrix.Divider != 0 && TouchPoint) { |
WiredHome | 78:faf49c381591 | 377 | /* Operation order is important since we are doing integer */ |
WiredHome | 78:faf49c381591 | 378 | /* math. Make sure you add all terms together before */ |
WiredHome | 78:faf49c381591 | 379 | /* dividing, so that the remainder is not rounded off */ |
WiredHome | 78:faf49c381591 | 380 | /* prematurely. */ |
WiredHome | 78:faf49c381591 | 381 | TouchPoint->x = ( (tpMatrix.An * screenpoint.x) + |
WiredHome | 78:faf49c381591 | 382 | (tpMatrix.Bn * screenpoint.y) + |
WiredHome | 78:faf49c381591 | 383 | tpMatrix.Cn |
WiredHome | 78:faf49c381591 | 384 | ) / tpMatrix.Divider ; |
WiredHome | 78:faf49c381591 | 385 | |
WiredHome | 78:faf49c381591 | 386 | TouchPoint->y = ( (tpMatrix.Dn * screenpoint.x) + |
WiredHome | 78:faf49c381591 | 387 | (tpMatrix.En * screenpoint.y) + |
WiredHome | 78:faf49c381591 | 388 | tpMatrix.Fn |
WiredHome | 78:faf49c381591 | 389 | ) / tpMatrix.Divider ; |
WiredHome | 78:faf49c381591 | 390 | } else { |
WiredHome | 79:544eb4964795 | 391 | touched = false; |
WiredHome | 78:faf49c381591 | 392 | } |
WiredHome | 78:faf49c381591 | 393 | } |
WiredHome | 79:544eb4964795 | 394 | return touched; |
WiredHome | 78:faf49c381591 | 395 | } |
WiredHome | 78:faf49c381591 | 396 | |
WiredHome | 78:faf49c381591 | 397 | |
WiredHome | 78:faf49c381591 | 398 | RetCode_t RA8875::TouchPanelSetMatrix(tpMatrix_t * matrixPtr) |
WiredHome | 78:faf49c381591 | 399 | { |
WiredHome | 78:faf49c381591 | 400 | if (matrixPtr == NULL || matrixPtr->Divider == 0) |
WiredHome | 78:faf49c381591 | 401 | return bad_parameter; |
WiredHome | 78:faf49c381591 | 402 | memcpy(&tpMatrix, matrixPtr, sizeof(tpMatrix_t)); |
WiredHome | 78:faf49c381591 | 403 | return noerror; |
WiredHome | 78:faf49c381591 | 404 | } |
WiredHome | 78:faf49c381591 | 405 | |
WiredHome | 78:faf49c381591 | 406 | // #### end of touch panel code additions |