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

📄 ts.c

📁 CNC.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
//! \return None.
//
//*****************************************************************************
void
TSIntHandler(void)
{
    unsigned long ulTemp;

    //
    // If the read count is ten, then write the remaining two words to the
    // touch screen controller.
    //
    if(g_ulTSCount == 10)
    {
        SSIDataPut(SSI_BASE, 0x0090);
        SSIDataPut(SSI_BASE, 0x0000);
    }

    //
    // Read all available data from the SSI read FIFO.
    //
    while(SSIDataNonBlockingGet(SSI_BASE, &ulTemp) != 0)
    {
        //
        // If the read count is six, then this is the good X coordinate value.
        //
        if(g_ulTSCount == 6)
        {
            //
            // Extract the actual X coordinate value from the word.
            //
            g_ulTSX = (ulTemp >> 5) & 0x03ff;
        }

        //
        // If the read count is two, then this is the good Y coordinate value.
        //
        if(g_ulTSCount == 2)
        {
            //
            // Extract the actual Y coordinate value from the word.
            //
            g_ulTSY = (ulTemp >> 5) & 0x03ff;
        }

        //
        // Decrement the count of words to be read.
        //
        g_ulTSCount--;
    }

    //
    // See if all ten words have been read from the touch screen controller.
    //
    if(g_ulTSCount == 0)
    {
        //
        // Deassert the SSI chip select.
        //
        GPIOPinWrite(GPIO_PORTA_BASE, SSI_CS, SSI_CS);
    }
}

//*****************************************************************************
//
//! Reads a position value from the touch screen.
//!
//! \param pulX is a pointer to the location to store the X position reading.
//! \param pulY is a pointer to the location to store the Y position reading.
//!
//! This will read a new position value from the touch screen.  The value from
//! the previous reading is returned and a new position acquisition is started,
//! incuring a minimal amount of delay.  If the previous reading is not yet
//! available, this routine will delay until it is available.
//!
//! \return None.
//
//*****************************************************************************
void
TSRead(unsigned long *pulX, unsigned long *pulY)
{
    //
    // Wait until all words have been read from the touch screen controller.
    //
    while(g_ulTSCount != 0)
    {
    }

    //
    // Return the values from the previous conversion.
    //
    *pulX = g_ulTSX;
    *pulY = g_ulTSY;

    //
    // See if the pen IRQ is being asserted, meaning that the touch screen is
    // being pressed.
    //
    if(GPIOPinRead(GPIO_PORTA_BASE, PEN_IRQ) == 0)
    {
        //
        // Assert the SSI chip select.
        //
        GPIOPinWrite(GPIO_PORTA_BASE, SSI_CS, 0);

        //
        // Tell the touch screen controller to sampling the X position four
        // times, then the Y position four times.
        //
        SSIDataPut(SSI_BASE, 0x00d3);
        SSIDataPut(SSI_BASE, 0x00d3);
        SSIDataPut(SSI_BASE, 0x00d3);
        SSIDataPut(SSI_BASE, 0x00d3);
        SSIDataPut(SSI_BASE, 0x0093);
        SSIDataPut(SSI_BASE, 0x0093);
        SSIDataPut(SSI_BASE, 0x0093);
        SSIDataPut(SSI_BASE, 0x0093);

        //
        // Set the count of words left to read from the touch screen controller
        // to ten.
        //
        g_ulTSCount = 10;
    }
    else
    {
        //
        // The touch screen is not being pressed, so do not sample it and
        // simply provide a reading that is not on the screen.
        //
        g_ulTSX = 0;
        g_ulTSY = 0;
    }
}

//*****************************************************************************
//
//! Calibrates the touch screen.
//!
//! \param pulX is a pointer to an array of three X screen coordinates used for
//! calibration.
//! \param pulY is a pointer to an array of three Y screen coordinates used for
//! calibration.
//! \param pulTSX is a pointer to an array of three X samples corresponding to
//! the three screen coordinates.
//! \param pulTSY is a pointer to an array of three Y samples corresponding to
//! the three screen coordinates.
//! \param plMatrix is a pointer to an array of seven coefficients that are
//! generated from the samples.
//!
//! This function takes three points on the screen, along with the
//! corresponding touch screen sample data, and computes the coefficients
//! required to convert touch screen sample data into screen coordinates.  The
//! coefficents produced by this routine are used by TSConvert() to perform the
//! actual sample mapping.
//!
//! \return None.
//
//*****************************************************************************
#if 0
void
TSCalibrate(unsigned long *pulX, unsigned long *pulY, unsigned long *pulTSX,
            unsigned long *pulTSY, long *plMatrix)
{
    //
    // Compute the coefficients used for calculating the X position.
    //
    plMatrix[0] = (((pulX[0] - pulX[2]) * (pulTSY[1] - pulTSY[2])) -
                   ((pulX[1] - pulX[2]) * (pulTSY[0] - pulTSY[2])));
    plMatrix[1] = (((pulTSX[0] - pulTSX[2]) * (pulX[1] - pulX[2])) -
                   ((pulX[0] - pulX[2]) * (pulTSX[1] - pulTSX[2])));
    plMatrix[2] = ((((pulTSX[2] * pulX[1]) - (pulTSX[1] * pulX[2])) *
                    pulTSY[0]) +
                   (((pulTSX[0] * pulX[2]) - (pulTSX[2] * pulX[0])) *
                    pulTSY[1]) +
                   (((pulTSX[1] * pulX[0]) - (pulTSX[0] * pulX[1])) *
                    pulTSY[2]));

    //
    // Compute the coefficients used for calculating the Y position.
    //
    plMatrix[3] = (((pulY[0] - pulY[2]) * (pulTSY[1] - pulTSY[2])) -
                   ((pulY[1] - pulY[2]) * (pulTSY[0] - pulTSY[2])));
    plMatrix[4] = (((pulTSX[0] - pulTSX[2]) * (pulY[1] - pulY[2])) -
                   ((pulY[0] - pulY[2]) * (pulTSX[1] - pulTSX[2])));
    plMatrix[5] = ((((pulTSX[2] * pulY[1]) - (pulTSX[1] * pulY[2])) *
                    pulTSY[0]) +
                   (((pulTSX[0] * pulY[2]) - (pulTSX[2] * pulY[0])) *
                    pulTSY[1]) +
                   (((pulTSX[1] * pulY[0]) - (pulTSX[0] * pulY[1])) *
                    pulTSY[2]));

    //
    // Compute the common divisor for the X and Y position.
    //
    plMatrix[6] = (((pulTSX[0] - pulTSX[2]) * (pulTSY[1] - pulTSY[2])) -
                   ((pulTSX[1] - pulTSX[2]) * (pulTSY[0] - pulTSY[2])));
}
#endif

//*****************************************************************************
//
//! Converts a touch screen sample to screen coordinates.
//!
//! \param pulX is a pointer to the input X coordinate, which is overwritten
//! with the converted X coordinate.
//! \param pulY is a pointer to the input Y coordinate, which is overwritten
//! with the converted Y coordinate.
//! \param plMatrix is a pointer to the coefficients required for the
//! conversion.
//!
//! This routine takes the coefficients computed by TSCalibrate() and applies
//! them to a new sample point, converting it into screen coordinates.
//!
//! \return None.
//
//*****************************************************************************
void
TSConvert(unsigned long *pulX, unsigned long *pulY, const long *plMatrix)
{
    unsigned long ulTemp;

    //
    // Save the input X coordinate since it is needed to compute both the X and
    // Y coordinates.
    //
    ulTemp = *pulX;

    //
    // Convert the X coordinate.
    //
    *pulX = (((plMatrix[0] * ulTemp) + (plMatrix[1] * *pulY) + plMatrix[2]) /
             plMatrix[6]);

    //
    // Convert the Y coordinate.
    //
    *pulY = (((plMatrix[3] * ulTemp) + (plMatrix[4] * *pulY) + plMatrix[5]) /
             plMatrix[6]);
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

⌨️ 快捷键说明

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