Keyboard (pocketmage_kb)

Keyboard API

PocketmageKB wraps the TCA8418 key matrix plus the USB HID stack and turns button presses into characters, honoring the modifier state machine (NORMAL, SHIFT, FUNC, FN_SHIFT) and a cooldown filter.

Access

#include <pocketmage.h>

PocketmageKB& kb = KB();
Adafruit_TCA8418& pad = keypad;   // raw driver

KB() is the single instance. setupKB(KB_IRQ) runs in PocketMage_INIT() and wires KB_IRQ as the wake source. The OS stays responsive through the shared PWR_BTN_event/keypad interrupt flow; an app polls updateKeypress() from its main loop.

Modifier state

enum KBState { NORMAL, SHIFT, FUNC, FN_SHIFT };   // pocketmage_globals.h

void setKeyboardState(int kbState);
int  getKeyboardState() const;
void toggleShift();
void toggleFn();

toggleShift / toggleFn flip the modifier bits (FUNC toggles the Fn layer, and Shift within it). getKeyboardState() drives the OLED modifier badge in the status line. Layers compose: FN+Shift selects the FN_SHIFT layer for the secondary symbols.

Key events

char updateKeypress();
bool acceptKey();
void checkUSBKB();
  • updateKeypress() returns the next character from either the USB HID FIFO (checked first) or the TCA8418 event FIFO, draining one key per call. It never blocks. The TCA8418 overflow register is cleared after each read so keys are not lost on a burst; the hardware FIFO is drained until empty before the TCA8418_event_ flag clears.
  • acceptKey() gating: returns true only if at least KB_COOLDOWN ms passed since the last accepted press. Use it before handling a key that must not auto-repeat.
  • checkUSBKB() services the USB HID connection (call periodically). If the attached device is not a keyboard (a USB speaker, for example), the keyboard host lets go of it after 5 s and frees about 25 KB of memory until the device is unplugged. The OS also calls it while an ELF app runs, so keyboards and speakers plugged in later work without the app doing anything.

The interrupt flag is exposed as a member (TCA8418_event_) and set by setTCA8418Event(); apps typically rely on the FIFO-drain behavior of updateKeypress() instead of the flag.

Interrupt control

void disableInterrupts();
void enableInterrupts();
void flush();

disableInterrupts/enableInterrupts gate the driver interrupts around long I/O; flush() drops any queued events (used at boot to clear stale presses, and after sleep).

Example

#include <pocketmage.h>

void pumpKeys() {
  char c = KB().updateKeypress();
  if (c == '\0') return;
  if (!KB().acceptKey()) return;
  // handle the character
}

C ABI (pm_*)

External ELF apps call these C wrappers rather than the C++ class above; they are generated from it by gen_app_api.py and declared in pm_sdk_app.h (pulled in via pm_app_api.h). See the function index and the projection model for the general rules.

int pm_kb_state();
void pm_kb_toggle_shift();
void pm_kb_toggle_fn();
bool pm_kb_accept_key();
char pm_kb_read();
void pm_kb_check_usbkb();
void pm_kb_flush();

All operate on the KB() singleton.

  • pm_kb_state() is getKeyboardState(). Returns the KBState modifier value (NORMAL, SHIFT, FUNC, FN_SHIFT) as int.
  • pm_kb_toggle_shift() is toggleShift().
  • pm_kb_toggle_fn() is toggleFn().
  • pm_kb_accept_key() is acceptKey(). Returns true only once KB_COOLDOWN ms have passed since the last accepted press.
  • pm_kb_read() is updateKeypress(). Returns the next character, or '\0' when none is pending; never blocks.
  • pm_kb_check_usbkb() is checkUSBKB(); services the USB HID connection.
  • pm_kb_flush() is flush(); drops any queued events.