• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

touchscreen.c

Go to the documentation of this file.
00001 /****************** COPYRIGHT (C) 2007-2010 RAISONANCE S.A.S. *****************/
00012 /******************************************************************************/
00013 
00014 /* Includes ------------------------------------------------------------------*/
00015 #include "circle.h"
00016 
00018 
00019 /* Private defines -----------------------------------------------------------*/
00020 #define TS_TOLERANCE 40
00021 
00022 /* Private variables ---------------------------------------------------------*/
00023 u16 TS_Sensibility = 3100;  // Sensibility of the touch
00024 
00025 // Position informations
00026 tTOUCHSCR_Info TOUCHSCR_Info = {0};
00027 tPOINT TS_ScreenSample[3] = {0};
00028 
00029 // Calibration informations
00030 tTOUCHSCR_Cal TS_CalibrationSetup =
00031 {
00032     10960,    0, - 821752,  // Default setup :
00033     147, 7946, -1088093,    // good for most Primers
00034     100000                  // out of the box
00035 };
00036 
00037 /* External variables ---------------------------------------------------------*/
00038 extern const tPOINT TS_RefSample[3];
00039 
00040 /* Private functions ----------------------------------------------------------*/
00041 
00042 /*******************************************************************************
00043 *
00044 *                                MedianFilter
00045 *
00046 *******************************************************************************/
00058 /******************************************************************************/
00059 NODEBUG2 u32 MedianFilter( const u16* values )
00060 {
00061     u16 Sorted[ADC_NB_SAMPLES]; //We only need so sort half of array
00062     u16 v;
00063     u32 i, j;
00064 
00065     Sorted[0] = values[0];
00066 
00067     for ( i = 1; i < ADC_NB_SAMPLES; i++ )
00068     {
00069         v = values[i * ADC_NB_CHANNELS];
00070         j = i;
00071         for ( ; j > 0; j-- )
00072         {
00073             if ( v > Sorted[j - 1] )
00074                 break;
00075             Sorted[j] = Sorted[j - 1];
00076         }
00077         Sorted[j] = v;
00078     }
00079 
00080     return Sorted[( ADC_NB_SAMPLES + 1 ) >> 1];
00081 }
00082 
00083 /*******************************************************************************
00084 *
00085 *                                TOUCHSCR_SetMode
00086 *
00087 *******************************************************************************/
00096 /******************************************************************************/
00097 NODEBUG2 void TOUCHSCR_SetMode( TOUCHSCR_Mode_enum  mode )
00098 {
00099     switch ( mode )
00100     {
00101     case TS_POINTER:
00102     case TS_NORMAL:
00103     case TS_DRAWING:
00104         TOUCHSCR_Info.Mode = mode;
00105         break;
00106     case TS_CALIBRATION:
00107         break;   // set this mode only in special cases..
00108     }
00109 }
00110 
00111 /*******************************************************************************
00112 *
00113 *                                TOUCHSCR Drawing
00114 *
00115 *******************************************************************************/
00121 /******************************************************************************/
00122 NODEBUG2 void TOUCHSCREEN_Drawing( void )
00123 {
00124     static s32 old_x = 0;
00125     static s32 old_y = 0;
00126     static s32 old_Pressed = 0;
00127     s32 xpos = 0, ypos = 0;
00128 
00129     // Get the coordinates of the touch point
00130     if ( TOUCHSCR_Info.TouchPress )
00131     {
00132         xpos = TOUCHSCR_Info.xPos;
00133         ypos = TOUCHSCR_Info.yPos;
00134         if ( xpos < 0 ) xpos = 0;
00135         if ( ypos < 0 ) ypos = 0;
00136         if ( xpos > ( Screen_Width - 1 ) ) xpos = ( Screen_Width - 1 );
00137         if ( ypos > ( Screen_Height - 1 ) ) ypos = ( Screen_Height - 1 );
00138 
00139         // To eliminate  the interference
00140         if ( old_Pressed )
00141         {
00142             if ( ( ( xpos - old_x ) * ( xpos - old_x ) + ( ypos - old_y ) * ( ypos - old_y ) )  > ( TS_TOLERANCE * TS_TOLERANCE ) )
00143             {
00144                 old_Pressed = 0;
00145             }
00146             else
00147             {
00148                 xpos = ( xpos + old_x ) / 2; // integrate the position to
00149                 ypos = ( ypos + old_y ) / 2;
00150             }
00151         }
00152 
00153         // Draw line between the n and n-1 points
00154         if ( old_Pressed )
00155         {
00156             DRAW_Line_Circle( old_x, old_y, xpos, ypos, RGB_BLACK );
00157         }
00158     }
00159     old_x = xpos;
00160     old_y = ypos;
00161     old_Pressed = TOUCHSCR_Info.TouchPress;
00162 }
00163 
00164 /*******************************************************************************
00165 *
00166 *                                CalculateCalibration
00167 *
00168 *******************************************************************************/
00183 /******************************************************************************/
00184 void TOUCHSCR_CalculateCalibration( tPOINT* Raw )
00185 {
00186     s32 Dx = Raw[0].X - Raw[2].X;
00187     s32 Dy = Raw[0].Y - Raw[2].Y;
00188 
00189     s32 Ex = Raw[1].X - Raw[2].X;
00190     s32 Ey = Raw[1].Y - Raw[2].Y;
00191 
00192     s32 Delta = Dx * Ey - Ex * Dy;
00193     TS_CalibrationSetup.R = Delta;
00194 
00195     // Three for X...
00196     s32 A = ( TS_RefSample[0].X - TS_RefSample[2].X ) * Ey
00197             - ( TS_RefSample[1].X - TS_RefSample[2].X ) * Dy;
00198 
00199     s32 B = ( TS_RefSample[1].X - TS_RefSample[2].X ) * Dx
00200             - ( TS_RefSample[0].X - TS_RefSample[2].X ) * Ex;
00201 
00202     // Much faster then direct computation (without accuracy loss)
00203     s32 C = ( TS_RefSample[2].X * Delta ) - ( A * Raw[2].X ) - ( B * Raw[2].Y );
00204 
00205     TS_CalibrationSetup.A = A;
00206     TS_CalibrationSetup.B = B;
00207     TS_CalibrationSetup.C = C;
00208 
00209     //...And three for Y
00210     s32 D  = ( TS_RefSample[0].Y - TS_RefSample[2].Y ) * Ey
00211              - ( TS_RefSample[1].Y - TS_RefSample[2].Y ) * Dy;
00212 
00213     s32 E  = ( TS_RefSample[1].Y - TS_RefSample[2].Y ) * Dx
00214              - ( TS_RefSample[0].Y - TS_RefSample[2].Y ) * Ex;
00215 
00216     s32 F  = ( TS_RefSample[2].Y * Delta ) - ( D * Raw[2].X ) - ( E * Raw[2].Y );
00217 
00218     TS_CalibrationSetup.D = D;
00219     TS_CalibrationSetup.E = E;
00220     TS_CalibrationSetup.F = F;
00221 }
00222 
00224 
00225 /*******************************************************************************
00226 *
00227 *                                TOUCHSCR_GetPos
00228 *
00229 *******************************************************************************/
00238 /******************************************************************************/
00239 u16 TOUCHSCR_GetPos( void )
00240 {
00241     return ( TOUCHSCR_Info.xPos | ( TOUCHSCR_Info.yPos << 8 ) );
00242 }
00243 
00244 /*******************************************************************************
00245 *
00246 *                                TOUCHSCR_GetPosX
00247 *
00248 *******************************************************************************/
00257 /******************************************************************************/
00258 s32 TOUCHSCR_GetPosX( void )
00259 {
00260     return TOUCHSCR_Info.xPos;
00261 }
00262 
00263 /*******************************************************************************
00264 *
00265 *                                TOUCHSCR_GetPosY
00266 *
00267 *******************************************************************************/
00276 /******************************************************************************/
00277 s32 TOUCHSCR_GetPosY( void )
00278 {
00279     return TOUCHSCR_Info.yPos;
00280 }
00281 
00282 /*******************************************************************************
00283 *
00284 *                                TOUCHSCR_GetAbsPos
00285 *
00286 *******************************************************************************/
00296 /******************************************************************************/
00297 u16 TOUCHSCR_GetAbsPos( void )
00298 {
00299     return ( TOUCHSCR_Info.xAbsPos | ( TOUCHSCR_Info.yAbsPos << 8 ) );
00300 }
00301 
00302 /*******************************************************************************
00303 *
00304 *                                TOUCHSCR_IsPressed
00305 *
00306 *******************************************************************************/
00315 /******************************************************************************/
00316 bool TOUCHSCR_IsPressed( void )
00317 {
00318     return ( TOUCHSCR_Info.TouchPress ) ;
00319 }
00320 
00321 /*******************************************************************************
00322 *
00323 *                                TOUCHSCR_GetMode
00324 *
00325 *******************************************************************************/
00334 /******************************************************************************/
00335 TOUCHSCR_Mode_enum TOUCHSCR_GetMode( void )
00336 {
00337     return ( TOUCHSCR_Info.Mode ) ;
00338 }
00339 
00340 /*******************************************************************************
00341 *
00342 *                                TOUCHSCR_SetSensibility
00343 *
00344 *******************************************************************************/
00353 /******************************************************************************/
00354 void TOUCHSCR_SetSensibility( u16 sens )
00355 {
00356     if ( sens < 4095 )
00357         TS_Sensibility = sens;
00358     else
00359         TS_Sensibility = 4095;
00360 }
00361 
00362