Welcome to K210 Module repository
6.The touchpad read the coordinates of the experiment
6 the touch panel to read the coordinates of the experiment
6 the touch panel to read the coordinates of the experiment 6.1 the experimental goals 6.2 experimental procedure 6.3 experimental results 6.4 the experiments are summarized
6.1 the experimental goals
This lesson is mainly for learning microPython reads the touchpad status and data functions.
The present experiments the reference code path is: CanMV\03-Hardware\touchscreen.py
6.2 experimental procedure
Module factory firmware has been integrated touchpad driver module, if you download the other firmware, please burn back to the factory firmware and then perform the experiment.
Import touchscreen, lcd, image, and time-related library.
import touchscreen as tsimport lcd, imageimport time
Initialize the LCD display and touchpad.
xxxxxxxxxxlcd.init()ts.init()
Through the image library to create a new blank image and in the image directly above is written“Please touch the screen”prompts the user to touch the screen.
xxxxxxxxxximg = image.Image()img.draw_string(100, 0, "Please touch the screen", color=(0, 0, 255), scale=1)
Wherein, image.draw_string(x, y, text[, color[, scale=1[, x_spacing=0[, y_spacing=0[, mono_space=True]]]]])Represents the add string parameter explanation:
x, y: Indicates a string starting point coordinates;
text: Represents the display string content;
color: Indicates color RGB value;
scale: Indicates a string size;
x_spacing: Indicates the spacing between words, allowing the characters to add if it is positive, or subtracting, if it is a negative number x pixels;
y_spacing: Indicates the line spacing, allowing the characters to add if it is positive, or subtracting, if it is a negative number)y-pixels;
mono_space: force text spacing is fixed, the default is True. For large text can be set to False to obtain a non-fixed-width character spacing, there will be a better display effect.
Create associated saved variables.
xxxxxxxxxxstatus_last = ts.STATUS_IDLEx_last = 0y_last = 0
Create a while loop, handling the Touch event, each read to a different state, it will be a touch panel of state and coordinates print it out, if it is one-touch continuous sliding, then the pixels are connected, can be custom drawn graphics.
xxxxxxxxxxwhile True:(status, x, y) = ts.read()if status_last != status:print(status, x, y)status_last = statusif status == ts.STATUS_MOVE:img.draw_line(x_last, y_last, x, y)elif status == ts.STATUS_PRESS:img.draw_line(x, y, x, y)lcd.display(img)x_last = xy_last = y
Wherein, (status, x, y) = ts.read()A read of the current screen State and a touch of the coordinates of the point value; the returned value is a 3 integer value consisting of tuples, status: Status, values have STATUS_RELEASE=1, the STATUS_PRESS=2, the STATUS_MOVE=3, the x: Touch the X-axis coordinate, y: Touch point of the Y-axis coordinates.
img.draw_line(x0, y0, x1, y1[, color[, thickness=1]])Expressed in the img the image to draw a line, x0,y0Represents the starting point coordinates, x1,y1Indicates the end point coordinates, colorRepresent the color, default is white, thicknessIndicates the line the thickness of the Pixel;
lcd.display(img)A LCD display img image.
6.3 experimental results
将K210模块通过microUSB数据线连接到电脑上,CanMV IDE点击连接按钮,连接完成后点击运行按钮,运行例程代码。也可以将代码作为main.py下载到K210模块上运行。
You can see the screen just above the blue string“Please touch the screen”, then the touch screen, do not let go of the situation slipped a finger, you can see the screen is drawn on a sliding track, loosen the hand is no longer draw, touch again to draw another sliding track.


Open the bottom of the IDE serial terminal, you can each time the touch screen state change will print the state value and the current XY coordinate values.

6.4 the experiments are summarized
Use CanMV IDE, with the factory firmware write a good MicroPython syntax, in draw graphics when you need to create a new blank img image, for storing the increase of Pixel point. The capacitive touch screen is similar to the phone screen, Please Touch by hand, do not use nails.