Krolyxon
Commit 360d2d2

Branches

Tags

Refs →

orion-rffirmwareBleMouse.cpp

173 lines · 5.5 KB · C++

1#include <BLEDevice.h>
2#include <BLEUtils.h>
3#include <BLEServer.h>
4#include "BLE2902.h"
5#include "BLEHIDDevice.h"
6#include "HIDTypes.h"
7#include "HIDKeyboardTypes.h"
8#include <driver/adc.h>
9#include "sdkconfig.h"
10
11#include "BleConnectionStatus.h"
12#include "BleMouse.h"
13
14#if defined(CONFIG_ARDUHAL_ESP_LOG)
15 #include "esp32-hal-log.h"
16 #define LOG_TAG ""
17#else
18 #include "esp_log.h"
19 static const char* LOG_TAG = "BLEDevice";
20#endif
21
22static const uint8_t _hidReportDescriptor[] = {
23 USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
24 USAGE(1), 0x02, // USAGE (Mouse)
25 COLLECTION(1), 0x01, // COLLECTION (Application)
26 USAGE(1), 0x01, // USAGE (Pointer)
27 COLLECTION(1), 0x00, // COLLECTION (Physical)
28 // ------------------------------------------------- Buttons (Left, Right, Middle, Back, Forward)
29 USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button)
30 USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1)
31 USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5)
32 LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0)
33 LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1)
34 REPORT_SIZE(1), 0x01, // REPORT_SIZE (1)
35 REPORT_COUNT(1), 0x05, // REPORT_COUNT (5)
36 HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits
37 // ------------------------------------------------- Padding
38 REPORT_SIZE(1), 0x03, // REPORT_SIZE (3)
39 REPORT_COUNT(1), 0x01, // REPORT_COUNT (1)
40 HIDINPUT(1), 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding
41 // ------------------------------------------------- X/Y position, Wheel
42 USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop)
43 USAGE(1), 0x30, // USAGE (X)
44 USAGE(1), 0x31, // USAGE (Y)
45 USAGE(1), 0x38, // USAGE (Wheel)
46 LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127)
47 LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127)
48 REPORT_SIZE(1), 0x08, // REPORT_SIZE (8)
49 REPORT_COUNT(1), 0x03, // REPORT_COUNT (3)
50 HIDINPUT(1), 0x06, // INPUT (Data, Variable, Relative) ;3 bytes (X,Y,Wheel)
51 // ------------------------------------------------- Horizontal wheel
52 USAGE_PAGE(1), 0x0c, // USAGE PAGE (Consumer Devices)
53 USAGE(2), 0x38, 0x02, // USAGE (AC Pan)
54 LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127)
55 LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127)
56 REPORT_SIZE(1), 0x08, // REPORT_SIZE (8)
57 REPORT_COUNT(1), 0x01, // REPORT_COUNT (1)
58 HIDINPUT(1), 0x06, // INPUT (Data, Var, Rel)
59 END_COLLECTION(0), // END_COLLECTION
60 END_COLLECTION(0) // END_COLLECTION
61};
62
63BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) :
64 _buttons(0),
65 hid(0)
66{
67 this->deviceName = deviceName;
68 this->deviceManufacturer = deviceManufacturer;
69 this->batteryLevel = batteryLevel;
70 this->connectionStatus = new BleConnectionStatus();
71}
72
73void BleMouse::begin(void)
74{
75 xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
76}
77
78void BleMouse::end(void)
79{
80}
81
82void BleMouse::click(uint8_t b)
83{
84 _buttons = b;
85 move(0,0,0,0);
86 _buttons = 0;
87 move(0,0,0,0);
88}
89
90void BleMouse::move(signed char x, signed char y, signed char wheel, signed char hWheel)
91{
92 if (this->isConnected())
93 {
94 uint8_t m[5];
95 m[0] = _buttons;
96 m[1] = x;
97 m[2] = y;
98 m[3] = wheel;
99 m[4] = hWheel;
100 this->inputMouse->setValue(m, 5);
101 this->inputMouse->notify();
102 }
103}
104
105void BleMouse::buttons(uint8_t b)
106{
107 if (b != _buttons)
108 {
109 _buttons = b;
110 move(0,0,0,0);
111 }
112}
113
114void BleMouse::press(uint8_t b)
115{
116 buttons(_buttons | b);
117}
118
119void BleMouse::release(uint8_t b)
120{
121 buttons(_buttons & ~b);
122}
123
124bool BleMouse::isPressed(uint8_t b)
125{
126 if ((b & _buttons) > 0)
127 return true;
128 return false;
129}
130
131bool BleMouse::isConnected(void) {
132 return this->connectionStatus->connected;
133}
134
135void BleMouse::setBatteryLevel(uint8_t level) {
136 this->batteryLevel = level;
137 if (hid != 0)
138 this->hid->setBatteryLevel(this->batteryLevel);
139}
140
141void BleMouse::taskServer(void* pvParameter) {
142 BleMouse* bleMouseInstance = (BleMouse *) pvParameter; //static_cast<BleMouse *>(pvParameter);
143 BLEDevice::init(String(bleMouseInstance->deviceName.c_str()));
144 BLEServer *pServer = BLEDevice::createServer();
145 pServer->setCallbacks(bleMouseInstance->connectionStatus);
146
147 bleMouseInstance->hid = new BLEHIDDevice(pServer);
148 bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport(0); // <-- input REPORTID from report map
149 bleMouseInstance->connectionStatus->inputMouse = bleMouseInstance->inputMouse;
150
151 bleMouseInstance->hid->manufacturer()->setValue(String(bleMouseInstance->deviceManufacturer.c_str()));
152
153 bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
154 bleMouseInstance->hid->hidInfo(0x00,0x02);
155
156 BLESecurity *pSecurity = new BLESecurity();
157
158 pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
159
160 bleMouseInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
161 bleMouseInstance->hid->startServices();
162
163 bleMouseInstance->onStarted(pServer);
164
165 BLEAdvertising *pAdvertising = pServer->getAdvertising();
166 pAdvertising->setAppearance(HID_MOUSE);
167 pAdvertising->addServiceUUID(bleMouseInstance->hid->hidService()->getUUID());
168 pAdvertising->start();
169 bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel);
170
171 ESP_LOGD(LOG_TAG, "Advertising started!");
172 vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY);
173}