Kleidos API Search

In development. It changes without notice.

Topic

Keyboard Drivers

The M5Cardputer ADV's key-matrix controller and the LILYGO T-Deck's keyboard.

  • Tca8418 scans the Cardputer ADV's matrix and queues each press and release by the key's number.
  • LilygoKeyboard reads the T-Deck's keyboard, which is a microcontroller of its own: it gives characters, and takes the level of its light.

What a key means is decided in Input Sources & the Wake Rule.

See also
Input Sources & the Wake Rule

Files

file src/drivers/keyboard/i_keyboard_controller.h
Chip-level keyboard-controller driver contract.
file src/drivers/keyboard/keyboard_factory.h
Probe factory for the keyboard-controller driver.
file src/drivers/keyboard/keyboard_selftest.h
Pure decision helpers + shared constants for the kleidos::drivers::keyboard::IKeyboardController self-test.
file src/drivers/keyboard/lilygo_keyboard.h
Raw transport driver for the LILYGO T-Deck I2C keyboard controller.
file src/drivers/keyboard/lilygo_keyboard_protocol.h
The LILYGO T-Deck keyboard controller's I2C commands, and the frames the driver sends them in.
file src/drivers/keyboard/null_keyboard_controller.h
No-op keyboard controller for the boards without a physical keyboard.
file src/drivers/keyboard/tca8418.h
Driver for the NXP TCA8418 keypad-matrix controller.
file src/drivers/keyboard/tca8418_regs.h
Register map for the NXP TCA8418 keypad/GPIO expander.

Classes

class kleidos::drivers::keyboard::IKeyboardController
Chip-level keyboard-controller driver interface. More about kleidos::drivers::keyboard::IKeyboardController
class kleidos::drivers::keyboard::LilygoKeyboard
Raw I2C transport for the LILYGO T-Deck keyboard controller. More about kleidos::drivers::keyboard::LilygoKeyboard
class kleidos::drivers::keyboard::NullKeyboardController
No-op keyboard controller used on the boards that have no keyboard. More about kleidos::drivers::keyboard::NullKeyboardController
class kleidos::drivers::keyboard::Tca8418
NXP TCA8418 keypad-matrix controller (raw FIFO event transport). More about kleidos::drivers::keyboard::Tca8418

Typedefs

usingkleidos::drivers::keyboard::lilygo::BacklightFrame
One backlight frame, as one write transaction carries it.
usingkleidos::drivers::keyboard::lilygo::KeyModeFrame
One key-mode frame, as one write transaction carries it.

Enumerations

enum class kleidos::drivers::keyboard::KeyboardFamily : uint8_t { kleidos::drivers::keyboard::KeyboardFamily::None , kleidos::drivers::keyboard::KeyboardFamily::Tca8418 , kleidos::drivers::keyboard::KeyboardFamily::Lilygo }
Keyboard-controller family selected by an address probe. More about kleidos::drivers::keyboard::KeyboardFamily

Functions

constexpr KeyboardFamilykleidos::drivers::keyboard::classifyKeyboard (bool tcaProbeOk, bool lilygoProbeOk)
Classify a keyboard controller from its per-family probe results (pure).
IKeyboardController *kleidos::drivers::keyboard::createKeyboardController (uint8_t address, int intPin)
Resolve and return the fitted keyboard-controller driver.
constexpr boolkleidos::drivers::keyboard::selfTestWriteVerified (uint8_t readback, uint8_t pattern, uint8_t mask)
Whether a register write-readback proves the I2C WRITE path.
constexpr boolkleidos::drivers::keyboard::selfTestFifoCountPlausible (uint8_t count)
Whether an event-FIFO count read is within the hardware's bounds.
constexpr BacklightFramekleidos::drivers::keyboard::lilygo::backlightFrame (uint8_t duty)
The write that sets the backlight to duty.
constexpr KeyModeFramekleidos::drivers::keyboard::lilygo::keyModeFrame ()
The write that puts the controller in key mode.

Variables

constexpr uint8_tkleidos::drivers::keyboard::kSelfTestMaxFifoEvents
Upper bound of a plausible event-FIFO count for the sanity read (10-deep FIFO).
constexpr uint8_tkleidos::drivers::keyboard::lilygo::kBacklightCommand
Sets the keyboard backlight's duty; one payload byte follows.
constexpr uint8_tkleidos::drivers::keyboard::lilygo::kAltBBacklightCommand
Sets the duty Alt+B lights the keys at; one payload byte. Never sent.
constexpr uint8_tkleidos::drivers::keyboard::lilygo::kRawModeCommand
Raw mode: a read returns the key matrix. Never sent.
constexpr uint8_tkleidos::drivers::keyboard::lilygo::kKeyModeCommand
Key mode: a read returns the key the C3 latched (0 = none) and clears it.
constexpr size_tkleidos::drivers::keyboard::lilygo::kBacklightFrameBytes
Bytes in a backlight frame: the command and its duty, and nothing after them.
constexpr size_tkleidos::drivers::keyboard::lilygo::kKeyModeFrameBytes
Bytes in a key-mode frame: the command alone.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRows
Physical matrix wired to the expander.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kColumns
Number of matrix columns wired up.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kMaxEventsPerPoll
Upper bound on events drained per poll.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegConfig
Configuration register.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegIntStatus
Interrupt status register.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegKeyLockEventCount
Key-lock and event-count register.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegKeyEventA
Key event FIFO (read-only).
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntStatus1
GPIO interrupt status, rows 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntStatus2
GPIO interrupt status, cols 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntStatus3
GPIO interrupt status, GPIO 8-9.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntEnable1
GPIO interrupt enable, rows 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntEnable2
GPIO interrupt enable, cols 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntEnable3
GPIO interrupt enable, GPIO 8-9.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegKeypadGpio1
Keypad/GPIO select, rows 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegKeypadGpio2
Keypad/GPIO select, cols 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegKeypadGpio3
Keypad/GPIO select, GPIO 8-9.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpiEventMode1
GPI event mode, rows 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpiEventMode2
GPI event mode, cols 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpiEventMode3
GPI event mode, GPIO 8-9.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioDirection1
GPIO direction, rows 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioDirection2
GPIO direction, cols 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioDirection3
GPIO direction, GPIO 8-9.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntLevel1
GPIO interrupt level, rows 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntLevel2
GPIO interrupt level, cols 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegGpioIntLevel3
GPIO interrupt level, GPIO 8-9.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegDebounceDisable1
Debounce disable, rows 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegDebounceDisable2
Debounce disable, cols 0-7.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kRegDebounceDisable3
Debounce disable, GPIO 8-9.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kConfigGpiInterruptEnable
Config bit: enable GPIO interrupts.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kConfigKeyInterruptEnable
Config bit: enable keypad interrupts.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kIntStatusGpioAndKey
Interrupt status mask: GPIO + key event.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kEventCountMask
Event-count field in 0x03 (low nibble).
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kSelfTestReadReg
Functional READ register (CFG).
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kSelfTestWriteReg
Safe unused-bank R/W register.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kSelfTestWritePattern
Both writable COL8-9 debounce bits.
constexpr uint8_tkleidos::drivers::keyboard::tca8418::kSelfTestWriteMask
Writable field mask (COL8-9 only).

Back to the top

Enumeration Type Documentation

KeyboardFamily #

enum class kleidos::drivers::keyboard::KeyboardFamily : uint8_t

Keyboard-controller family selected by an address probe.

Enumerator
None 

No supported controller responded.

Tca8418 

NXP TCA8418 keypad-matrix controller.

Lilygo 

LILYGO ESP32-C3 I2C keyboard controller.

Back to the top

Function Documentation

backlightFrame() #

BacklightFrame kleidos::drivers::keyboard::lilygo::backlightFrame(uint8_t duty)
constexpr

The write that sets the backlight to duty.

Parameters
NameDirectionMeaning
dutyinDuty, 0 (off) to 255 (full).
Returns
{0x01, duty}: the whole transaction, nothing after it.

classifyKeyboard() #

KeyboardFamily kleidos::drivers::keyboard::classifyKeyboard(bool tcaProbeOk, bool lilygoProbeOk)
constexpr

Classify a keyboard controller from its per-family probe results (pure).

The TCA8418 has no identity register, so each family is identified by whether its own probe (address ACK + a functional/benign read) succeeded. A caller runs only the probe(s) for the families compiled into the build and passes their success flags, so an un-probed family can never false-match. The TCA8418 takes precedence if both somehow report (they sit at different addresses and are never both compiled).

Parameters
NameDirectionMeaning
tcaProbeOkinWhether the TCA8418 CFG-register probe succeeded.
lilygoProbeOkinWhether the LILYGO address-ACK + benign read succeeded.
Returns
The detected KeyboardFamily, or KeyboardFamily::None.

createKeyboardController() #

IKeyboardController * kleidos::drivers::keyboard::createKeyboardController(uint8_t address, int intPin)

Resolve and return the fitted keyboard-controller driver.

Probes address on the internal I2C bus for the keyboard families compiled into this build, then returns the matching driver (constructed but not yet initialized — the caller runs IKeyboardController::init). On no match, a probe failure, or a board with no keyboard family compiled in, returns the shared kleidos::drivers::keyboard::NullKeyboardController.

Parameters
NameDirectionMeaning
addressin7-bit I2C address of the keyboard controller.
intPininKeyboard INT GPIO, or -1 if unused: the TCA8418's active-low interrupt, or the LILYGO C3's line, which its stock firmware never drives.
Returns
A driver owned by function-local static storage (never delete, never nullptr).

keyModeFrame() #

KeyModeFrame kleidos::drivers::keyboard::lilygo::keyModeFrame()
constexpr

The write that puts the controller in key mode.

Returns
{0x04}: the whole transaction, sent on its own.

selfTestFifoCountPlausible() #

bool kleidos::drivers::keyboard::selfTestFifoCountPlausible(uint8_t count)
constexpr

Whether an event-FIFO count read is within the hardware's bounds.

The TCA8418 key-lock/event-count register reports the number of queued key events in its low nibble; a value beyond the physical FIFO depth signals a bad read or a misbehaving controller.

Parameters
NameDirectionMeaning
countinEvent count from the key-lock/event-count register (masked).
Return values
ValueMeaning
truecount is within [0, kSelfTestMaxFifoEvents].
falsecount exceeds the physical FIFO depth (implausible).

selfTestWriteVerified() #

bool kleidos::drivers::keyboard::selfTestWriteVerified(uint8_t readback, uint8_t pattern, uint8_t mask)
constexpr

Whether a register write-readback proves the I2C WRITE path.

Compares only the writable field bits selected by mask; reserved bits (which read back as 0) are ignored, so a full-register mask of 0xFF collapses to exact equality.

Parameters
NameDirectionMeaning
readbackinValue read back after writing pattern.
patterninTest pattern written to the register.
maskinWritable-field bitmask for the register.
Return values
ValueMeaning
trueThe writable bits of readback match pattern.
falseThe write did not take effect on the writable bits.

Back to the top

Variable Documentation

kRows #

uint8_t kleidos::drivers::keyboard::tca8418::kRows
constexpr

Physical matrix wired to the expander.

Number of matrix rows wired up.

Back to the top