forked from lemmingDev/ESP32-BLE-Gamepad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBleGamepad.cpp
206 lines (178 loc) · 6.73 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
#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 ""
#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 16)
USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button)
USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1)
USAGE_MAXIMUM(1), 0x10, // USAGE_MAXIMUM (Button 16)
LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0)
LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1)
REPORT_SIZE(1), 0x01, // REPORT_SIZE (1)
REPORT_COUNT(1), 0x10, // REPORT_COUNT (16)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;16 button bits
// ------------------------------------------------- Padding
// No padding needed
// ------------------------------------------------- X/Y position, Z/rZ position
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
USAGE(1), 0x30, // USAGE (X)
USAGE(1), 0x31, // USAGE (Y)
USAGE(1), 0x32, // USAGE (Z)
USAGE(1), 0x35, // USAGE (rZ)
LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127)
LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127)
REPORT_SIZE(1), 0x08, // REPORT_SIZE (8)
REPORT_COUNT(1), 0x04, // REPORT_COUNT (4)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;4 bytes (X,Y,Z,rZ)
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
USAGE(1), 0x33, // USAGE (rX) Left Trigger
USAGE(1), 0x34, // USAGE (rY) Right Trigger
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 rX, rY
USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
USAGE(1), 0x39, // USAGE (Hat switch)
USAGE(1), 0x39, // USAGE (Hat switch)
LOGICAL_MINIMUM(1), 0x01, // LOGICAL_MINIMUM (1)
LOGICAL_MAXIMUM(1), 0x08, // LOGICAL_MAXIMUM (8)
REPORT_SIZE(1), 0x04, // REPORT_SIZE (4)
REPORT_COUNT(1), 0x02, // REPORT_COUNT (2)
HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;1 byte Hat1, Hat2
END_COLLECTION(0), // END_COLLECTION
END_COLLECTION(0) // END_COLLECTION
};
// HID data bits size in bytes
#define HID_DATA_SIZE 9
BleGamepad::BleGamepad(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : hid(0)
{
this->deviceName = deviceName;
this->deviceManufacturer = deviceManufacturer;
this->batteryLevel = batteryLevel;
this->connectionStatus = new BleConnectionStatus();
this->_previousState = new uint8_t[HID_DATA_SIZE] {0};
this->_currentState = new uint8_t[HID_DATA_SIZE] {0};
this->autoNotify = true;
}
void BleGamepad::begin(void)
{
xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
}
void BleGamepad::end(void)
{
}
void BleGamepad::notify(void)
{
if (this->isConnected() && memcmp(_previousState, _currentState, HID_DATA_SIZE))
{
this->inputGamepad->setValue(_currentState, HID_DATA_SIZE);
this->inputGamepad->notify();
memcpy(_previousState, _currentState, HID_DATA_SIZE);
}
}
void BleGamepad::setAxes(signed char x, signed char y, signed char z, signed char rZ, char rX, char rY, signed char hat)
{
_currentState[2] = x;
_currentState[3] = y;
_currentState[4] = z;
_currentState[5] = rZ;
_currentState[6] = (signed char)(rX - 128);
_currentState[7] = (signed char)(rY - 128);
_currentState[8] = hat;
if (_currentState[6] == -128)
{
_currentState[6] = -127;
}
if (_currentState[7] == -128)
{
_currentState[7] = -127;
}
if(autoNotify)
notify();
}
void BleGamepad::setHat(uint8_t hat)
{
_currentState[8] = hat;
if(autoNotify)
notify();
}
void BleGamepad::press(uint16_t b)
{
uint16_t *buttons = (uint16_t *)_currentState;
*buttons = *buttons | b;
if(autoNotify)
notify();
}
void BleGamepad::release(uint16_t b)
{
uint16_t *buttons = (uint16_t *)_currentState;
*buttons = *buttons & ~b;
if(autoNotify)
notify();
}
bool BleGamepad::isPressed(uint16_t b)
{
uint16_t *buttons = (uint16_t *)_currentState;
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->connectionStatus->inputGamepad = BleGamepadInstance->inputGamepad;
BleGamepadInstance->hid->manufacturer()->setValue(BleGamepadInstance->deviceManufacturer);
BleGamepadInstance->hid->pnp(0x01, 0x02e5, 0xabcd, 0x0110);
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();
BleGamepadInstance->onStarted(pServer);
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);
}