ToF4M Unit

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

支持以下产品:

ToF4M

UiFlow2 应用示例

获取距离值

在 UiFlow2 中打开 tof4m_core_example.m5f2 项目。

此示例获取 ToF4M 单元的距离值并将其显示在屏幕上。

UiFlow2 代码块:

example.png

示例输出:

None

MicroPython 示例

获取距离值

此示例获取 ToF4M 单元的距离值并将其显示在屏幕上。

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_data_ready

如果新数据已准备就绪,则返回 true,否则返回 false。

返回:

如果新数据已准备就绪,则返回 true。

返回类型:

bool

UiFlow2 代码块:

get_data_ready.png

MicroPython 代码块:

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

以毫米为单位的距离。

返回:

以毫米为单位的距离;如果测距无效,则为 “无”。

返回类型:

int or None

UiFlow2 代码块:

get_distance.png

MicroPython 代码块:

distance = tof_0.get_distance
property get_distance_mode

获取距离模式。

返回:

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

返回类型:

int

UiFlow2 代码块:

get_distance_mode.png

MicroPython 代码块:

mode = tof_0.get_distance_mode
property get_measurement_timing_budget

获取测距持续时间(以毫秒为单位)。

返回:

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

返回类型:

int

UiFlow2 代码块:

get_measurement_timing_budget.png

MicroPython 代码块:

budget = tof_0.get_measurement_timing_budget
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()
set_distance_mode(mode)

Set the distance mode.

参数:

mode (int) – 1=short, 2=long.

UiFlow2 代码块:

set_distance_mode.png

MicroPython 代码块:

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

Set a new I2C address to the instantiated object.

参数:

new_address (int) – The new I2C address.

UiFlow2 代码块:

set_i2c_address.png

MicroPython 代码块:

tof_0.set_i2c_address(42)
set_measurement_timing_budget(val)

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

参数:

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

UiFlow2 代码块:

set_measurement_timing_budget.png

MicroPython 代码块:

tof_0.set_measurement_timing_budget(100)