反射式红外 Unit

支持以下产品:

反射式红外 Unit

MicroPython 应用示例

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from unit import ReflectiveIRUnit
 9
10
11rect0 = None
12reflectiveir_0 = None
13
14
15def reflectiveir_0_not_detected_event(reflectiveir):
16    global rect0, reflectiveir_0
17    rect0.setColor(color=0x33CC00, fill_c=0x33CC00)
18
19
20def reflectiveir_0_detected_event(reflectiveir):
21    global rect0, reflectiveir_0
22    rect0.setColor(color=0xCC0000, fill_c=0xCC0000)
23
24
25def setup():
26    global rect0, reflectiveir_0
27
28    M5.begin()
29    rect0 = Widgets.Rectangle(44, 97, 30, 30, 0xFFFFFF, 0xFFFFFF)
30
31    reflectiveir_0 = ReflectiveIRUnit((33, 32))
32    reflectiveir_0.set_callback(
33        reflectiveir_0_not_detected_event, ReflectiveIRUnit.EVENT_NOT_DETECTED
34    )
35    reflectiveir_0.set_callback(reflectiveir_0_detected_event, ReflectiveIRUnit.EVENT_DETECTED)
36    reflectiveir_0.enable_irq()
37
38
39def loop():
40    global rect0, reflectiveir_0
41    M5.update()
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")

UiFlow2 应用示例

example.png

stickc_plus_reflectiverir_example.m5f2

class ReflectiveIRUnit

Constructors

class ReflectiveIRUnit(port: tuple)

创建一个 ReflectiveIRUnit 对象。

参数:

port (tuple) – 指定 Reflective IR Unit 连接的端口。

UIFLOW2:

init.png

Methods

ReflectiveIRUnit.get_analog_value() int

该方法用于读取 Reflective IR Unit 的 ADC 值,并返回一个整数。该值范围为 0 到 65535。

UIFLOW2:

get_analog_value.png

ReflectiveIRUnit.get_digital_value() int

该方法允许读取 Reflective IR Unit 的数字值,并返回一个整数值。该值范围为 0 到 1。

UIFLOW2:

get_digital_value.png

ReflectiveIRUnit.enable_irq() None

启用障碍物检测事件

UIFLOW2:

enable_irq.png

ReflectiveIRUnit.disable_irq() None

禁用障碍物检测事件

UIFLOW2:

disable_irq.png

ReflectiveIRUnit.set_callback(handler, trigger=ReflectiveIRUnit.EVENT_DETECTED | ReflectiveIRUnit.EVENT_NOT_DETECTED) None

为 Reflective IR Unit 设置回调函数。

参数:
  • handler – 要设置的回调函数。

  • trigger – 回调函数的触发条件。

UIFLOW2:

set_callback.png

Constants

ReflectiveIRUnit.EVENT_DETECTED
ReflectiveIRUnit.EVENT_NOT_DETECTED

选择 Reflective IR Unit 的 EVENT 类型。