ToF4M Unit

这是 ToF4M Unit 的驱动库,用于从 VL53L1CXV0FY 传感器获取距离数据。

支持以下产品:

ToF4M

UiFlow2 应用示例

获取距离值

在 UiFlow2 中打开 tof4m_core_example.m5f2 项目。

该示例获取 ToF4M Unit 的距离值,并将其显示在屏幕上。

UiFlow2 代码块:

example.png

示例输出:

None

MicroPython 应用示例

获取距离值

该示例获取 ToF4M Unit 的距离值,并将其显示在屏幕上。

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 hardware import I2C
 9from hardware import Pin
10from unit import TOF4MUnit
11
12
13title0 = None
14label1 = None
15i2c1 = None
16tof4m_0 = None
17
18
19distance = None
20
21
22def setup():
23    global title0, label1, i2c1, tof4m_0, distance
24
25    M5.begin()
26    Widgets.fillScreen(0x222222)
27    title0 = Widgets.Title("ToF4MUnit CoreS3 Test", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
28    label1 = Widgets.Label("label1", 1, 121, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29
30    i2c1 = I2C(1, scl=Pin(22), sda=Pin(21), freq=100000)
31    tof4m_0 = TOF4MUnit(i2c1, 0x29)
32    tof4m_0.set_distance_mode(2)
33    tof4m_0.set_measurement_timing_budget(500)
34    tof4m_0.set_continuous_start_measurement()
35
36
37def loop():
38    global title0, label1, i2c1, tof4m_0, distance
39    M5.update()
40    if tof4m_0.get_data_ready:
41        label1.setText(str((str("Distance:") + str((str(distance) + str("mm"))))))
42
43
44if __name__ == "__main__":
45    try:
46        setup()
47        while True:
48            loop()
49    except (Exception, KeyboardInterrupt) as e:
50        try:
51            from utility import print_error_msg
52
53            print_error_msg(e)
54        except ImportError:
55            print("please update to latest firmware")

示例输出:

None

API参考

TOF4MUnit

unit.tof4m.TOF4MUnit

VL53L1X 的别名

VL53L1CXV0FY

class driver.vl53l1x.VL53L1X(i2c, address=41)

基类:object

创建一个 VL53L1X 对象。

参数:
  • i2c (I2C) – ToF4M Unit 连接的 I2C 总线。

  • address (int) – 设备的 I2C 地址。默认值为 0x29。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from hardware import I2C
from unit import TOFUnit

i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
tof_0 = TOFUnit(i2c0)
property get_distance

以毫米为单位的距离。

返回:

以毫米为单位的距离;如果测量无效则为 None。

返回类型:

int or None

UiFlow2 代码块:

get_distance.png

MicroPython 代码块:

distance = tof_0.get_distance
set_continuous_start_measurement()

启动连续测量操作。

UiFlow2 代码块:

set_continuous_start_measurement.png

MicroPython 代码块:

tof_0.set_continuous_start_measurement()
set_continuous_stop_measurement()

停止测量操作。

UiFlow2 代码块:

set_continuous_stop_measurement.png

MicroPython 代码块:

tof_0.set_continuous_stop_measurement()
property get_data_ready

Returns true if new data is ready, otherwise false.

返回:

如果新数据已就绪,则为 True。

返回类型:

bool

UiFlow2 代码块:

get_data_ready.png

MicroPython 代码块:

if tof_0.get_data_ready:
    distance = tof_0.get_distance
property get_measurement_timing_budget

获取以毫秒为单位的测量持续时间。

返回:

以毫秒为单位的定时预算。

返回类型:

int

UiFlow2 代码块:

get_measurement_timing_budget.png

MicroPython 代码块:

budget = tof_0.get_measurement_timing_budget
set_measurement_timing_budget(val)

以毫秒为单位设置测量定时预算。

参数:

val (int) – 以毫秒为单位的时间预算。

UiFlow2 代码块:

set_measurement_timing_budget.png

MicroPython 代码块:

tof_0.set_measurement_timing_budget(100)
property get_distance_mode

获取距离模式。

返回:

距离模式(1=短距离,2=长距离)。

返回类型:

int

UiFlow2 代码块:

get_distance_mode.png

MicroPython 代码块:

mode = tof_0.get_distance_mode
set_distance_mode(mode)

设置距离模式。

参数:

mode (int) – 1=短,2=长

UiFlow2 代码块:

set_distance_mode.png

MicroPython 代码块:

tof_0.set_distance_mode(2)  # Long distance mode
set_i2c_address(new_address)

为已实例化的对象设置新的 I2C 地址。

参数:

new_address (int) – 新的 I2C 地址。

UiFlow2 代码块:

set_i2c_address.png

MicroPython 代码块:

tof_0.set_i2c_address(42)