-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt2usb_bridge.ino
168 lines (114 loc) · 2.91 KB
/
bt2usb_bridge.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
#include <BluetoothHCI.h>
#include <BluetoothHIDMaster.h>
#include "Keyboard.h"
//#include "WiFiManager.h"
#define BT_USB_OFFSET 61
#define KEY_ASCII_OFFET 13
#define SCAN_NAME "HD2 Macropad"
#define DEVICE_NAME "BT2USB Keyoard"
//#define WIFI_PASSPHRASE "superearth"
BluetoothHIDMaster hid;
BluetoothHCI hci;
//WiFiManager captivePortal(DEVICE_NAME, WIFI_PASSPHRASE);
uint8_t modifierMap[128] = {};
void setupModifierMap() {
modifierMap[0xe0] = KEY_LEFT_CTRL;
}
void kb(void *cbdata, int key) {
bool state = (bool)cbdata;
int mapKey = key;
uint8_t modifierKey = modifierMap[key];
if (modifierKey) {
mapKey = modifierKey;
} else {
mapKey = key + BT_USB_OFFSET;
}
if (state) {
Keyboard.press(mapKey);
} else {
Keyboard.release(mapKey);
}
char keyChar = (key + KEY_ASCII_OFFET) + '0';
Serial.printf("Keyboard: %d %s = '%c'\n", key, state ? "DOWN" : "UP", state ? keyChar : ' ');
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
setupModifierMap();
Serial.begin();
delay(3000);
Keyboard.begin();
hid.onKeyDown(kb, (void *)true);
hid.onKeyUp(kb, (void *)false);
connectMacropad();
}
bool bleScanning = false;
bool bleRescan = false;
bool bleFound = false;
unsigned long lastScan = 0;
void connectMacropad(){
unsigned long now = millis();
if(bleFound || bleScanning || now - lastScan < 5000){
if(!bleFound && !hid.connected()){
bleRescan = true;
}
return;
}
bleScanning = true;
lastScan = now;
hid.end();
delay(1000);
hci.install();
hci.begin();
auto l = hci.scanBLE(BluetoothHCI::any_cod);
Serial.printf("%-8s | %-17s | %-4s | %s\n", "Class", "Address", "RSSI", "Name");
Serial.printf("%-8s | %-17s | %-4s | %s\n", "--------", "-----------------", "----", "----------------");
for (auto e : l) {
Serial.printf("%08lx | %17s | %4d | %s\n", e.deviceClass(), e.addressString(), e.rssi(), e.name());
String deviceName = e.name();
if(deviceName == SCAN_NAME){
bleFound = true;
bleRescan = false;
hci.scanFree();
hci.uninstall();
delay(1000);
hid.begin(true);
hid.connectBLE(e.address(), e.addressType());
Serial.println("Device connected");
bleScanning = false;
return;
}
}
Serial.println("No device found");
if(!hid.connected()){
bleRescan = true;
}
bleScanning = false;
}
void loop() {
if (BOOTSEL) {
while (BOOTSEL) {
delay(1);
}
Serial.println("Cleanup after reset");
bleFound = false;
digitalWrite(LED_BUILTIN, LOW);
hid.disconnect();
hid.clearPairing();
delay(1000);
Serial.println("Rescanning");
connectMacropad();
}
if(!hid.connected() || bleRescan){
connectMacropad();
}
digitalWrite(LED_BUILTIN, bleFound ? HIGH : LOW);
yield();
delay(1);
}
/*void setup1(){
bool success = captivePortal.autoConnect();
}
void loop1(){
yield();
delay(1);
}*/