forked from T-vK/ESP32-BLE-Mouse
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BleAbsMouse.cpp
171 lines (142 loc) · 5.84 KB
/
BleAbsMouse.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
#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 "BleAbsMouse.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
#define REPORTID_TOUCH 0x01
#define LSB(v) ((v >> 8) & 0xff)
#define MSB(v) (v & 0xff)
static const uint8_t _hidReportDescriptor[] = {
0x05, 0x0d, /* USAGE_PAGE (Digitizer) */
0x09, 0x04, /* USAGE (Touch Screen) */
0xa1, 0x01, /* COLLECTION (Application) */
0x85, REPORTID_TOUCH, /* REPORT_ID */
/* declare a finger collection */
0x09, 0x20, /* Usage (Stylus) */
0xA1, 0x00, /* Collection (Physical) */
/* Declare a finger touch (finger up/down) */
0x09, 0x42, /* Usage (Tip Switch) */
0x09, 0x32, /* USAGE (In Range) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x02, /* REPORT_COUNT (2) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
/* Declare the remaining 6 bits of the first data byte as constant -> the driver will ignore them */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x06, /* REPORT_COUNT (6) */
0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */
/* Define absolute X and Y coordinates of 16 bit each (percent values multiplied with 100) */
/* http://www.usb.org/developers/hidpage/Hut1_12v2.pdf */
/* Chapter 16.2 says: "In the Stylus collection a Pointer physical collection will contain the axes reported by the stylus." */
0x05, 0x01, /* Usage Page (Generic Desktop) */
0x09, 0x01, /* Usage (Pointer) */
0xA1, 0x00, /* Collection (Physical) */
0x09, 0x30, /* Usage (X) */
0x09, 0x31, /* Usage (Y) */
0x16, 0x00, 0x00, /* Logical Minimum (0) */
0x26, 0x10, 0x27, /* Logical Maximum (10000) */
0x36, 0x00, 0x00, /* Physical Minimum (0) */
0x46, 0x10, 0x27, /* Physical Maximum (10000) */
0x66, 0x00, 0x00, /* UNIT (None) */
0x75, 0x10, /* Report Size (16), */
0x95, 0x02, /* Report Count (2), */
0x81, 0x02, /* Input (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
0xc0, /* END_COLLECTION */
0xc0 /* END_COLLECTION */
// With this declaration a data packet must be sent as:
// byte 1 -> "touch" state (bit 0 = pen up/down, bit 1 = In Range)
// byte 2,3 -> absolute X coordinate (0...10000)
// byte 4,5 -> absolute Y coordinate (0...10000)
};
BleAbsMouse::BleAbsMouse(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->isPressed = false;
}
void BleAbsMouse::begin(void)
{
xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
}
void BleAbsMouse::end(void)
{
}
void BleAbsMouse::click(int16_t x, int16_t y)
{
this->move(x, y);
this->release();
}
void BleAbsMouse::move(int16_t x, int16_t y)
{
this->send(3, x, y);
this->isPressed = true;
}
void BleAbsMouse::release()
{
this->send(0, 0, 0);
this->isPressed = false;
}
void BleAbsMouse::send(int state, int16_t x, int16_t y)
{
if (this->isConnected())
{
uint8_t m[5];
m[0] = state;
m[1] = MSB(x);
m[2] = LSB(x);
m[3] = MSB(y);
m[4] = LSB(y);
this->inputAbsMouse->setValue(m, 5);
this->inputAbsMouse->notify();
}
}
bool BleAbsMouse::isConnected(void) {
return this->connectionStatus->connected;
}
void BleAbsMouse::setBatteryLevel(uint8_t level) {
this->batteryLevel = level;
if (hid != 0)
this->hid->setBatteryLevel(this->batteryLevel);
}
void BleAbsMouse::taskServer(void* pvParameter) {
BleAbsMouse* bleMouseInstance = (BleAbsMouse *) pvParameter; //static_cast<BleAbsMouse *>(pvParameter);
BLEDevice::init(bleMouseInstance->deviceName);
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(bleMouseInstance->connectionStatus);
bleMouseInstance->hid = new BLEHIDDevice(pServer);
bleMouseInstance->inputAbsMouse = bleMouseInstance->hid->inputReport(REPORTID_TOUCH); // <-- input REPORTID from report map
bleMouseInstance->connectionStatus->inputAbsMouse = bleMouseInstance->inputAbsMouse;
bleMouseInstance->hid->manufacturer()->setValue(bleMouseInstance->deviceManufacturer);
bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
bleMouseInstance->hid->hidInfo(0x00,0x02);
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
bleMouseInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
bleMouseInstance->hid->startServices();
bleMouseInstance->onStarted(pServer);
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->setAppearance(HID_MOUSE);
pAdvertising->addServiceUUID(bleMouseInstance->hid->hidService()->getUUID());
pAdvertising->start();
bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel);
ESP_LOGD(LOG_TAG, "Advertising started!");
vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY);
}