Home

Awesome

ESP32-BLE-CompositeHID

Forked from ESP32-BLE-Gamepad by lemmingDev to provide support support for composite human interface devices.

This library will let your ESP32 microcontroller behave as a bluetooth mouse, keyboard, gamepad (XInput or generic), or a combination of any of these devices.

License

Published under the MIT license. Please see license.txt.

Dependencies

XInput gamepad features

Generic gamepad features (from ESP32-BLE-Gamepad)

Mouse features

Keyboard features

Composite BLE host features (adapted from ESP32-BLE-Gamepad)

Installation

Example

#include <BleCompositeHID.h>
#include <KeyboardDevice.h>
#include <MouseDevice.h>
#include <GamepadDevice.h>

GamepadDevice gamepad;
KeyboardDevice keyboard;
MouseDevice mouse;
BleCompositeHID compositeHID("CompositeHID Keyboard Mouse Gamepad", "Mystfit", 100);

void setup() {
    Serial.begin(115200);

     // Add all devices to the composite HID device to manage them
    compositeHID.addDevice(&keyboard);
    compositeHID.addDevice(&mouse);
    compositeHID.addDevice(&gamepad);

    // Start the composite HID device to broadcast HID reports
    compositeHID.begin();

    delay(3000);
}

void loop() {
  if(compositeHID.isConnected()){

    // Test mouse by moving it in a circle
    int startTime = millis();
    int reportCount = 0;

    int8_t lastX = 0;
    int8_t lastY = 0;
    bool gamepadPressed = false;

    while(millis() - startTime < 8000){
        reportCount++;
        int8_t x = round(cos((float)millis() / 1000.0f) * 10.0f);
        int8_t y = round(sin((float)millis() / 1000.0f) * 10.0f);
        mouse->mouseMove(x, y);

        // Test keyboard presses
        if(reportCount % 100 == 0){
            keyboard->keyPress(KEY_A);
            keyboard->keyRelease(KEY_A);
        }

        // Test gamepad button presses
        if(reportCount % 100 == 0){
            gamepadPressed = !gamepadPressed;
            if(gamepadPressed)
                gamepad->press(BUTTON_1);
            else
                gamepad->release(BUTTON_1);
        }
        
        delay(16);
    }
  }
}

By default, reports are sent on every button press/release or axis/slider/hat/simulation movement, however this can be disabled, and then you manually call sendReport on each device instance as shown in the IndividualAxes.ino example.

VID and PID values can be set. See TestAll.ino for example.

There is also Bluetooth specific information that you can use (optional):

Instead of BleCompositeHID bleCompositeHID; you can do BleCompositeHID bleCompositeHID("Bluetooth Device Name", "Bluetooth Device Manufacturer", 100);. The third parameter is the initial battery level of your device. By default the battery level will be set to 100%, the device name will be Composite HID and the manufacturer will be Espressif.

The battery level can be set during operation by calling, for example, bleCompositeHID.setBatteryLevel(80);

Credits for ESP32-BLE-CompositeHID

Credit goes to lemmingDev for his work on ESP32-BLE-Gamepad which most of the gamepad portion of this library was based upon.

USB HID codes for keyboards created by MightyPork, 2016 (see KeyboardHIDCodes.h)

Credits for ESP32-BLE-Gamepad

Credits to T-vK as this library is based on his ESP32-BLE-Mouse library (https://github.com/T-vK/ESP32-BLE-Mouse) that he provided.

Credits to chegewara as the ESP32-BLE-Mouse library is based on this piece of code that he provided.

Credits to wakwak-koba for the NimBLE code that he provided.

You might also be interested in:

or the NimBLE versions at