forked from lemmingDev/ESP32-BLE-Gamepad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBleGamepad.cpp
214 lines (187 loc) · 7.23 KB
/
BleGamepad.cpp
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
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "BLE2902.h"
#include "BLEHIDDevice.h"
#include "HIDTypes.h"
#include "HIDKeyboardTypes.h"
#include <driver/adc.h>
#include "sdkconfig.h"
#include "BleConnectionStatus.h"
#include "BleGamepad.h"
#if defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG ""
#else
#include "esp_log.h"
static const char* LOG_TAG = "BLEDevice";
#endif
static const uint8_t _hidReportDescriptor[] = {
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
USAGE(1), 0x05, // USAGE (Gamepad)
COLLECTION(1), 0x01, // COLLECTION (Application)
USAGE(1), 0x01, // USAGE (Pointer)
COLLECTION(1), 0x00, // COLLECTION (Physical)
REPORT_ID(1), 0x01, // REPORT_ID (1)
// ------------------------------------------------- Buttons (1 to 5)
USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button)
USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1)
USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5)
LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0)
LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1)
REPORT_SIZE(1), 0x01, // REPORT_SIZE (1)
REPORT_COUNT(1), 0x05, // REPORT_COUNT (5)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits
// ------------------------------------------------- Padding
REPORT_SIZE(1), 0x01, // REPORT_SIZE (1)
REPORT_COUNT(1), 0x03, // REPORT_COUNT (3)
HIDINPUT(1), 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding
// ------------------------------------------------- X/Y position
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
USAGE(1), 0x30, // USAGE (X)
USAGE(1), 0x31, // USAGE (Y)
LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127)
LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127)
REPORT_SIZE(1), 0x08, // REPORT_SIZE (8)
REPORT_COUNT(1), 0x02, // REPORT_COUNT (2)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;2 bytes (X,Y)
END_COLLECTION(0), // END_COLLECTION
END_COLLECTION(0), // END_COLLECTION
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
USAGE(1), 0x05, // USAGE (Gamepad)
COLLECTION(1), 0x01, // COLLECTION (Application)
USAGE(1), 0x01, // USAGE (Pointer)
COLLECTION(1), 0x00, // COLLECTION (Physical)
REPORT_ID(1), 0x02, // REPORT_ID (2)
// ------------------------------------------------- Buttons (1 to 5)
USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button)
USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1)
USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5)
LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0)
LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1)
REPORT_SIZE(1), 0x01, // REPORT_SIZE (1)
REPORT_COUNT(1), 0x05, // REPORT_COUNT (5)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits
// ------------------------------------------------- Padding
REPORT_SIZE(1), 0x01, // REPORT_SIZE (1)
REPORT_COUNT(1), 0x03, // REPORT_COUNT (3)
HIDINPUT(1), 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding
// ------------------------------------------------- X/Y position
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
USAGE(1), 0x30, // USAGE (X)
USAGE(1), 0x31, // USAGE (Y)
LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127)
LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127)
REPORT_SIZE(1), 0x08, // REPORT_SIZE (8)
REPORT_COUNT(1), 0x02, // REPORT_COUNT (2)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;2 bytes (X,Y)
END_COLLECTION(0), // END_COLLECTION
END_COLLECTION(0) // END_COLLECTION
};
BleGamepad::BleGamepad(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) :
_buttons(0),
hid(0)
{
this->deviceName = deviceName;
this->deviceManufacturer = deviceManufacturer;
this->batteryLevel = batteryLevel;
this->connectionStatus = new BleConnectionStatus();
}
void BleGamepad::begin(void)
{
xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
}
void BleGamepad::end(void)
{
}
void BleGamepad::setAxes(signed char x, signed char y)
{
if (this->isConnected())
{
uint8_t m[3];
m[0] = _buttons;
m[1] = x;
m[2] = y;
this->inputGamepad->setValue(m, sizeof(m));
this->inputGamepad->notify();
}
}
void BleGamepad::setStick2(signed char x, signed char y, uint8_t b )
{
if (this->isConnected())
{
uint8_t m[3];
m[0] = b;
m[1] = x;
m[2] = y;
this->inputGamepad2->setValue(m, sizeof(m));
this->inputGamepad2->notify();
}
}
void BleGamepad::setStick(signed char x, signed char y, uint8_t b )
{
if (this->isConnected())
{
uint8_t m[3];
m[0] = b;
m[1] = x;
m[2] = y;
this->inputGamepad->setValue(m, sizeof(m));
this->inputGamepad->notify();
}
}
void BleGamepad::buttons(uint16_t b)
{
if (b != _buttons)
{
_buttons = b;
setAxes(0, 0);
}
}
void BleGamepad::press(uint16_t b)
{
buttons(_buttons | b);
}
void BleGamepad::release(uint16_t b)
{
buttons(_buttons & ~b);
}
bool BleGamepad::isPressed(uint16_t b)
{
if ((b & _buttons) > 0)
return true;
return false;
}
bool BleGamepad::isConnected(void) {
return this->connectionStatus->connected;
}
void BleGamepad::setBatteryLevel(uint8_t level) {
this->batteryLevel = level;
if (hid != 0)
this->hid->setBatteryLevel(this->batteryLevel);
}
void BleGamepad::taskServer(void* pvParameter) {
BleGamepad* BleGamepadInstance = (BleGamepad *) pvParameter; //static_cast<BleGamepad *>(pvParameter);
BLEDevice::init(BleGamepadInstance->deviceName);
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(BleGamepadInstance->connectionStatus);
BleGamepadInstance->hid = new BLEHIDDevice(pServer);
BleGamepadInstance->inputGamepad = BleGamepadInstance->hid->inputReport(1); // <-- input REPORTID from report map
BleGamepadInstance->inputGamepad2 = BleGamepadInstance->hid->inputReport(2); // <-- input REPORTID from report map
BleGamepadInstance->connectionStatus->inputGamepad = BleGamepadInstance->inputGamepad;
BleGamepadInstance->connectionStatus->inputGamepad2 = BleGamepadInstance->inputGamepad2;
BleGamepadInstance->hid->manufacturer()->setValue(BleGamepadInstance->deviceManufacturer);
BleGamepadInstance->hid->pnp(0x02, 0x8282, 0x0132, 0x0106);
BleGamepadInstance->hid->hidInfo(0x00,0x01);
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
BleGamepadInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
BleGamepadInstance->hid->startServices();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->setAppearance(HID_GAMEPAD);
pAdvertising->addServiceUUID(BleGamepadInstance->hid->hidService()->getUUID());
pAdvertising->start();
BleGamepadInstance->hid->setBatteryLevel(BleGamepadInstance->batteryLevel);
ESP_LOGD(LOG_TAG, "Advertising started!");
vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY);
}