Reflective IR Unit

Support the following products:

Reflective IR Unit

Micropython Example:

 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:

example.png

stickc_plus_reflectiverir_example.m5f2

class ReflectiveIRUnit

Constructors

class ReflectiveIRUnit(port: tuple)

Create a ReflectiveIRUnit object.

参数:

port (tuple) – Specify the port to which the Reflective IR Unit is connected.

UIFLOW2:

init.png

Methods

ReflectiveIRUnit.get_analog_value() int

This method allows read the ADC value of the Reflective IR Unit and return an integer value. The value ranges from 0 to 65535.

UIFLOW2:

get_analog_value.png

ReflectiveIRUnit.get_digital_value() int

This method allows read the digital value of the Reflective IR Unit and return an integer value. The value ranges from 0 to 1.

UIFLOW2:

get_digital_value.png

ReflectiveIRUnit.enable_irq() None

Enable Obstacle detection event

UIFLOW2:

enable_irq.png

ReflectiveIRUnit.disable_irq() None

Disable Obstacle detection event

UIFLOW2:

disable_irq.png

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

Set the callback function for the Reflective IR Unit.

参数:
  • handler – The callback function to be set.

  • trigger – The trigger condition for the callback function.

UIFLOW2:

set_callback.png

Constants

ReflectiveIRUnit.EVENT_DETECTED
ReflectiveIRUnit.EVENT_NOT_DETECTED

select the EVENT type of the Reflective IR Unit.