BLE-Gamepad-Client 0.3.2
Loading...
Searching...
No Matches
Adding support for a controller

To add support for a controller model that is not yet supported, you need to provide an instance of the BLEControllerAdapter struct and register it using BLEControllerRegistry::addControllerAdapter().

Below is a simplified example of an adapter. Its decoder reads the first two bytes from the payload array, scales them to the range -1.0f to 1.0f, and assigns them to the corresponding BLEControlsEvent members representing left stick deflection.

#include <Arduino.h>
#include <BLEController.h>
BLEController controller;
size_t decodeControls(BLEControlsEvent& e, uint8_t payload[], size_t payloadLen) {
if (payloadLen < 2) {
// Not enough data to decode
return 0;
}
e.leftStickX = (2.0f * payload[0]) / 255.0f - 1.0f;
e.leftStickY = (2.0f * payload[1]) / 255.0f - 1.0f;
// Return the number of bytes read; any non-zero value indicates success
return 2;
}
void setup(void) {
Serial.begin(115200);
myAdapter.controls.serviceUUID = NimBLEUUID((uint16_t)0x1812);
myAdapter.controls.decoder = decodeControls;
controller.begin();
}
void loop() {
if (controller.isConnected()) {
controller.readControls(e);
Serial.printf("lx: %.2f, ly: %.2f\n", e.leftStickX, e.leftStickY);
}
delay(100);
}
static bool addControllerAdapter(const BLEControllerAdapter &adapter)
Registers an adapter for a new controller type. Adapter is used to set up a connection and to decode ...
Definition BLEControllerRegistry.cpp:170
Definition BLEController.h:9
bool begin()
Initializes a controller instance and NimBLE stack if it's not already initialized.
Definition BLEController.cpp:22
bool isConnected() const
Is controller connected to the board.
Definition BLEController.cpp:54
void readControls(BLEControlsEvent &event) const
Read the controls state from the connected controller.
Definition BLEController.cpp:104
Definition BLEControllerAdapter.h:14
Definition BLEControlsEvent.h:5
float leftStickX
Left stick deflection along the X-axis. Takes values between -1.0 and 1.0. No deflection should yield...
Definition BLEControlsEvent.h:26
float leftStickY
Left stick deflection along the Y-axis. Takes values between -1.0 and 1.0. No deflection should yield...
Definition BLEControlsEvent.h:34

You can find a more advanced example in the repository: xbox.cpp