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

📄 touch.cpp

📁 PXA270 MainstoneII平台下完整触摸屏驱动源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:


void DdsiTouchPanelGetPoint(TOUCH_PANEL_SAMPLE_FLAGS *pTipStateFlags, INT *pUncalX, INT *pUncalY)
{
    static BOOL TouchIrq = TRUE;
    UINT32 InterruptType = SYSINTR_NOP;

	DEBUGMSG(DEBUG_FUNC, (TEXT("+DdsiTouchPanelGetPoint\r\n")));

    if (!pTipStateFlags || !pUncalX || !pUncalY)
    {
        DEBUGMSG(TRUE, (TEXT("ERROR: DdsiTouchPanelGetPoint: Invalid parameter(s).\r\n")));
        return;
    }

    if (TouchIrq)
    {
		// Do Touch irq

        TouchIrq = FALSE;

        // The pen was previously in the up state - it just transitioned to the down state.

        InterruptType   = SYSINTR_TOUCH;
        *pTipStateFlags = PDDSampleTouchScreen(pUncalX, pUncalY);

        // Disable the touch interrupt and clear the falling edge that just occurred.
        //WriteAC97(UCB_INT_CS, TSPX_INT, DEV_TOUCH);
        //WriteAC97(UCB_FE_IE, 0, DEV_TOUCH);

        // The next expected interrupt will come from the sampling timer (pen-up doesn't
        // cause an interrupt).
        g_NextExpectedInterrupt = PEN_UP_OR_TIMER;

    }
    else
    {
        // Do Touch Timer 

        // The pen was previously in the down state - the sampling timer just interrupted.
        // The pen could now be either up or down at this point - we need to check.

        InterruptType   = SYSINTR_TOUCH_CHANGED;
        *pTipStateFlags = PDDSampleTouchScreen(pUncalX, pUncalY);

        // The next expected interrupt will come from the sampling timer (pen-up doesn't
        // cause an interrupt).
        g_NextExpectedInterrupt = PEN_UP_OR_TIMER;

    }

    // If we're expecting the next interrupt to come from a pen-up or from the timer and
    // the pen isn't currently down, send the MDD a pen-up "event".
    if ((g_NextExpectedInterrupt == PEN_UP_OR_TIMER) && !PenIsDown())
    {
		// Do Pen Up

        TouchIrq = TRUE;

        // No TouchSampleDownFlag (so we're telling the MDD that the pen is now up).
        *pTipStateFlags = TouchSampleValidFlag;   // send pen up to mdd

        // The next expected interrupt will come from the pen-down event.
        g_NextExpectedInterrupt = PEN_DOWN;

    }

    // Make sure the next expected interrupt is configured and enabled.
    //
    PrepareNextInterrupt();

    // Tell the OAL to clear and unmask the interrupt the just occurred.
    //
    InterruptDone(InterruptType);

	DEBUGMSG(DEBUG_FUNC, (TEXT("-DdsiTouchPanelGetPoint\r\n")));

}



// --------------------------------------------


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;

        if (diff0 < diff1)
            *sample=(ULONG)(val0 + ((diff2 < diff0) ? val2 : val1));
        else
            *sample=(ULONG)(val2 + ((diff2 < diff1) ? val0 : val1));

        *sample>>=1;

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


BOOL sampleADC(USHORT *sample,unsigned axis)
{
    //UINT32 adccr=0;
    //UINT32 adcDataReg;

    //BOOL noError=FALSE;

    //adccr = (axis == UCB_ADC_X ? ADC_INPUT_TSPY : ADC_INPUT_TSPX) | ADC_ENA ;

    //if (WriteAC97(UCB_ADC_CR,*(UINT16 *)&adccr, DEV_TOUCH))
    //{
    //    XllpOstDelayMicroSeconds((P_XLLP_OST_T) g_pOSTRegs, 50);

    //    g_pGPIORegs->GPCR0 = XLLP_GPIO_BIT_PWM_OUT0;

    //    adccr |= ADC_START;
    //    if (WriteAC97(UCB_ADC_CR,*(UINT16 *)&adccr, DEV_TOUCH))
    //    {
    //        g_pGPIORegs->GPSR0 = XLLP_GPIO_BIT_PWM_OUT0;

    //        XllpOstDelayMicroSeconds((P_XLLP_OST_T) g_pOSTRegs, 50);

    //        do
    //        {                     // wait for sample completion
    //            ReadAC97(UCB_ADC_DATA,(UINT16 *) &adcDataReg,DEV_TOUCH);

    //        } while (!TEST(adcDataReg,ADC_DATA_VAL));



    //        *sample=TEST(adcDataReg,ADC_DATA) ;
    //        // disable adc
    //        CLEAR(adccr,ADC_ENA);
    //        WriteAC97(UCB_ADC_CR,*(UINT16 *)&adccr,DEV_TOUCH);
    //        noError=TRUE;
    //    }
    //}

    //return(noError);


    // For WM9712 Codec
	DEBUGMSG(DEBUG_FUNC, (TEXT("+sampleADC\r\n")));

	UINT16 adcDataReg;
	UINT16 adctemp = 0;
	UINT16 timeout_flag = 0;
	UINT16 adcControl1 = 0;
	BOOL   noError = FALSE;

	if (axis == WM9712_ADC_X)
	{
		adctmep = WM9712_DC1_ADC_X;
	}
	else
	{
		adctmep = WM9712_DC1_ADC_Y;
	}

	ReadAC97(WM9712_DIGITISER_CONTORL1, &adcControl1, DEV_TOUCH);

	// Set polling mode, axis and delay, clear Measurement Type, Delay time, then redefine the ADC measurement type.
	adcControl1 &= ~(WM9712_TOUCHCTRL1_ADR_MASK | WM9712_TOUCHCTRL1_DEL_MASK);
	adcControl1 |= (adctmep | WM9712_DC1_POLL | WM9712_TOUCHCTRL1_DEL_DEFAULT);

	if (WriteAC97(WM9712_DIGITISER_CONTORL1, adcControl1, DEV_TOUCH))
	{
		XllpOstDelayMicroSeconds((P_XLLP_OST_T) g_pOSTRegs, 50);
		while (timeout_flag++ < 5)
		{
			ReadAC97(WM9712_DIGITISER_DATA, &adcDataReg, DEV_TOUCH);
			// Check to make sure the data is for X or Y, if match the data we request, quit the reading loop and continue to get the data.
			if ((adcDataReg & WM9712_TOUCHCTRL1_ADR_MASK) == adctmep)
			{
				break;
			}
			XllpOstDelayMicroSeconds((P_XLLP_OST_T) g_pOSTRegs, 20);

		} // end of while

		if (adcDataReg & WM9712_DATA_PENDOWN)
		{
			*sample = adcDataReg & WM9712_DATA_VAL;
			//TouchPanel ADC data, low 12 bits in 7Ah
			noError = TRUE;
		}		
	}	

	DEBUGMSG(DEBUG_FUNC, (TEXT("-sampleADC\r\n")));
	return(noError);
}


USHORT getTouchCoordinate(unsigned axis)
{
    //UINT32 tcr=0;
    //BOOL noError=FALSE;
    //USHORT sample=MAX_ADC_VAL;

    ////Bias the ADC, set position mode and choose the correct axis.

    //tcr = TSC_BIAS_ENA | TSC_MODE_POSITION | ((axis==UCB_ADC_X) ? (TSPX_POW | TSMX_GND) : (TSPY_POW | TSMY_GND));
    //if (WriteAC97(UCB_TCH_CR,*(UINT16 *)&tcr, DEV_TOUCH))
    //    noError=sampleADC(&sample,axis);


	// For WM9712 Codec
	DEBUGMSG(DEBUG_FUNC, (TEXT("+getTouchCoordinate\r\n")));

	BOOL   noError = FALSE;
	USHORT sample = MAX_ADC_VAL;
	noError = sampleADC(&sample, axis);

	DEBUGMSG(DEBUG_FUNC, (TEXT("-getTouchCoordinate\r\n")));

    return(sample);
}


extern "C" BOOL TouchDriverCalibrationPointGet(TPDC_CALIBRATION_POINT *pTCP)
{
    INT32 cDisplayWidth = pTCP->cDisplayWidth;
    INT32 cDisplayHeight = pTCP->cDisplayHeight;

    int CalibrationRadiusX = cDisplayWidth/10;
    int CalibrationRadiusY = cDisplayHeight/10;


    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);
}


extern "C" BOOL DdsiTouchPanelGetDeviceCaps(INT iIndex,LPVOID  lpOutput)
{

    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);
    }
    return (TRUE);
}


BOOL DdsiTouchPanelSetMode(INT iIndex,LPVOID  lpInput)
{
    BOOL  ReturnCode = FALSE;

    switch (iIndex)
    {
    case TPSM_SAMPLERATE_LOW_ID:
    case TPSM_SAMPLERATE_HIGH_ID:
        SetLastError(ERROR_SUCCESS);
        ReturnCode = TRUE;
        break;

    default:
        SetLastError(ERROR_INVALID_PARAMETER);
        break;
    }
    return(ReturnCode);
}


TOUCH_PANEL_SAMPLE_FLAGS PDDSampleTouchScreen(INT *x,INT *y)
{
    UINT32 i;
    TOUCH_PANEL_SAMPLE_FLAGS TmpStateFlags;
    TOUCHPANEL_POINT_SAMPLES rgPointSamples;
    INT TmpX = 0;
    INT TmpY = 0;

    // It is more reliable to read X and Y alternatively rather then read all X's then all Y's.
    // This way we have a better chance to detect and reject a bad sample if the pen is up
    // briefly while the reading takes place. Otherwise, we might have all three X readings
    // when pen is up momentarily and all three Y readings when pen is down again. The
    // function evaluateSample() will not be able to detect and reject the sample in this case.
    //
    for(i=0;i<NUMBER_SAMPLES_PER_POINT;i++)
    {
        //getTouchCoordinate(UCB_ADC_X);             // Prime the Pump for the X
        rgPointSamples[i].XSample=getTouchCoordinate(WM9712_ADC_X);  // For WM9712

        //getTouchCoordinate(UCB_ADC_Y);             // Prime the Pump for the Y
        rgPointSamples[i].YSample=getTouchCoordinate(WM9712_ADC_Y);  // For WM9712
    }

    TmpStateFlags = TouchSampleDownFlag;
    if (evaluateSample(rgPointSamples[0].XSample,rgPointSamples[1].XSample,rgPointSamples[2].XSample,
                       DELTA_X_COORD_VARIANCE,&TmpX) == TouchSampleIgnore)
    {
        TmpStateFlags |= TouchSampleIgnore;
        DEBUGMSG(1,(TEXT("Invalid X sample\r\n")));
    }
    else
    {
        TmpStateFlags |= evaluateSample(rgPointSamples[0].YSample,rgPointSamples[1].YSample,rgPointSamples[2].YSample,
                                        DELTA_Y_COORD_VARIANCE,&TmpY);
    }

    *x=TmpX;
    *y=TmpY;

    return(TmpStateFlags);
}

⌨️ 快捷键说明

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