### Example: Set and Get Device Information Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Example demonstrating setting and retrieving various device information strings. ```cpp config.setModelNumber("1.0"); config.setFirmwareRevision("2.0"); config.setSerialNumber("SN-12345"); config.setHardwareRevision("REV-A"); config.setSoftwareRevision("Software v1.5"); const char* model = config.getModelNumber(); ``` -------------------------------- ### Multi-Configuration Setup for ESP32 BLE Gamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/quick-start.md This example demonstrates how to set up and switch between different controller configurations (Gamepad, Joystick, Flight Sim) dynamically. It utilizes serial commands to change modes and reconfigures the BLE gamepad accordingly. Ensure the library is installed and the ESP32 board is set up correctly. ```cpp #include #include enum ControllerMode { MODE_GAMEPAD, MODE_JOYSTICK, MODE_FLIGHT_SIM }; BleGamepad bleGamepad; ControllerMode currentMode = MODE_GAMEPAD; void configureGamepad() { BleGamepadConfiguration config; config.setControllerType(CONTROLLER_TYPE_GAMEPAD); config.setButtonCount(16); config.setHatSwitchCount(1); config.setWhichAxes(true, true, true, true, true, true, false, false); bleGamepad.begin(&config); } void configureJoystick() { BleGamepadConfiguration config; config.setControllerType(CONTROLLER_TYPE_JOYSTICK); config.setButtonCount(8); config.setHatSwitchCount(0); config.setWhichAxes(true, true, true, false, false, false, false, false); bleGamepad.begin(&config); } void configureFlightSim() { BleGamepadConfiguration config; config.setControllerType(CONTROLLER_TYPE_MULTI_AXIS); config.setButtonCount(32); config.setWhichSimulationControls(true, true, false, false, false); bleGamepad.begin(&config); } void setup() { Serial.begin(115200); // Read mode from EEPROM or default configureGamepad(); } void loop() { // Check for serial commands if (Serial.available()) { char cmd = Serial.read(); if (cmd == 'g') { bleGamepad.end(); currentMode = MODE_GAMEPAD; configureGamepad(); Serial.println("Switched to gamepad mode"); } else if (cmd == 'j') { bleGamepad.end(); currentMode = MODE_JOYSTICK; configureJoystick(); Serial.println("Switched to joystick mode"); } else if (cmd == 'f') { bleGamepad.end(); currentMode = MODE_FLIGHT_SIM; configureFlightSim(); Serial.println("Switched to flight sim mode"); } } if (bleGamepad.isConnected()) { // Operation based on current mode switch (currentMode) { case MODE_GAMEPAD: // Gamepad logic break; case MODE_JOYSTICK: // Joystick logic break; case MODE_FLIGHT_SIM: // Flight sim logic break; } } delay(20); } ``` -------------------------------- ### Initialize BleGamepad with Custom Configuration Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Initializes the gamepad and starts BLE advertising. This method should be called in the setup() function. It accepts an optional BleGamepadConfiguration object to customize the HID descriptor, VID/PID, and feature enables. ```cpp void setup() { Serial.begin(115200); BleGamepadConfiguration config; config.setButtonCount(32); config.setVid(0xe502); config.setPid(0xabcd); bleGamepad.begin(&config); } ``` -------------------------------- ### Complete BLE Gamepad Connection Example (Arduino C++) Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-connection.md This snippet shows the full setup and loop for an ESP32 BLE Gamepad. It covers initializing the gamepad, configuring its settings, detecting connection/disconnection events, processing rumble feedback from the host, and handling serial commands for button presses and bond management. Ensure the ArduinoBLE library is installed. ```cpp #include #include BleGamepad bleGamepad("ESP32 Gamepad", "My Company", 100); BleGamepadConfiguration config; uint32_t lastConnectionCheck = 0; bool wasConnected = false; void setup() { Serial.begin(115200); config.setEnableOutputReport(true); config.setOutputReportLength(64); config.setTXPowerLevel(9); bleGamepad.begin(&config); Serial.println("BLE Gamepad started"); Serial.print("Device Address: "); Serial.println(bleGamepad.getStringAddress()); Serial.print("Device Name: "); Serial.println(bleGamepad.getDeviceName()); } void loop() { bool isConnected = bleGamepad.isConnected(); // Detect connection state change if (isConnected && !wasConnected) { Serial.println("Client connected!"); Serial.println(bleGamepad.getStringAddress()); // Get peer info NimBLEConnInfo peerInfo = bleGamepad.getPeerInfo(); // ... use peer info ... } else if (!isConnected && wasConnected) { Serial.println("Client disconnected!"); } wasConnected = isConnected; if (isConnected) { // Check for host output (rumble feedback) if (bleGamepad.isOutputReceived()) { uint8_t* output = bleGamepad.getOutputBuffer(); uint8_t leftMotor = output[0]; uint8_t rightMotor = output[1]; Serial.print("Rumble - Left: "); Serial.print(leftMotor); Serial.print(" Right: "); Serial.println(rightMotor); // Drive actual motors... } // Normal gamepad operation if (Serial.available()) { char cmd = Serial.read(); if (cmd == 'p') { bleGamepad.press(BUTTON_1); } else if (cmd == 'r') { bleGamepad.release(BUTTON_1); } else if (cmd == 'd') { // Delete current bond and clear bleGamepad.deleteBond(false); Serial.println("Bond deleted"); } else if (cmd == 'a') { // Delete all bonds bleGamepad.deleteAllBonds(false); Serial.println("All bonds deleted"); } else if (cmd == 'x') { // Enter pairing mode bleGamepad.enterPairingMode(); Serial.println("Entered pairing mode"); } } } delay(10); } ``` -------------------------------- ### Basic ESP32 BLE Gamepad Setup Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/00-START-HERE.md This snippet demonstrates the fundamental setup for the ESP32 BLE Gamepad library. It includes the necessary includes, object instantiation, and the basic structure for `setup()` and `loop()` functions to initialize the gamepad and send a button press. ```cpp #include BleGamepad bleGamepad; void setup() { bleGamepad.begin(); } void loop() { if (bleGamepad.isConnected()) { bleGamepad.press(BUTTON_1); delay(100); bleGamepad.release(BUTTON_1); delay(900); } } ``` -------------------------------- ### Example: Setting Motion Controls (C++) Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md This example demonstrates setting gyroscope and accelerometer values using their respective methods. ```cpp bleGamepad.setGyroscope(100, 200, 300); bleGamepad.setAccelerometer(-50, 0, 1024); ``` -------------------------------- ### Dual HID and NUS Operation Example Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md Configure and use the BleGamepad library to operate as a HID gamepad while also enabling and utilizing the Nordic UART Service (NUS) for serial communication. The `setup` function initializes serial communication, enables NUS in the configuration, and begins the BleGamepad. The `handleCommand` function processes incoming NUS data, and the `loop` function demonstrates sending HID reports and NUS messages. ```cpp void setup() { Serial.begin(115200); BleGamepadConfiguration config; config.setEnableNordicUARTService(true); // Enable in config bleGamepad.begin(&config); bleGamepad.setNUSDataReceivedCallback(handleCommand); } void handleCommand(const uint8_t* data, size_t length) { // Process remote commands while gamepad operates if (length > 0 && data[0] == 'V') { int level = data[1]; // Volume level bleGamepad.setBatteryLevel(level); } } void loop() { if (bleGamepad.isConnected()) { BleNUS* nus = bleGamepad.getNUS(); // HID gamepad operation bleGamepad.press(BUTTON_1); bleGamepad.setAxes(100, 200, 0, 0, 0, 0, 0, 0); delay(100); bleGamepad.release(BUTTON_1); // Auxiliary communications nus->println("Gamepad status ok"); delay(500); } } ``` -------------------------------- ### Example: Setting Individual Simulation Controls (C++) Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md This example demonstrates how to set the accelerator, brake, and steering values using individual control methods. ```cpp bleGamepad.setAccelerator(32767); bleGamepad.setBrake(0); bleGamepad.setSteering(16384); ``` -------------------------------- ### Press and Release Start Button Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Demonstrates pressing and then releasing the Start button. Special buttons must be enabled via configuration. The Start button has an ID of 0. ```cpp bleGamepad.pressStart(); delay(50); bleGamepad.releaseStart(); ``` -------------------------------- ### Minimal ESP32 BLE Gamepad Setup Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/quick-start.md This snippet demonstrates the simplest possible gamepad setup using default configurations. It includes basic button press and release logic within the loop. ```cpp #include #include BleGamepad bleGamepad; void setup() { Serial.begin(115200); bleGamepad.begin(); // Uses default configuration } void loop() { if (bleGamepad.isConnected()) { bleGamepad.press(BUTTON_1); delay(100); bleGamepad.release(BUTTON_1); delay(900); } } ``` -------------------------------- ### begin Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Initializes the gamepad and begins BLE advertising. This method must be called in setup() before using other BleGamepad methods. ```APIDOC ## begin ### Description Initializes the gamepad and begins BLE advertising. Must be called in setup() before using other methods. ### Signature ```cpp void begin(BleGamepadConfiguration *config = new BleGamepadConfiguration()) ``` ### Parameters #### Parameters - **config** (BleGamepadConfiguration*) - Optional - Default: Default configuration - Pointer to configuration object defining HID descriptor, VID/PID, feature enables, etc. ### Example ```cpp void setup() { Serial.begin(115200); BleGamepadConfiguration config; config.setButtonCount(32); config.setVid(0xe502); config.setPid(0xabcd); bleGamepad.begin(&config); } ``` ``` -------------------------------- ### ESP32 BLE Gamepad Basic Example Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/README.md This example demonstrates how to turn the ESP32 into a Bluetooth LE gamepad. It shows how to press buttons, move axes, and set DPAD positions. Ensure the library is included and the gamepad is initialized. ```C++ /* * This example turns the ESP32 into a Bluetooth LE gamepad that presses buttons and moves axis * * At the moment we are using the default settings, but they can be canged using a BleGamepadConfig instance as parameter for the begin function. * * Possible buttons are: * BUTTON_1 through to BUTTON_16 * (16 buttons by default. Library can be configured to use up to 128) * * Possible DPAD/HAT switch position values are: * DPAD_CENTERED, DPAD_UP, DPAD_UP_RIGHT, DPAD_RIGHT, DPAD_DOWN_RIGHT, DPAD_DOWN, DPAD_DOWN_LEFT, DPAD_LEFT, DPAD_UP_LEFT * (or HAT_CENTERED, HAT_UP etc) * * bleGamepad.setAxes sets all axes at once. There are a few: * (x axis, y axis, z axis, rx axis, ry axis, rz axis, slider 1, slider 2) * * Alternatively, bleGamepad.setHIDAxes sets all axes at once. in the order of: * (x axis, y axis, z axis, rz axis, ry axis, rz axis, slider 1, slider 2) <- order HID report is actually given in * * Library can also be configured to support up to 5 simulation controls * (rudder, throttle, accelerator, brake, steering), but they are not enabled by default. * * Library can also be configured to support different function buttons * (start, select, menu, home, back, volume increase, volume decrease, volume mute) * start and select are enabled by default */ #include #include BleGamepad bleGamepad; void setup() { Serial.begin(115200); Serial.println("Starting BLE work!"); bleGamepad.begin(); // The default bleGamepad.begin() above enables 16 buttons, all axes, one hat, and no simulation controls or special buttons } void loop() { if (bleGamepad.isConnected()) { Serial.println("Press buttons 5, 16 and start. Move all enabled axes to max. Set DPAD (hat 1) to down right."); bleGamepad.press(BUTTON_5); bleGamepad.press(BUTTON_16); bleGamepad.pressStart(); bleGamepad.setAxes(32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767); //(X, Y, Z, RX, RY, RZ) //bleGamepad.setHIDAxes(32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767); //(X, Y, Z, RZ, RX, RY) bleGamepad.setHat1(HAT_DOWN_RIGHT); // All axes, sliders, hats etc can also be set independently. See the IndividualAxes.ino example delay(500); Serial.println("Release button 5 and start. Move all axes to min. Set DPAD (hat 1) to centred."); bleGamepad.release(BUTTON_5); bleGamepad.releaseStart(); bleGamepad.setHat1(HAT_CENTERED); bleGamepad.setAxes(0, 0, 0, 0, 0, 0, 0, 0); //(X, Y, Z, RX, RY, RZ) //bleGamepad.setHIDAxes(0, 0, 0, 0, 0, 0, 0, 0); //(X, Y, Z, RZ, RX, RY) delay(500); } } ``` -------------------------------- ### Dual HID + NUS Operation Example Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md This example demonstrates how to initialize the BleGamepad library with NUS enabled, set a callback for receiving data over NUS, and perform both HID gamepad operations and auxiliary communication within the loop. ```APIDOC ## Dual HID + NUS Operation The gamepad can operate simultaneously with NUS for HID input/output plus auxiliary serial communication: ```cpp void setup() { Serial.begin(115200); BleGamepadConfiguration config; config.setEnableNordicUARTService(true); // Enable in config bleGamepad.begin(&config); bleGamepad.setNUSDataReceivedCallback(handleCommand); } void handleCommand(const uint8_t* data, size_t length) { // Process remote commands while gamepad operates if (length > 0 && data[0] == 'V') { int level = data[1]; // Volume level bleGamepad.setBatteryLevel(level); } } void loop() { if (bleGamepad.isConnected()) { BleNUS* nus = bleGamepad.getNUS(); // HID gamepad operation bleGamepad.press(BUTTON_1); bleGamepad.setAxes(100, 200, 0, 0, 0, 0, 0, 0); delay(100); bleGamepad.release(BUTTON_1); // Auxiliary communications nus->println("Gamepad status ok"); delay(500); } } ``` ``` -------------------------------- ### Example: Set Simulation Range Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Example of setting the minimum and maximum values for simulation controls. ```cpp config.setSimulationMin(0); config.setSimulationMax(32767); ``` -------------------------------- ### BleNUS Constructor Example Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md Demonstrates how to include the necessary headers and create a BleGamepad instance, which internally handles the BleNUS creation when beginNUS() is called. ```cpp #include #include "BleNUS.h" BleGamepad bleGamepad; // BleNUS created internally by BleGamepad when beginNUS() is called ``` -------------------------------- ### Example: Enable Nordic UART Service and Begin Gamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md This snippet shows how to enable the Nordic UART Service and then initialize the BleGamepad with the specified configuration. ```cpp config.setEnableNordicUARTService(true); bleGamepad.begin(&config); ``` -------------------------------- ### Manual HID Report Example Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/configuration.md Example demonstrating manual HID report sending. Set autoReport to false, then call sendReport() after making changes to buttons, axes, or hats. ```cpp config.setAutoReport(false); bleGamepad.begin(&config); // In loop: bleGamepad.press(BUTTON_1); bleGamepad.setX(100); bleGamepad.setY(200); bleGamepad.sendReport(); // Send accumulated changes ``` -------------------------------- ### Example: Enable Output Reports Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Enables output reports and sets the expected report length to 64 bytes. ```cpp config.setEnableOutputReport(true); config.setOutputReportLength(64); ``` -------------------------------- ### GUID Version Configuration Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Methods to set and get the GUID/Device Version reported in the HID descriptor. ```APIDOC ## setGuidVersion / getGuidVersion ### Description Sets/gets the GUID/Device Version reported in the HID descriptor. ### Method Signature ```cpp void setGuidVersion(uint16_t value) uint16_t getGuidVersion() ``` ### Example ```cpp config.setGuidVersion(0x0110); // Version 1.10 ``` ``` -------------------------------- ### Configure BLE Gamepad with Full Settings Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md This example demonstrates a comprehensive configuration of the BleGamepad library, including controller type, button count, axes, simulation controls, motion sensors, USB identifiers, axis ranges, device information, report settings, Nordic UART service, and transmit power. Ensure all necessary libraries are included and the gamepad is initialized with the custom configuration. ```cpp #include #include BleGamepad bleGamepad("My Gamepad", "My Company", 100); BleGamepadConfiguration config; void setup() { Serial.begin(115200); // Controller and button configuration config.setControllerType(CONTROLLER_TYPE_GAMEPAD); config.setButtonCount(32); // 32 buttons config.setHatSwitchCount(1); // 1 D-pad // Special buttons config.setWhichSpecialButtons(true, true, false, false, false, false, false, false); // Axes (all enabled by default) config.setWhichAxes(true, true, true, true, true, true, true, true); // Simulation controls config.setWhichSimulationControls(true, true, true, true, true); // Motion sensors config.setIncludeGyroscope(true); config.setIncludeAccelerometer(true); // USB identifiers config.setVid(0xe502); config.setPid(0xabcd); // Axis ranges config.setAxesMin(0x0000); config.setAxesMax(0x7FFF); // Device information config.setModelNumber("1.0"); config.setSerialNumber("SN-12345"); config.setFirmwareRevision("1.2.3"); // Report settings config.setAutoReport(true); config.setEnableOutputReport(true); config.setOutputReportLength(64); // Nordic UART config.setEnableNordicUARTService(true); // Transmit power config.setTXPowerLevel(9); // Begin gamepad with configuration bleGamepad.begin(&config); } void loop() { if (bleGamepad.isConnected()) { // Use gamepad... } } ``` -------------------------------- ### BleGamepadConfiguration Constructor Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Creates a new BleGamepadConfiguration object with default settings. Use this to start configuring your BLE gamepad. ```cpp BleGamepadConfiguration config; config.setButtonCount(32); config.setVid(0xe502); config.setPid(0xabcd); bleGamepad.begin(&config); ``` -------------------------------- ### Example: Manual Reporting Configuration Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Demonstrates disabling auto-reporting and manually sending a report after changing button and axis states. ```cpp config.setAutoReport(false); // Manual reporting // In loop: bleGamepad.press(BUTTON_1); bleGamepad.setX(100); bleGamepad.sendReport(); // Send manually ``` -------------------------------- ### Get NUS Instance Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Provides direct access to the BleNUS instance for advanced manipulation. ```cpp BleNUS* getNUS() ``` -------------------------------- ### Command Reception with BleGamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md Illustrates how to set up BleGamepad to receive commands over NUS and process them using a switch statement. This example handles basic commands like 'Power', 'Configuration', and 'Reset'. ```cpp void setup() { Serial.begin(115200); bleGamepad.begin(); bleGamepad.beginNUS(); bleGamepad.setNUSDataReceivedCallback(processNUSCommand); } void processNUSCommand(const uint8_t* data, size_t length) { if (length == 0) return; char cmd = data[0]; switch (cmd) { case 'P': // Power command Serial.println("Power control received"); break; case 'C': // Configuration Serial.println("Config command received"); break; case 'R': // Reset Serial.println("Reset command received"); break; default: Serial.print("Unknown command: "); Serial.println(cmd); } } ``` -------------------------------- ### Force Pairing Mode Example for Connection Issues Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/TroubleshootingGuide.md Demonstrates three different methods for handling pairing mode to resolve connection problems where the ESP32 may not appear on a PC. ```cpp // See ForcePairingMode.ino example for the 3 different methods of dealing with this ``` -------------------------------- ### Example: Set Individual Slider Values Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Demonstrates how to set the values for slider 1 and slider 2 using their respective methods. ```cpp bleGamepad.setSlider1(16384); bleGamepad.setSlider2(32767); ``` -------------------------------- ### Build a Custom Gamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/quick-start.md This example shows how to create a fully-featured custom gamepad, including buttons, analog sticks, and battery level reporting. It uses analog pins for sticks and digital pins for buttons. ```cpp #include #include // Pins #define BTN_A 4 #define BTN_B 5 #define STICK_LX A0 #define STICK_LY A1 #define STICK_RX A2 #define STICK_RY A3 #define BATTERY_PIN A4 BleGamepad bleGamepad("Custom Pad", "Maker", 100); BleGamepadConfiguration config; int16_t readAxis(uint8_t pin, uint8_t deadzone = 50) { int raw = analogRead(pin); int16_t value = map(raw, 0, 4095, -32767, 32767); if (abs(value) < deadzone) return 0; return value; } uint8_t readBattery() { int raw = analogRead(BATTERY_PIN); return map(raw, 0, 4095, 0, 100); } void setup() { Serial.begin(115200); pinMode(BTN_A, INPUT_PULLUP); pinMode(BTN_B, INPUT_PULLUP); config.setButtonCount(16); config.setHatSwitchCount(1); config.setAutoReport(true); bleGamepad.begin(&config); Serial.println("Custom Gamepad Ready"); } void loop() { if (bleGamepad.isConnected()) { // Read buttons if (digitalRead(BTN_A) == LOW) { bleGamepad.press(BUTTON_1); } else { bleGamepad.release(BUTTON_1); } if (digitalRead(BTN_B) == LOW) { bleGamepad.press(BUTTON_2); } else { bleGamepad.release(BUTTON_2); } // Read analog sticks bleGamepad.setLeftThumb( readAxis(STICK_LX), readAxis(STICK_LY) ); bleGamepad.setRightThumb( readAxis(STICK_RX), readAxis(STICK_RY) ); // Update battery static uint32_t lastBatt = 0; if (millis() - lastBatt > 10000) { lastBatt = millis(); bleGamepad.setBatteryLevel(readBattery()); } delay(20); } else { // Disconnected - sleep delay(100); } } ``` -------------------------------- ### Press Gamepad Buttons Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/types.md Use `press()` to activate standard buttons and `pressSpecialButton()` for special buttons like START. Ensure the `bleGamepad` object is initialized before use. ```cpp bleGamepad.press(BUTTON_1); bleGamepad.pressSpecialButton(START_BUTTON); ``` -------------------------------- ### Example: Set Individual Hat Switch Values Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Demonstrates setting specific hat switch positions using their individual methods. HAT_CENTERED is represented by 0. ```cpp bleGamepad.setHat1(HAT_DOWN_RIGHT); bleGamepad.setHat2(HAT_UP); bleGamepad.setHat3(HAT_CENTERED); ``` -------------------------------- ### Bidirectional Communication with BleGamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md Demonstrates bidirectional communication using BleGamepad's NUS. This example shows how to check for incoming data, read it, echo it back, and also send gamepad state information. ```cpp void loop() { if (bleGamepad.isConnected()) { BleNUS* nus = bleGamepad.getNUS(); // Check for incoming data if (nus->available()) { int byte = nus->read(); Serial.print("Command: "); Serial.println((char)byte); // Echo back nus->print("Got: "); nus->println((char)byte); } // Send gamepad state if (bleGamepad.isPressed(BUTTON_1)) { nus->println("Button 1 pressed"); } delay(10); } } ``` -------------------------------- ### BLE Gamepad onStarted Method Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Override this virtual method in derived classes to implement custom behavior when the BLE server starts. It receives a pointer to the NimBLEServer instance. ```cpp virtual void onStarted(NimBLEServer *pServer) ``` -------------------------------- ### Get String Address Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Retrieves the BLE address of the device formatted as a string. ```cpp String getStringAddress() ``` -------------------------------- ### Button Configuration Methods Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Methods for setting and getting the number of buttons included in the HID descriptor. ```APIDOC ## setButtonCount ### Description Sets the number of buttons to include in the HID descriptor. The value must be between 1 and 128. ### Method `void setButtonCount(uint16_t value)` ### Parameters #### Path Parameters - **value** (uint16_t) - Required - Number of buttons (1-128) ### Request Example ```cpp config.setButtonCount(32); ``` ## getButtonCount ### Description Returns the currently configured button count. ### Method `uint16_t getButtonCount()` ### Returns - **uint16_t** - Number of buttons configured. ``` -------------------------------- ### Get Device Information Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Retrieve device information strings from the Device Information service. ```cpp const char *getModelNumber() const char *getFirmwareRevision() const char *getHardwareRevision() const char *getSoftwareRevision() const char *getSerialNumber() ``` -------------------------------- ### BleGamepad NUS Integration Example Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md Demonstrates two methods for sending data over the Nordic UART Service (NUS) using the BleGamepad library. The first option uses the `sendDataOverNUS` method directly through `BleGamepad`, while the second option retrieves the `BleNUS` instance using `getNUS()` for direct method calls. ```cpp bleGamepad.begin(); bleGamepad.beginNUS(); // Option 1: Use through BleGamepad bleGamepad.sendDataOverNUS((uint8_t*)"Hello", 5); // Option 2: Get direct access BleNUS* nus = bleGamepad.getNUS(); nus->println("Hello"); ``` -------------------------------- ### BleGamepadConfiguration Constructor Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Creates a new BleGamepadConfiguration object with factory default settings. These defaults cover controller type, button and hat switch counts, axis enablement, simulation and motion controls, sensor status, device identification (VID, PID, GUID), axis ranges, string revisions, and output report settings. ```APIDOC ## BleGamepadConfiguration() ### Description Creates a new configuration object with factory defaults. ### Default Values: - Controller Type: CONTROLLER_TYPE_GAMEPAD - Auto Report: true - Button Count: 16 - Hat Switch Count: 1 - All Axes: enabled (X, Y, Z, Rx, Ry, Rz, Slider1, Slider2) - Simulation Controls: disabled - Gyroscope: disabled - Accelerometer: disabled - VID: 0xe502 - PID: 0xbbab - GUID Version: 0x0110 - Axes Min: 0x0000 (0) - Axes Max: 0x7FFF (32767) - Simulation Min: 0x0000 - Simulation Max: 0x7FFF - Motion Min: 0x0000 - Motion Max: 0x7FFF - Model Number: "1.0.0" - Software Revision: "1.0.0" - Serial Number: "0123456789" - Firmware Revision: "0.7.4" - Hardware Revision: "1.0.0" - Output Report: disabled - Nordic UART Service: disabled - Output Report Length: 64 bytes - TX Power Level: 9 dBm ### Example: ```cpp BleGamepadConfiguration config; config.setButtonCount(32); config.setVid(0xe502); config.setPid(0xabcd); bleGamepad.begin(&config); ``` ``` -------------------------------- ### Example: Set Transmit Power Level Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Sets the transmit power level for the BLE connection to 3 dBm. This configuration should be applied before initializing the gamepad. ```cpp config.setTXPowerLevel(3); // 3 dBm transmit power ``` -------------------------------- ### Setting TX Power Level for Connectivity Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/TroubleshootingGuide.md Example showing how to set a stronger TX power level (up to 9) to improve BLE connection stability and range. ```cpp // Try setting a stronger TX power level (9 is the highest) as shown in example CharacteristicsConfiguration.ino ``` -------------------------------- ### Initialize BLE Gamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/README.md Initializes the BLE Gamepad with a device name and manufacturer. Configures HID features like button count and simulation controls before starting the gamepad. ```cpp BleGamepad bleGamepad("Device Name", "Manufacturer"); BleGamepadConfiguration config; // Configure HID features config.setButtonCount(32); config.setWhichSimulationControls(true, true, false, false, false); bleGamepad.begin(&config); ``` -------------------------------- ### USB Vendor/Product ID Configuration Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Methods to set and get the USB Vendor ID (VID) and Product ID (PID) for device identification. ```APIDOC ## setVid / setPid ### Description Set USB Vendor ID and Product ID. These identify the device to the host. ### Method Signature ```cpp void setVid(uint16_t value) void setPid(uint16_t value) ``` ### Parameters #### value - **value** (uint16_t) - Required - 16-bit VID/PID value ## getVid / getPid ### Description Returns the configured VID/PID. ### Method Signature ```cpp uint16_t getVid() uint16_t getPid() ``` ### Returns - **uint16_t** - VID/PID value ### Example ```cpp config.setVid(0xe502); config.setPid(0xbbab); ``` ``` -------------------------------- ### Telemetry Streaming with BleGamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md Shows how to stream telemetry data, such as sensor readings, over NUS while simultaneously using the gamepad functionality. This example reads an analog sensor and controls a button based on its value. ```cpp void loop() { if (bleGamepad.isConnected()) { BleNUS* nus = bleGamepad.getNUS(); // Read sensor and send over NUS int sensorValue = analogRead(A0); nus->print("Sensor: "); nus->println(sensorValue); // Also use gamepad if (sensorValue > 500) { bleGamepad.press(BUTTON_1); } else { bleGamepad.release(BUTTON_1); } delay(100); } } ``` -------------------------------- ### Analog Stick ESP32 BLE Gamepad Configuration Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/quick-start.md Set up a standard gamepad with analog sticks and buttons, including a D-pad. This example reads analog inputs and maps them to gamepad axes. ```cpp #include #include #define STICK_LEFT_X A0 #define STICK_LEFT_Y A1 #define STICK_RIGHT_X A2 #define STICK_RIGHT_Y A3 BleGamepad bleGamepad; BleGamepadConfiguration config; int16_t readStick(uint8_t pin) { int raw = analogRead(pin); // 0-4095 return map(raw, 0, 4095, 0, 32767); } void setup() { Serial.begin(115200); config.setButtonCount(16); config.setHatSwitchCount(1); // D-pad bleGamepad.begin(&config); } void loop() { if (bleGamepad.isConnected()) { // Read analog sticks int16_t leftX = readStick(STICK_LEFT_X); int16_t leftY = readStick(STICK_LEFT_Y); int16_t rightX = readStick(STICK_RIGHT_X); int16_t rightY = readStick(STICK_RIGHT_Y); // Apply deadzone if (abs(leftX) < 1000) leftX = 0; if (abs(leftY) < 1000) leftY = 0; if (abs(rightX) < 1000) rightX = 0; if (abs(rightY) < 1000) rightY = 0; // Set axes (order: x, y, z, rX, rY, rZ, slider1, slider2) bleGamepad.setAxes(leftX, leftY, rightX, 0, 0, rightY, 0, 0); // Handle buttons for (int i = 1; i <= 16; i++) { if (isButtonPressed(i)) { bleGamepad.press(BUTTON_1 + (i - 1)); } else { bleGamepad.release(BUTTON_1 + (i - 1)); } } delay(20); // 50Hz } } bool isButtonPressed(int buttonNum) { // Your button reading logic here return false; } ``` -------------------------------- ### Configure Special Buttons Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/configuration.md Enable or disable specific special buttons like Start, Select, Menu, Home, Back, Volume Up, Volume Down, and Volume Mute. These buttons use dedicated HID pages (Desktop or Consumer) and increase the HID report size. ```cpp config.setIncludeStart(true); config.setIncludeSelect(true); config.setIncludeMenu(false); // Or in one call: config.setWhichSpecialButtons(true, true, false, false, false, false, false, false); ``` -------------------------------- ### Using Custom Configuration with bleGamepad.begin() Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/TroubleshootingGuide.md Ensure you are using a custom configuration by calling `bleGamepad.begin()` with your configuration struct. This is crucial for custom settings to be applied correctly. ```cpp bleGamepad.begin(&bleGamepadConfig); ``` -------------------------------- ### Get Output Report Buffer Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-connection.md Returns a pointer to the output report buffer, which contains data from the host (e.g., rumble commands). Data is valid only when isOutputReceived() returns true. The buffer size can be configured. ```cpp uint8_t* getOutputBuffer() ``` ```cpp BleGamepadConfiguration config; config.setEnableOutputReport(true); config.setOutputReportLength(64); bleGamepad.begin(&config); // In loop: if (bleGamepad.isOutputReceived()) { uint8_t* buffer = bleGamepad.getOutputBuffer(); // Example: Rumble motor control uint8_t leftRumble = buffer[0]; // Left motor intensity uint8_t rightRumble = buffer[1]; // Right motor intensity // Drive actual motors... } ``` -------------------------------- ### Set GUID Version Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/configuration.md Set the GUID version, which represents the device version in the HID descriptor. This value is used for device identification. ```cpp config.setGuidVersion(0x0110); // Version 1.10 ``` -------------------------------- ### Get Device Manufacturer Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Retrieves the name of the manufacturer for the BLE device. ```cpp String getDeviceManufacturer() ``` -------------------------------- ### Get Device Name Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Retrieves the human-readable name of the BLE device. ```cpp String getDeviceName() ``` -------------------------------- ### Lifecycle Method: begin Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md Initializes the Nordic UART Service and registers characteristics. This method is automatically called by `BleGamepad::beginNUS()`. ```APIDOC ## Lifecycle Method: begin ### Signature ```cpp void begin() ``` ### Description Initializes the Nordic UART Service and registers characteristics. Called automatically by `BleGamepad::beginNUS()`. ### Request Example ```cpp bleGamepad.begin(); bleGamepad.beginNUS(); // Initializes NUS service ``` ``` -------------------------------- ### Get BLE Address Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Retrieves the unique Bluetooth Low Energy address of the device. ```cpp NimBLEAddress getAddress() ``` -------------------------------- ### Get Simulation Range Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Retrieve the current minimum and maximum values for simulation controls. ```cpp int16_t getSimulationMin() int16_t getSimulationMax() ``` -------------------------------- ### Get Configured Button Count Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Retrieves the currently configured number of buttons for the gamepad. ```cpp uint16_t getButtonCount() ``` -------------------------------- ### Get Peer Connection Info Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Fetches detailed connection information about the connected BLE peer. ```cpp NimBLEConnInfo getPeerInfo() ``` -------------------------------- ### Basic Serial-like Communication with BleGamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blenус.md Demonstrates setting up BleGamepad for basic serial-like communication using the NUS (Nordic UART Service). Includes sending periodic data and registering a callback for received data. ```cpp #include #include BleGamepad bleGamepad; void onNUSDataReceived(const uint8_t* data, size_t length) { Serial.print("Received from BLE: "); for (size_t i = 0; i < length; i++) { Serial.write(data[i]); } Serial.println(); } void setup() { Serial.begin(115200); bleGamepad.begin(); bleGamepad.beginNUS(); bleGamepad.setNUSDataReceivedCallback(onNUSDataReceived); } void loop() { if (bleGamepad.isConnected()) { BleNUS* nus = bleGamepad.getNUS(); // Send periodic data nus->println("Gamepad Status: Connected"); delay(1000); } } ``` -------------------------------- ### Get Motion Range Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Retrieve the current minimum and maximum values for gyroscope and accelerometer data. ```cpp int16_t getMotionMin() int16_t getMotionMax() ``` -------------------------------- ### Axis Range Configuration Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Methods to set and get the minimum and maximum values for all regular axes. ```APIDOC ## setAxesMin / setAxesMax ### Description Set the minimum and maximum values for all regular axes (X, Y, Z, Rx, Ry, Rz, Slider1, Slider2). ### Method Signature ```cpp void setAxesMin(int16_t value) void setAxesMax(int16_t value) ``` ### Parameters #### value - **value** (int16_t) - Required - Signed 16-bit value ### Common Ranges - `0x0000` (0) to `0x7FFF` (32767) — Non-negative range (default) - `0x8001` (-32767) to `0x7FFF` (32767) — Full signed range (not recommended on non-Windows systems) ## getAxesMin / getAxesMax ### Description Returns the configured minimum and maximum values for axes. ### Method Signature ```cpp int16_t getAxesMin() int16_t getAxesMax() ``` ### Example ```cpp // Full signed range for Windows compatibility config.setAxesMin(0x8001); // -32767 config.setAxesMax(0x7FFF); // 32767 // Or non-negative range config.setAxesMin(0x0000); // 0 config.setAxesMax(0x7FFF); // 32767 ``` ``` -------------------------------- ### BleGamepad Initialization (C++) Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/types.md Initializes the BleGamepad object with a device name and manufacturer string. This is a fundamental step for setting up the BLE gamepad. ```cpp BleGamepad bleGamepad("ESP32 Gamepad", "Manufacturer"); ``` -------------------------------- ### Hat Switch Configuration Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Methods to set and get the number of hat switches (D-pad) supported by the gamepad. ```APIDOC ## setHatSwitchCount ### Description Sets the number of hat switches (D-pad / directional inputs) to include (0-4). ### Method Signature ```cpp void setHatSwitchCount(uint8_t value) ``` ### Parameters #### value - **value** (uint8_t) - Required - Number of hat switches (0-4) ## getHatSwitchCount ### Description Returns the configured hat switch count. ### Method Signature ```cpp uint8_t getHatSwitchCount() ``` ### Returns - **uint8_t** - Number of hat switches ### Example ```cpp config.setHatSwitchCount(2); // Two D-pads ``` ``` -------------------------------- ### Get Enable Nordic UART Service Status Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Retrieves the current status of the Nordic UART Service (NUS). ```cpp bool getEnableNordicUARTService() ``` -------------------------------- ### Get Device Name Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-connection.md Retrieves the device name that was set during the BleGamepad constructor. This is the name broadcast by the BLE device. ```cpp String deviceName = bleGamepad.getDeviceName(); Serial.println(deviceName); ``` -------------------------------- ### BleGamepad Constructor Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Creates a new BleGamepad instance with optional custom device information. You can specify the device name, manufacturer, initial battery level, and whether to delay advertising. ```APIDOC ## BleGamepad Constructor ### Description Creates a new BleGamepad instance with optional custom device information. ### Signature ```cpp BleGamepad(std::string deviceName = "ESP32 BLE Gamepad", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100, bool delayAdvertising = false) ``` ### Parameters #### Parameters - **deviceName** (std::string) - Optional - Default: "ESP32 BLE Gamepad" - Bluetooth device name advertised to hosts - **deviceManufacturer** (std::string) - Optional - Default: "Espressif" - Manufacturer name for device information - **batteryLevel** (uint8_t) - Optional - Default: 100 - Initial battery percentage (0-100) - **delayAdvertising** (bool) - Optional - Default: false - Delay BLE advertising until begin() or enterPairingMode() is called ### Example ```cpp #include BleGamepad bleGamepad("My Gamepad", "My Company", 100); ``` ``` -------------------------------- ### Reduce Axes to Save Space Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/README.md Configure which axes are enabled to conserve BLE bandwidth. This example enables only the X and Y axes. ```cpp config.setWhichAxes(true, true, false, false, false, false, false, false); // Only X, Y ``` -------------------------------- ### Get Current BLE Transmit Power Level Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Retrieve the currently configured BLE transmit power level in dBm. ```cpp int8_t powerLevel = bleGamepad.getTXPowerLevel(); ``` -------------------------------- ### Instantiate BleGamepad Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Creates a new BleGamepad instance with optional custom device information like name, manufacturer, and initial battery level. The delayAdvertising parameter can be used to postpone BLE advertising. ```cpp #include BleGamepad bleGamepad("My Gamepad", "My Company", 100); ``` -------------------------------- ### Configure USB Vendor and Product IDs Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad-configuration.md Set the USB Vendor ID (VID) and Product ID (PID) to identify the device to the host. These are 16-bit values. ```cpp config.setVid(0xe502); config.setPid(0xbbab); ``` -------------------------------- ### Get Device Manufacturer Name Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-connection.md Retrieves the manufacturer name that was set during the BleGamepad constructor. This is useful for identifying the hardware manufacturer. ```cpp String mfg = bleGamepad.getDeviceManufacturer(); Serial.println(mfg); ``` -------------------------------- ### Get Device Bluetooth Address Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-connection.md Retrieves the device's own Bluetooth address as a NimBLEAddress object. This is useful for identifying the device. ```cpp NimBLEAddress addr = bleGamepad.getAddress(); ``` -------------------------------- ### Initialize Nordic UART Service Source: https://github.com/lemmingdev/esp32-ble-gamepad/blob/master/_autodocs/api-reference-blegamepad.md Call this after `begin()` to enable serial communication alongside gamepad functionality. ```cpp bleGamepad.begin(); bleGamepad.beginNUS(); // Enable serial communication alongside gamepad ```