视觉识别模块
0、学前先看&供电说明
1、固件下载与烧录
2、安装编程环境
3、调试运行例程
4、离线运行例程
5、Micropython 快速入门
6、GUI程序使用说明
7、二维云台安装教程
8、FPIOA引脚分配
9、RGB灯
10、蜂鸣器
11、按键
12、串口通信
13、I2C通讯
14、PWM
15、WDT看门狗
16、定时器计时
17、傅里叶变换
18、SHA256加密
19、AES加密
20、多线程
21、文件读写
22、图像显示
23、触摸显示
24、摄像头显示
25、图像翻转
26、照相机
27、绘制圆形
28、绘制矩形
29、绘制椭圆
30、绘制箭头
31、绘制十字准心
32、绘制文字
汝城县职业中等专业学校知识库-信息中心朱老师编辑
-
+
首页
23、触摸显示
 ## 例程实验效果简介 > 本节我们演示K230触摸屏的使用,需要选配屏幕 例程代码位于 【源码汇总 / 02.Basic / 16.touch.py】 我们用CanMV IDE打开例程代码,将K230用USB连接到电脑上 点击左下角运行按钮即可开始运行程序 初始界面是白色的 我们用手指在屏幕上移动,可以看到随着手指的移动,屏幕上绘制出黑色的线条 ## 例程代码 ### 完整代码 \# Import required modules \# 导入所需的模块 import time, os, urandom, sys \# Import display and media related modules \# 导入显示和媒体相关模块 from media.display import \* from media.media import \* \# Import touch sensor module \# 导入触摸传感器模块 from machine import TOUCH \# Initialize touch sensor on pin 0 \# 在引脚0上初始化触摸传感器 tp = TOUCH(0) \# Define display resolution constants \# 定义显示分辨率常量 DISPLAY\_WIDTH = 640 DISPLAY\_HEIGHT = 480 def display\_test(): """ Function to test display and touch functionality 测试显示和触摸功能的函数 """ print("display and touch test") \# Create main background image with white color \# 创建白色背景的主图像 img = image.Image(DISPLAY\_WIDTH, DISPLAY\_HEIGHT, image.ARGB8888) img.clear() img.draw\_rectangle(0, 0, DISPLAY\_WIDTH, DISPLAY\_HEIGHT,color\=(255,255,255),fill\=True) \# Create secondary image for drawing \# 创建用于绘画的次要图像 img2 = image.Image(DISPLAY\_WIDTH, DISPLAY\_HEIGHT, image.ARGB8888) img2.clear() \# Initialize display with ST7701 driver \# 使用ST7701驱动初始化显示器 Display.init(Display.ST7701, width = DISPLAY\_WIDTH, height = DISPLAY\_HEIGHT, to\_ide = True) \# Initialize media manager \# 初始化媒体管理器 MediaManager.init() try: \# Variables to store previous touch coordinates \# 存储上一次触摸坐标的变量 last\_x = None last\_y = None while True: os.exitpoint() \# Read touch point data \# 读取触摸点数据 point = tp.read(1) if len(point): print(point) pt = point\[0\] \# Handle touch events (down or move) \# 处理触摸事件(按下或移动) if pt.event == 0 or pt.event == TOUCH.EVENT\_DOWN or pt.event == TOUCH.EVENT\_MOVE: if((last\_x is not None) and (last\_y is not None) and pt.event is not 2): \# Draw line between previous and current touch points \# 在上一个触摸点和当前触摸点之间画线 img2.draw\_line(last\_x,last\_y,pt.x, pt.y, color\=(0,0,0), thickness = 5) Display.show\_image(img2, layer = Display.LAYER\_OSD2, alpha = 128) last\_x = pt.x last\_y = pt.y \# Update display with background image \# 更新显示背景图像 Display.show\_image(img) time.sleep(0.05) except KeyboardInterrupt as e: print("user stop: ", e) except BaseException as e: print(f"Exception {e}") \# Cleanup and deinitialize display \# 清理并反初始化显示器 Display.deinit() os.exitpoint(os.EXITPOINT\_ENABLE\_SLEEP) time.sleep\_ms(100) \# Release media resources \# 释放媒体资源 MediaManager.deinit() if \_\_name\_\_ == "\_\_main\_\_": \# Enable exit points and run display test \# 启用退出点并运行显示测试 os.exitpoint(os.EXITPOINT\_ENABLE) display\_test() ### 代码结构 1. 基础设置: * 导入必要模块 * 初始化触摸设备 * 定义显示常量 * 创建和初始化图像 * 初始化显示器和媒体管理器 2. 主循环中: * 检查是否需要退出 * 读取触摸数据 * 如果有触摸点: * 检查触摸事件类型 * 如果有上一个坐标点,绘制连线 * 更新坐标 * 显示图像 * 短暂休眠 3. 异常处理: * 处理键盘中断 * 处理其他异常 4. 清理工作: * 销毁显示器 * 启用休眠模式 * 释放媒体缓冲区 完整流程图如下:  ## TOUCH 相关文档 > 以下内容选自CanMV K230 TOUCH API > > [2.16 TOUCH 模块 API 手册 — CanMV K230](https://www.kendryte.com/k230_canmv/zh/main/zh/api/machine/K230_CanMV_TOUCH%E6%A8%A1%E5%9D%97API%E6%89%8B%E5%86%8C.html) ### 1\. 概述 触摸模块基于 RTT 的触摸框架,支持单点和多点电容触摸屏及电阻触摸屏。 ### 2\. API 介绍 TOUCH 类位于 `machine` 模块下。 **示例** from machine import TOUCH \# 实例化 TOUCH 设备 0 tp = TOUCH(0) \# 获取 TOUCH 数据 p = tp.read() print(p) \# 打印触摸点坐标 \# print(p\[0\].x) \# print(p\[0\].y) \# print(p\[0\].event) #### 构造函数 \# when index is 0 touch = TOUCH(index, type = TOUCH.TYPE\_CST328, rotate = -1) \# when index is 1 touch = TOUCH(index, type = TOUCH.TYPE\_CST328, rotate = -1, range\_x = -1, range\_y = -1, i2c : I2C = None, slave\_addr = None, rst : Pin = None, int : Pin = None) **参数** * `index`: `TOUCH` 设备号,为 `0` 时,表示使用系统自带的触摸,为 `1` 时,表示使用 `CanMV` 专有的触摸驱动 * `type`: 触摸驱动类型,具体定义参考[触摸类型](https://www.kendryte.com/k230_canmv/zh/main/zh/api/machine/K230_CanMV_TOUCH%E6%A8%A1%E5%9D%97API%E6%89%8B%E5%86%8C.html#id6) * `rotate`: 面板输出坐标与屏幕坐标的旋转,取值范围为 \[0-3\],具体定义参考[坐标旋转](https://www.kendryte.com/k230_canmv/zh/main/zh/api/machine/K230_CanMV_TOUCH%E6%A8%A1%E5%9D%97API%E6%89%8B%E5%86%8C.html#id5)。 * `range_x`: `index=1` 时有效,触摸输出坐标的宽度最大值 * `range_y`: `index=1` 时有效,触摸输出坐标的高度最大值 * `i2c`: `index=1` 时有效,触摸使用 `I2C` 总线对象 * `slave_addr`: `index=1` 时有效,触摸芯片的从机地址,可选参数,不传入使用驱动默认值 * `rst`: `index=1` 时有效,触摸复位引脚对象 * `int`: `index=1` 时有效,触摸中断引脚对象,当前不支持 #### `read` 方法 TOUCH.read(\[count\]) 获取触摸数据。 **参数** * `count`: 最多读取的触摸点数量,取值范围为 \[0:10\],默认为 0,表示读取所有触摸点。 **返回值** 返回触摸点数据,类型为元组 `([tp[, tp...]])`,其中每个 `tp` 是一个 `touch_info` 类实例。 #### `deinit` 方法 TOUCH.deinit() 释放 TOUCH 资源。 **参数** 无 **返回值** 无 ### 3\. TOUCH\_INFO 类 TOUCH\_INFO 类用于存储触摸点的信息,用户可通过相关只读属性访问。 * `event`: 事件码,具体参考[触摸事件](https://www.kendryte.com/k230_canmv/zh/main/zh/api/machine/K230_CanMV_TOUCH%E6%A8%A1%E5%9D%97API%E6%89%8B%E5%86%8C.html#id4)。 * `track_id`: 触点 ID,用于多点触摸。 * `width`: 触点宽度。 * `x`: 触点的 x 坐标。 * `y`: 触点的 y 坐标。 * `timestamp`: 触点时间戳。 ### 4\. 常量 #### 4.1 触摸事件 * `EVENT_NONE`: 无事件。 * `EVENT_UP`: 触摸按下后抬起。 * `EVENT_DOWN`: 触摸按下开始。 * `EVENT_MOVE`: 触摸按下后移动。 #### 4.2 坐标旋转 * `ROTATE_0`: 坐标不旋转。 * `ROTATE_90`: 坐标旋转 90 度。 * `ROTATE_180`: 坐标旋转 180 度。 * `ROTATE_270`: 坐标旋转 270 度。 #### 4.3 触摸类型 * `TYPE_CST328`: `CanMV` 专有触摸驱动 * `TYPE_CST226SE`: `CanMV` 专有触摸驱动 * `TYPE_GT911`: `CanMV` 专有触摸驱动
admin
2025年12月30日 14:22
转发
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
Word文件
PDF文档
PDF文档(打印)
分享
链接
类型
密码
更新密码
有效期
AI