.Touch()

back to FEHLCD

Description

This function is used to detect whether or not the Proteus screen is being touched. In addition, two pointers to floating point variables can be passed into the function, and after the function is called the variables that were pointed to will contain the x and y coordinate of where the touch of the screen was located.

It should be noted that it is extremely difficult to touch pixels on the x-axis less than 15, and greater than 300. On the y-axis is it difficult to touch pixels less than 20 and greater than 220. As such, the use of these coordinates as inputs are discouraged.

Additionally, the communication protocol that the touch screen uses is based on a queue, or buffer, system in which touch events are stored in a queue until they are retrieved by the user code with this function. So, any time there is a sleep statement or something similar in the code where the user has the opportunity to touch the screen multiple times before a touch is expected or desired, the “touch buffer” will need to be cleared before the user touches for the expected occurrence so that the new, proper touch information is used instead of old, incorrect touch information. The second example below shows how to do this, using the LCD.ClearBuffer() function.

Syntax

.Touch(float *x_pos, float *y_pos);

Parameters

*x_pos: A pointer to the variable containing the x position

*y_pos: A pointer to the variable containing the y position

Return

A bool variable that is true if touch is present and false otherwise.

Example #1

#include <FEHLCD.h>

int main(void)
{
    float x_position, y_position;

    if(LCD.Touch(&x_position, &y_position)){
        LCD.WriteLine("The screen is under pressure");
        LCD.Write("At x coordinate: ");
        LCD.WriteLine(x_position);
        LCD.Write("At y coordinate: ");
        LCD.WriteLine(y_position);
    }
}

Example #2

#include <FEHLCD.h>

int main(void)
{
    float x_position, y_position;
    float x_trash, y_trash;

    while(true)
    {
        /* Clear the touch buffer so touches made before
           this call are discarded */
        LCD.ClearBuffer();
        /* Wait until the user touches the screen */
        while(!LCD.Touch(&x_position,&y_position)) {};
        /* Wait until the touch releases */
        while(LCD.Touch(&x_trash,&y_trash)) {};

        /* Print proper x and y coordinates to screen */
        LCD.WriteLine("The screen was under pressure");
        LCD.Write("At x coordinate: ");
        LCD.WriteLine(x_position);
        LCD.Write("At y coordinate: ");
        LCD.WriteLine(y_position);

        /* Sleep for 3 seconds
         * This is when the user might touch the screen
         * but we don't want these touches to affect
         * what we print to the screen. */
        Sleep(3.0);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *