usb hid device -> esp32s3 -> bluetooth hid output
Posted: Mon Feb 03, 2025 5:45 pm
hello,
i'm trying to incorporate an esp32s3 into a custom keyboard build. basically i want a wireless qmk setup, and instead of using the more expensive nRF52840, i opted for the esp. however as someone who is relatively new to programming i severely underestimated how much trouble this board would give me. what i'm trying to do is
rp2040 outputting HID via usb -> esp32s3 acting as host and wirelessly sending HID to laptop
i've been able to get the example hid host project to flash and work (confirmed keypresses coming from a connected keyboard while monitoring), but i have no idea how to modify the code to get it to send those keypresses over bluetooth.
i tried using chatgpt to modify it but it's using outdated information and i can't seem to compile at all.. would anyone be able to point me in the right direction/tell me what i'm doing wrong?
i'm trying to incorporate an esp32s3 into a custom keyboard build. basically i want a wireless qmk setup, and instead of using the more expensive nRF52840, i opted for the esp. however as someone who is relatively new to programming i severely underestimated how much trouble this board would give me. what i'm trying to do is
rp2040 outputting HID via usb -> esp32s3 acting as host and wirelessly sending HID to laptop
i've been able to get the example hid host project to flash and work (confirmed keypresses coming from a connected keyboard while monitoring), but i have no idea how to modify the code to get it to send those keypresses over bluetooth.
i tried using chatgpt to modify it but it's using outdated information and i can't seem to compile at all.. would anyone be able to point me in the right direction/tell me what i'm doing wrong?
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <unistd.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/event_groups.h"
- #include "freertos/queue.h"
- #include "esp_err.h"
- #include "esp_log.h"
- #include "driver/gpio.h"
- #include "usb/usb_host.h"
- #include "usb/hid_host.h"
- #include "usb/hid_usage_keyboard.h"
- #include "usb/hid_usage_mouse.h"
- #include "esp_bt.h"
- #include "esp_bt_main.h"
- #include "esp_gap_bt_api.h"
- #include "esp_hid_gap.h"
- #include "esp_hidh.h"
- static const char *TAG = "HID_BT_USB";
- static esp_hidh_dev_t *hid_dev = NULL;
- /**
- * @brief Bluetooth HID event callback
- */
- static void hidd_event_callback(void *arg, esp_hidh_event_t event, void *param) {
- switch (event) {
- case ESP_HIDH_OPEN_EVENT:
- ESP_LOGI(TAG, "Bluetooth HID device connected");
- break;
- case ESP_HIDH_CLOSE_EVENT:
- ESP_LOGI(TAG, "Bluetooth HID device disconnected");
- break;
- default:
- break;
- }
- }
- /**
- * @brief Initialize Bluetooth HID Device mode
- */
- void bluetooth_hid_init(void) {
- esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
- ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BTDM));
- ESP_ERROR_CHECK(esp_bluedroid_init());
- ESP_ERROR_CHECK(esp_bluedroid_enable());
- ESP_ERROR_CHECK(esp_hid_gap_init());
- ESP_ERROR_CHECK(esp_hidh_init(hidd_event_callback, NULL));
- ESP_LOGI(TAG, "Bluetooth HID Initialized");
- }
- /**
- * @brief Send keyboard event over Bluetooth
- */
- void send_keyboard_event(uint8_t modifier, uint8_t key, bool pressed) {
- if (hid_dev) {
- uint8_t report[8] = {modifier, 0, key, 0, 0, 0, 0, 0};
- esp_hidh_dev_input_report(hid_dev, 1, report, sizeof(report));
- ESP_LOGI(TAG, "Sent key %02X %s over Bluetooth", key, pressed ? "PRESSED" : "RELEASED");
- }
- }
- /**
- * @brief Send mouse movement over Bluetooth
- */
- void send_mouse_event(int8_t x, int8_t y, uint8_t buttons) {
- if (hid_dev) {
- uint8_t report[4] = {buttons, x, y, 0};
- esp_hidh_dev_input_report(hid_dev, 2, report, sizeof(report));
- ESP_LOGI(TAG, "Sent mouse X:%d Y:%d Buttons:%02X over Bluetooth", x, y, buttons);
- }
- }
- /**
- * @brief app_main() - Initialize USB HID & Bluetooth HID
- */
- void app_main(void) {
- ESP_LOGI(TAG, "Starting HID Host example");
- bluetooth_hid_init();
- // USB HID Host Initialization
- ESP_ERROR_CHECK(hid_host_install(NULL));
- ESP_LOGI(TAG, "USB HID Initialized");
- }