ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
keyboard.cpp
Go to the documentation of this file.
1#if defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
2 defined(USE_ESP32_VARIANT_ESP32S31) || defined(USE_ESP32_VARIANT_ESP32H4)
3
4#include "keyboard.h"
5#include "esphome/core/log.h"
6
8
9ESPHOME_LOG_TAG(TAG, "tinyusb_keyboard");
10
11void TinyUSBKeyboard::dump_config() {
12 ESP_LOGCONFIG(TAG, "TinyUSB Keyboard");
13#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
14 LOG_BINARY_SENSOR(" ", "Caps Lock", this->capslock_binary_sensor_);
15 LOG_BINARY_SENSOR(" ", "Num Lock", this->numlock_binary_sensor_);
16 LOG_BINARY_SENSOR(" ", "Scroll Lock", this->scrolllock_binary_sensor_);
17#endif
18}
19
20#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
21// The Set_Report callback has no component argument; there is one keyboard
22static TinyUSBKeyboard *global_keyboard = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
23
24TinyUSBKeyboard::TinyUSBKeyboard() { global_keyboard = this; }
25
26void TinyUSBKeyboard::set_led_state(uint8_t led_state) {
27 this->led_state_ = led_state;
28 this->led_pending_ = 1;
30}
31
33 if (!this->led_pending_) {
34 return;
35 }
36 this->led_pending_ = 0;
37 const uint8_t led = this->led_state_;
38 PUBLISH_STATE(this->capslock_binary_sensor_, (led & KEYBOARD_LED_CAPSLOCK) != 0);
39 PUBLISH_STATE(this->numlock_binary_sensor_, (led & KEYBOARD_LED_NUMLOCK) != 0);
40 PUBLISH_STATE(this->scrolllock_binary_sensor_, (led & KEYBOARD_LED_SCROLLLOCK) != 0);
41}
42#endif
43
44void TinyUSBKeyboard::set_keyboard_(uint8_t keycode, uint8_t modifiers) {
45 this->keycode_ = keycode;
46 this->modifiers_ = modifiers;
47 this->keyboard_pending_ = true;
48 this->flush_();
49}
50
51void TinyUSBKeyboard::set_consumer_(uint16_t usage) {
52 this->usage_ = usage;
53 this->consumer_pending_ = true;
54 this->flush_();
55}
56
58#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
59 // A key action calls flush_() directly and may disable the loop before loop() publishes
60 this->publish_led_state_();
61#endif
62 if (!tud_mounted()) {
63 // No host is listening, and a key held from before it attached would be wrong, so drop the state
64 ESP_LOGV(TAG, "No host, dropping report");
65 this->keyboard_pending_ = false;
66 this->consumer_pending_ = false;
67 this->wakeup_requested_ = false;
68 this->disable_loop();
69 return;
70 }
71 if (tud_suspended()) {
72 // A key press wakes a sleeping host, like a real keyboard; the report goes out once the bus resumes.
73 // Signal once per suspend: a host that did not arm remote wakeup will not answer, and one that did
74 // must not be signalled on every pass.
75 if (!this->wakeup_requested_) {
76 this->wakeup_requested_ = true;
77 if (!tud_remote_wakeup()) {
78 ESP_LOGV(TAG, "Host did not allow remote wakeup");
79 }
80 }
81 this->enable_loop();
82 return;
83 }
84 this->wakeup_requested_ = false;
85 // tud_hid_*_report() returns false while the endpoint still holds the previous report, which is
86 // what happens for a press and a release in one action list. Keep retrying so the release gets out.
87 if (this->keyboard_pending_) {
88 uint8_t keycodes[6] = {this->keycode_};
89 if (tud_hid_keyboard_report(REPORT_ID_KEYBOARD, this->modifiers_, keycodes)) {
90 this->keyboard_pending_ = false;
91 }
92 }
93 if (this->consumer_pending_ && !this->keyboard_pending_ &&
94 tud_hid_report(REPORT_ID_CONSUMER, &this->usage_, sizeof(this->usage_))) {
95 this->consumer_pending_ = false;
96 }
97 if (this->keyboard_pending_ || this->consumer_pending_) {
98 this->enable_loop();
99 } else {
100 this->disable_loop();
101 }
102}
103
104void TinyUSBKeyboard::loop() {
105#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
106 // Only a pending report may wake a suspended host
107 if (!this->keyboard_pending_ && !this->consumer_pending_) {
108 this->publish_led_state_();
109 // A lock sensor automation may have sent a report that still needs a retry
110 if (!this->keyboard_pending_ && !this->consumer_pending_) {
111 this->disable_loop();
112 return;
113 }
114 }
115#endif
116 this->flush_();
117}
118
119} // namespace esphome::tinyusb_keyboard
120
121// TinyUSB resolves these at link time when HID is enabled
122extern "C" {
123const uint8_t *tud_hid_descriptor_report_cb(uint8_t /*instance*/) {
125}
126
127uint16_t tud_hid_get_report_cb(uint8_t /*instance*/, uint8_t /*report_id*/, hid_report_type_t /*report_type*/,
128 uint8_t * /*buffer*/, uint16_t /*reqlen*/) {
129 return 0;
130}
131
132// Keyboard LED state from the host
133void tud_hid_set_report_cb(uint8_t /*instance*/, uint8_t report_id, hid_report_type_t report_type,
134 const uint8_t *buffer, uint16_t bufsize) {
135#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
136 if (report_id != esphome::tinyusb_keyboard::REPORT_ID_KEYBOARD || report_type != HID_REPORT_TYPE_OUTPUT ||
137 bufsize < 1) {
138 return;
139 }
140 esphome::tinyusb_keyboard::global_keyboard->set_led_state(buffer[0]);
141#endif
142}
143}
144
145#endif // USE_ESP32_VARIANT_ESP32P4 || USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3 ||
146 // USE_ESP32_VARIANT_ESP32S31 || USE_ESP32_VARIANT_ESP32H4
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void enable_loop()
Enable this component's loop.
Definition component.h:262
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
uint32_t buffer
Definition gif.cpp:50
uint16_t tud_hid_get_report_cb(uint8_t, uint8_t, hid_report_type_t, uint8_t *, uint16_t)
Definition keyboard.cpp:127
void tud_hid_set_report_cb(uint8_t, uint8_t report_id, hid_report_type_t report_type, const uint8_t *buffer, uint16_t bufsize)
Definition keyboard.cpp:133
const uint8_t * tud_hid_descriptor_report_cb(uint8_t)
Definition keyboard.cpp:123
ESPHOME_LOG_TAG(TAG, "tinyusb_keyboard")
constexpr uint8_t HID_REPORT_DESCRIPTOR[]
Definition keyboard.h:27
uint8_t report_id
Definition tt21100.cpp:1