Key Unit

Unit Key is a single mechanical key input unit with built-in RGB LED. The key shaft adopts Blue switch with tactile bump and audible click features. Embedded with one programable RGB LED - SK6812, supports 256 level brightness. Two digital IOs are available for key status and LED control key status and lighting control. Suitable for multiple HMI applications.

Support the following products:

KeyUnit

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

example.png

cores3_key_example.m5f2

class KeyUnit

Constructors

class KeyUnit(port: tuple)

Initialize the KeyUnit.

参数:

port (tuple) – The port to which the KeyUnit is connected. port[0]: key pin, port[1]: LEDs pin.

UIFLOW2:

init.png

Methods

KeyUnit.get_key_state() int

Get the state of the key.

返回:

0: released, 1: pressed, 2: long pressed.

UIFLOW2:

get_key_state.png

KeyUnit.set_color(color: int) None

Set the color of the LED.

参数:

color (int) – The color of the LED.

UIFLOW2:

set_color.png

KeyUnit.set_brightness(br: int) None

Set the brightness of the LED.

参数:

br (int) – The brightness of the LED, range from 0 to 100.

UIFLOW2:

set_brightness.png

KeyUnit.isHolding()

Returns whether the Button object is in a long press state.

UIFLOW2:

isHolding.png

KeyUnit.isPressed()

Returns whether the Button object is in a pressed state.

UIFLOW2:

isPressed.png

KeyUnit.isReleased()

Returns whether the Button object is in a released state.

UIFLOW2:

isReleased.png

KeyUnit.wasClicked()

Returns True when the Button object is briefly pressed and released.

UIFLOW2:

wasClicked.png

KeyUnit.wasDoubleClicked()

Returns True when the Button object is double-clicked after a certain amount of time.

UIFLOW2:

wasDoubleClicked.png

KeyUnit.wasHold()

Returns True when the Button object is held down for a certain amount of time.

UIFLOW2:

wasHold.png

KeyUnit.wasPressed()

Returns True when the Button object is pressed.

UIFLOW2:

wasPressed.png

KeyUnit.wasReleased()

Returns True when the Button object is released.

UIFLOW2:

wasReleased.png

KeyUnit.wasSingleClicked()

Returns True when the Button object is single-clicked after a certain amount of time.

UIFLOW2:

wasSingleClicked.png

Event Handling

KeyUnit.setCallback(type: Callback_Type, cb)

Sets the event callback function.

UIFLOW2:

setCallback.png

Constants

KeyUnit.CB_TYPE

A CB_TYPE object.

class CB_TYPE

Constants

CB_TYPE.WAS_CLICKED

Single click event type.

CB_TYPE.WAS_DOUBLECLICKED

Double click event type.

CB_TYPE.WAS_HOLD

Long press event type.

CB_TYPE.WAS_PRESSED

Press event type

CB_TYPE.WAS_RELEASED

Release event type