brw1

Dependencies:   mbed

Committer:
reiniermarcel
Date:
Mon Nov 30 11:13:18 2015 +0000
Revision:
0:a115ff47d1c1
ok

Who changed what in which revision?

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