基于S3C6410的Touch驱动详解

Windows CE操作系统中触摸屏驱动是一种分层驱动。其驱动模型如图1所示。上层是模型设备驱动程序(Model Device Driver, MDD),下层是依赖平台的驱动程序(Platform Dependent Driver, PDD)。MDD通常无需修改直接使用,MDD链接PDD层并定义它希望调用的函数接口:设备驱动程序提供器接口(Device Driver Service Provider Interface, DDSI)。同时MDD把不同的函数集提供给操作系统,这些函数叫做设备驱动程序接口(Device Driver Interface, DDI),这部分为也就是我们通常驱动需要实现的部分。

 

Windows CE的触摸屏驱动链接了tch_cal.libtchmdd.lib两个静态链接库。触摸屏驱动由GWES加载,GWES通过DDI调用驱动程序获取设备状态,设置驱动功能等,而驱动本身通过DDSI直接获得硬件信息来确定当前触摸屏的状态。

Windows CE触摸屏驱动要求的DDI接口包括:TouchPanelGetDeviceCapsTouchPanelEnableTouchPanelDisableTouchPanelSetModeTouchPanelReadCalibrationPointTouchPanelReadCalibrationAbortTouchPanelSetCalibrationTouchPanelCalibrateAPointTouchPanelPowerHandler

Windows CE触摸屏驱动要求的DDSI接口包括:DdsiTouchPanelAttachDdsiTouchPanelDetachDdsiTouchPanelDisableDdsiTouchPanelEnableDdsiTouchPanelGetDeviceCapsDdsiTouchPanelGetPointDdsiTouchPanelPowerHandler

 

Windows CE触摸屏驱动程序采用中断方式对触摸笔的按下状态进行检测,如果检测到触摸笔按下将产生中断并触发一个事件通知一个工作线程开始采集数据。同时,驱动将打开一个硬件定时器,只要检测到触摸笔仍然在按下状态将定时触发同一个事件通知工作线程采集数据,直到触摸笔抬起后关闭该定时器,并重新检测按下状态。驱动中采用了触摸屏中断以及定时器中断两个中断源,不仅可以监控触摸笔按下和抬起状态,而且可以检测触摸笔按下时的拖动轨迹。

触摸屏驱动在初始化过程调用TouchPanelEnable函数使能触摸屏。该函数调用的DDSI函数为:DdsiTouchPanelEnableDdsiTouchPanelDisable。该函数实现如下内容:

1) 创建事件hTouchPanelEventhCalibrationSampleAvailablehTouchPanelEvent事件在正常状态下当有触摸笔按下或者按下后需要定时采集数据时被触发。而hCalibrationSampleAvailable事件在校准状态下当有校准数据输入时被触发;

2) 检查并初始化所需的中断gIntrTouch(触摸屏中断)和gIntrTouchChanged(定时器中断),并将中断gIntrTouchgIntrTouchChanged关联到事件hTouchPanelEvent。当gIntrTouchgIntrTouchChanged中断产生时将触发hTouchPanelEvent事件;

3) 创建一个ISR线程TouchPanelpISRTouchPanelpISR用于等待和处理触摸屏事件hTouchPanelEvent,它是整个驱动程序中唯一的事件源。

 

TouchPanelEnable()代码如下:

view plain copy to clipboard print ?
  1. BOOL  
  2. TouchPanelEnable(  
  3.     PFN_TOUCH_PANEL_CALLBACK    pfnCallback  
  4.     )  
  5. {  
  6.     BOOL    ReturnValue;  
  7.   
  8.     //   
  9.     // Do the 'attach' code.  Normally, this would have been   
  10.     // done in the ThreadAttach block, but this driver is set   
  11.     // up to be statically linked to GWE, in which case none of   
  12.     // the DLL related calls would even be invoked.   
  13.     //   
  14.     //创建事件hTouchPanelEvent和hCalibrationSampleAvailable   
  15.     TouchPanelpAttach();  
  16.   
  17.     EnterCriticalSection( &csMutex );  
  18.   
  19.     //   
  20.     // Insure the device is disabled and no one is attached to the logical   
  21.     // interrupt.   
  22.     // Power on the device.   
  23.     // Connect the logical interrupt to the device.   
  24.     //   
  25.   
  26.     InterruptDone( gIntrTouch );  
  27.     InterruptDisable( gIntrTouch );  
  28.     // SYSINTR_NOP是什么意思?   
  29.     if( SYSINTR_NOP != gIntrTouchChanged )   
  30.     {  
  31.         InterruptDone( gIntrTouchChanged );  
  32.         InterruptDisable( gIntrTouchChanged );  
  33.     }  
  34.   
  35.     v_pfnCgrPointCallback = pfnCallback;  
  36.     if (v_pfnCgrCallback != NULL)  
  37.         v_pfnPointCallback = v_pfnCgrCallback;  
  38.     else  
  39.         v_pfnPointCallback = pfnCallback;  
  40.   
  41.     ghevCalibrationActivity = NULL;  
  42.   
  43.     //检查并初始化所需的中断gIntrTouch(触摸屏中断)和gIntrTouchChanged(定时器中断),   
  44.     //并将中断gIntrTouch、gIntrTouchChanged关联到事件hTouchPanelEvent。   
  45.     ReturnValue = DdsiTouchPanelEnable();  
  46.     // 将hTouchPanelEvent事件与中断号gIntrTouch绑定,并使能中断   
  47.     if (ReturnValue && !InterruptInitialize(gIntrTouch, hTouchPanelEvent, NULL, 0))   
  48.     {  
  49.         DEBUGMSG(ZONE_ERROR, (TEXT("TouchPanelEnable: InterruptInitialize(gIntrTouch %d failed/r/n"),  
  50.                               gIntrTouch));  
  51.         DdsiTouchPanelDisable();  
  52.         ReturnValue = FALSE;  
  53.     }  
  54.     // 将hTouchPanelEvent事件与中断号gIntrTouchChanged绑定,并使能中断   
  55.     if ( ( SYSINTR_NOP != gIntrTouchChanged ) &&  
  56.     ReturnValue && !InterruptInitialize( gIntrTouchChanged, hTouchPanelEvent, NULL, 0))  
  57.     {  
  58.         DEBUGMSG(ZONE_ERROR, (TEXT("TouchPanelEnable: InterruptInitialize(gIntrTouchChanged %d failed/r/n"),  
  59.                               gIntrTouchChanged));  
  60.         InterruptDisable(gIntrTouch);  
  61.         DdsiTouchPanelDisable();  
  62.         ReturnValue = FALSE;  
  63.     }  
  64.     if (ReturnValue)  
  65.     {  
  66.         // Create the ISR thread.  If creation fails, perform cleanup and return failure.   
  67.         //创建一个ISR线程TouchPanelpISR。TouchPanelpISR用于等待和处理   
  68.         //触摸屏事件hTouchPanelEvent,它是整个驱动程序中唯一的事件源。   
  69.         bTerminate=FALSE;  
  70.         if (!(hThread = CreateThread( NULL, 0, TouchPanelpISR, 0, 0, NULL)))   
  71.         {  
  72.             // 创建线程失败   
  73.             TouchPanelpDetach();  
  74.             InterruptDisable(gIntrTouch);  
  75.             if( SYSINTR_NOP != gIntrTouchChanged )  
  76.                 InterruptDisable(gIntrTouchChanged);  
  77.             DdsiTouchPanelDisable();  
  78.             ReturnValue = FALSE;  
  79.         }  
  80.         else  
  81.         {  
  82.             // Get thread priority from registry   
  83.             TouchPanelpGetPriority(&gThreadPriority, &gThreadHighPriority);  
  84.             // Set our interrupt thread's priority   
  85.             //设置中断优先级   
  86.             CeSetThreadPriority(hThread, gThreadPriority);  
  87.         }  
  88.     }  
  89.     LeaveCriticalSection(&csMutex);  
  90.     return(ReturnValue);  
  91. }  
BOOL TouchPanelEnable( PFN_TOUCH_PANEL_CALLBACK pfnCallback ) { BOOL ReturnValue; // // Do the 'attach' code. Normally, this would have been // done in the ThreadAttach block, but this driver is set // up to be statically linked to GWE, in which case none of // the DLL related calls would even be invoked. // //创建事件hTouchPanelEvent和hCalibrationSampleAvailable TouchPanelpAttach(); EnterCriticalSection( &csMutex ); // // Insure the device is disabled and no one is attached to the logical // interrupt. // Power on the device. // Connect the logical interrupt to the device. // InterruptDone( gIntrTouch ); InterruptDisable( gIntrTouch ); // SYSINTR_NOP是什么意思? if( SYSINTR_NOP != gIntrTouchChanged ) { InterruptDone( gIntrTouchChanged ); InterruptDisable( gIntrTouchChanged ); } v_pfnCgrPointCallback = pfnCallback; if (v_pfnCgrCallback != NULL) v_pfnPointCallback = v_pfnCgrCallback; else v_pfnPointCallback = pfnCallback; ghevCalibrationActivity = NULL; //检查并初始化所需的中断gIntrTouch(触摸屏中断)和gIntrTouchChanged(定时器中断), //并将中断gIntrTouch、gIntrTouchChanged关联到事件hTouchPanelEvent。 ReturnValue = DdsiTouchPanelEnable(); // 将hTouchPanelEvent事件与中断号gIntrTouch绑定,并使能中断 if (ReturnValue && !InterruptInitialize(gIntrTouch, hTouchPanelEvent, NULL, 0)) { DEBUGMSG(ZONE_ERROR, (TEXT("TouchPanelEnable: InterruptInitialize(gIntrTouch %d failed/r/n"), gIntrTouch)); DdsiTouchPanelDisable(); ReturnValue = FALSE; } // 将hTouchPanelEvent事件与中断号gIntrTouchChanged绑定,并使能中断 if ( ( SYSINTR_NOP != gIntrTouchChanged ) && ReturnValue && !InterruptInitialize( gIntrTouchChanged, hTouchPanelEvent, NULL, 0)) { DEBUGMSG(ZONE_ERROR, (TEXT("TouchPanelEnable: InterruptInitialize(gIntrTouchChanged %d failed/r/n"), gIntrTouchChanged)); InterruptDisable(gIntrTouch); DdsiTouchPanelDisable(); ReturnValue = FALSE; } if (ReturnValue) { // Create the ISR thread. If creation fails, perform cleanup and return failure. //创建一个ISR线程TouchPanelpISR。TouchPanelpISR用于等待和处理 //触摸屏事件hTouchPanelEvent,它是整个驱动程序中唯一的事件源。 bTerminate=FALSE; if (!(hThread = CreateThread( NULL, 0, TouchPanelpISR, 0, 0, NULL))) { // 创建线程失败 TouchPanelpDetach(); InterruptDisable(gIntrTouch); if( SYSINTR_NOP != gIntrTouchChanged ) InterruptDisable(gIntrTouchChanged); DdsiTouchPanelDisable(); ReturnValue = FALSE; } else { // Get thread priority from registry TouchPanelpGetPriority(&gThreadPriority, &gThreadHighPriority); // Set our interrupt thread's priority //设置中断优先级 CeSetThreadPriority(hThread, gThreadPriority); } } LeaveCriticalSection(&csMutex); return(ReturnValue); }

TouchPanelpISR函数是实现触摸屏数据采集关键函数,它实现的内容为:

1) 等待循环,用于接收hTouchPanelEvent事件,并构成函数的主体;

2) 通过调用DdsiTouchPanelGetPoint函数获取当前触摸屏位置和状态信息;

3) 在获取有效数据且在校准状态下,收集并提交按下的位置信息;

4) 在正常状态下,校准数据,并检查校准后数据的有效性;

5) 最后调用由GWES传入的回调函数,提交位置信息和状态信息。

TouchPanelpISR()代码如下:

view plain copy to clipboard print ?
  1. static ULONG  
  2. TouchPanelpISR(  
  3.     PVOID   Reserved  //@parm Reserved, not used.   
  4.     )  
  5. {  
  6.     TOUCH_PANEL_SAMPLE_FLAGS    SampleFlags = 0;  
  7.     INT32                       RawX, CalX;  
  8.     INT32                       RawY, CalY;  
  9.     UINT32                      MaxX =  DisplayWidth * X_SCALE_FACTOR;  
  10.     UINT32                      MaxY =  DisplayHeight * Y_SCALE_FACTOR;  
  11.     UINT32                      CurrentDown = 0;  
  12.     static LONG CX;  
  13.     static LONG CY;  
  14.     static LONG XBase;  
  15.     static LONG YBase;  
  16.     static int  CalibrationSampleCount;  
  17.     static BOOL     fSetBase;  
  18.     static DWORD    BaseTime;  
  19.     static BOOL     fGotSample;  
  20.   
  21.     PFN_TOUCH_PANEL_CALLBACK pfnCallback;  
  22.   
  23.     // Need to be all kmode so that we can write to shared memory.   
  24.   
  25.     while  ( !bTerminate )  
  26.     {  
  27.         //等待触摸屏产生中断,用于接收hTouchPanelEvent事件,并构成函数的主体;   
  28.         WaitForSingleObject( hTouchPanelEvent, gdwTouchIstTimeout );  
  29.         EnterCriticalSection( &csMutex );  
  30.         DEBUGMSG(ZONE_THREAD, (TEXT("TCH_INTR/r/n")) );  
  31.   
  32.         // Give the pdd the down state of the previous sample   
  33.         if ( CurrentDown )  
  34.             SampleFlags |= TouchSamplePreviousDownFlag;  
  35.         else  
  36.             SampleFlags &= ~TouchSamplePreviousDownFlag;  
  37.         //调用DdsiTouchPanelGetPoint函数获取当前触摸屏位置[RawX,RawY]和状态信息[SampleFlags]   
  38.         DdsiTouchPanelGetPoint( &SampleFlags, &RawX, &RawY );    // Get the point info   
  39.   
  40.         if ( SampleFlags & TouchSampleIgnore )//忽略采集到的数据   
  41.         {  
  42.             // do nothing, not a valid sample   
  43.             LeaveCriticalSection( &csMutex );  
  44.             continue;  
  45.         }  
  46.   
  47.         if ( SampleFlags & TouchSampleValidFlag )  
  48.         {  
  49.             // Set the previous down state for our use, since the pdd may not   
  50.             // have preserved it.   
  51.             // 之前有校准笔针按下时,会将SampleFlags的TouchSamplePreviousDownFlag   
  52.             // 位置1   
  53.             if ( CurrentDown )  
  54.                 SampleFlags |= TouchSamplePreviousDownFlag;  
  55.             else  
  56.                 SampleFlags &= ~TouchSamplePreviousDownFlag;  
  57.             // 当笔针有按下动作时,会将SampleFlags的TouchSampleDownFlag位置1.   
  58.             // 这时CurrentDown的TouchSampleDownFlag位也会置1   
  59.             CurrentDown = SampleFlags & TouchSampleDownFlag;  
  60.         }  
  61.         //在获取有效数据且在校准状态下,收集并提交按下的位置信息   
  62.         if ( CalibrationState )  
  63.         {  
  64.                 //   
  65.                 // At this point we know that calibration is active.   
  66.                 //   
  67.                 // Typically, the user touches the panel then converges to the   
  68.                 // displayed crosshair. When the tip state transitions to   
  69.                 // the up state, we forward the last valid point to the callback   
  70.                 // function.   
  71.                 //   
  72.                 DEBUGMSG(ZONE_SAMPLES, (TEXT("**** Calibration point (%d, %d), flags 0x%4.4X/r/n"),  
  73.                        RawX, RawY, SampleFlags) );  
  74.   
  75.             //  Skip if not valid.采样数据无效   
  76.             if ( !(SampleFlags & TouchSampleValidFlag) )  
  77.             {  
  78.                 LeaveCriticalSection( &csMutex );  
  79.                 continue;  
  80.             }  
  81.   
  82.             //  Signal the Power Manager activity event if one has been set up   
  83.             if ( ghevCalibrationActivity != NULL)   
  84.             {  
  85.                 SetEvent(ghevCalibrationActivity);//通知校准事件   
  86.             }  
  87.   
  88.             //  Must see down transition.   
  89.             // SampleFlags的TouchSamplePreviousDownFlag位为0,表示还没有校准笔针按下   
  90.             if ( (SampleFlags & (TouchSampleDownFlag|TouchSamplePreviousDownFlag)) ==  
  91.             TouchSampleDownFlag )  
  92.             {   // 进入笔针校准状态   
  93.                 CalibrationState = CalibrationDown;  
  94.                 fSetBase = TRUE;  
  95.                 CalibrationSampleCount = 0;  
  96.                 fGotSample = FALSE;  
  97.             }  
  98.   
  99.             //  Only look at stuff if we saw a down transition.   
  100.             if ( (CalibrationState == CalibrationDown) && !fGotSample )  
  101.             {  
  102.                 if ( SampleFlags & TouchSampleDownFlag )  
  103.                 {  
  104.                     long DeltaX, DeltaY;  
  105.   
  106.                     CalibrationSampleCount++;//校准次数累加。需校准5次   
  107.                     CX = RawX;  
  108.                     CY = RawY;  
  109.                     if ( fSetBase )  
  110.                     {  
  111.                         XBase = CX;  
  112.                         YBase = CY;  
  113.                         BaseTime = GetTickCount();  
  114.                         fSetBase = FALSE;  
  115.                     }  
  116.                     DeltaX = CX - XBase;  
  117.                     DeltaY = CY - YBase;  
  118.                     if ( (GetTickCount() - BaseTime) > CAL_HOLD_STEADY_TIME )  
  119.                     {  
  120.                         fGotSample = TRUE;  
  121.                     }  
  122.                     else if ( ( ABS(DeltaX) > CAL_DELTA_RESET ) ||  
  123.                                 ( ABS(DeltaY) > CAL_DELTA_RESET ) )  
  124.                     {  
  125.                         RETAILMSG(1, (TEXT("M %ld,%ld  %ld,%ld  %ld,%ld"),  
  126.                         XBase,YBase, CX,CY, DeltaX,DeltaY));  
  127.                         fSetBase = TRUE;  
  128.                     }  
  129.                 }  
  130.                 else  
  131.                 {  
  132.                     // They lifted the pen, see if we will accept coordinate.   
  133.                     if ( CalibrationSampleCount >= MIN_CAL_COUNT )  
  134.                     {  
  135.                         fGotSample = TRUE;  
  136.                     }  
  137.                     else  
  138.                     {  
  139.                         CalibrationState = CalibrationWaiting;  
  140.                     }  
  141.                 }  
  142.                 //收集并提交校准按下的位置信息   
  143.                 if ( fGotSample )  
  144.                 {  
  145.                     CalibrationState = CalibrationValid;  
  146.                     lCalibrationXCoord = CX;//保存校验数据到lCalibrationXCoord,lCalibrationYCoord   
  147.                     lCalibrationYCoord = CY;  
  148.                     SetEvent(hCalibrationSampleAvailable);  
  149.                 }  
  150.             }  
  151.             LeaveCriticalSection( &csMutex );  
  152.         }  
  153.         else        //在正常状态下,校准数据,并检查校准后数据的有效性;   
  154.         {  
  155.             pfnCallback = v_pfnPointCallback;  
  156.             if ( pfnCallback != NULL )  
  157.             {  
  158.                 if( SampleFlags & TouchSampleIsCalibratedFlag )  
  159.                 {     // Sample already calibrated by PDD   
  160.                     // XY坐标植已经校准过,直接读取采样的值。   
  161.                     CalX = RawX;  
  162.                     CalY = RawY;  
  163.                 }  
  164.                 else  
  165.                 {     // Not previously calibrated, do it now.   
  166.                     // 之前没有校准屏幕,调用校准函数进行校准。这里只校准一个点.   
  167.                     TouchPanelCalibrateAPoint( RawX, RawY, &CalX, &CalY );//看看是如何校准的.不是要校准5个点吗?   
  168.                     // 校准完后将已经校准标志位置1,以后就不用校准了   
  169.                     // 为什么校准一个点就置校准标志位为1?   
  170.                     SampleFlags |= TouchSampleIsCalibratedFlag;  
  171.                 }  
  172.   
  173.                 LeaveCriticalSection( &csMutex );  
  174.   
  175.                 // Bounds check this value   
  176.                 if( CalX < 0 )// 最小坐标值为0   
  177.                     CalX = 0;  
  178.                 else if( MaxX && ((UINT32)CalX >= MaxX) )//支持子画面????????   
  179.                     CalX = MaxX - X_SCALE_FACTOR;  
  180.                 if( CalY < 0 )  
  181.                     CalY = 0;  
  182.                 else if( MaxY && ((UINT32)CalY >= MaxY) )  
  183.                     CalY = MaxY - Y_SCALE_FACTOR ;  
  184.                   
  185.                 DEBUGMSG( ZONE_SAMPLES,  
  186.                 (TEXT("**** Queuing point (%d, %d), flags 0x%4.4X/r/n"),  
  187.                 CalX, CalY, SampleFlags) );  
  188. #ifdef DEBUG   
  189.                 {  
  190.                     static DWORD SampleCt;  
  191.   
  192.                     if( SampleFlags & TouchSampleDownFlag )  
  193.                         SampleCt++;  
  194.                     else  
  195.                     {  
  196.                         DEBUGMSG( ZONE_TIMING,  
  197.                         (TEXT("%d down samples queued/r/n"),  
  198.                         SampleCt) );  
  199.                         SampleCt = 0;  
  200.                     }  
  201.                 }  
  202. #endif              
  203.                 // 调用由GWES传入的回调函数,提交位置信息和状态信息???????????????   
  204.                 (pfnCallback)( SampleFlags, CalX, CalY);//这一句是什么意思???   
  205.             }  
  206.                 else  
  207.                 {  
  208.                 LeaveCriticalSection( &csMutex );  
  209.                 }  
  210.         }  
  211.     }  
  212.     ExitThread(1);  
  213.     return ( TRUE );  
  214. }  

从基于S3C6410的Touch驱动详解(之一)可以看到,在触摸屏驱动程序中DdsiTouchPanelEnableDdsiTouchPanelDisableDdsiTouchPanelGetPoint三个DDSI接口函数是驱动实现的关键所在。

view plain copy to clipboard print ?
  1. // 对触摸屏数据的采样,采样的数据以*pUncalX,*pUncalY形式回传   
  2. VOID  
  3. DdsiTouchPanelGetPoint(TOUCH_PANEL_SAMPLE_FLAGS *pTipState, INT *pUncalX, INT *pUncalY)  
  4. {  
  5.     static int PrevX=0;  
  6.     static int PrevY=0;  
  7.     int TmpX = 0;  
  8.     int TmpY = 0;  
  9.   
  10.     TSPMSG((_T("[TSP] ++DdsiTouchPanelGetPoint()/r/n")));  
  11.   
  12.     // 处理触摸屏中断.触摸笔按下时产生触摸屏中断gIntrTouch时触发   
  13.     if (g_pVIC1Reg->VICRAWINTR & (1<<(PHYIRQ_PENDN-VIC1_BIT_OFFSET)))        // gIntrTouch Interrupt Case   
  14.     {  
  15.         TSPMSG((_T("[TSP] gIntrTouch(PHYIRQ_PENDN) Case/r/n")));  
  16.   
  17.         *pTipState = TouchSampleValidFlag;  
  18.         //笔针没有按下   
  19.         if ((g_pADCReg->ADCDAT0 & D_UPDOWN_UP)  
  20.         || (g_pADCReg->ADCDAT1 & D_UPDOWN_UP))  
  21.         {  
  22.             TSPMSG((_T("[TSP] Pen Up/r/n")));  
  23.   
  24.             g_bTSP_DownFlag = FALSE;  
  25.   
  26.             g_pADCReg->ADCTSC = ADCTSC_WAIT_PENDOWN;  
  27.   
  28.             *pUncalX = PrevX;  
  29.             *pUncalY = PrevY;  
  30.   
  31.             TSP_SampleStop();  
  32.         }  
  33.         else//笔针按下状态    
  34.         {  
  35.             TSPMSG((_T("[TSP] Pen Down/r/n")));  
  36.   
  37.             g_bTSP_DownFlag = TRUE;//设置笔针按下去状态为真   
  38.   
  39.             g_pADCReg->ADCTSC = ADCTSC_WAIT_PENUP;//ADCTSC设置为等待抬起模式   
  40.   
  41.             *pTipState |= TouchSampleIgnore;  
  42.   
  43.             *pUncalX = PrevX;  
  44.             *pUncalY = PrevY;  
  45.             //added by lqm.must be recovered later.   
  46.             //RETAILMSG(1,(_T("[gIntrTouch] *pUncalX = %d, *pUncalY = %d/n"),*pUncalX,*pUncalY));   
  47.   
  48.             *pTipState |= TouchSampleDownFlag;  
  49.   
  50.             TSP_SampleStart();  //笔针按下后会开启定时器   
  51.         }  
  52.   
  53.         g_pADCReg->ADCCLRWK = CLEAR_ADCWK_INT;  
  54.         // 通知触摸中断已经完成   
  55.         InterruptDone(gIntrTouch);        // Not handled in MDD   
  56.     }  
  57.     // 当触摸屏中断产生时,同时会打开一个定时器,当笔针未松开时定时触发同一个   
  58.     // 事件通知这个工作线程继续采集数据,直到触摸笔抬起后关闭该定时器。   
  59.     // 故上面的if虽然执行了,也会继续执行后面的else。   
  60.     // 处理定时器中断.触摸笔按下后,定时器被打开,   
  61.     // 定时器将定时产生中断gIntrTouchChanged,并触发事件,直到触摸笔抬起为止。   
  62.     else    // gIntrTouchTimer Interrupt Case   
  63.     {  
  64.         TSPMSG((_T("[TSP] gIntrTouchChanged(PHYIRQ_TIMER3) Case/r/n")));  
  65.   
  66.         // Check for Pen-Up case on the event of timer3 interrupt   
  67.         // 在开启定时器后检测到笔针已经抬起   
  68.         if ((g_pADCReg->ADCDAT0 & D_UPDOWN_UP)  
  69.         || (g_pADCReg->ADCDAT1 & D_UPDOWN_UP))  
  70.         {  
  71.             TSPMSG((_T("[TSP] Pen Up +/r/n")));  
  72.   
  73.             g_bTSP_DownFlag = FALSE;  
  74.   
  75.             g_pADCReg->ADCTSC = ADCTSC_WAIT_PENDOWN;//等待按下去模式   
  76.             // 将采样的笔针坐标存放起来   
  77.             *pUncalX = PrevX;  
  78.             *pUncalY = PrevY;  
  79.             *pTipState = TouchSampleValidFlag;  
  80.   
  81.             TSP_SampleStop();//停止定时器中断   
  82.         }  
  83.         else if (g_bTSP_DownFlag)//定时器开启的同时笔针仍然处于按下状态   
  84.         {  
  85.             if (TSP_GetXY(&TmpX, &TmpY) == TRUE)//采集触摸笔按下的位置.一次采样8组数据并取平均   
  86.             {  
  87. #ifdef NEW_FILTER_SCHEME   
  88.                 if(Touch_Pen_Filtering(&TmpX, &TmpY))// 过滤采集的数据.这里通过定时器实现3次数据采集   
  89. #else   
  90.                 if(Touch_Pen_Filtering_Legacy(&TmpX, &TmpY))  
  91. #endif   
  92.                 {  
  93.                     *pTipState = TouchSampleValidFlag | TouchSampleDownFlag;  
  94.                     *pTipState &= ~TouchSampleIgnore;//数据过滤成功,清掉TouchSampleIgnore标识   
  95.                 }  
  96.                 else // Invalid touch pen.数据过滤不成功,则返回TouchSampleIgnore   
  97.                 {  
  98.                     //                    *pTipState = TouchSampleValidFlag;   
  99.                     //                    *pTipState |= TouchSampleIgnore;   
  100.                     *pTipState = TouchSampleIgnore;  
  101.                 }  
  102.                 // 将采样的笔针坐标存放起来   
  103.                 *pUncalX = PrevX = TmpX;  
  104.                 *pUncalY = PrevY = TmpY;  
  105.   
  106.                 g_pADCReg->ADCTSC = ADCTSC_WAIT_PENUP;//等待笔针抬起   
  107.             }  
  108.             else  
  109.             {  
  110.                 *pTipState = TouchSampleIgnore;  
  111.             }  
  112.         }  
  113.         else  
  114.         {  
  115.             RETAILMSG(TSP_ZONE_ERROR,(_T("[TSP] Unknown State/r/n")));  
  116.   
  117.             *pTipState = TouchSampleIgnore;  
  118.             TSP_SampleStop();  
  119.         }  
  120.         // timer3 interrupt status clear, Do not use OR/AND operation on TINTC_CSTAT directly   
  121.         // 清除定时器3中断位???   
  122.         g_pPWMReg->TINT_CSTAT = TINT_CSTAT_INTMASK(g_pPWMReg->TINT_CSTAT) | TIMER3_PENDING_CLEAR;  
  123.         // 结束定时器3中断   
  124.         InterruptDone(gIntrTouchChanged);        // Not Handled in MDD   
  125.     }  
  126.   
  127.     TSPMSG((_T("[TSP] --DdsiTouchPanelGetPoint()/r/n")));  
  128. }  

 

这段代码有点难理解,要通过两个中断源以及事件一起理解。 我们先看MDD层是如何调用这个函数的。

DdsiTouchPanelGetPoint( &SampleFlags, &RawX, &RawY ); 

 

在MDD层中,通过上面的调用方式,从DdsiTouchPanelGetPoint()函数中回传了三个参数,第一个用于表明触摸正处于的状态,RawX,RawY是调用的函数从TP触摸点取样得到的点的数据,这里的数据还是原始的ADC采样的数据,即直接从ADC转换的寄存器里面得到的,并没有对该数据进行处理。

先看看SampleFlags这个变量,一起有如下几种状态:

 TouchSampleValidFlag   = 0x01,  //@EMEM The sample is valid.
 TouchSampleDownFlag    = 0x02,  //@EMEM The finger/stylus is down.
 TouchSampleIsCalibratedFlag  = 0x04,  //@EMEM The XY data has already been calibrated.
 TouchSamplePreviousDownFlag  = 0x08,  //@EMEM The state of the previous valid sample.
 TouchSampleIgnore    = 0x10,     //@EMEM Ignore this sample.

 TouchSampleMouse    = 0x40000000 // reserved

TouchSampleValidFlag标志位表示已经获得TP采样事件,准备开始采样;

TouchSampleDownFlag标志位表示检测到TP已经按下去;

TouchSampleIsCalibratedFlag标志位表示TP已经校准,没有必要再校准了;

TouchSamplePreviousDownFlag标志位是用于校准计数用的,每校准一次要采样五组数据;

TouchSampleIgnore标志位表示触摸失败,采样数据无效;

 

上面的程序中,大的框架如下:

if (g_pVIC1Reg->VICRAWINTR & (1<<(PHYIRQ_PENDN-VIC1_BIT_OFFSET)))

      ......

      if ((g_pADCReg->ADCDAT0 & D_UPDOWN_UP)
         || (g_pADCReg->ADCDAT1 & D_UPDOWN_UP))

      {

       .........

       TSP_SampleStop();

      }

       else

       {

        ........

        TSP_SampleStart();

       }

else

{

        if ((g_pADCReg->ADCDAT0 & D_UPDOWN_UP)
             || (g_pADCReg->ADCDAT1 & D_UPDOWN_UP))

        {

         ........

         TSP_SampleStop();//停止定时器中断

        }

         else

         {

          ........

         }

}

 

      在这里,第一个大的if里面,是检测到了触摸屏中断,然后判断TP是否按下,按下则开起定时器3,用于定时产生中断,这时会结束触摸屏中断,中断转交给定时器。在大的else中,就是定时器中断所需执行的代码了。这里并不是第一个if语句判断有效,后面的else就不执行了,因为触摸屏中断结束后,定时器中断会定时的继续产生中断,从而又会继续判断前面的if标志位,这样就会执行到else语句中来。

      在else语句中,会调用TSP_GetXY(&TmpX, &TmpY)函数,用于采样TP按下的点。具体代码如下:

 

view plain copy to clipboard print ?
  1. static BOOL  
  2. TSP_GetXY(int *px, int *py)  
  3. {  
  4.     int i,j,k;  
  5.     int temp;  
  6.     int x[TSP_SAMPLE_NUM], y[TSP_SAMPLE_NUM];  
  7.     int dx, dy;  
  8.     int TimeOut = 100;  // about 100ms   
  9.   
  10.     EnterCriticalSection(&g_csTouchADC);  
  11.     //一起采样8次坐标值   
  12.     for (i = 0; i < TSP_SAMPLE_NUM; i++)  
  13.     {  
  14.         g_pADCReg->ADCTSC = ADCTSC_AUTO_ADC;    // Auto Conversion   
  15.         g_pADCReg->ADCCON |= ENABLE_START_EN;    // ADC Conversion Start   
  16.   
  17.         while (g_pADCReg->ADCCON & ENABLE_START_EN)// ADC开始转换后100ms内等待该位清0,超过100ms将提示不能转换。   
  18.         {    // Wait for Start Bit Cleared   
  19.             if(TimeOut-- < 0)  
  20.             {  
  21.                 RETAILMSG(ZONE_ERROR,(TEXT("ADC cannot start/n")));//ADC cannot start就是在这里打印出来的???   
  22.                 goto ADCfails;  
  23.             }  
  24.             Sleep(1);  
  25.         }  
  26.   
  27.         TimeOut = 100;  // about 100ms   
  28.         while (!(g_pADCReg->ADCCON & ECFLG_END))//继续等待ADC转换100ms,超时还没转换完则提示不能转换完成。   
  29.         {    // Wait for ADC Conversion Ended   
  30.             if(TimeOut-- < 0)  
  31.             {  
  32.                 RETAILMSG(ZONE_ERROR,(TEXT("ADC Conversion cannot be done/n")));  
  33.                 goto ADCfails;  
  34.             }  
  35.             Sleep(1);  
  36.         }  
  37.         // 从ADCDAT0,ADCDAT1中读取转换的数据   
  38.         x[i] = D_XPDATA_MASK(g_pADCReg->ADCDAT0);  
  39.         y[i] = D_YPDATA_MASK(g_pADCReg->ADCDAT1);  
  40.     }  
  41.   
  42. ADCfails:  
  43.     LeaveCriticalSection(&g_csTouchADC);  
  44.     // x[i],y[i]从小到大排序   
  45.     for (j = 0; j < TSP_SAMPLE_NUM -1; ++j)  
  46.     {  
  47.         for (k = j+1; k < TSP_SAMPLE_NUM; ++k)  
  48.         {  
  49.             if(x[j]>x[k])  
  50.             {  
  51.                 temp = x[j];  
  52.                 x[j]=x[k];  
  53.                 x[k]=temp;  
  54.             }  
  55.   
  56.             if(y[j]>y[k])  
  57.             {  
  58.                 temp = y[j];  
  59.                 y[j]=y[k];  
  60.                 y[k]=temp;  
  61.             }  
  62.         }  
  63.     }  
  64.   
  65. #ifdef    DETAIL_SAMPLING   
  66.     // 8 samples Interpolation (weighted 4 samples)   
  67.     // 8次采样时最小一位,最大两位为无效数据   
  68.     *px = (x[2] + ((x[3]+x[4])<<1) + (x[3]+x[4]) + x[5]);  
  69.     *py = (y[2] + ((y[3]+y[4])<<1) + (y[3]+y[4]) + y[5]);  
  70.     // 求8次采样的平均值   
  71.     if ((*px & 0x7) > 3) *px = (*px>>3) + 1;  
  72.         else *px = *px>>3;  
  73.   
  74.     if ((*py & 0x7) > 3) *py = (*py>>3) + 1;  
  75.         else *py = *py>>3;  
  76.     //求出有效值的最大偏差   
  77.     dx = x[5] - x[2];  
  78.     dy = y[5] - y[2];  
  79. #else   
  80.     // 2 samples average   
  81.     // 求两次采样的平均值   
  82.     *px = (x[1] + x[2] + 1)>>1;  
  83.     *py = (y[1] + y[2] + 1)>>1;  
  84.     //求出有效值的最大偏差   
  85.     dx = x[2] - x[1];  
  86.     dy = y[2] - y[1];  
  87. #endif   
  88.     // 有效值的最大偏差大于40时,返回失败   
  89.     if ((dx > TSP_INVALIDLIMIT) || (dy > TSP_INVALIDLIMIT))  
  90.     {  
  91.         return FALSE;  
  92.     }  
  93.     else  
  94.     {  
  95.         return TRUE;  
  96.     }  
  97. }  

 

      这里用到了一组简单的数学算法,详细见程序的注释。ADC采样一起采样8次,舍掉最小值,最大值,再加权求平均,最终得到一组TP采样值,回传。

      采样的数据会继续过滤一次,通过调用Touch_Pen_Filtering(&TmpX, &TmpY)函数实现。具体代码如下:

view plain copy to clipboard print ?
  1. static BOOL  
  2. Touch_Pen_Filtering(INT *px, INT *py)  
  3. {  
  4.     BOOL RetVal = TRUE;  
  5.     // TRUE  : Valid pen sample   
  6.     // FALSE : Invalid pen sample   
  7.     INT Filter_Margin;  
  8.     static int count = 0;  
  9.     static INT x[2], y[2];  
  10.     INT TmpX, TmpY;  
  11.     INT dx, dy;  
  12.   
  13.     if(*px <0 && *py <0)  
  14.     {  
  15.         count = 0;  
  16.         return FALSE;  
  17.     }  
  18.     else  
  19.     {  
  20.         count++;  
  21.     }  
  22.   
  23.     if (count > 2)// 只采样两组数据,两次后开始下面的Filter算法   
  24.     {  
  25.         // apply filtering rule   
  26.         count = 2;  
  27.   
  28.         // average between x,y[0] and *px,y   
  29.         // 第一组数据和第三组数据取平均   
  30.         TmpX = (x[0] + *px)>>1;  
  31.         TmpY = (y[0] + *py)>>1;  
  32.   
  33.         // difference between x,y[1] and TmpX,Y   
  34.         // 求出第二组数据与第一,三组数据的平均值的差值   
  35.         dx = (x[1] > TmpX) ? (x[1] - TmpX) : (TmpX - x[1]);  
  36.         dy = (y[1] > TmpY) ? (y[1] - TmpY) : (TmpY - y[1]);  
  37.   
  38.     // Filter_Margin = 第一,二组数据的X,Y坐标的差值 + TSP_FILTER_LIMIT(3)   
  39.         Filter_Margin = (x[1] > x[0]) ? (x[1]-x[0]) : (x[0]-x[1]);// 求出第一组数据与第二组数据的X坐标的差值   
  40.         Filter_Margin += (y[1] > y[0]) ? (y[1]-y[0]) : (y[0]-y[1]);//求出第一组数据与第二组数据的Y坐标的差值并累加到Filter_Margin   
  41.         Filter_Margin += TSP_FILTER_LIMIT;  
  42.   
  43.     // 如果dx,dy大于Filter_Margin,则保存上一组采样数据[第二组],并返回失败   
  44.     // 这里可以拓展实现触摸屏的滑动???   
  45.         if ((dx > Filter_Margin) || (dy > Filter_Margin))   
  46.      {  
  47.             // Invalid pen sample   
  48.             *px = x[1];  
  49.             *py = y[1]; // previous valid sample   
  50.             RetVal = FALSE;  
  51.             count = 0;  
  52.         }  
  53.         else//否则保留后两组数据到x[0],y[0]以及x[1],y[1].程序执行到这里,笔针采样数据才有效   
  54.         {  
  55.             // Valid pen sample   
  56.             x[0] = x[1]; y[0] = y[1];  
  57.             x[1] = *px; y[1] = *py; // reserve pen samples   
  58.   
  59.             RetVal = TRUE;  
  60.         }  
  61.     }  
  62.     else // (count > 2)   
  63.     { // till 2 samples, no filtering rule   
  64.         // 保存前两组采样得到的数据。   
  65.         // 第一组采样的数据放到x[0],y[0]   
  66.         // 第二组采样的数据放到x[1],y[1]   
  67.         x[0] = x[1]; y[0] = y[1];  
  68.         x[1] = *px; y[1] = *py; // reserve pen samples   
  69.   
  70.         RetVal = FALSE;  
  71.     }  
  72.   
  73.     return RetVal;  
  74. }  

 

      看到这里也许会觉得奇怪,为什么会是count>2呢?

      这里是联合定时器3的定时中断实现。只有当采样了两组数据之后,该函数才会返回真,否则采样的数据是不作数的。实际上是一起采样了三组数据,具体算法见程序中注释。

      经过过滤后的数据再回传给MDD,然后提交。

      这里提交的数据其实仍然是直接从ADCDAT寄存器中读出的数据,并没有做相应处理。由于TP本身的非线性,程序为了兼容各种不同的非线性TP,微软通过一组数学算法将这个很复杂的问题轻而易举的通过程序解决了。

 

     

Windows CE中通过在函数DdsiTouchPanelGetDeviceCaps 中设置校准点的个数,在TouchDriverCalibrationPointGet中获取每个校准点的屏幕坐标。常用的校准点数量为5。校准UI将在校准点坐标处相应显示一个十字叉,用户需要精确地在该十字叉位置按下触摸屏,驱动通过TouchPanelReadCalibrationPoint函数读取相应的触摸屏坐标值,然后开始下一个校准点。循环设定的次数后,将采集到的触摸屏坐标值和校准点屏幕坐标送到TouchPanelSetCalibration函数中进行处理。该函数将产生校准基准参数。

TouchPanelSetCalibration函数执行的动作是一套数学算法,具体内容为:

在触摸屏数据与其位置偏移关系且屏幕像素与其位置偏移关系同为线性关系假设情况下,触摸屏返回的位置信息与像素位置信息之间成2D坐标变换关系。则对于触摸屏按下点的触摸屏坐标(Tx,Ty)与其在显示设备位置关系上匹配的点的屏幕坐标(Sx,Sy)之间的转换关系,可以通过下述坐标变换表示:

Sx =  A1*Tx + B1*Ty + C1

Sy =  A2*Tx + B2*Ty + C2

TouchPanelSetCalibration的具体工作就是通过校准的动作获取的屏幕坐标和触摸屏坐标TouchCoordinate来确定A1B1C1A2, B2, C2

      

        在开发产品时,针对不同的屏,注册表中默认的校正值通常是不准的,这时就需要得到一组准确的值。这组值怎么样获得呢?很明显,上面的函数DdsiTouchPanelGetPoint()回传的点的数据并不是我们想要的,因为它并没有做任何的处理。我们真正需要的是经过上面的公式换算后得到的值。其实我们只需要校正一遍TP后,Touch驱动程序会自动将校正好的一组正确的值重刷到注册表,我们只需要打开我们手上机器的注册表,查看其键值就行了。具体在localmachine->hardware->touch中可以看到。

分享至
一键收藏,随时查看,分享好友!
0人
了这篇文章
类别: 嵌入式┆阅读( 0)┆评论( 0) ┆ 返回博主首页┆ 返回博客首页
上一篇 QtSpeech会让Qt说话 下一篇 Qt4之动态加载Ui 窗体控件

你可能感兴趣的:(基于S3C6410的Touch驱动详解)