-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBleHidController.cpp
More file actions
304 lines (254 loc) · 10 KB
/
BleHidController.cpp
File metadata and controls
304 lines (254 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "BleHidController.h"
//@formatter:off
// Combined report map (keyboard + gamepad)
static const uint8_t hidReportMapData[] = {
// **** Keyboard (Report ID 1) ****
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x06, // Usage (Keyboard)
0xA1, 0x01, // Collection (Application)
0x85, REPORT_ID_KEYBOARD, // Report ID
// Modifiers (Ctrl, Shift, Alt, Meta)
0x05, 0x07, // Usage Page (Keyboard)
0x19, 0xE0, // Usage Minimum (Left Control)
0x29, 0xE7, // Usage Maximum (Right Meta)
0x15, 0x00, // Logical Minimum 0
0x25, 0x01, // Logical Maximum 1
0x95, 0x08, // Report Count 8
0x75, 0x01, // Report Size 1 bit
0x81, 0x02, // Input (Data, Variable, Absolute)
// 6 simultaneous keys (keycode table)
0x05, 0x07, // Usage Page (Keyboard)
0x19, 0x00, // Usage Minimum (0 = No events)
0x29, 0xFF, // Usage Maximum (0xFF)
0x15, 0x00, // Logical Minimum 0
0x26, 0xFF, 0x00, // Logical Maximum 0x00FF
0x95, 0x06, // Report Count 6
0x75, 0x08, // Report Size 8 bits
0x81, 0x00, // Input (Data, Array, Absolute)
// End Collection
0xC0,
// **** Gamepad (Report ID 2) ****
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x05, // Usage (Gamepad)
0xA1, 0x01, // Collection (Application)
0x85, REPORT_ID_GAMEPAD, // Report ID
// 16 buttons
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (Button 1)
0x29, 0x10, // Usage Maximum (Button 16)
0x15, 0x00, // Logical Minimum 0
0x25, 0x01, // Logical Maximum 1
0x75, 0x01, // Report Size 1 bit
0x95, 0x10, // Report Count 16
0x81, 0x02, // Input (Data, Variable, Absolute)
// D-Pad (hat switch)
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x39, // Usage (Hat switch)
0x15, 0x00, // Logical Min 0
0x25, 0x07, // Logical Max 7 (8 directions)
0x75, 0x04, // Report Size 4 bits
0x95, 0x01, // Report Count 1
0x81, 0x42, // Input (Data, Variable, Absolute, Null state)
0x75, 0x04, // Report Size 4 bits (padding)
0x95, 0x01, // Report Count 1
0x81, 0x03, // Input (Constant, Variable, Absolute)
// Left stick: X, Y
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x30, // Usage X
0x09, 0x31, // Usage Y
0x16, 0x00, 0x80, // Logical Min -32768
0x26, 0xFF, 0x7F, // Logical Max 32767
0x75, 0x10, // Report Size 16 bits
0x95, 0x02, // Report Count 2
0x81, 0x02, // Input (Data, Variable, Absolute)
// Right stick: Rx, Ry
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x33, // Usage Rx
0x09, 0x34, // Usage Ry
0x16, 0x00, 0x80, // Logical Min -32768
0x26, 0xFF, 0x7F, // Logical Max 32767
0x75, 0x10, // Report Size 16 bits
0x95, 0x02, // Report Count 2
0x81, 0x02, // Input (Data, Variable, Absolute)
// Triggers LT/RT: Z, Rz
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x32, // Usage Z (LT)
0x09, 0x35, // Usage Rz (RT)
0x15, 0x00, // Logical Min -0
0x26, 0xFF, 0x03, // Logical Max 1023
0x75, 0x10, // Report Size 16 bits
0x95, 0x02, // Report Count 2
0x81, 0x02, // Input (Data, Variable, Absolute)
// End Collection
0xC0
};
//@formatter:on
bool BleHidController::_deviceConnected = false;
// Internal callbacks
class BleHidController::ServerCallbacks : public NimBLEServerCallbacks
{
void onConnect(NimBLEServer* pSrv, NimBLEConnInfo& connInfo) override {
_deviceConnected = true;
pSrv->updateConnParams(connInfo.getConnHandle(), 6, 7, 0, 600);
}
void onDisconnect(NimBLEServer* pSrv, NimBLEConnInfo& connInfo, int reason) override {
_deviceConnected = false;
NimBLEDevice::startAdvertising();
}
};
// Class implementation
BleHidController::BleHidController() = default;
void BleHidController::begin(const char* deviceName, const char* deviceManufacturer, const uint16_t vendorId, const uint16_t productId, const uint16_t version) {
if (_hidDevice != nullptr) {
return; // Already initialized
}
NimBLEDevice::init(deviceName);
NimBLEDevice::setPower(BLE_TX_POWER);
NimBLEDevice::setSecurityAuth(BLE_SM_PAIR_AUTHREQ_BOND);
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT);
_server = NimBLEDevice::createServer();
static ServerCallbacks serverCb;
_server->setCallbacks(&serverCb);
_server->advertiseOnDisconnect(false);
_hidDevice = new NimBLEHIDDevice(_server);
_hidDevice->setManufacturer(deviceManufacturer);
_hidDevice->setPnp(PNP_VENDOR_SRC_USB, vendorId, productId, version);
_hidDevice->setHidInfo(0x00, 0x01);
_hidDevice->setBatteryLevel(BATTERY_LEVEL);
_hidDevice->setReportMap(const_cast<uint8_t*>(hidReportMapData), sizeof(hidReportMapData));
_kbInputReport = _hidDevice->getInputReport(REPORT_ID_KEYBOARD);
_gpInputReport = _hidDevice->getInputReport(REPORT_ID_GAMEPAD);
// _hidDevice->startServices(); // Deprecated: Services are now started by the server when start()
NimBLEAdvertising* adv = NimBLEDevice::getAdvertising();
adv->setName(deviceName);
adv->addServiceUUID(_hidDevice->getHidService()->getUUID());
adv->addServiceUUID(_hidDevice->getBatteryService()->getUUID());
adv->setAppearance(GENERIC_HID); // !HERE Meta Quest 3 doesn't seem to accept HID_GAMEPAD
adv->enableScanResponse(true);
adv->start();
}
// ***************************************************************
// KEYBOARD API
// ***************************************************************
void BleHidController::sendKeyboardState() {
_kbInputReport->setValue(reinterpret_cast<uint8_t*>(&_kbState), sizeof(_kbState));
(void)_kbInputReport->notify();
}
/**
* Add modifier bit and send report
*
* @param modifier
*/
void BleHidController::keyModPress(const uint8_t modifier) {
if (!_deviceConnected || _kbInputReport == nullptr) return;
_kbState.modifiers |= modifier;
sendKeyboardState();
}
/**
* Remove modifier bit and send report
*
* @param modifier
*/
void BleHidController::keyModRelease(const uint8_t modifier) {
if (!_deviceConnected || _kbInputReport == nullptr) return;
_kbState.modifiers &= ~modifier;
sendKeyboardState();
}
/**
* Add keycode to the first available slot in the keys array and send report
*
* @param keycode
*/
void BleHidController::keyPress(const uint8_t keycode) {
if (keycode == KEY_NONE || !_deviceConnected || _kbInputReport == nullptr) return;
// Verify if key already exists
for (const unsigned char key : _kbState.keys) {
if (key == keycode) {
return;
}
}
for (unsigned char& key : _kbState.keys) {
if (key == 0x00) {
key = keycode;
break;
}
}
sendKeyboardState();
}
/**
* Remove keycode from the keys array and send report
*
* @param keycode
*/
void BleHidController::keyRelease(const uint8_t keycode) {
if (!_deviceConnected || _kbInputReport == nullptr) return;
// Remove the keycode from the keys array
for (unsigned char& key : _kbState.keys) {
if (key == keycode) {
key = 0x00;
break;
}
}
sendKeyboardState();
}
/**
* Clear all active key presses and modifier states and send report
*/
void BleHidController::keyReleaseAll() {
if (!_deviceConnected || _kbInputReport == nullptr) return;
_kbState = KeyReport{};
sendKeyboardState();
}
// ***************************************************************
// GAMEPAD API
// ***************************************************************
void BleHidController::sendGamepadState() {
if (!_deviceConnected || _gpInputReport == nullptr) return;
_gpInputReport->setValue(reinterpret_cast<uint8_t*>(&_gpState), sizeof(_gpState));
(void)_gpInputReport->notify();
}
void BleHidController::buttonPress(const uint16_t button) {
_gpState.buttons |= button;
sendGamepadState();
}
void BleHidController::buttonRelease(const uint16_t button) {
_gpState.buttons &= ~button;
sendGamepadState();
}
void BleHidController::dpadPress(const uint8_t dpad) {
_gpState.dpad = dpad;
sendGamepadState();
}
void BleHidController::dpadRelease() {
_gpState.dpad = DPAD_CENTERED;
sendGamepadState();
}
void BleHidController::setLeftStick(const int16_t lx, const int16_t ly, const bool sendState) {
_gpState.leftX = lx;
_gpState.leftY = ly;
if (sendState) sendGamepadState();
}
void BleHidController::setRightStick(const int16_t rx, const int16_t ry, const bool sendState) {
_gpState.rightX = rx;
_gpState.rightY = ry;
if (sendState) sendGamepadState();
}
void BleHidController::setLeftTrigger(const uint16_t lt, const bool sendState) {
_gpState.lt = lt;
if (sendState) sendGamepadState();
}
void BleHidController::setRightTrigger(const uint16_t rt, const bool sendState) {
_gpState.rt = rt;
if (sendState) sendGamepadState();
}
void BleHidController::sendGamepad(uint16_t const buttons, const uint8_t dpad, const int16_t lx, const int16_t ly, const int16_t rx, const int16_t ry, const uint16_t lt, const uint16_t rt) {
_gpState.buttons = buttons;
_gpState.dpad = dpad & 0x0F;
_gpState.leftX = lx;
_gpState.leftY = ly;
_gpState.rightX = rx;
_gpState.rightY = ry;
_gpState.lt = lt;
_gpState.rt = rt;
sendGamepadState();
}