Button Unit

BUTTON is a single button Unit. The button status can be detected by the input pin by simply capturing the high/low electrical level. If the button is pressed, the signal level will be high if the button is released, the signal level will be low.

Support the following products:

ButtonUnit

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 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:

example.png

button_core2_example.m5f2

class ButtonUnit

Constructors

class ButtonUnit(pin_num, active_low, pullup_active)

Initialize a Button instance with the specified pin, active-low configuration, and pull-up resistor state.

Parameters:
  • pin_num – The GPIO pin number connected to the button.

  • active_low (bool) – Determines whether the button signal is active-low. Default is True.

  • pullup_active (bool) – Specifies whether the internal pull-up resistor is enabled. Default is True.

UIFLOW2:

init.png

Methods

ButtonUnit.count_reset()

Reset the count value to zero.

UIFLOW2:

count_reset.png

ButtonUnit.isHolding()

Check if the button is currently being held.

UIFLOW2:

isHolding.png

ButtonUnit.setCallback(type, cb)

Set a callback function for a specified button event type.

Parameters:
  • type – The event type (e.g., WAS_CLICKED, WAS_DOUBLECLICKED).

  • cb – The callback function to be executed for the event.

UIFLOW2:

setCallback.png

ButtonUnit.tick(pin)

Monitor the state transitions of a button based on its pin state and trigger appropriate handlers.

UIFLOW2:

tick.png