⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsc2046touch.cpp

📁 TI TCS2046 Toutch PAD IC Driver for WindowsCE
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	RETAILMSG(1,(TEXT("END Calling DdsiTouchPanelPowerHandler()\r\n")));
}

//
// DDSI Implementation
//
// @doc EX_TOUCH_DDSI EXTERNAL DRIVERS DDSI TOUCH_PANEL
//
// @func BOOL | DdsiTouchPanelGetDeviceCaps|
// Queries for capabilities of the TSC2046 Device
//
// @comm
// Implemented in the PDD.
//
extern "C" BOOL DdsiTouchPanelGetDeviceCaps(INT iIndex,LPVOID  lpOutput)
{

	RETAILMSG(1, (TEXT("Calling DdsiTouchPanelGetDeviceCaps(0x%x)\r\n"),iIndex));
    if (lpOutput == NULL)
    {
        ERRORMSG(1,(__TEXT("TouchPanelGetDeviceCaps: invalid parameter.\r\n")));
        SetLastError(ERROR_INVALID_PARAMETER);
        return (FALSE);
    }

    switch (iIndex)
    {
    case TPDC_SAMPLE_RATE_ID:
        {
            TPDC_SAMPLE_RATE  *pTSR = (TPDC_SAMPLE_RATE*)lpOutput;
            pTSR->SamplesPerSecondLow = TOUCHPANEL_SAMPLE_RATE_LOW;
            pTSR->SamplesPerSecondHigh = TOUCHPANEL_SAMPLE_RATE_HIGH;
            pTSR->CurrentSampleRateSetting = 90;
        }
        break;

    case TPDC_CALIBRATION_POINT_COUNT_ID:
        {
            TPDC_CALIBRATION_POINT_COUNT *pTCPC = (TPDC_CALIBRATION_POINT_COUNT*)lpOutput;
            pTCPC->flags = 0;
            pTCPC->cCalibrationPoints = 5;
        }
        break;

    case TPDC_CALIBRATION_POINT_ID:
        return(TouchDriverCalibrationPointGet((TPDC_CALIBRATION_POINT*)lpOutput));

    default:
        ERRORMSG(1,(__TEXT("TouchPanelGetDeviceCaps: invalid parameter.\r\n")));
        SetLastError(ERROR_INVALID_PARAMETER);
        return (FALSE);
    }
	RETAILMSG(1, (TEXT("END Calling DdsiTouchPanelGetDeviceCaps(0x%x)\r\n"),iIndex));
    return (TRUE);
}

//
// DDSI Implementation
//
// @doc EX_TOUCH_DDSI EXTERNAL DRIVERS DDSI TOUCH_PANEL
//
// @func BOOL | DdsiTouchPanelSetMode|
// Sets Information about TSC2046 Touch Panel function
//
// @comm
// Implemented in the PDD.
//
BOOL DdsiTouchPanelSetMode(INT iIndex, LPVOID lpInput)
{
    BOOL  ReturnCode = FALSE;
	RETAILMSG(1,(TEXT("Calling DdsiTouchPanelSetMode(0x%x)\r\n"),iIndex));
    switch (iIndex)
	{
        case TPSM_SAMPLERATE_LOW_ID:
        case TPSM_SAMPLERATE_HIGH_ID:
            SetLastError(ERROR_SUCCESS);
            ReturnCode = TRUE;
            break;
        default:
            SetLastError(ERROR_INVALID_PARAMETER);
            break;
    }
	RETAILMSG(1,(TEXT("END Calling DdsiTouchPanelSetMode(0x%x)\r\n"),iIndex));
    return (ReturnCode);
}

//
//--------------------------------------------------------------
// Local functions called.
//--------------------------------------------------------------
//

//--------------------------------------------------------------
// Function: EnableTouchPanelIRQ
// 
// Purpose:  Enable TSC2046 /PENIRQ Interrupt mode so that 
//           we can detect pen down interrupts. 
//--------------------------------------------------------------
void EnableTouchPanelIRQ(void)
{
	SPITransaction(0x90, FALSE);
	HWWait(10);
}

//--------------------------------------------------------------
// Function: PrepareNextInterrupt
// 
// Purpose:  Interrupt Preparation 
//--------------------------------------------------------------
BOOL PrepareNextInterrupt(EXPECTED_INT_EVENT irqMode)
{
    if (irqMode == PEN_DOWN) 
	{
		EnableTouchPanelIRQ();
	}
	else 
	{ 
		HWEnableTouchTimerIRQ(TOUCH_TIMER_INCREMENT);
	}
    return(TRUE);
}

//--------------------------------------------------------------
// Function: getTouchCoordinate
// 
// Purpose:  This function will setup the touch controller of
//       TSC2046 to collect 4 pair of samples. The first 
//       pair of XY samples will be discarded, which gives a 
//       better variation in the sample if the TSC2046 has 
//       a larger input capacitance.
//--------------------------------------------------------------

void getTouchCoordinate()
{
 	UINT16 i;
	// disable /PENIRQ when reading
	for(i=0;i<NUMBER_SAMPLES_PER_POINT;i++)
	{
    	// Read X Axis ([A2 A1 A0] = [1 0 1] and use these data
		rgPointSamples[i].XSample = SPITransaction(0xD1, FALSE);
//		RETAILMSG(1,(TEXT("i= %d x= %x\r\n"),i,rgPointSamples[i].XSample));
	}
	for(i=0;i<NUMBER_SAMPLES_PER_POINT;i++)
	{
		// Read Y Axis ([A2 A1 A0] = [0 0 1] and use these data
    	rgPointSamples[i].YSample = SPITransaction(0x91, FALSE);
//		RETAILMSG(1,(TEXT("i= %d y= %x\r\n"),i,rgPointSamples[i].YSample));
	}
}

//--------------------------------------------------------------
// Function: evaluateSample
// 
// Purpose:  This function will used only by the PDD. Each time 
//       on a pen down, 4 pairs XY from the ADC are collected and
//       3 of them will be used here. This function implements   
//       the algorithm to the best sample from 3 pairs of samples
//       by discarding one that is too way out and take a mean of
//       the rest two. 
//  
// Returns:  TouchSampleValidFlag and average of the 2 good samples
//--------------------------------------------------------------
INT evaluateSample(USHORT val0,USHORT val1,USHORT val2,int maxError,INT *sample)
{
  LONG	diff0,diff1,diff2;
  INT	retval=TouchSampleIgnore;

  if ((val0 <= MAX_ADC_VAL) && (val1 <= MAX_ADC_VAL) && (val2 <= MAX_ADC_VAL)) 
  {
    // Calculate the absolute value of the differences of the sample
    diff0 = val0 - val1;
    diff1 = val1 - val2;
    diff2 = val2 - val0;
    diff0 = diff0 > 0  ? diff0 : -diff0;
    diff1 = diff1 > 0  ? diff1 : -diff1;
    diff2 = diff2 > 0  ? diff2 : -diff2;

	// Eliminate the one away from other two and add the two
	if (diff0 < diff1) 
		*sample=(UINT16)(val0 + ((diff2 < diff0) ? val2 : val1));
    else  
		*sample=(UINT16)(val2 + ((diff2 < diff1) ? val0 : val1));
    
	// Get the average of the two good samples
    *sample>>=1;

    if ((diff0 < maxError) && (diff1 < maxError) && (diff2 < maxError)) 
        retval = TouchSampleValidFlag;
  }
  return(retval);
}

// Sample touch data
TOUCH_PANEL_SAMPLE_FLAGS PDDSampleTouchScreen(INT *x,INT *y)
{
	INT iReadX = 0, iReadY = 0;
	TOUCH_PANEL_SAMPLE_FLAGS TmpStateFlags;
	
	// read touch screen sample of X and Y
	getTouchCoordinate();

	// get evalited sample X and Y
	TmpStateFlags = TouchSampleDownFlag;
    if (evaluateSample(rgPointSamples[1].XSample,rgPointSamples[2].XSample,
		    rgPointSamples[3].XSample, DELTA_X_COORD_VARIANCE,&iReadX) == TouchSampleIgnore)
    {
        TmpStateFlags |= TouchSampleIgnore;
        RETAILMSG(1,(TEXT("Invalid X sample\r\n")));
    }
    else
    {
        TmpStateFlags |= evaluateSample(rgPointSamples[1].YSample,rgPointSamples[2].YSample,
			rgPointSamples[3].YSample,DELTA_Y_COORD_VARIANCE,&iReadY);
    }
	*x=iReadX;
	*y=iReadY;
    RETAILMSG(1,(TEXT("X= %x Y= %x\r\n"), iReadX, iReadY));
	return(TmpStateFlags);
}

// Get Calibration Values
extern "C" BOOL TouchDriverCalibrationPointGet(TPDC_CALIBRATION_POINT *pTCP)
{
    INT32 cDisplayWidth = pTCP->cDisplayWidth;
    INT32 cDisplayHeight = pTCP->cDisplayHeight;
    int CalibrationRadiusX = cDisplayWidth/10;
    int CalibrationRadiusY = cDisplayHeight/10;

	RETAILMSG(1,(TEXT("Call TouchDriverCalibrationPointGet\r\n")));

    switch (pTCP->PointNumber)
    {
    case  0:  // Middle
        pTCP->CalibrationX = cDisplayWidth/2;
        pTCP->CalibrationY = cDisplayHeight/2;
        break;
    case  1:  // Upper Left
        pTCP->CalibrationX = CalibrationRadiusX*2;
        pTCP->CalibrationY = CalibrationRadiusY*2;
        break;
    case  2:  // Lower Left
        pTCP->CalibrationX = CalibrationRadiusX*2;
        pTCP->CalibrationY = cDisplayHeight - CalibrationRadiusY*2;
        break;
    case  3:  // Lower Right
        pTCP->CalibrationX = cDisplayWidth - CalibrationRadiusX*2;
        pTCP->CalibrationY = cDisplayHeight - CalibrationRadiusY*2;
        break;
    case  4:  // Upper Right
        pTCP->CalibrationX = cDisplayWidth - CalibrationRadiusX*2;
        pTCP->CalibrationY = CalibrationRadiusY*2;
        break;
    default:
        pTCP->CalibrationX = cDisplayWidth/2;
        pTCP->CalibrationY = cDisplayHeight/2;
        SetLastError(ERROR_INVALID_PARAMETER);
        return (FALSE);
    }
    return (TRUE);
}

///////////////////////////////////////////////////////////
///// End of TSC2046Touch.CPP Functions
///////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
///// END of FILE
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -