forked from happyhappysundays/SparkBox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSparkComms.ino
492 lines (413 loc) · 13.1 KB
/
SparkComms.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include "Spark.h"
#include "SparkComms.h"
const uint8_t notifyOn[] = {0x1, 0x0};
// client callback for connection to Spark
class MyClientCallback : public BLEClientCallbacks
{
void onConnect(BLEClient *pclient)
{
DEBUG("callback: Spark connected");
set_conn_status_connected(SPK);
//oled.displayOn(); //debug
}
void onDisconnect(BLEClient *pclient)
{
// if (pclient->isConnected()) {
connected_sp = false;
DEBUG("callback: Spark disconnected");
set_conn_status_disconnected(SPK);
}
#ifndef CLASSIC
bool onConnParamsUpdateRequest(BLEClient* pClient, const ble_gap_upd_params* params) {
DEBUG("Connection callback");
got_param_callback = true;
return true;
};
#endif
};
// server callback for connection to BLE app
class MyServerCallback : public BLEServerCallbacks
{
void onConnect(BLEServer *pserver)
{
if (pserver->getConnectedCount() == 1) {
set_conn_status_connected(APP);
DEBUG("callback: BLE app connected and is connected");
}
else {
DEBUG("callback: BLE app connected and is not really connected");
}
}
void onDisconnect(BLEServer *pserver)
{
// if (pserver->getConnectedCount() == 1) {
DEBUG("callback: BLE app disconnected");
set_conn_status_disconnected(APP);
}
};
// BLE MIDI
#ifdef BLE_APP_MIDI
class MyMIDIServerCallback : public BLEServerCallbacks
{
void onConnect(BLEServer *pserver)
{
//set_conn_status_connected(APP);
DEBUG("callback: BLE MIDI connected");
}
void onDisconnect(BLEServer *pserver)
{
DEBUG("callback: BLE MIDI disconnected");
//set_conn_status_disconnected(APP);
}
};
#endif
#ifdef CLASSIC
// server callback for connection to BT classic app
void bt_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
if(event == ESP_SPP_SRV_OPEN_EVT){
DEBUG("callback: Classic BT Spark app connected");
set_conn_status_connected(APP);
}
if(event == ESP_SPP_CLOSE_EVT ){
DEBUG("callback: Classic BT Spark app disconnected");
set_conn_status_disconnected(APP);
}
}
#endif
void notifyCB_sp(BLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
int i;
byte b;
for (i = 0; i < length; i++) {
b = pData[i];
ble_in.add(b);
}
ble_in.commit();
}
#ifdef BLE_CONTROLLER
// This works with IK Multimedia iRig Blueboard and the Akai LPD8 wireless - interestingly they have the same UUIDs
void notifyCB_pedal(BLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify){
int i;
byte b;
for (i = 0; i < length; i++) {
b = pData[i];
midi_in.add(b);
}
midi_in.commit();
set_conn_received(BLE_MIDI);
}
#endif
class CharacteristicCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic* pCharacteristic) {
int j, l;
const char *p;
byte b;
l = pCharacteristic->getValue().length();
p = pCharacteristic->getValue().c_str();
for (j=0; j < l; j++) {
b = p[j];
ble_app_in.add(b);
}
ble_app_in.commit();
};
};
static CharacteristicCallbacks chrCallbacks_s, chrCallbacks_r;
// BLE APP MIDI
#ifdef BLE_APP_MIDI
class MIDICharacteristicCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic* pCharacteristic) {
int j, l;
const char *p;
byte b;
l = pCharacteristic->getValue().length();
p = pCharacteristic->getValue().c_str();
for (j=0; j < l; j++) {
b = p[j];
ble_midi_in.add(b);
}
ble_midi_in.commit();
};
};
static MIDICharacteristicCallbacks chrCallbacksMIDI;
#endif
BLEUUID SpServiceUuid(C_SERVICE);
#ifdef BLE_CONTROLLER
BLEUUID PedalServiceUuid(PEDAL_SERVICE);
#endif
void connect_spark() {
if (found_sp && !connected_sp) {
if (pClient_sp != nullptr && pClient_sp->isConnected())
DEBUG("HMMMM - connect_spark() SAYS I WAS CONNECTED ANYWAY");
if (pClient_sp->connect(sp_device)) {
#if defined CLASSIC && !defined HELTEC_WIFI
pClient_sp->setMTU(517);
#endif
connected_sp = true;
pService_sp = pClient_sp->getService(SpServiceUuid);
if (pService_sp != nullptr) {
pSender_sp = pService_sp->getCharacteristic(C_CHAR1);
pReceiver_sp = pService_sp->getCharacteristic(C_CHAR2);
if (pReceiver_sp && pReceiver_sp->canNotify()) {
#ifdef CLASSIC
pReceiver_sp->registerForNotify(notifyCB_sp);
p2902_sp = pReceiver_sp->getDescriptor(BLEUUID((uint16_t)0x2902));
if (p2902_sp != nullptr)
p2902_sp->writeValue((uint8_t*)notifyOn, 2, true);
#else
if (!pReceiver_sp->subscribe(true, notifyCB_sp, true)) {
connected_sp = false;
DEBUG("Spark disconnected");
NimBLEDevice::deleteClient(pClient_sp);
}
#endif
}
}
DEBUG("connect_spark(): Spark connected");
}
}
}
#ifdef BLE_CONTROLLER
void connect_pedal() {
if (found_pedal && !connected_pedal) {
if (pClient_pedal->connect(pedal_device)) {
#if defined CLASSIC && !defined HELTEC_WIFI
pClient_sp->setMTU(517);
#endif
connected_pedal = true;
pService_pedal = pClient_pedal->getService(PedalServiceUuid);
if (pService_pedal != nullptr) {
pReceiver_pedal = pService_pedal->getCharacteristic(PEDAL_CHAR);
if (pReceiver_pedal && pReceiver_pedal->canNotify()) {
#ifdef CLASSIC
pReceiver_pedal->registerForNotify(notifyCB_pedal);
p2902_pedal = pReceiver_pedal->getDescriptor(BLEUUID((uint16_t)0x2902));
if(p2902_pedal != nullptr)
p2902_pedal->writeValue((uint8_t*)notifyOn, 2, true);
#else
if (!pReceiver_pedal->subscribe(true, notifyCB_pedal, true)) {
connected_pedal = false;
DEBUG("Pedal disconnected");
NimBLEDevice::deleteClient(pClient_pedal);
}
#endif
}
}
DEBUG("connect_pedal(): pedal connected");
set_conn_status_connected(BLE_MIDI);
}
}
}
#endif
bool connect_to_all() {
int i, j;
int counts;
uint8_t b;
unsigned long t;
// set up connection status tracking array
t = millis();
for (i = 0; i < NUM_CONNS; i++) {
conn_status[i] = false;
for (j = 0; j < 3; j++)
conn_last_changed[j][i] = t;
}
is_ble = true;
got_param_callback = false;
BLEDevice::init(SPARK_BT_NAME);
pClient_sp = BLEDevice::createClient();
pClient_sp->setClientCallbacks(new MyClientCallback());
BLEDevice::setMTU(517);
#ifdef BLE_CONTROLLER
pClient_pedal = BLEDevice::createClient();
#endif
pScan = BLEDevice::getScan();
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallback());
pService = pServer->createService(S_SERVICE);
#ifdef CLASSIC
pCharacteristic_receive = pService->createCharacteristic(S_CHAR1, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
pCharacteristic_send = pService->createCharacteristic(S_CHAR2, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
#else
pCharacteristic_receive = pService->createCharacteristic(S_CHAR1, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR);
pCharacteristic_send = pService->createCharacteristic(S_CHAR2, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
#endif
pCharacteristic_receive->setCallbacks(&chrCallbacks_r);
pCharacteristic_send->setCallbacks(&chrCallbacks_s);
#ifdef CLASSIC
pCharacteristic_send->addDescriptor(new BLE2902());
#endif
#ifdef BLE_APP_MIDI
// pServerMIDI = BLEDevice::createServer();
// pServerMIDI->setCallbacks(new MyMIDIServerCallback());
pServiceMIDI = pServer->createService(MIDI_SERVICE);
#ifdef CLASSIC
pCharacteristicMIDI = pServiceMIDI->createCharacteristic(MIDI_CHAR, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_NOTIFY);
#else
pCharacteristicMIDI = pServiceMIDI->createCharacteristic(MIDI_CHAR, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::NOTIFY);
#endif
pCharacteristicMIDI->setCallbacks(&chrCallbacksMIDI);
#ifdef CLASSIC
pCharacteristicMIDI->addDescriptor(new BLE2902());
#endif
#endif
pService->start();
#ifdef BLE_APP_MIDI
pServiceMIDI->start();
#endif
#ifndef CLASSIC
pServer->start();
#endif
pAdvertising = BLEDevice::getAdvertising(); // create advertising instance
pAdvertising->addServiceUUID(pService->getUUID()); // tell advertising the UUID of our service
#ifdef BLE_APP_MIDI
pAdvertising->addServiceUUID(pServiceMIDI->getUUID()); // tell advertising the UUID of our service
#endif
pAdvertising->setScanResponse(true);
// Connect to Spark
connected_sp = false;
found_sp = false;
#ifdef BLE_CONTROLLER
connected_pedal = false;
found_pedal = false;
#endif
DEBUG("Scanning...");
counts = 0;
while (!found_sp && counts < MAX_SCAN_COUNT) { // assume we only use a pedal if on already and hopefully found at same time as Spark, don't wait for it
counts++;
pResults = pScan->start(4);
for(i = 0; i < pResults.getCount() && !found_sp; i++) {
device = pResults.getDevice(i);
if (device.isAdvertisingService(SpServiceUuid)) {
DEBUG("Found Spark");
found_sp = true;
connected_sp = false;
sp_device = new BLEAdvertisedDevice(device);
}
#ifdef BLE_CONTROLLER
if (device.isAdvertisingService(PedalServiceUuid) || strcmp(device.getName().c_str(),"iRig BlueBoard") == 0) {
DEBUG("Found pedal");
found_pedal = true;
connected_pedal = false;
pedal_device = new BLEAdvertisedDevice(device);
}
#endif
}
}
if (!found_sp) return false; // failed to find the Spark within the number of counts allowed (MAX_SCAN_COUNT)
// Set up client
connect_spark();
#ifdef BLE_CONTROLLER
connect_pedal();
#endif
#ifdef CLASSIC
DEBUG("Starting classic bluetooth");
// now advertise Serial Bluetooth
bt = new BluetoothSerial();
bt->register_callback(bt_callback);
if (!bt->begin (SPARK_BT_NAME)) {
DEBUG("Classic bluetooth init fail");
while (true);
}
// flush anything read from App - just in case
while (bt->available())
b = bt->read();
DEBUG("Spark 40 Audio set up");
#endif
DEBUG("Available for app to connect...");
pAdvertising->start();
return true;
}
// app_available both returns whether any data is available but also selects which type of bluetooth to use based
// on whether there is any input there
bool app_available() {
if (!ble_app_in.is_empty()) {
is_ble = true;
return true;
}
#ifdef CLASSIC
if (bt->available()) {
is_ble = false;
return true;
}
#endif
// if neither have input, then there definitely is no input
return false;
}
uint8_t app_read() {
set_conn_received(APP);
if (is_ble) {
uint8_t b;
ble_app_in.get(&b);
return b;
}
#ifdef CLASSIC
else {
return bt->read();
}
#endif
// Never gets here
return(0);
}
void app_write(byte *buf, int len) {
set_conn_sent(APP);
if (is_ble) {
pCharacteristic_send->setValue(buf, len);
pCharacteristic_send->notify(true);
}
#ifdef CLASSIC
else {
bt->write(buf, len);
}
#endif
}
void app_write_timed(byte *buf, int len) { // same as app_write but with a slight delay for classic bluetooth - it seems to need it
set_conn_sent(APP);
if (is_ble) {
pCharacteristic_send->setValue(buf, len);
pCharacteristic_send->notify(true);
}
#ifdef CLASSIC
else {
bt->write(buf, len);
delay(50); // this helps the timing of a 'fake' store hardware preset
}
#endif
}
bool sp_available() {
return !ble_in.is_empty();
}
uint8_t sp_read() {
uint8_t b;
set_conn_received(SPK);
ble_in.get(&b);
return b;
}
void sp_write(byte *buf, int len) {
set_conn_sent(SPK);
pSender_sp->writeValue(buf, len, false);
}
// for some reason getRssi() crashes with two clients!
int ble_getRSSI() {
#ifdef BLE_CONTROLLER
return 0;
#else
return pClient_sp->getRssi();
#endif
}
// Code to enable UI changes
void set_conn_received(int connection) {
conn_last_changed[FROM][connection] = millis();
}
void set_conn_sent(int connection) {
conn_last_changed[TO][connection] = millis();
}
void set_conn_status_connected(int connection) {
if (conn_status[connection] == false) {
conn_status[connection] = true;
conn_last_changed[STATUS][connection] = millis();
}
}
void set_conn_status_disconnected(int connection) {
if (conn_status[connection] == true) {
conn_status[connection] = false;
conn_last_changed[STATUS][connection] = millis();
}
}