ESPHome
2026.12.0-dev
Loading...
Searching...
No Matches
esphome
components
tinyusb_keyboard
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
6
#include "
esphome/core/component.h
"
7
8
#ifdef USE_TINYUSB_KEYBOARD_LED_STATE
9
#include <atomic>
10
#include "
esphome/components/binary_sensor/binary_sensor.h
"
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
17
namespace
esphome::tinyusb_keyboard
{
18
19
static
constexpr
uint8_t REPORT_ID_KEYBOARD = 1;
20
static
constexpr
uint8_t REPORT_ID_CONSUMER = 2;
21
static
constexpr
uint8_t ENDPOINT_IN = 0x81;
22
static
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.
27
inline
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
33
inline
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
39
class
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
48
TinyUSBKeyboard
();
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
binary_sensor.h
esphome::Component
Definition
component.h:148
esphome::Component::disable_loop
void disable_loop()
Disable this component's loop.
Definition
component.cpp:210
esphome::tinyusb_keyboard::TinyUSBKeyboard
Definition
keyboard.h:39
esphome::tinyusb_keyboard::TinyUSBKeyboard::publish_led_state_
void publish_led_state_()
Definition
keyboard.cpp:32
esphome::tinyusb_keyboard::TinyUSBKeyboard::keyboard_pending_
bool keyboard_pending_
Definition
keyboard.h:80
esphome::tinyusb_keyboard::TinyUSBKeyboard::modifiers_
uint8_t modifiers_
Definition
keyboard.h:79
esphome::tinyusb_keyboard::TinyUSBKeyboard::consumer_pending_
bool consumer_pending_
Definition
keyboard.h:81
esphome::tinyusb_keyboard::TinyUSBKeyboard::flush_
void flush_()
Sends whatever changed; a report the endpoint cannot take yet is retried from loop()
Definition
keyboard.cpp:57
esphome::tinyusb_keyboard::TinyUSBKeyboard::usage_
uint16_t usage_
Definition
keyboard.h:77
esphome::tinyusb_keyboard::TinyUSBKeyboard::led_state_
std::atomic< uint8_t > led_state_
Definition
keyboard.h:85
esphome::tinyusb_keyboard::TinyUSBKeyboard::keycode_
uint8_t keycode_
Definition
keyboard.h:78
esphome::tinyusb_keyboard::TinyUSBKeyboard::set_keyboard_
void set_keyboard_(uint8_t keycode, uint8_t modifiers)
Definition
keyboard.cpp:44
esphome::tinyusb_keyboard::TinyUSBKeyboard::led_pending_
std::atomic< uint8_t > led_pending_
Definition
keyboard.h:86
esphome::tinyusb_keyboard::TinyUSBKeyboard::set_consumer_
void set_consumer_(uint16_t usage)
Definition
keyboard.cpp:51
esphome::tinyusb_keyboard::TinyUSBKeyboard::wakeup_requested_
bool wakeup_requested_
Definition
keyboard.h:82
component.h
esphome::tinyusb_keyboard
Definition
keyboard.cpp:7
esphome::tinyusb_keyboard::CONFIGURATION_DESCRIPTOR
constexpr uint8_t CONFIGURATION_DESCRIPTOR[]
Definition
keyboard.h:33
esphome::tinyusb_keyboard::HID_REPORT_DESCRIPTOR
constexpr uint8_t HID_REPORT_DESCRIPTOR[]
Definition
keyboard.h:27
Generated by
1.12.0