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