-
Notifications
You must be signed in to change notification settings - Fork 2
/
BLE_HID_test.ino
175 lines (153 loc) · 6.13 KB
/
BLE_HID_test.ino
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
/*
This code makes esp32 as TouchScreen
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
Ported to Arduino ESP32 by Evandro Copercini
updates by chegewara
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "BLE2902.h"
#include "BLEHIDDevice.h"
#include "HIDTypes.h"
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define LSB(_x) ((_x) & 0xFF)
#define MSB(_x) ((_x) >> 8)
BLEHIDDevice* hid;
BLECharacteristic* input;
BLECharacteristic* output;
bool connected = false;
int screenX0 = 0;
int screenY0 = 0;
int screenX1 = 1000;
int screenY1 = 1000;
int logicalMinX = 0;
int logicalMinY = 0;
int logicalMaxX = 32767;
int logicalMaxY = 32767;
int logicalX, logicalY;
//uint8_t _switch = 1;
class MyCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
connected = true;
BLE2902* desc = (BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(true);
}
void onDisconnect(BLEServer* pServer) {
connected = false;
BLE2902* desc = (BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
desc->setNotifications(false);
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("TestTestAiueo");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyCallbacks());
hid = new BLEHIDDevice(pServer);
input = hid->inputReport(4); // <-- input REPORTID from report map
std::string name = "ElectronicAiueo";
hid->manufacturer()->setValue(name);
hid->pnp(0x01, 0x0430, 0x0508, 0x00); //pnp(uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version)
hid->hidInfo(0x00, 0x01); //hidInfo(uint8_t country, uint8_t flags)
BLESecurity *pSecurity = new BLESecurity();
// pSecurity->setKeySize();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
const uint8_t report[] = {
0x05, 0x0d, // USAGE_PAGE (Digitizers)
0x09, 0x04, // USAGE (Touch Screen)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x04, // REPORT_ID (4)
0x09, 0x22, // USAGE (Finger)
0xa1, 0x00, // COLLECTION (Physical)
0x09, 0x42, // USAGE (Tip Switch)
0x09, 0x51, // USAGE (Contact Identifier) *added line
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)
0x95, 0x0e, // REPORT_COUNT (14)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x75, 0x10, // REPORT_SIZE (16)
0x95, 0x01, // REPORT_COUNT (1)
0x55, 0x0d, // UNIT_EXPONENT (-3)
0x65, 0x33, // UNIT (Inch,EngLinear)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x7f, // LOGICAL_MAXIMUM (32767)
0x09, 0x30, // USAGE (X)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x09, 0x31, // USAGE (Y)
0x81, 0x02, // INPUT (Data,Var,Abs)
0xc0, // END_COLLECTION
0xc0, // END_COLLECTION
};
hid->reportMap((uint8_t*)report, sizeof(report));
hid->startServices();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World says Neil");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->setAppearance(HID_TABLET);
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
}
void loop() {
// put your main code here, to run repeatedly:
if (connected) {
if (Serial.available() > 0) {
String func;
int x;
int y;
// int c;
func = Serial.readStringUntil(';');
x = func.substring(0, 4).toInt();
y = func.substring(4, 8).toInt();
// c = func.substring(8).toInt();
Serial.println(x);
Serial.println(y);
// Serial.println(c);
if (screenX0 < x and x <= screenX1) {
if (screenY0 < y and y <= screenY1) {
send(x, y, 1);
delay(10);
send(x, y, 0);
// if (c==0 or c==1){
// send(x, y, c);
// }
} else {
Serial.println("y-value: out of range");
}
} else {
Serial.println("x-value: out of range");
}
}
}
}
void send(int paramX, int paramY, int _switch) {
logicalX = map(paramX, screenX0, screenX1, logicalMinX, logicalMaxX);
logicalY = map(paramY, screenY0, screenY1, logicalMinY, logicalMaxY);
uint8_t m[6];
m[0] = _switch | _switch;// m[0] = 0x10 | _switch;
m[1] = 0;
m[2] = LSB(logicalX);
m[3] = MSB(logicalX);
m[4] = LSB(logicalY);
m[5] = MSB(logicalY);
input->setValue(m, 6);
input->notify();
}