Relay2 Module

支持以下产品:

Relay2Module

MicroPython 应用示例:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4import os, sys, io
 5import M5
 6from M5 import *
 7from module import Relay2Module
 8from hardware import *
 9
10
11title0 = None
12label0 = None
13label1 = None
14label2 = None
15label3 = None
16relay2_0 = None
17
18
19def setup():
20    global title0, label0, label1, label2, label3, relay2_0
21
22    M5.begin()
23    Widgets.fillScreen(0x222222)
24    title0 = Widgets.Title("2Relay Module Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
25    label0 = Widgets.Label("Relay 1:", 2, 53, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
26    label1 = Widgets.Label("Relay 2:", 2, 119, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
27    label2 = Widgets.Label("I2C Addr:", 1, 177, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28    label3 = Widgets.Label(
29        "FW Version:", 181, 177, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
30    )
31
32    relay2_0 = Relay2Module(address=0x25)
33    label2.setText(str((str("I2C Addr:") + str((relay2_0.get_i2c_address())))))
34    label3.setText(str((str("FW Version:") + str((relay2_0.get_firmware_version())))))
35
36
37def loop():
38    global title0, label0, label1, label2, label3, relay2_0
39    M5.update()
40    label0.setText(str((str("Relay 1:") + str((relay2_0.get_relay_status(1))))))
41    label1.setText(str((str("Relay 2:") + str((relay2_0.get_relay_status(2))))))
42    if BtnA.isPressed():
43        relay2_0.set_relay_state(1, 1)
44    else:
45        relay2_0.set_relay_state(1, 0)
46    if BtnB.isPressed():
47        relay2_0.set_relay_state(2, 1)
48    else:
49        relay2_0.set_relay_state(2, 0)
50
51
52if __name__ == "__main__":
53    try:
54        setup()
55        while True:
56            loop()
57    except (Exception, KeyboardInterrupt) as e:
58        try:
59            from utility import print_error_msg
60
61            print_error_msg(e)
62        except ImportError:
63            print("please update to latest firmware")

UiFlow2 应用示例:

example.png

class Relay2Module

Constructors

class Relay2Module(address)

使用指定的 I2C 地址初始化 2Relay Module。

参数:

address (int|list|tuple) – Relay2Module 的 I2C 地址。

UIFLOW2:

init.png

Methods

Relay2Module.set_relay_state(num, state) None

设置指定继电器的状态。

参数:
  • num (int) – 继电器编号(1 或 2)。

  • state (bool) – True 表示开启,False 表示关闭。

UIFLOW2:

set_relay_state.png

Relay2Module.get_relay_status(num) bool

获取指定继电器的状态。

参数:

num (int) – 继电器编号(1 或 2)。

UIFLOW2:

get_relay_status.png

Relay2Module.set_all_relay_state(state) None

同时设置两个继电器的状态。

参数:

state (bool) – True 时打开两个继电器,False 时关闭两个继电器。

UIFLOW2:

set_all_relay_state.png

Relay2Module.get_firmware_version() int

获取 Relay2 Module 的固件版本。

UIFLOW2:

get_firmware_version.png

Relay2Module.set_i2c_address(addr) None

为 Relay2 Module 设置新的 I2C 地址(0x08 ~ 0x77)。

参数:

addr (int) – 要设置的新 I2C 地址。

UIFLOW2:

set_i2c_address.png

Relay2Module.get_i2c_address() int

获取 Relay2 Module 当前的 I2C 地址。

UIFLOW2:

get_i2c_address.png