3 #if defined(PLATFORM_ESP32)
6 #include "crsf_protocol.h"
11 #include "devButton.h"
13 #include <BleGamepad.h>
14 #include <NimBLEDevice.h>
16 class ELRSGamepad
: public BleGamepad
{
18 ELRSGamepad() : BleGamepad("ExpressLRS Joystick", "ELRS", 100) {};
21 void onStarted(NimBLEServer
*pServer
) {
22 NimBLEDevice::setPower(ESP_PWR_LVL_P9
);
26 static ELRSGamepad
*bleGamepad
;
28 void BluetoothJoystickUpdateValues()
30 if (bleGamepad
->isConnected())
32 // map first 8 channels to axis
34 for (uint8_t i
= 0; i
< 8; i
++)
36 data
[i
] = map(ChannelData
[i
], CRSF_CHANNEL_VALUE_MIN
, CRSF_CHANNEL_VALUE_MAX
, 0, 32767);
38 bleGamepad
->setAxes(data
[0], data
[1], data
[4], data
[5], data
[2], data
[3], data
[6], data
[7]);
40 // map other 8 channels to buttons
41 for (uint8_t i
= 8; i
< 16; i
++)
43 if (ChannelData
[i
] >= CRSF_CHANNEL_VALUE_2000
) {
44 bleGamepad
->press(i
- 7);
46 bleGamepad
->release(i
- 7);
51 bleGamepad
->sendReport();
55 void BluetoothJoystickBegin()
57 // bleGamepad is null if it hasn't been started yet
58 if (bleGamepad
!= nullptr)
61 // construct the BLE immediately to prevent reentry from events/timeout
62 bleGamepad
= new ELRSGamepad();
64 POWERMGNT::setPower(MinPower
);
66 handset
->setRCDataCallback(BluetoothJoystickUpdateValues
);
68 BleGamepadConfiguration
*gamepadConfig
= new BleGamepadConfiguration();
69 gamepadConfig
->setAutoReport(false);
71 DBGLN("Starting BLE Joystick!");
72 bleGamepad
->begin(gamepadConfig
);
75 static void initialize()
77 registerButtonFunction(ACTION_BLE_JOYSTICK
, [](){
78 connectionState
= bleJoystick
;
84 BluetoothJoystickBegin();
85 return DURATION_NEVER
;
90 if (connectionState
== bleJoystick
) {
94 return DURATION_NEVER
;
97 device_t BLE_device
= {
98 .initialize
= initialize
,