ByteSwitch Unit
Unit ByteSwitch 是一款 8 路触摸开关输入单元,配备 8 路开关输入和 9 颗 WS2812C RGB LED。它采用 STM32 微控制器,并支持 I2C 通信。板载两个 Port A 接口,支持级联多个 Unit ByteSwitch 模块,适用于复杂系统。该模块可实现开关输入检测与动态灯光反馈,适合用于智能家居控制、游戏设备、教育平台、工业状态显示以及互动展览等场景。
支持以下产品:
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 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 应用示例
byteswitch_cores3_example.m5f2
class ByteSwitchUnit
Constructors
Methods
- ByteSwitchUnit.set_led_show_mode(mode)
设置 LED 显示模式。
- 参数:
mode (int) – 要设置的 LED 显示模式。选项: -
BYTESWITCH_LED_USER_MODE:0 -BYTESWITCH_LED_SYS_MODE:1
UIFLOW2:

- ByteSwitchUnit.set_led_brightness(num, brightness)
设置指定 LED 的亮度。
UIFLOW2:

- ByteSwitchUnit.set_led_color(num, color, led_show_mode, btn_is_pressed)
设置指定 LED 的颜色。
- 参数:
UIFLOW2:


- ByteSwitchUnit.set_indicator_brightness(brightness)
设置指示灯 LED 的亮度。
- 参数:
brightness (int) – 亮度等级(0-255)。
UIFLOW2:

- ByteSwitchUnit.set_indicator_color(color)
设置指示灯的颜色,使用 RGB888 格式。
- 参数:
color (int) – 要设置的 RGB888 颜色值。
UIFLOW2:

- ByteSwitchUnit.rgb888_to_rgb233(color)
将 RGB888 颜色值转换为 RGB233 格式。
- 参数:
color (int) – RGB888 颜色值,以 32 位整数表示。
- ByteSwitchUnit.set_rgb233(num, color)
设置特定 LED 的颜色,格式为 RGB233。
- ByteSwitchUnit.get_irq_enable()
获取当前 IRQ 使能状态。
- ByteSwitchUnit.save_to_flash()
将当前用户设置保存到 flash。
UIFLOW2:














