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 13 14title0 = None 15label_dis = None 16bus2 = None 17chain_tof_0 = None 18last_time = None 19distance = None 20 21 22def setup(): 23 global title0, label_dis, bus2, chain_tof_0, last_time, distance 24 25 M5.begin() 26 Widgets.setRotation(1) 27 Widgets.fillScreen(0x222222) 28 title0 = Widgets.Title("Chain ToF Example", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu24) 29 label_dis = Widgets.Label("Distance: --", 20, 92, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu24) 30 31 bus2 = ChainBus(2, tx=21, rx=22) 32 chain_tof_0 = ToFChain(bus2, 1) 33 chain_tof_0.set_rgb_color(0x33ccff) 34 chain_tof_0.set_rgb_brightness(10, save=False) 35 chain_tof_0.set_measure_mode(ToFChain.MODE_CONTINUOUS) 36 37 38def loop(): 39 global title0, label_dis, bus2, chain_tof_0, last_time, distance 40 M5.update() 41 if (time.ticks_diff((time.ticks_ms()), last_time)) >= 200: 42 last_time = time.ticks_ms() 43 distance = chain_tof_0.get_distance() 44 label_dis.setText(str((str('Distance: ') + str(((str(distance) + str(' mm'))))))) 45 46 47if __name__ == '__main__': 48 try: 49 setup() 50 while True: 51 loop() 52 except (Exception, KeyboardInterrupt) as e: 53 try: 54 bus2.deinit() 55 from utility import print_error_msg 56 print_error_msg(e) 57 except ImportError: 58 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()


