StamPLC AC

ACStamPLC 是用于驱动 AC 扩展板上的继电器和 RGB LED 的类。

支持以下产品:

StampPLC

UiFlow2 应用示例

继电器和 RGB LED 控制

在 UiFlow2 中打开 stamplc_ac_example.m5f2 项目。

此示例演示了 AC 继电器和 RGB LED 的交互式控制。按下按钮 A 可切换继电器状态。当继电器打开时,红色 LED 亮起;当关闭时,红色 LED 熄灭。

UiFlow2 代码块:

stamplc_ac_example.png

示例输出:

MicroPython 应用示例

继电器和 RGB LED 控制

此示例演示了 AC 继电器和 RGB LED 的交互式控制。按下按钮 A 可切换继电器状态。当继电器打开时,红色 LED 亮起;当关闭时,红色 LED 熄灭。

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 stamplc import ACStamPLC
 9
10
11
12title0 = None
13label0 = None
14label1 = None
15stamplc_ac_0 = None
16relay_state = None
17
18
19def btnA_wasClicked_event(state):
20    global title0, label0, label1, stamplc_ac_0, relay_state
21    relay_state = not relay_state
22    stamplc_ac_0.set_relay(relay_state)
23    if relay_state:
24        stamplc_ac_0.set_red_led(True)
25    else:
26        stamplc_ac_0.set_red_led(False)
27
28def setup():
29    global title0, label0, label1, stamplc_ac_0, relay_state
30
31    M5.begin()
32    Widgets.fillScreen(0x000000)
33    title0 = Widgets.Title("StamPLC AC Ctrl", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu24)
34    label0 = Widgets.Label("Press button A toggle", 5, 35, 1.0, 0xffffff, 0x000000, Widgets.FONTS.DejaVu18)
35    label1 = Widgets.Label("relay state", 5, 60, 1.0, 0xffffff, 0x000000, Widgets.FONTS.DejaVu18)
36
37    BtnA.setCallback(type=BtnA.CB_TYPE.WAS_CLICKED, cb=btnA_wasClicked_event)
38
39    stamplc_ac_0 = ACStamPLC()
40    relay_state = False
41    stamplc_ac_0.set_red_led(False)
42    stamplc_ac_0.set_green_led(False)
43    stamplc_ac_0.set_blue_led(False)
44
45
46def loop():
47    global title0, label0, label1, stamplc_ac_0, relay_state
48    M5.update()
49
50
51if __name__ == '__main__':
52    try:
53        setup()
54        while True:
55            loop()
56    except (Exception, KeyboardInterrupt) as e:
57        try:
58            from utility import print_error_msg
59            print_error_msg(e)
60        except ImportError:
61            print("please update to latest firmware")

示例输出:

API

ACStamPLC

class ACStamPLC

创建一个 ACStamPLC 对象。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from stamplc import ACStamPLC

ac = ACStamPLC()
set_relay(state)

切换 AC 继电器输出。

参数:

state (bool) – True 闭合继电器,False 断开继电器。

UiFlow2 代码块:

set_relay.png

MicroPython 代码块:

ac.set_relay(state)
set_red_led(state)

控制 RGB LED 的红色通道。

参数:

state (bool) – True 点亮 LED,False 熄灭 LED。

UiFlow2 代码块:

set_red_led.png

MicroPython 代码块:

ac.set_red_led(state)
set_green_led(state)

控制 RGB LED 的绿色通道。

参数:

state (bool) – True 点亮 LED,False 熄灭 LED。

UiFlow2 代码块:

set_green_led.png

MicroPython 代码块:

ac.set_green_led(state)
set_blue_led(state)

控制 RGB LED 的蓝色通道。

参数:

state (bool) – True 点亮 LED,False 熄灭 LED。

UiFlow2 代码块:

set_blue_led.png

MicroPython 代码块:

ac.set_blue_led(state)