ByteButton Unit

Unit ByteButton is an 8-button touch switch input unit equipped with 8 button inputs and 9 WS2812C RGB LEDs. It uses the STM32 microcontroller and supports I2C communication. The board includes two Port A interfaces and supports cascading multiple Unit ByteButton modules, making it suitable for complex systems. It can achieve button input detection and dynamic lighting feedback, ideal for smart home control, gaming devices, educational platforms, industrial status displays, and interactive exhibitions.

Support the following products:

ByteButtonUnit

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4import os, sys, io
 5import M5
 6from M5 import *
 7from hardware import *
 8from unit import ByteButtonUnit
 9import time
10
11
12title0 = None
13label0 = None
14label1 = None
15i2c0 = None
16bytebutton_0 = None
17
18
19state_byte = None
20i = None
21
22
23def setup():
24    global title0, label0, label1, i2c0, bytebutton_0, state_byte, i
25
26    M5.begin()
27    Widgets.fillScreen(0x222222)
28    title0 = Widgets.Title(
29        "ByteButton CoreS3 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
30    )
31    label0 = Widgets.Label("label0", 4, 87, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
32    label1 = Widgets.Label("label1", 5, 125, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
33
34    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
35    bytebutton_0 = ByteButtonUnit(i2c0, 0x47)
36    bytebutton_0.set_led_show_mode(ByteButtonUnit.BYTEBUTTON_LED_USER_MODE)
37    bytebutton_0.set_indicator_color(0x33FF33)
38    for i in range(8):
39        bytebutton_0.set_led_color(i, 0xFF0000, ByteButtonUnit.BYTEBUTTON_LED_USER_MODE)
40        bytebutton_0.set_indicator_brightness(255)
41        time.sleep(0.2)
42        bytebutton_0.set_led_color(i + 1, 0x333300, ByteButtonUnit.BYTEBUTTON_LED_USER_MODE)
43    time.sleep(1)
44    for i in range(7, -1, -1):
45        bytebutton_0.set_led_color(i, 0x66FF99, ByteButtonUnit.BYTEBUTTON_LED_USER_MODE)
46        time.sleep(0.2)
47    time.sleep(1)
48    bytebutton_0.set_led_show_mode(ByteButtonUnit.BYTEBUTTON_LED_SYS_MODE)
49    for i in range(8):
50        bytebutton_0.set_led_color(i, 0xFFFFFF, ByteButtonUnit.BYTEBUTTON_LED_SYS_MODE, False)
51        bytebutton_0.set_led_color(i, 0xFF0000, ByteButtonUnit.BYTEBUTTON_LED_SYS_MODE, True)
52
53
54def loop():
55    global title0, label0, label1, i2c0, bytebutton_0, state_byte, i
56    M5.update()
57    state_byte = bytebutton_0.get_byte_button_status()
58    label0.setText(
59        str(
60            [
61                (str("B0:") + str(((state_byte >> 0) & 0x01))),
62                (str("B1:") + str(((state_byte >> 1) & 0x01))),
63                (str("B2:") + str(((state_byte >> 2) & 0x01))),
64                (str("B3:") + str(((state_byte >> 3) & 0x01))),
65            ]
66        )
67    )
68    label1.setText(
69        str(
70            [
71                (str("B4:") + str(((state_byte >> 4) & 0x01))),
72                (str("B5:") + str(((state_byte >> 5) & 0x01))),
73                (str("B6:") + str(((state_byte >> 6) & 0x01))),
74                (str("B7:") + str(((state_byte >> 7) & 0x01))),
75            ]
76        )
77    )
78
79
80if __name__ == "__main__":
81    try:
82        setup()
83        while True:
84            loop()
85    except (Exception, KeyboardInterrupt) as e:
86        try:
87            from utility import print_error_msg
88
89            print_error_msg(e)
90        except ImportError:
91            print("please update to latest firmware")

UIFLOW2 Example:

example.png

bytebutton_cores3_example.m5f2

class ByteButtonUnit

Constructors

class ByteButtonUnit(i2c, address)

Initialize the ByteButtonUnit with a specified I2C address.

Parameters:
  • i2c (I2C) – The I2C interface instance for communication.

  • address (int) – The I2C address of the ByteButtonUnit, default is 0x47.

UIFLOW2:

init.png

Methods

ByteButtonUnit.get_byte_button_status() int

Get the status of all buttons as an integer, where each bit represents the state of each button.

UIFLOW2:

get_byte_button_status.png

ByteButtonUnit.get_button_state(num) bool

Get the state of a specific button.

Parameters:

num (int) – The index of the button (0-7).

UIFLOW2:

get_button_state.png

ByteButtonUnit.get_led_show_mode() int

Get the current LED show mode.

UIFLOW2:

get_led_show_mode.png

ByteButtonUnit.set_led_show_mode(mode)

Set the LED show mode.

Parameters:

mode (int) –

The LED show mode to set.

Options:
  • BYTEBUTTON_LED_USER_MODE: 0

  • BYTEBUTTON_LED_SYS_MODE: 1

UIFLOW2:

set_led_show_mode.png

ByteButtonUnit.set_led_brightness(num, brightness)

Set the brightness of a specific LED.

Parameters:
  • num (int) – The index of the LED (0-7).

  • brightness (int) – The brightness level (0-255).

UIFLOW2:

set_led_brightness.png

ByteButtonUnit.get_led_brightness(num) int

Get the brightness of a specific LED.

Parameters:

num (int) – The index of the LED (0-7).

UIFLOW2:

get_led_brightness.png

ByteButtonUnit.set_led_color(num, color, led_show_mode, btn_is_pressed)

Set the color of a specific LED.

Parameters:
  • num (int) – The index of the LED (0-7).

  • color (int) – The RGB888 color value to set.

  • led_show_mode (int) – The LED show mode, default is BYTEBUTTON_LED_SYS_MODE.

  • btn_is_pressed (bool) – Whether the button is pressed (affects color in SYS mode).

UIFLOW2:

set_sys_mode_led_color.png

set_user_mode_led_color.png

ByteButtonUnit.get_led_color(num, led_show_mode, btn_is_pressed) int

Get the color of a specific LED.

Parameters:
  • num (int) – The index of the LED (0-7).

  • led_show_mode (int) – The LED show mode, default is BYTEBUTTON_LED_SYS_MODE.

  • btn_is_pressed (bool) – Whether the button is pressed (affects color in SYS mode).

UIFLOW2:

get_sys_mode_led_color.png

get_user_mode_led_color.png

ByteButtonUnit.set_indicator_brightness(brightness)

Set the brightness of the indicator LED.

Parameters:

brightness (int) – The brightness level (0-255).

UIFLOW2:

set_indicator_brightness.png

ByteButtonUnit.get_indicator_brightness() int

Get the brightness of the indicator LED.

UIFLOW2:

get_indicator_brightness.png

ByteButtonUnit.set_indicator_color(color)

Set the color of the indicator LED in RGB888 format.

Parameters:

color (int) – The RGB888 color value to set.

UIFLOW2:

set_indicator_color.png

ByteButtonUnit.get_indicator_color() int

Get the color of the indicator LED in RGB888 format.

UIFLOW2:

get_indicator_color.png

ByteButtonUnit.rgb888_to_rgb233(color)

Convert an RGB888 color value to RGB233 format.

Parameters:

color (int) – The RGB888 color value as a 32-bit integer.

ByteButtonUnit.set_rgb233(num, color)

Set the color of a specific LED in RGB233 format.

Parameters:
  • num (int) – The index of the LED (0-7).

  • color (int) – The RGB233 color value to set.

ByteButtonUnit.get_rgb233(num)

Get the color of a specific LED in RGB233 format.

Parameters:

num (int) – The index of the LED (0-7).

ByteButtonUnit.set_irq_enable(enable)

Enable or disable IRQ functionality.

Parameters:

enable (bool) – Whether to enable (True) or disable (False) IRQ.

ByteButtonUnit.get_irq_enable()

Get the current IRQ enable status.

ByteButtonUnit.save_to_flash()

Save the current user settings to flash.

UIFLOW2:

save_to_flash.png

ByteButtonUnit.get_firmware_version() int

Get the firmware version of the ByteButtonUnit.

UIFLOW2:

get_firmware_version.png

ByteButtonUnit.set_i2c_address(new_addr)

Set a new I2C address for the ByteButtonUnit.

Parameters:

new_addr (int) – The new I2C address to set. Must be in the range 0x08 to 0x78.

UIFLOW2:

set_i2c_address.png

ByteButtonUnit.get_i2c_address() int

Get the current I2C address of the ByteButtonUnit.

UIFLOW2:

get_i2c_address.png