Chain ToF
ToFChain 是用于 Chain 总线上的 ToF(飞行时间)传感器设备的辅助类。它提供了读取距离测量值、配置测量参数(时间、模式、状态)以及检查测量完成状态的方法。
支持以下产品:
UiFlow2 示例
距离测量显示
在 UiFlow2 中打开 m5core_chain_tof_basic_example.m5f2 项目。
本示例演示如何从 Chain ToF 传感器读取距离测量值并在屏幕上显示。示例将传感器配置为连续测量模式,并实时更新距离值。
UiFlow2 代码块:
示例输出:
None
MicroPython 示例
距离测量显示
本示例演示如何从 Chain ToF 传感器读取距离测量值并在屏幕上显示。示例将传感器配置为连续测量模式,并实时更新距离值。
MicroPython 代码块:
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from chain import ChainBus 9from chain import ToFChain 10import time 11 12 13title0 = None 14label_dis = None 15bus2 = None 16chain_tof_0 = None 17last_time = None 18distance = None 19 20 21def setup(): 22 global title0, label_dis, bus2, chain_tof_0, last_time, distance 23 24 M5.begin() 25 Widgets.setRotation(0) 26 Widgets.fillScreen(0x222222) 27 title0 = Widgets.Title("Chain ToF Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 28 label_dis = Widgets.Label( 29 "Distance: --", 20, 92, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 30 ) 31 32 bus2 = ChainBus(2, tx=21, rx=22) 33 chain_tof_0 = ToFChain(bus2, 1) 34 chain_tof_0.set_rgb_color(0x33CCFF) 35 chain_tof_0.set_rgb_brightness(10, save=False) 36 chain_tof_0.set_measure_mode(ToFChain.MODE_CONTINUOUS) 37 38 39def loop(): 40 global title0, label_dis, bus2, chain_tof_0, last_time, distance 41 M5.update() 42 if (time.ticks_diff((time.ticks_ms()), last_time)) >= 200: 43 last_time = time.ticks_ms() 44 distance = chain_tof_0.get_distance() 45 label_dis.setText(str((str("Distance: ") + str((str(distance) + str(" mm")))))) 46 47 48if __name__ == "__main__": 49 try: 50 setup() 51 while True: 52 loop() 53 except (Exception, KeyboardInterrupt) as e: 54 try: 55 bus2.deinit() 56 from utility import print_error_msg 57 58 print_error_msg(e) 59 except ImportError: 60 print("please update to latest firmware")
示例输出:
None
API参考
ToFChain
- class chain.tof.ToFChain(bus, device_id)
基类:
KeyChain用于通过 Chain 总线与 ToF(飞行时间)设备交互的 ToF Chain 类。
UiFlow2 代码块:

MicroPython 代码块:
from chain import ChainBus from chain import ToFChain bus2 = ChainBus(2, tx=21, rx=22) chain_tof_0 = ToFChain(bus2, 1)
- get_distance()
获取距离测量值。
- 返回:
距离(单位:毫米),如果失败则返回 None。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
distance = chain_tof_0.get_distance()
- set_measure_time(time_ms=33)
设置测量时间。
UiFlow2 代码块:

MicroPython 代码块:
success = chain_tof_0.set_measure_time(33)
- get_measure_time()
获取测量时间。
- 返回:
测量时间(单位:毫秒),如果失败则返回 None。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
time = chain_tof_0.get_measure_time()
- set_measure_mode(mode=2)
设置测量模式。
- 参数:
mode (int) – 测量模式。使用
ToFChain.MODE_STOP(0)、ToFChain.MODE_SINGLE(1) 或ToFChain.MODE_CONTINUOUS(2)。默认值:2。- 返回:
如果操作成功则返回 True,否则返回 False。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
success = chain_tof_0.set_measure_mode(ToFChain.MODE_CONTINUOUS)
- get_measure_mode()
获取测量模式。
- 返回:
测量模式。
ToFChain.MODE_STOP(0)、ToFChain.MODE_SINGLE(1) 或ToFChain.MODE_CONTINUOUS(2)。如果失败则返回 None。- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
mode = chain_tof_0.get_measure_mode()
- set_measure_status(status)
设置测量状态。
UiFlow2 代码块:

MicroPython 代码块:
success = chain_tof_0.set_measure_status(1)
- get_measure_status()
获取测量状态。
- 返回:
测量状态。0 表示未测量,1 表示正在测量。如果失败则返回 None。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
status = chain_tof_0.get_measure_status()


