ByteSwitch Unit

Unit ByteSwitch is an 8-switch touch switch input unit equipped with 8 switch 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 ByteSwitch modules, making it suitable for complex systems. It can achieve switch 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:

ByteSwitchUnit

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

UIFLOW2 Example:

example.png

byteswitch_cores3_example.m5f2

class ByteSwitchUnit

Constructors

class ByteSwitchUnit(i2c, address)

Initialize the ByteSwitchUnit with a specified I2C address.

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

  • address (int) – The I2C address of the ByteSwitchUnit, default is 0x46.

UIFLOW2:

init.png

Methods

ByteSwitchUnit.get_byte_switch_state() int

Get the status of all switchs as an integer, where each bit represents the state of each switch.

UIFLOW2:

get_byte_switch_status.png

ByteSwitchUnit.get_switch_state(num) bool

Get the state of a specific switch.

Parameters:

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

UIFLOW2:

get_switch_state.png

ByteSwitchUnit.get_led_show_mode() bool

Get the current LED show mode.

UIFLOW2:

get_led_show_mode.png

ByteSwitchUnit.set_led_show_mode(mode)

Set the LED show mode.

Parameters:

mode (int) –

The LED show mode to set.

Options:
  • BYTESWITCH_LED_USER_MODE: 0

  • BYTESWITCH_LED_SYS_MODE: 1

UIFLOW2:

set_led_show_mode.png

ByteSwitchUnit.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

ByteSwitchUnit.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

ByteSwitchUnit.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 BYTESWITCH_LED_SYS_MODE.

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

UIFLOW2:

set_sys_mode_led_color.png

set_user_mode_led_color.png

ByteSwitchUnit.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 BYTESWITCH_LED_SYS_MODE.

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

UIFLOW2:

get_sys_mode_led_color.png

get_user_mode_led_color.png

ByteSwitchUnit.set_indicator_brightness(brightness)

Set the brightness of the indicator LED.

Parameters:

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

UIFLOW2:

set_indicator_brightness.png

ByteSwitchUnit.get_indicator_brightness() int

Get the brightness of the indicator LED.

UIFLOW2:

get_indicator_brightness.png

ByteSwitchUnit.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

ByteSwitchUnit.get_indicator_color() int

Get the color of the indicator LED in RGB888 format.

UIFLOW2:

get_indicator_color.png

ByteSwitchUnit.rgb888_to_rgb233(color)

Convert an RGB888 color value to RGB233 format.

Parameters:

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

ByteSwitchUnit.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.

ByteSwitchUnit.get_rgb233(num)

Get the color of a specific LED in RGB233 format.

Parameters:

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

ByteSwitchUnit.set_irq_enable(enable)

Enable or disable IRQ functionality.

Parameters:

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

ByteSwitchUnit.get_irq_enable()

Get the current IRQ enable status.

ByteSwitchUnit.save_to_flash()

Save the current user settings to flash.

UIFLOW2:

save_to_flash.png

ByteSwitchUnit.get_firmware_version() int

Get the firmware version of the ByteSwitchUnit.

UIFLOW2:

get_firmware_version.png

ByteSwitchUnit.set_i2c_address(new_addr)

Set a new I2C address for the ByteSwitchUnit.

Parameters:

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

UIFLOW2:

set_i2c_address.png

ByteSwitchUnit.get_i2c_address() int

Get the current I2C address of the ByteSwitchUnit.

UIFLOW2:

get_i2c_address.png