15#include <circle/sched/task.h>
16#include <circle/usb/usbkeyboard.h>
17#include <circle/usb/usbhcidevice.h>
18#include <circle/devicenameservice.h>
19#include <circle/logger.h>
20#include <circle/types.h>
21#include <circle/timer.h>
48 using TKeyPressedHandler = void (*)(
const char *pString);
49 using TKeyStatusHandlerRaw = void (*)(
unsigned char ucModifiers,
const unsigned char RawKeys[6]);
61 void Configure(TKeyPressedHandler pKeyPressedHandler,
62 TKeyStatusHandlerRaw pKeyStatusHandlerRaw =
nullptr,
63 CUSBHCIDevice *pUSBHost =
nullptr,
64 unsigned keyRepeatDelayMs = KeyRepeatDelayDefaultMs,
65 unsigned keyRepeatRateCps = KeyRepeatRateDefaultCps);
92 void Run(
void)
override;
106 static void KeyboardRemovedHandler(CDevice *pDevice,
void *pContext);
108 static void KeyPressedTrampoline(
const char *pString);
110 static void KeyStatusTrampoline(
unsigned char ucModifiers,
const unsigned char RawKeys[6]);
114 void OnKeyboardRemoved(
void);
118 void HandleKeyPressed(
const char *pString,
boolean fromAutoRepeat);
122 void HandleRawKeyStatus(
unsigned char ucModifiers,
const unsigned char RawKeys[6]);
124 void ServiceAutoRepeat();
126 void StopAutoRepeat();
128 void TryActivateAutoRepeat();
131 void QueueAutoRepeat(
const char *pString);
135 boolean ShouldQueueAutoRepeat(
const char *pString)
const;
138 static constexpr unsigned KeyRepeatDelayDefaultMs = 500;
139 static constexpr unsigned KeyRepeatRateDefaultCps = 20;
140 CUSBKeyboardDevice *
volatile m_pKeyboardDevice{
nullptr};
141 CUSBHCIDevice *m_pUSBHost;
143 static constexpr size_t AutoRepeatMaxSequence = 8;
144 struct AutoRepeatState
147 boolean pendingStart;
148 unsigned char rawKeyCode;
149 char sequence[AutoRepeatMaxSequence + 1];
150 unsigned sequenceLength;
156 unsigned char m_PreviousRawKeys[6];
157 unsigned char m_PendingAutoRepeatRawKey;
158 unsigned m_KeyRepeatDelayMs;
159 unsigned m_KeyRepeatRateCps;
161 TKeyPressedHandler m_pKeyPressedHandler;
162 TKeyStatusHandlerRaw m_pKeyStatusHandlerRaw;
Central coordinator for hardware bring-up and runtime control.
Definition kernel.h:73
Cooperative task managing keyboard devices and event translation.
Definition TKeyboard.h:46
boolean Initialize(void)
Initialize keyboard devices and start the task.
Definition TKeyboard.cpp:188
boolean IsKeyboardConnected(void) const
Check if keyboard is connected.
Definition TKeyboard.cpp:263
TKeyPressedHandler GetKeyPressedHandler() const
Get current key pressed handler.
Definition TKeyboard.cpp:173
void Configure(TKeyPressedHandler pKeyPressedHandler, TKeyStatusHandlerRaw pKeyStatusHandlerRaw=nullptr, CUSBHCIDevice *pUSBHost=nullptr, unsigned keyRepeatDelayMs=KeyRepeatDelayDefaultMs, unsigned keyRepeatRateCps=KeyRepeatRateDefaultCps)
Configure callbacks and optional host controller.
Definition TKeyboard.cpp:150
void Run(void) override
Entry point of the keyboard task.
Definition TKeyboard.cpp:236
static CTKeyboard * Get(void)
Access the singleton keyboard task instance.
Definition TKeyboard.cpp:37
void SetKeyStatusHandlerRaw(TKeyStatusHandlerRaw handler)
Replace the raw key status handler.
Definition TKeyboard.cpp:168
void OnConfigUpdated()
Notify keyboard that configuration changed.
Definition TKeyboard.cpp:269
void SetKeyPressedHandler(TKeyPressedHandler handler)
Replace the key pressed handler.
Definition TKeyboard.cpp:163
boolean UpdateKeyboard(boolean bDevicesUpdated)
Check for and handle keyboard plug and play events.
Definition TKeyboard.cpp:205
TKeyStatusHandlerRaw GetKeyStatusHandlerRaw() const
Get current raw key status handler.
Definition TKeyboard.cpp:178
~CTKeyboard(void)
Cleanup the keyboard task on shutdown.
Definition TKeyboard.cpp:183
void Update()
Periodic update hook called from the scheduler loop.
void UpdateLEDs(void)
Update keyboard LED state (must be called from main loop).
Definition TKeyboard.cpp:253
CTKeyboard(void)
Construct the singleton keyboard task; dependencies are hooked via Configure().
Definition TKeyboard.cpp:123