VT100 Terminal App
Circle VT100 module documentation
Loading...
Searching...
No Matches
TKeyboard.h
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// Module: CTKeyboard
3// Description: Handles USB keyboard input and manages keyboard state.
4// Author: R. Zuehlsdorff, ralf.zuehlsdorff@t-online.de
5// Created: 2026-01-21
6// License: MIT License (https://opensource.org/license/mit/)
7//------------------------------------------------------------------------------
8// Change Log:
9// 2026-01-21 R. Zuehlsdorff Initial creation
10//------------------------------------------------------------------------------
11
12#pragma once
13
14// Include Circle core components
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>
22
33// Forward declarations
34class CKernel;
35
36
45class CTKeyboard : public CTask
46{
47public:
48 using TKeyPressedHandler = void (*)(const char *pString);
49 using TKeyStatusHandlerRaw = void (*)(unsigned char ucModifiers, const unsigned char RawKeys[6]);
51 static CTKeyboard *Get(void);
52
54 CTKeyboard(void);
61 void Configure(TKeyPressedHandler pKeyPressedHandler,
62 TKeyStatusHandlerRaw pKeyStatusHandlerRaw = nullptr,
63 CUSBHCIDevice *pUSBHost = nullptr,
64 unsigned keyRepeatDelayMs = KeyRepeatDelayDefaultMs,
65 unsigned keyRepeatRateCps = KeyRepeatRateDefaultCps);
67 ~CTKeyboard(void);
68
71 boolean Initialize(void);
72
76 boolean UpdateKeyboard(boolean bDevicesUpdated);
77
79 void UpdateLEDs(void);
80
83 boolean IsKeyboardConnected(void) const;
84
86 void OnConfigUpdated();
87
89 void Update();
90
92 void Run(void) override;
93
95 void SetKeyPressedHandler(TKeyPressedHandler handler);
97 void SetKeyStatusHandlerRaw(TKeyStatusHandlerRaw handler);
99 TKeyPressedHandler GetKeyPressedHandler() const;
101 TKeyStatusHandlerRaw GetKeyStatusHandlerRaw() const;
102
103private:
104 // Static callback handlers
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]);
111
112 // Internal methods
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;
136
137private:
138 static constexpr unsigned KeyRepeatDelayDefaultMs = 500;
139 static constexpr unsigned KeyRepeatRateDefaultCps = 20;
140 CUSBKeyboardDevice *volatile m_pKeyboardDevice{nullptr};
141 CUSBHCIDevice *m_pUSBHost;
142
143 static constexpr size_t AutoRepeatMaxSequence = 8;
144 struct AutoRepeatState
145 {
146 boolean active;
147 boolean pendingStart;
148 unsigned char rawKeyCode;
149 char sequence[AutoRepeatMaxSequence + 1];
150 unsigned sequenceLength;
151 u64 pressStartUs;
152 u64 nextRepeatUs;
153 u64 delayUs;
154 u64 intervalUs;
155 } m_AutoRepeat;
156 unsigned char m_PreviousRawKeys[6];
157 unsigned char m_PendingAutoRepeatRawKey;
158 unsigned m_KeyRepeatDelayMs;
159 unsigned m_KeyRepeatRateCps;
160
161 TKeyPressedHandler m_pKeyPressedHandler;
162 TKeyStatusHandlerRaw m_pKeyStatusHandlerRaw;
163};
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