Touch Equipment
Last updated
Last updated
Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University
Touch (touch chip) is an important part of human-computer interaction in UI design. A complete UI design should include input information and output information. Screen devices such as LCD are responsible for display output, while Touch devices are responsible for collecting contact information as information input.
Touch devices and hosts generally use the I2C bus protocol to exchange data, so a touch device is a standard I2C slave device. In order to improve the real-time performance of receiving touch data, touch chips will provide interrupt support. When a touch event (lift, press, move) occurs, an interrupt will be triggered to notify the MCU of a touch event. The host can read the touch point information through the interrupt callback function.
The communication connection between Touch device and host is shown in the figure below:
In order to facilitate the use of Touch devices, RT-Thread abstracts the Touch device driver framework and provides a unified operation interface to the upper layer to improve the reusability of the upper layer code.
Interface: standard device interface (open/close/read/control).
Working mode: Supports interrupt and polling modes.
Support reading multiple points of data
Click the Touch list to view the currently supported Touch types.
The application accesses the Touch device through the I/O device management interface provided by RT-Thread. The relevant interface is as follows:
function | describe |
rt_device_find() | Find the device and get the device handle according to the Touch device name |
rt_device_open() | Open the Touch device |
rt_device_read() | Read contact data |
rt_device_control() | Controlling Touch Devices |
rt_device_set_rx_indicate() | Set the receiving callback function |
rt_device_close() | Turn off your Touch device |
The application obtains the device handle according to the Touch device name, and then can operate the Touch device. The device search function is as follows:
parameter | describe |
name | Touch device name |
return | —— |
Device handle | If the corresponding device is found, the corresponding device handle will be returned. |
RT_NULL | No corresponding device object was found |
The usage examples are as follows:
Through the device handle, the application can open and close the device, and open the device through the following function:
parameter | describe |
dev | Device handle |
oflags | Device mode flags |
return | —— |
RT_EOK | Device opened successfully |
- RT_EBUSY | If the device registration parameter includes the RT_DEVICE_FLAG_STANDALONE parameter, the device will not be allowed to be opened repeatedly. |
-RT_EINVAL | Unsupported open parameters |
Other error codes | Device open failed |
The oflags parameter supports the following parameters:
There are two modes for receiving and sending data on the Touch device: interrupt mode and polling mode. When using, only one of these two modes can be selected . If the Touch opening parameter oflags does not specify the interrupt mode, the polling mode is used by default.
An example of using a Touch device in polling mode is as follows:
The following is an example of opening a Touch device in interrupt mode:
Through the command control word, the application can configure the Touch device through the following functions:
parameter | describe |
dev | Device handle |
cmd | Command control word, see below for details |
arg | Control parameters, detailed description see below |
return | —— |
RT_EOK | Function execution successful |
-RT_ENOSYS | Execution failed, dev is empty |
Other error codes | Execution failed |
The cmd currently supports the following command control characters:
Set the resolution of Touch X axis coordinates.
Set the resolution of the Touch Y axis coordinates.
Swap the X and Y axis coordinates
Turn off Touch Turn off interruption
Enable Touch device interrupts
When using the interrupt mode to read the contact data, an interrupt will be triggered when a touch event occurs at the bottom layer. Since the speed of the interrupt trigger is greater than the speed of the Touch device reading (I2C reading data is generally slow), the interrupt needs to be turned off in the receiving callback function, and then the semaphore is released. In the contact information reading thread, the data will be read after the semaphore is requested, and the interrupt will be turned on after the data reading is completed. Note that the interrupt interface must be turned on and off in pairs. Turning on an interrupt once corresponds to turning off an interrupt once, so that the device can read data in normal interrupt mode.
The following function can be used to set the data receiving indication. When Touch receives data, it notifies the upper application thread that data has arrived:
parameter | describe |
dev | Device handle |
rx_ind | Callback function pointer |
dev | Device handle (callback function parameter) |
size | Buffer data size (callback function parameter) |
return | —— |
RT_EOK | Set up for success |
The callback function of this function is provided by the caller. If the Touch device is opened in interrupt receiving mode, when the Touch receives data and generates an interrupt, the callback function will be called and the Touch device handle will be placed in the dev parameter for the caller to obtain.
Contact information composition
event: touch event, including lift event, press event, and move event.
track_id: Each touch point has its own touch track. This data is used to save the touch track ID.
width: touch point width.
x_coordinate: X-axis coordinate of the touch point.
y_coordinate: Y-axis coordinate of the touch point.
timestamp: Touch event timestamp.
Read touch point information interface
The following interface can be called to read the touch point information:
parameter | describe |
dev | Device handle |
pos | Read data offset, this parameter is not used by Touch |
buffer | Buffer pointer. The read data will be saved in the buffer. |
size | The Touch device driver framework determines the number of touch points to be read based on this parameter. |
return | —— |
The actual size of the data read | Returns the number of contact information read |
0 | You need to read the errno of the current thread to determine the error status |
The code snippet for reading a touch point information is as follows:
The code snippet for reading the five touch point information is as follows:
When the application completes the Touch operation, it can turn off the Touch device, which can be done through the following function:
parameter | describe |
dev | Device handle |
return | —— |
RT_EOK | Shut down the device successfully |
-RT_ERROR | The device has been completely shut down. You cannot shut down the device again. |
Other error codes | Failed to shut down the device |
Closing the device interface and opening the device interface must be used in pairs. Each time you open the device, you must close it once. Only in this way can the device be completely closed. Otherwise, the device will still be in an open state.
The specific usage of the Touch device interrupt method to read the touch information of five points can be referred to the following sample code. The main steps of the sample code are as follows:
First look up the Touch settings to get the device handle.
Open the Touch device in a way that interrupts receiving data.
Set the interrupt receive callback.
Create a thread and semaphore for reading Touch data, release the semaphore in the interrupt reception callback, keep requesting the semaphore in the data reception thread, and read the Touch data once the semaphore is requested.