CardKB Unit
Support the following products:
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 CardKBUnit 9from hardware import * 10 11 12label0 = None 13i2c0 = None 14cardkb_0 = None 15 16 17def cardkb_0_pressed_event(kb): 18 global label0, i2c0, cardkb_0 19 label0.setText(str(cardkb_0.get_string())) 20 21 22def setup(): 23 global label0, i2c0, cardkb_0 24 25 M5.begin() 26 Widgets.fillScreen(0x222222) 27 label0 = Widgets.Label("label0", 132, 109, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 28 29 i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000) 30 cardkb_0 = CardKBUnit(i2c0) 31 cardkb_0.set_callback(cardkb_0_pressed_event) 32 33 34def loop(): 35 global label0, i2c0, cardkb_0 36 M5.update() 37 cardkb_0.tick() 38 39 40if __name__ == "__main__": 41 try: 42 setup() 43 while True: 44 loop() 45 except (Exception, KeyboardInterrupt) as e: 46 try: 47 from utility import print_error_msg 48 49 print_error_msg(e) 50 except ImportError: 51 print("please update to latest firmware")
UIFLOW2 Example:
Key codes
Import the CardKB character constants with from unit import KeyCode.
For example, KeyCode.KEYCODE_ENTER is 10 (0x0A). These names exist in
UiFlow2 and differ from usb.device.hid.KeyCode. See
Keyboard key codes for the complete list and the I2C versus
UART/ESP-NOW return-value distinction.
class CardKBUnit
Constructors
- class CardKBUnit(*args, mode=CardKBUnit.CardKB_I2C_MODE, **kwargs)
Factory returning the driver for the selected communication mode. The keyboard firmware and connection must already support that mode; selecting
modedoes not reconfigure the keyboard firmware.- Parameters:
mode (int) –
CardKB_I2C_MODE(0, default),CardKB_UART_MODE(1), orCardKB_ESP_NOW_MODE(2).- Returns:
unit.cardkb.CardKBI2Cfor I2C mode,unit.cardkb.CardKBUARTfor UART mode, orunit.cardkb.CardKBESPNOWfor ESP-NOW mode.- Raises:
ValueError – If
modeis not one of the three supported values.
These are the concrete returned classes, not subclasses of
CardKBUnit. The methods below belong to the returned instance; for example, callcardkb.get_key()rather than looking upCardKBUnit.get_key.I2C: pass the I2C object as the first positional argument. The optional
addresskeyword defaults to0x5Fand applies only to I2C.from unit import CardKBUnit from hardware import I2C, Pin # Choose GPIO pins for your controller. i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000) cardkb = CardKBUnit(i2c0, address=0x5F, mode=CardKBUnit.CardKB_I2C_MODE)
UART: pass the UART ID as the first positional argument and
port=(rx_pin, tx_pin). The driver uses 115200 baud, 8N1.cardkb = CardKBUnit(2, port=(33, 32), mode=CardKBUnit.CardKB_UART_MODE)
ESP-NOW: no bus argument is needed.
cardkb = CardKBUnit(mode=CardKBUnit.CardKB_ESP_NOW_MODE)
UIFLOW2:

Methods
- CardKBUnit.get_key()
Read the key value.
- Returns:
An integer key code in I2C mode, or a
(key_id, key_state)tuple in UART and ESP-NOW modes. ReturnsNonewhen no key is available. The state isCardKBUnit.KEY_STATE_PRESSorCardKBUnit.KEY_STATE_RELEASE.
In I2C mode the integer is a character/special-key value compatible with the CardKB
KeyCode.KEYCODE_*constants. V2 UART/ESP-NOW key IDs are raw HID usage IDs; see Keyboard key codes before converting or comparing them.UIFLOW2:

- CardKBUnit.is_pressed() bool
Check if the key is pressed.
- Returns:
True if the key is pressed, False otherwise
UIFLOW2:

- CardKBUnit.set_callback(handler)
Set the key press event callback.
- Parameters:
handler – Callback function. I2C callbacks receive the CardKB instance; UART and ESP-NOW callbacks receive a key tuple.
UIFLOW2:

Example:
from unit import CardKBUnit def cb(keyboard): print(keyboard.get_key()) cardkb = CardKBUnit(i2c) cardkb.set_callback(cb) while True: cardkb.tick()
- CardKBUnit.tick()
Update the key status.
UIFLOW2:




