FT6336/FT6336U I2C capacitive touch controller driver for MicroPython — MicroPython 异步驱动
实验性异步版本:尚未完成实物验收。
同步来源:同步版本。维护者说明同步版本已验证;本次未逐包复测同步来源,适用硬件和固件范围以原有记录为准。
异步版本:本包 Python 源码及示例已通过 MicroPython 1.23 / 1.29 编译检查;软件回归只覆盖选定路径,不代表本包全部 API、实际器件及并发行为已验证。
目录
- 简介
- 主要功能
- 硬件要求
- 软件环境
- 文件结构
- 文件说明
- 快速开始
- 异步设计与使用
- API Async Matrix
- Source Demo to Async Demo Mapping
- 超时、取消、并发与残余阻塞
- Hardware Acceptance
- 注意事项
- 版本记录
- 联系方式
- 许可协议
简介
ft6336_async_driver 是当前仓库中独立发布的 MicroPython 异步包。设备/功能名称和 API 均来自包内运行时文件;它不是对同步 API 的无依据重命名。源包:input/ft6336_driver。
主要功能
- 可解析的公开类:
FT6336,FT6336Async,SyncAdapterMixin。 - 可解析的公开调用入口:32 个(详见 API 矩阵)。
- 异步等级:
event_bridge;依据:IRQ/事件通知由协程消费;总线访问仍可能同步。
硬件要求
| 项目 | 从代码取得的事实 |
|---|---|
| 通信/IO | I2C, Pin( |
| 示例 GPIO | 示例未声明固定 GPIO 编号 |
| 供电、地址与电平 | 仅按模块实物标签、数据手册及代码常量配置;。 |
软件环境
| 项目 | 值 |
|---|---|
| 运行时 | MicroPython + uasyncio |
| 包版本 | 0.1.0 |
| 作者 | FreakStudio |
| 依赖 | 无声明的第三方依赖 |
文件结构
├── code/_gs_ft6336_ft6336.py
├── code/ft6336_async.py
├── code/main.py
├── examples/main_sync.py
├── LICENSE
└── package.json
文件说明
code/:实际发布的运行时 Python 模块;以package.json.urls为安装映射依据。code/main.py(若存在):异步演示入口,不是默认发布运行时映射。examples/(若存在):同步保真基线或额外示例,不应当作安装后的运行时模块。package.json、LICENSE:发布元数据和许可证的唯一事实来源。
快速开始
- 按
package.json.urls将运行时文件复制到设备;不要把examples/当作运行时依赖。 - 按上文硬件/运行环境配置总线、GPIO、网络或凭证。
- 使用包内真实
code/main.py的 import 与构造方式运行。以下是当前文件逐字读取的异步入口;部署时请保留try/finally并调用对象实际提供的deinit()或aclose()。
try:
import uasyncio as asyncio
except ImportError:
import asyncio
async def main():
# Python env : MicroPython v1.23.0
# -*- coding: utf-8 -*-
# @Time : 2026/07/23
# @Author : FreakStudio
# @File : main.py
# @Description : 测试FT6336触摸驱动类
# @License : Apache-2.0
from machine import I2C, Pin
# ======================================== 导入相关模块 =========================================
# 导入驱动类
from _gs_ft6336_ft6336 import FT6336
# ======================================== 全局变量 ============================================
# 引脚配置占位符(请替换为实际硬件连接)
SCL_PIN = 22
SDA_PIN = 21
I2C_FREQ = 400000
RST_PIN = 25
IRQ_PIN = 26
# I2C 设备信息
FT6336_ADDR = 0x38
FT6336_CHIP_ID = 0x64
# 打印间隔(ms)
print_interval = 500
last_print_time = 0
# ======================================== 功能函数 ============================================
# ======================================== 自定义类 ============================================
# ======================================== 初始化配置 ==========================================
# 上电等待
await asyncio.sleep(3)
print("FreakStudio: Testing FT6336/FT6336U touch driver")
# 配置中断引脚
irq_pin_obj = Pin(IRQ_PIN, Pin.IN, Pin.PULL_UP)
# 初始化 I2C
i2c = I2C(0, scl=Pin(SCL_PIN), sda=Pin(SDA_PIN), freq=I2C_FREQ)
print("I2C initialized: SCL=%d, SDA=%d, freq=%d" % (SCL_PIN, SDA_PIN, I2C_FREQ))
# I2C 设备扫描
devices = i2c.scan()
if len(devices) == 0:
raise RuntimeError("No I2C device found on bus")
print("I2C scan result: %s" % str([hex(d) for d in devices]))
# 查找目标设备地址
if FT6336_ADDR not in devices:
raise RuntimeError("Device not found at expected address 0x%02X" % FT6336_ADDR)
# 芯片 ID 验证
try:
# 直接读取验证(不依赖驱动类的verify参数)
chip_id = i2c.readfrom_mem(FT6336_ADDR, 0xA3, 1)[0]
if chip_id == FT6336_CHIP_ID:
print("Device found at 0x%02X, chip ID: 0x%02X (expected)" % (FT6336_ADDR, chip_id))
else:
print("Device found at 0x%02X, chip ID: 0x%02X (unexpected, expected 0x%02X)" % (FT6336_ADDR, chip_id, FT6336_CHIP_ID))
except OSError as e:
raise RuntimeError("Failed to read chip ID: %s" % e)
# 初始化触摸驱动(启用芯片ID验证)
touch = FT6336(
i2c=i2c,
address=FT6336_ADDR,
reset_pin=Pin(RST_PIN, Pin.OUT),
irq_pin=irq_pin_obj,
gesture=True,
verify=True,
debug=False,
)
print("FT6336 driver initialized")
# ======================================== 主程序 ===========================================
try:
while True:
current_time = time.ticks_ms()
# 读取触摸点(低频自动执行)
point = touch.read_point()
if point is not None:
print("Touch: x=%d, y=%d, points=%d" % (point["x"], point["y"], point["points"]))
else:
# 触摸状态空闲
pass
# 定时打印触摸点数和手势(低频自动执行)
if time.ticks_diff(current_time, last_print_time) >= print_interval:
# 读取触摸点数
try:
count = touch.get_touch_count()
if count > 0:
print("Touch count: %d" % count)
except RuntimeError as e:
print("Failed to read touch count: %s" % e)
# 读取手势代码
try:
gesture = touch.get_gesture()
if gesture != 0:
gesture_names = {0: "None", 1: "Swipe Up", 2: "Swipe Down", 3: "Swipe Left", 4: "Swipe Right"}
name = gesture_names.get(gesture, "Unknown(%d)" % gesture)
print("Gesture: %s" % name)
except RuntimeError as e:
print("Failed to read gesture: %s" % e)
last_print_time = current_time
await asyncio.sleep_ms(10)
except KeyboardInterrupt:
print("Program interrupted by user")
except OSError as e:
print("Hardware communication error: %s" % e)
except Exception as e:
print("Unknown error: %s" % e)
finally:
print("Cleaning up resources...")
# 释放触摸驱动资源
touch.deinit()
del touch
print("Program exited")
if __name__ == '__main__':
asyncio.run(main())
异步设计与使用
源状态:sync_source。本包归属当前异步仓库;原始实现的作者和许可证以源包元数据及 LICENSE 为准。协程入口应以 asyncio.run(main()) 启动。若类提供 start()/stop()、deinit() 或 aclose(),必须按其实际签名管理后台任务和资源,且在 finally 中清理。
API Async Matrix
| API | Level | Async strategy | Residual blocking | Timeout/cancellation |
|---|---|---|---|---|
FT6336.deinit |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336.enable_gesture |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336.get_gesture |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336.get_touch_count |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336.read_chip_id |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336.read_point |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336.reset |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336Async.deinit_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336Async.enable_gesture_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336Async.get_gesture_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336Async.get_touch_count_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336Async.read_chip_id_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336Async.read_point_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
FT6336Async.reset_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
SyncAdapterMixin.aclose |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
aclose |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
const |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
deinit |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
deinit_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
enable_gesture |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
enable_gesture_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
get_gesture |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
get_gesture_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
get_touch_count |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
get_touch_count_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
main |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
read_chip_id |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
read_chip_id_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
read_point |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
read_point_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
reset |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
reset_async |
event_bridge |
IRQ/事件通知由协程消费;总线访问仍可能同步 | 底层总线/同步临界区可能阻塞事件循环 | 请使用实现中已有 timeout 参数;取消后执行清理。 |
Source Demo to Async Demo Mapping
| Source sync step | Source API/protocol action | Async implementation | Preserved output/error meaning | Residual blocking |
|---|---|---|---|---|
| 源包示例 | code/main.py |
code/main.py 或本文快速开始 |
保留实际构造与调用;异常语义以运行时代码为准 | 存在 |
超时、取消、并发与残余阻塞
任何 await 不会自动使底层 I2C/SPI/UART/Pin 操作变为非阻塞。取消时应停止后台任务并在 finally 释放资源;没有实现超时参数的 API 不应被假定具有超时。协议命令、帧界限、ACK、静默间隔及错误含义只以当前实现和源示例可证明的内容为准。
Hardware Acceptance
| Level | Evidence | Not covered |
|---|---|---|
| Link | 未实测:仅完成静态代码与文档取证。 | 总线/网络连接、设备身份响应。 |
| Basic function | 未实测。 | 一次真实业务 API 成功及结果正确性。 |
| Complete business flow | 未实测。 | 端到端设备、对端、供电、时序和异常恢复。 |
注意事项
| 类别 | 说明 |
|---|---|
| 并发 | 同一硬件实例应由调用方串行化;UART 额外遵守其并发契约。 |
| 阻塞 | 不把短同步总线访问或严格时序脉冲描述为真正可中断。 |
| 验收 | 文档生成不等于硬件验证;接入真实设备后分别记录三层验收证据。 |
版本记录
| 版本号 | 日期 | 作者 | 修改说明 |
|---|---|---|---|
0.1.0 |
2026-09-17 | FreakStudio | 基于当前异步代码、来源包和许可证重新取证 README。 |
联系方式
请通过本仓库的 issue 或维护者渠道联系;包内元数据作者为:FreakStudio。
许可协议
许可证声明:Apache-2.0。以下为本包 LICENSE 的逐字内容:
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2026 Waveshare
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.