ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
keyboard.h
Go to the documentation of this file.
1#pragma once
2
3#if defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
4 defined(USE_ESP32_VARIANT_ESP32S31) || defined(USE_ESP32_VARIANT_ESP32H4)
5
7
8#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
9#include <atomic>
11#endif
12
13#include "tusb.h"
14// tusb.h only pulls the HID class in when CFG_TUD_HID is set, which the static analysis build does not do
15#include "class/hid/hid_device.h"
16
18
19static constexpr uint8_t REPORT_ID_KEYBOARD = 1;
20static constexpr uint8_t REPORT_ID_CONSUMER = 2;
21static constexpr uint8_t ENDPOINT_IN = 0x81;
22static constexpr uint8_t POLL_INTERVAL_MS = 10;
23
24// One HID interface carrying a keyboard and a consumer control (media keys), told apart by report id.
25// Report ids rule out the boot protocol, so the interface declares no boot protocol, as TinyUSB's
26// composite example does.
27inline constexpr uint8_t HID_REPORT_DESCRIPTOR[] = {
28 TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(REPORT_ID_KEYBOARD)),
29 TUD_HID_REPORT_DESC_CONSUMER(HID_REPORT_ID(REPORT_ID_CONSUMER)),
30};
31
32// esp_tinyusb ships default configuration descriptors for CDC, MSC and NCM only, so the keyboard supplies its own
33inline constexpr uint8_t CONFIGURATION_DESCRIPTOR[] = {
34 TUD_CONFIG_DESCRIPTOR(1, 1, 0, TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
35 TUD_HID_DESCRIPTOR(0, 0, HID_ITF_PROTOCOL_NONE, sizeof(HID_REPORT_DESCRIPTOR), ENDPOINT_IN, CFG_TUD_HID_EP_BUFSIZE,
36 POLL_INTERVAL_MS),
37};
38
39class TinyUSBKeyboard final : public Component {
40#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
41 SUB_BINARY_SENSOR(capslock)
42 SUB_BINARY_SENSOR(numlock)
43 SUB_BINARY_SENSOR(scrolllock)
44#endif
45
46 public:
47#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
49#endif
50 void setup() override { this->disable_loop(); }
51 void loop() override;
52 void dump_config() override;
53
55 void press_key(uint8_t keycode, uint8_t modifiers) { this->set_keyboard_(keycode, modifiers); }
56 void release_keys() { this->set_keyboard_(0, 0); }
58 void press_media(uint16_t usage) { this->set_consumer_(usage); }
59 void release_media() { this->set_consumer_(0); }
60
61#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
63 void set_led_state(uint8_t led_state);
64#endif
65
66 protected:
67 void set_keyboard_(uint8_t keycode, uint8_t modifiers);
68 void set_consumer_(uint16_t usage);
70 void flush_();
71#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
72 void publish_led_state_();
73#endif
74
75 // The wanted state of each report, resent until the endpoint accepts it so a release is never lost.
76 // Only the latest state is kept, so consecutive key actions need a short delay between them.
77 uint16_t usage_{0};
78 uint8_t keycode_{0};
79 uint8_t modifiers_{0};
80 bool keyboard_pending_{false};
81 bool consumer_pending_{false};
82 bool wakeup_requested_{false};
83#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
84 // Written by the TinyUSB task; uint8_t because atomic<bool> is an out-of-line call on Xtensa
85 std::atomic<uint8_t> led_state_{0};
86 std::atomic<uint8_t> led_pending_{0};
87#endif
88};
89
90} // namespace esphome::tinyusb_keyboard
91
92#endif // USE_ESP32_VARIANT_ESP32P4 || USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3 ||
93 // USE_ESP32_VARIANT_ESP32S31 || USE_ESP32_VARIANT_ESP32H4
void disable_loop()
Disable this component's loop.
void flush_()
Sends whatever changed; a report the endpoint cannot take yet is retried from loop()
Definition keyboard.cpp:57
void set_keyboard_(uint8_t keycode, uint8_t modifiers)
Definition keyboard.cpp:44
std::atomic< uint8_t > led_pending_
Definition keyboard.h:86
constexpr uint8_t CONFIGURATION_DESCRIPTOR[]
Definition keyboard.h:33
constexpr uint8_t HID_REPORT_DESCRIPTOR[]
Definition keyboard.h:27