Button Unit

BUTTON 是一款单按键 Unit。通过读取输入引脚的高/低电平即可检测按键状态。按键按下时,信号电平为 high;按键松开时,信号电平为 low

支持以下产品:

ButtonUnit

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 ButtonUnit
 9
10
11title0 = None
12label1 = None
13label0 = None
14button_0 = None
15
16
17def setup():
18    global title0, label1, label0, button_0
19
20    M5.begin()
21    Widgets.fillScreen(0x222222)
22    title0 = Widgets.Title(
23        "ButtonUnit Core2 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
24    )
25    label1 = Widgets.Label(
26        "Button State:", 395, 150, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
27    )
28    label0 = Widgets.Label(
29        "Button Counter Value:", 1, 102, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
30    )
31
32    button_0 = ButtonUnit((33, 32), True, type=2)
33
34
35def loop():
36    global title0, label1, label0, button_0
37    M5.update()
38    label0.setText(str((str("Button Counter Value:") + str((button_0.count_value)))))
39
40
41if __name__ == "__main__":
42    try:
43        setup()
44        while True:
45            loop()
46    except (Exception, KeyboardInterrupt) as e:
47        try:
48            from utility import print_error_msg
49
50            print_error_msg(e)
51        except ImportError:
52            print("please update to latest firmware")

UiFlow2 应用示例

example.png

button_core2_example.m5f2

class ButtonUnit

“””构造函数”””

class ButtonUnit(pin_num, active_low, pullup_active)

使用指定的引脚、低电平有效配置和上拉电阻状态初始化一个 Button 实例。

参数:
  • pin_num – 连接到按钮的 GPIO 引脚编号。

  • active_low (bool) – 确定按钮信号是否为低电平有效。默认值为 True。

  • pullup_active (bool) – 指定是否启用内部上拉电阻。默认值为 True。

UIFLOW2:

init.png

Methods

ButtonUnit.count_reset()

将计数值重置为 0。

UIFLOW2:

count_reset.png

ButtonUnit.isHolding()

检查按钮当前是否处于按住状态。

UIFLOW2:

isHolding.png

ButtonUnit.setCallback(type, cb)

为指定的按钮事件类型设置回调函数。

参数:
  • type – 事件类型(例如 WAS_CLICKED、WAS_DOUBLECLICKED)。

  • cb – 为该事件执行的回调函数。

UIFLOW2:

setCallback.png

ButtonUnit.tick(pin)

基于其引脚状态监控按钮的状态转换,并触发相应的处理函数。

UIFLOW2:

tick.png