Krolyxon
Commit a90ac8e

Branches

Tags

Refs →

orion-rflibBleMouseBleMouse.cpp

165 lines · 5.7 KB · C++

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