-
Notifications
You must be signed in to change notification settings - Fork 7
/
BLE.ino
224 lines (191 loc) · 4.48 KB
/
BLE.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
void start_BLE(bool reset) {
ble.begin(DEBUG); //verbose on-off
if (reset) {
/* Initialise the module */
if(DEBUG){
Serial.print(F("Initialising the Bluefruit LE module: "));
Serial.println(F("Performing a factory reset: "));
}
ble.factoryReset();
//Disable command echo from Bluefruit
ble.echo(false);
ble.info();
//rename device
ble.println("AT+GAPDEVNAME=BLE_WASD");
/* Enable HID Service */
if(DEBUG){
Serial.println(F("Enable HID Service (including Keyboard): "));
}
if (! ble.sendCommandCheckOK(F( "AT+BleKeyboardEn=On" ))) {
if(DEBUG){
Serial.println(F("Could not enable Keyboard"));
}
}
/* Add or remove service requires a reset */
if (! ble.reset() ) {
if(DEBUG){
Serial.println(F("Couldn't reset??"));
}
}
}
}
void send_key(uint8_t c, uint8_t is_long_key, uint8_t no_release) {
uint8_t hid_key;
if (is_long_key) {
hid_key = PS2Long_to_HID_keymap[c];
}
else {
hid_key = PS2_to_HID_keymap[c];
}
//peek and intercept keys before they get sent for special functions
bool send_key = special_functions(hid_key, is_long_key);
//convert to command
String key = hex_to_str(hid_key);
String mod = hex_to_str(modifiers);
String cmd = "AT+BLEKEYBOARDCODE=" + mod + "-00-" + key + "-00-00-00-00";
if(send_key){
//send bluetooth
ble.println(cmd);
if (!no_release) {
ble.println("AT+BLEKEYBOARDCODE=00-00");
}
if(DEBUG){
Serial.println("hid key: " + key);
}
}
}
void release_key() {
ble.println("AT+BLEKEYBOARDCODE=00-00");
}
uint8_t is_modifier(uint8_t c, uint8_t is_long_key) {
if (is_long_key) {
switch (c) {
case (PS2_LEFT_GUI - 0xE000):
case (PS2_RIGHT_CTRL - 0xE000):
case (PS2_RIGHT_ALT - 0xE000):
case (PS2_RIGHT_GUI - 0xE000):
return 1;
break;
}
}
else {
switch (c) {
case PS2_LEFT_CTRL:
case PS2_LEFT_SHIFT:
case PS2_LEFT_ALT:
case PS2_RIGHT_SHIFT:
return 1;
break;
}
}
return 0;
}
uint8_t is_media(uint8_t c) {
switch (c) {
case (PS2_PLAY_PAUSE - 0xE000):
case (PS2_STOP - 0xE000):
case (PS2_FWD_TRACK - 0xE000):
case (PS2_REV_TRACK - 0xE000):
case (PS2_VOL_UP - 0xE000):
case (PS2_VOL_DWN - 0xE000):
case (PS2_MUTE - 0xE000):
return 1;
break;
}
return 0;
}
void send_media(uint8_t c) {
String str;
switch (c) {
case (PS2_PLAY_PAUSE - 0xE000):
str = "PLAYPAUSE";
break;
case (PS2_STOP - 0xE000):
str = "MEDIASTOP";
break;
case (PS2_FWD_TRACK - 0xE000):
str = "MEDIANEXT";
break;
case (PS2_REV_TRACK - 0xE000):
str = "MEDIAPREVIOUS";
break;
case (PS2_VOL_UP - 0xE000):
str = "VOLUME+,100";
break;
case (PS2_VOL_DWN - 0xE000):
str = "VOLUME-,100";
break;
case (PS2_MUTE - 0xE000):
str = "MUTE";
break;
}
ble.println("AT+BLEHIDCONTROLKEY=" + str);
}
void set_modifier(uint8_t c, uint8_t is_long_key) {
if (is_long_key) {
switch (c) {
case (PS2_LEFT_GUI - 0xE000):
modifiers |= 1 << 3;
break;
case (PS2_RIGHT_CTRL - 0xE000):
modifiers |= 1 << 4;
break;
case (PS2_RIGHT_ALT - 0xE000):
modifiers |= 1 << 6;
break;
case (PS2_RIGHT_GUI - 0xE000):
modifiers |= 1 << 7;
break;
}
}
else {
switch (c) {
case PS2_LEFT_CTRL:
modifiers |= 1 << 0;
break;
case PS2_LEFT_SHIFT:
modifiers |= 1 << 1;
break;
case PS2_LEFT_ALT:
modifiers |= 1 << 2;
break;
case PS2_RIGHT_SHIFT:
modifiers |= 1 << 5;
break;
}
}
}
void unset_modifier(uint8_t c, uint8_t is_long_key) {
if (is_long_key) {
switch (c) {
case (PS2_LEFT_GUI - 0xE000):
modifiers &= ~(1 << 3);
break;
case (PS2_RIGHT_CTRL - 0xE000):
modifiers &= ~(1 << 4);
break;
case (PS2_RIGHT_ALT - 0xE000):
modifiers &= ~(1 << 6);
break;
case (PS2_RIGHT_GUI - 0xE000):
modifiers &= ~(1 << 7);
break;
}
}
else {
switch (c) {
case PS2_LEFT_CTRL:
modifiers &= ~(1 << 0);
break;
case PS2_LEFT_SHIFT:
modifiers &= ~(1 << 1);
break;
case PS2_LEFT_ALT:
modifiers &= ~(1 << 2);
break;
case PS2_RIGHT_SHIFT:
modifiers &= ~(1 << 5);
break;
}
}
}