K230 Touch Screen (TOUCH) Usage Tutorial#
Overview#
K230 supports touch input for both capacitive screens and resistive screens, providing a TOUCH class interface based on the RT-Smart package. Developers can use it to read touch point coordinates, event types, and other information, making it widely applicable in scenarios such as HMI and interactive terminals.
Quick Start Example#
The following example demonstrates how to read touch point data from the touch screen:
from machine import TOUCH
# Instantiate touch device 0 (system default touch)
tp = TOUCH(0)
# Read current touch point data (returns a tuple, each element is a TOUCH_INFO object)
p = tp.read()
print(p)
# If there are touch points, you can access the coordinates and event type of the first touch point
# print(p[0].x) # X coordinate
# print(p[0].y) # Y coordinate
# print(p[0].event) # Event type (e.g., EVENT_DOWN, EVENT_MOVE, EVENT_UP)
Interface Description#
Interface Name |
Description |
|---|---|
|
Creates a touch screen instance, |
|
Returns current touch point information, returns a tuple where each element is a TOUCH_INFO object, |
|
Releases touch resources |
Constructor Parameter Description#
dev: Touch device number, 0 for system default, non-zero for custom device.rotate: Coordinate rotation, see Constants, default -1 (use device default).range_x,range_y: Valid only for custom devices, specifies the touch screen resolution.i2c: Valid only for custom devices, I2C bus object, required.rst,int: Valid only for custom devices, reset/interrupt pin objects, optional.Other parameters are deprecated.
TOUCH_INFO Object Properties#
event: Event code, see Constantstrack_id: Touch point IDwidth: Touch point widthx: X coordinatey: Y coordinatetimestamp: Timestamp
Detailed Examples#
Instantiating the Device#
# System default touch
tp = TOUCH(0)
# Custom touch device (need to specify i2c, resolution, etc.)
# tp = TOUCH(1, i2c=my_i2c, range_x=800, range_y=480)
Reading Touch Data#
p = tp.read()
print(p)
The returned p is a tuple, where each element is a TOUCH_INFO object. Each object contains:
x: X coordinatey: Y coordinateevent: Touch event (e.g., EVENT_DOWN, EVENT_MOVE, EVENT_UP)track_id,width,timestamp, etc.
Example Output Reference#
(<TOUCH_INFO x=120 y=65 event=0 ...>,)
If there are multiple touch points, e.g., supporting multi-finger operation:
(<TOUCH_INFO x=120 y=65 event=0 ...>, <TOUCH_INFO x=250 y=130 event=0 ...>)
Constants#
Touch Events#
TOUCH.EVENT_NONE: No eventTOUCH.EVENT_UP: Lift upTOUCH.EVENT_DOWN: Press downTOUCH.EVENT_MOVE: Move
Coordinate Rotation#
TOUCH.ROTATE_0: No rotationTOUCH.ROTATE_90: Rotate 90 degreesTOUCH.ROTATE_180: Rotate 180 degreesTOUCH.ROTATE_270: Rotate 270 degreesTOUCH.ROTATE_SWAP_XY: XY swap
Touch Types (reserved for compatibility, actual values are all 0)#
TOUCH.TYPE_CST226SETOUCH.TYPE_CST328TOUCH.TYPE_GT911
Application Scenarios#
Scenario |
Example |
|---|---|
GUI Interaction |
Button clicks and slide toggles driven by touch points |
Custom Gesture Recognition |
Multi-point sliding, rotation and other gesture recognition |
Simple Drawing Board or Signature Pad |
Line drawing based on touch point trajectories |
Frequently Asked Questions#
Q: No touch points read, and the returned value is an empty tuple?
A: This means the screen is not being touched. tp.read() only returns touch points when the screen is pressed.
Q: How to handle multi-touch?
A: The read result is a tuple containing multiple TOUCH_INFO entries. Use a for loop to iterate through them.
Q: Does it support coordinate rotation?
A: Yes, coordinate rotation can be enabled through the rotate parameter (such as TOUCH.ROTATE_90).
