Key Unit

Unit Key 是一款内置 RGB LED 的单机械按键输入单元。按键轴体采用 Blue switch,具备段落感与清脆点击声特性。内嵌 1 颗可编程 RGB LED - SK6812,支持 256 级亮度调节。提供 2 路数字 IO,用于按键状态与 LED 控制,实现按键状态检测与灯光控制。适用于多种 HMI 应用场景。

支持以下产品:

KeyUnit

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 KeyUnit
 9
10
11label0 = None
12key_0 = None
13
14
15def key_0_wasPressed_event(state):  # noqa: N802
16    global label0, key_0
17    key_0.set_color(0x6600CC)
18    label0.setText(str("pressed"))
19
20
21def key_0_wasReleased_event(state):  # noqa: N802
22    global label0, key_0
23    key_0.set_color(0x33CC00)
24    label0.setText(str("released"))
25
26
27def setup():
28    global label0, key_0
29
30    M5.begin()
31    Widgets.fillScreen(0x222222)
32    label0 = Widgets.Label("label0", 108, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
33
34    key_0 = KeyUnit((8, 9))
35    key_0.setCallback(type=key_0.CB_TYPE.WAS_PRESSED, cb=key_0_wasPressed_event)
36    key_0.setCallback(type=key_0.CB_TYPE.WAS_RELEASED, cb=key_0_wasReleased_event)
37
38
39def loop():
40    global label0, key_0
41    M5.update()
42    key_0.tick(None)
43
44
45if __name__ == "__main__":
46    try:
47        setup()
48        while True:
49            loop()
50    except (Exception, KeyboardInterrupt) as e:
51        try:
52            from utility import print_error_msg
53
54            print_error_msg(e)
55        except ImportError:
56            print("please update to latest firmware")

UiFlow2 应用示例

example.png

cores3_key_example.m5f2

class KeyUnit

Constructors

class KeyUnit(port: tuple)

初始化 KeyUnit。

参数:

port (tuple) – KeyUnit 连接的端口。port[0]:按键引脚,port[1]:LED 引脚。

UiFlow2

init.png

Methods

KeyUnit.get_key_state() int

获取按键的状态。

返回:

0:已释放,1:按下,2:长按。

UiFlow2

get_key_state.png

KeyUnit.set_color(color: int) None

设置 LED 的颜色。

参数:

color (int) – LED 的颜色。

UiFlow2

set_color.png

KeyUnit.set_brightness(br: int) None

设置 LED 的亮度。

参数:

br (int) – LED 的亮度,范围为 0 到 100。

UiFlow2

set_brightness.png

KeyUnit.isHolding()

返回 Button 对象是否处于长按状态。

UiFlow2

isHolding.png

KeyUnit.isPressed()

返回 Button 对象是否处于按下状态。

UiFlow2

isPressed.png

KeyUnit.isReleased()

返回 Button 对象是否处于释放状态。

UiFlow2

isReleased.png

KeyUnit.wasClicked()

当 Button 对象被短按并释放时返回 True。

UiFlow2

wasClicked.png

KeyUnit.wasDoubleClicked()

当 Button 对象在一定时间内被双击时返回 True。

UiFlow2

wasDoubleClicked.png

KeyUnit.wasHold()

当 Button 对象被按住一段时间时返回 True。

UiFlow2

wasHold.png

KeyUnit.wasPressed()

当按下 Button 对象时返回 True。

UiFlow2

wasPressed.png

KeyUnit.wasReleased()

当 Button 对象释放时返回 True。

UiFlow2

wasReleased.png

KeyUnit.wasSingleClicked()

当 Button 对象在一定时间后被单击时,返回 True。

UiFlow2

wasSingleClicked.png

Event Handling

KeyUnit.setCallback(type: Callback_Type, cb)

设置事件回调函数。

UiFlow2

setCallback.png

Constants

KeyUnit.CB_TYPE

一个 CB_TYPE 对象。

class CB_TYPE

Constants

CB_TYPE.WAS_CLICKED

单击事件类型。

CB_TYPE.WAS_DOUBLECLICKED

双击事件类型。

CB_TYPE.WAS_HOLD

长按事件类型。

CB_TYPE.WAS_PRESSED

按下事件类型

CB_TYPE.WAS_RELEASED

释放事件类型