-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patharduino_nRF52840.ino
1206 lines (1042 loc) · 27.9 KB
/
arduino_nRF52840.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
This file was forked from https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino
Its quite different now as we have added connection commands, mouse commands and other bits
Work is copyright Ace Centre 2020 - MIT Licence. If you go and use this in a commercial project - thats great. But we'd appreciate even an email thanks :)
Note: What follows is the original Adafruit comment code.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <bluefruit.h>
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
/*
* Implement the Bluefruit command AT+BLEKEYBOARDCODE command.
* Nothing else useful. Useful for testing a Python program designed to work
* with a BF LE UART Friend. Lots of this code is from an Adafruit example.
*/
//#define DEBUG // uncomment this line to see additional prints in Serial monitor
#define BLE_NAME "RelayKeys"
#define ADD_NEW_DEV_PROCESS_TIMEOUT 30000 // in millseconds
#define SWAP_CONN_PROCESS_TIMEOUT 30000 // in millseconds
// The following defines which pin for the User button to go into BLE mode.
// NB: using this neat trick: https://electronics.stackexchange.com/a/280379
#if defined(_VARIANT_ITSY52840_)
//Adafruit itsybitsy
#define USER_SW 4
#include <Adafruit_DotStar.h>
Adafruit_DotStar statusLED(1, 8, 6, DOTSTAR_BGR);
#elif defined(_VARIANT_FEATHER52840_)
//Adafruit feather nrf52840
#define USER_SW 7
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel statusLED = Adafruit_NeoPixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
#else
#ifdef DEBUG
Serial.print("Unsupported hardware. Possibly not critical unless you want to initiate BLE mode with a button");
#endif
// lazy - but going to pretend its a itsybitsy. Its the one with a wierd board name.
#define USER_SW 4
#endif
BLEDis bledis;
BLEHidAdafruit blehid;
BLEUart bleuart;
volatile uint32_t addDevProsStartTicks = 0;
volatile uint8_t flag_addDevProsStarted = 0;
char bleDeviceNameList[15][32] = {0};
volatile uint8_t bleDeviceNameListIndex = 0;
volatile uint8_t flag_bleSwapConnProsStarted = 0;
volatile uint32_t swapConnProsStartTicks = 0;
volatile uint8_t maxBleDevListSize = 3;
volatile uint8_t switchBleConnStartIndex = 0;
volatile uint8_t switchBleConnCurrIndex = 0;
///
using namespace Adafruit_LittleFS_Namespace;
#define FILENAME "/devNameList.txt"
#define MODE_FILENAME "/config.txt"
File file(InternalFS);
volatile bool flag_saveListToFile = false;
///
uint8_t max_prph_connection;
uint8_t connection_count;
bool ble_mode = false;
uint16_t target_ble_conn = 0;
uint16_t response_ble_conn = 0;
void set_keyboard_led(uint8_t led_bitmap);
void updateStatusLED() {
if(flag_addDevProsStarted) {
statusLED.setPixelColor(0, 16, 16, 0); // set yellow color
}else if(ble_mode) {
statusLED.setPixelColor(0, 0, 0, 16); // set blue color
} else {
statusLED.setPixelColor(0, 0, 16, 0); // set green color
}
statusLED.show();
}
uint8_t detect_click() {
uint32_t press_time = millis();
uint8_t click_counter = 0;
while((millis() - press_time) < 500) {
if(digitalRead(USER_SW) == false && (millis() - press_time) < 500) {
click_counter++;
delay(100);
while(digitalRead(USER_SW) == false) {
delay(100); // wait until button released
}
press_time = millis();
}
}
return click_counter;
}
void save_devList_toFile(void)
{
file.open(FILENAME, FILE_O_WRITE);
// file existed
if (file)
{
#ifdef DEBUG
Serial.print("List has ");
Serial.print(bleDeviceNameListIndex);
Serial.println(" devices");
for (int i = 0; i < bleDeviceNameListIndex; i++)
{
Serial.print(i);
Serial.print(". ");
Serial.print(bleDeviceNameList[i]);
Serial.println();
}
#endif
// file.truncate(file.size());
file.seek(0);
file.write((const uint8_t *)bleDeviceNameList, sizeof(bleDeviceNameList));
file.write((const uint8_t *)&bleDeviceNameListIndex, 1);
#ifdef DEBUG
Serial.print("Saving Device List to file ");
Serial.print(file.size());
Serial.println();
#endif
file.close();
}
else
{
#ifdef DEBUG
Serial.print(FILENAME " Write Failed");
#endif
}
}
void load_devList_fromFile(void)
{
file.open(FILENAME, FILE_O_READ);
// file existed
if (file)
{
#ifdef DEBUG
Serial.print("Loading Device List from file ");
Serial.print(file.size());
Serial.println();
#endif
bleDeviceNameListIndex = 0;
memset(bleDeviceNameList, 0, sizeof(bleDeviceNameList));
file.read((void *)bleDeviceNameList, sizeof(bleDeviceNameList));
file.read((void *)&bleDeviceNameListIndex, 1);
if(bleDeviceNameListIndex > 15){
bleDeviceNameListIndex = 0;
}
#ifdef DEBUG
Serial.print("List has ");
Serial.print(bleDeviceNameListIndex);
Serial.println(" devices");
for (int i = 0; i < bleDeviceNameListIndex; i++)
{
Serial.print(i);
Serial.print(". ");
Serial.print(bleDeviceNameList[i]);
Serial.println();
}
#endif
file.close();
}
else
{
#ifdef DEBUG
Serial.print(FILENAME " Read Failed");
#endif
}
}
void load_mode_file(){
file.open(MODE_FILENAME, FILE_O_READ);
// file existed
if (file)
{
file.read((void *)&ble_mode, 1);
file.close();
} else {
#ifdef DEBUG
Serial.println(MODE_FILENAME " Read Failed");
#endif
}
}
void change_mode() {
#ifdef DEBUG
Serial.print("Changing operating mode");
#endif
ble_mode = !ble_mode;
file.open(MODE_FILENAME, FILE_O_WRITE);
if(file) {
file.seek(0);
file.write((const uint8_t *)&ble_mode, 1);
file.close();
} else {
#ifdef DEBUG
Serial.print(MODE_FILENAME " Write Failed");
#endif
}
BLEConnection *connection = NULL;
for(int i=0;i<max_prph_connection;i++) {
connection = Bluefruit.Connection(i);
connection->disconnect();
}
delay(1000);
NVIC_SystemReset();
}
void setup()
{
Serial.begin(115200);
//while ( !Serial && millis() < 2000) delay(10); // for nrf52840 with native usb
// Initialize Internal File System
InternalFS.begin();
load_mode_file();
pinMode(USER_SW, INPUT_PULLUP);
statusLED.begin();
updateStatusLED();
if(ble_mode) {
max_prph_connection = 2;
} else {
max_prph_connection = 1;
}
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin(max_prph_connection, 0);
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.setName(BLE_NAME);
Bluefruit.Periph.setConnectCallback(bleConnectCallback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
// Configure and Start Device Information Service
bledis.setManufacturer("Ace Centre");
bledis.setModel("RelayKeysv1");
bledis.begin();
// Configure and Start BLE Uart Service
bleuart.begin();
bleuart.setRxCallback(prph_bleuart_rx_callback);
/* Start BLE HID
* Note: Apple requires BLE device must have min connection interval >= 20m
* ( The smaller the connection interval the faster we could send data).
* However for HID and MIDI device, Apple could accept min connection interval
* up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max
* connection interval to 11.25 ms and 15 ms respectively for best performance.
*/
blehid.begin();
// Set callback for set LED from central
blehid.setKeyboardLedCallback(set_keyboard_led);
/* Set connection interval (min, max) to your perferred value.
* Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms
* min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms
*/
/* Bluefruit.setConnInterval(9, 12); */
// Set up and start advertising
startAdv();
//
load_devList_fromFile();
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD);
// Include BLE UART service
//Bluefruit.Advertising.addService(bleuart);
// Include BLE HID service
Bluefruit.Advertising.addService(blehid);
// There is enough room for the dev name in the advertising packet
Bluefruit.Advertising.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
#ifdef DEBUG
Serial.println("RelayKeys UP & Running");
#endif
}
typedef void (*action_func_t)(char *myLine);
typedef struct
{
char command[32 + 1]; // max 32 characters plus '\0' terminator
action_func_t action;
} command_action_t;
// force lower case
void toLower(char *s)
{
while (*s)
{
if (isupper(*s))
{
*s += 0x20;
}
s++;
}
}
void deleteDevList(char *myLine)
{
at_response("at+deletedevlist\n");
InternalFS.remove(FILENAME);
bleDeviceNameListIndex = 0;
memset(bleDeviceNameList, 0, sizeof(bleDeviceNameList));
BLEConnection *connection = NULL;
for(int i=0;i<max_prph_connection;i++) {
connection = Bluefruit.Connection(i);
connection->disconnect();
}
at_response("SUCCESS.\n");
}
void sendBLEMouseMove(char *line)
{
char buff[256];
err_t ret = 1;
int32_t x = 0;
int32_t y = 0;
int32_t wy = 0;
int32_t wx = 0;
// expected input, X,Y,WY,WX
char *p = strtok(line, "=");
p = strtok(NULL, ",");
for (size_t i = 0; p != NULL; i++)
{
if (i == 0)
{ // X
x = strtol(p, NULL, 10);
}
else if (i == 1)
{ // Y
y = strtol(p, NULL, 10);
}
else if (i == 2)
{
wy = strtol(p, NULL, 10);
}
else if (i == 3)
{
wx = strtol(p, NULL, 10);
}
else
{
// Invalid input
at_response("INVALID_INPUT\n");
return;
}
p = strtok(NULL, ",");
}
if (x != 0 || y != 0)
{
ret = blehid.mouseMove(target_ble_conn, x, y);
}
if ((int)ret != 1)
{
snprintf(buff, sizeof(buff), "ERROR %d\n", (int)ret);
at_response(buff);
}
else
{
if (wy != 0)
{
ret = blehid.mouseScroll(target_ble_conn, wy);
}
if ((int)ret != 1)
{
snprintf(buff, sizeof(buff), "ERROR %d\n", (int)ret);
at_response(buff);
return;
}
if (wx != 0)
{
ret = blehid.mousePan(target_ble_conn, wx);
}
if ((int)ret != 1)
{
snprintf(buff, sizeof(buff), "ERROR %d\n", (int)ret);
at_response(buff);
return;
}
at_response("OK\n");
}
}
typedef struct
{
char thechar;
uint8_t button;
} mouse_button_map_t;
const mouse_button_map_t mouse_buttons_map[] = {
{'l', MOUSE_BUTTON_LEFT},
{'r', MOUSE_BUTTON_RIGHT},
{'m', MOUSE_BUTTON_MIDDLE},
{'b', MOUSE_BUTTON_BACKWARD},
{'f', MOUSE_BUTTON_FORWARD},
{'0', 0},
};
void sendBLEMouseButton(char *line)
{
char buff[256];
err_t ret;
int b = 0;
int mode = 0;
// expected input, Button[,Action]
char *p = strtok(line, "=");
p = strtok(NULL, ",");
for (size_t i = 0; p != NULL; i++)
{
if (i == 0)
{ // Button
for (int c = 0; c < sizeof(mouse_buttons_map) / sizeof(mouse_buttons_map[0]);
c++)
{
if (mouse_buttons_map[c].thechar == p[0])
{
b = mouse_buttons_map[c].button;
break;
}
}
}
else if (i == 1)
{ // Action
toLower(p);
if (strcmp(p, "click") == 0)
{
mode = 1;
}
else if (strcmp(p, "doubleclick") == 0)
{
mode = 2;
}
// PRESS/HOLD and no action all are PRESS action
}
else
{
// Invalid input
at_response("INVALID_INPUT\n");
return;
}
p = strtok(NULL, ",");
}
if (b != 0 && mode == 1)
{ // CLICK
ret = blehid.mouseButtonPress(target_ble_conn, b);
if (ret == 1)
{
delay(40);
ret = blehid.mouseButtonRelease(target_ble_conn);
}
}
else if (b != 0 && mode == 2)
{ // DOUBLECLICK
ret = blehid.mouseButtonPress(target_ble_conn, b);
if (ret == 1)
{
delay(40);
ret = blehid.mouseButtonRelease(target_ble_conn);
}
if (ret == 1)
{
ret = blehid.mouseButtonPress(target_ble_conn, b);
}
if (ret == 1)
{
delay(40);
ret = blehid.mouseButtonRelease(target_ble_conn);
}
}
else
{ // PRESS/RELEASE
if (b == 0)
{
ret = blehid.mouseButtonRelease(target_ble_conn);
}
else
{
ret = blehid.mouseButtonPress(target_ble_conn, b);
}
}
if ((int)ret != 1)
{
snprintf(buff, sizeof(buff), "ERROR %d\n", (int)ret);
at_response(buff);
}
else
{
at_response("OK\n");
}
}
void sendBLEKeyboardCode(char *myLine)
{
char buff[256];
err_t ret;
uint8_t keys[8]; // USB keyboard HID report
memset(keys, 0, sizeof(keys));
// myLine example: "00-00-04-00-00-00-00-00"
char *p = strtok(myLine, "=");
p = strtok(NULL, "-");
for (size_t i = 0; p && (i < sizeof(keys)); i++)
{
keys[i] = strtoul(p, NULL, 16);
p = strtok(NULL, "-");
}
ret = blehid.keyboardReport(target_ble_conn, keys[0], &keys[2]);
if ((int)ret != 1)
{
snprintf(buff, sizeof(buff), "ERROR %d\n", (int)ret);
at_response(buff);
}
else
{
at_response("OK\n");
}
}
void sendBleSendCurrentDeviceName(char *myLine)
{
char buff[256];
at_response("at+blecurrentdevicename\n");
if (!Bluefruit.connected())
{
at_response("NONE\n");
}
else
{
BLEConnection *connection = NULL;
char bleDeviceName[32] = {0};
connection = Bluefruit.Connection(target_ble_conn);
connection->getPeerName(bleDeviceName, sizeof(bleDeviceName));
if (strlen(bleDeviceName) == 0)
{
at_response("NONE\n");
}else {
snprintf(buff, sizeof(buff), "%s\n", bleDeviceName);
at_response(buff);
}
}
}
void addNewBleDevice(char *myLine)
{
char buff[256];
at_response("at+bleaddnewdevice\n");
if (bleDeviceNameListIndex >= maxBleDevListSize)
{
at_response("ERROR: Device list is full\n");
}
else
{
BLEConnection *connection = NULL;
char bleDeviceName[32] = {0};
connection = Bluefruit.Connection(target_ble_conn);
connection->getPeerName(bleDeviceName, sizeof(bleDeviceName));
delay(500);
connection->disconnect();
//Serial.println("Disconnected from " + String(bleDeviceName));
flag_addDevProsStarted = 1;
updateStatusLED();
addDevProsStartTicks = millis();
snprintf(buff, sizeof(buff), "Connect your device with %s\n", BLE_NAME);
at_response(buff);
}
}
void removeBleDevice(char *myLine)
{
at_response("at+bleremovedevice\n");
char tempName[32] = {0};
char tempNameIndex = 0;
char flag_start = 0;
char i = 0;
BLEConnection *connection = NULL;
char bleDeviceName[32] = {0};
char centralBleDeviceName[32] = {0};
if (strlen(myLine) == 0)
{
at_response("ERROR: Syntax\n");
return;
}
connection = Bluefruit.Connection(target_ble_conn);
connection->getPeerName(bleDeviceName, sizeof(bleDeviceName));
for (char i = 0; i < strlen(myLine); i++)
{
if (myLine[i] == '"')
{
flag_start = 1;
}
else
{
if (flag_start)
{
if (myLine[i] == '"')
{
flag_start = 0;
break;
}
else
{
tempName[tempNameIndex++] = myLine[i];
}
}
}
}
if (strlen(tempName) == 0)
{
at_response("ERROR: Syntax\n");
return;
}
//Serial.println("Remove Device: " + String(tempName));
if (!strcmp((char *)tempName, (char *)bleDeviceName))
{
connection->disconnect();
}
if(ble_mode) {
connection = Bluefruit.Connection(response_ble_conn);
connection->getPeerName(centralBleDeviceName, sizeof(centralBleDeviceName));
if (!strcmp((char *)tempName, (char *)centralBleDeviceName))
{
at_response("ERROR: Can't remove central device\n");
return;
}
}
flag_start = 0;
for (i = 0; i < maxBleDevListSize; i++)
{
if (!strcmp((char *)tempName, (char *)bleDeviceNameList[i]))
{
//Serial.println("Device found in list - " + String(tempName));
at_response("SUCCESS\n");
flag_start = 1;
bleDeviceNameListIndex--;
flag_saveListToFile = true;
}
if (flag_start)
{
if (i < (maxBleDevListSize - 1))
{
memset(bleDeviceNameList[i], NULL, sizeof(bleDeviceNameList[i]));
strcpy(bleDeviceNameList[i], bleDeviceNameList[i + 1]);
}
else
{
memset(bleDeviceNameList[maxBleDevListSize - 1], NULL, sizeof(bleDeviceNameList[maxBleDevListSize - 1]));
}
}
}
if (!flag_start)
{
at_response("ERROR: Name not found in the list\n");
}
}
void switchBleConnection(char *myLine)
{
char buff[256];
at_response("at+switchconn\n");
if (!Bluefruit.connected())
{
switchBleConnStartIndex = 1;
//Serial.println("Current Dev Index: " + String(switchBleConnStartIndex));
switchBleConnCurrIndex = 1;
flag_bleSwapConnProsStarted = 1;
swapConnProsStartTicks = millis();
snprintf(buff, sizeof(buff), "Trying to connect with - %s\n", bleDeviceNameList[switchBleConnCurrIndex - 1]);
at_response(buff);
}
else
{
BLEConnection *connection = NULL;
char tempBleDevName[32] = {0};
connection = Bluefruit.Connection(target_ble_conn);
connection->getPeerName(tempBleDevName, sizeof(tempBleDevName));
//Serial.println("Current Dev Name: " + String(tempBleDevName));
for (char i = 0; i < maxBleDevListSize; i++)
{
if (!strcmp((char *)tempBleDevName, (char *)bleDeviceNameList[i]))
{
switchBleConnStartIndex = i + 1;
//Serial.println("Current Dev Index: " + String(switchBleConnStartIndex));
if (bleDeviceNameListIndex == 1)
{
at_response("ERROR: No other device present in list\n");
break;
}
else
{
connection->disconnect();
}
if (switchBleConnStartIndex >= bleDeviceNameListIndex)
{
switchBleConnCurrIndex = 1;
}
else
{
switchBleConnCurrIndex = switchBleConnStartIndex + 1;
}
flag_bleSwapConnProsStarted = 1;
swapConnProsStartTicks = millis();
snprintf(buff, sizeof(buff), "Trying to connect with - %s\n", bleDeviceNameList[switchBleConnCurrIndex - 1]);
at_response(buff);
break;
}
}
}
}
void printBleDevList(char *myLine)
{
char buff[256];
at_response("at+printdevlist\n");
for (char j = 0; j < bleDeviceNameListIndex; j++)
{
snprintf(buff, sizeof(buff), "%d:%s\n", (j+1), bleDeviceNameList[j]);
at_response(buff);
}
//Serial.println("Index:" + String(bleDeviceNameListIndex));
}
void setBleMaxDevListSize(char *myLine)
{
at_response("at+blemaxdevlistsize\n");
char *p = strtok(myLine, "=");
p = strtok(NULL, "\0");
uint8_t tempNum = atoi(p);
if (tempNum > 15 || tempNum < 1)
{
maxBleDevListSize = 3;
at_response("ERROR: Invalid Value\n");
}
else
{
maxBleDevListSize = tempNum;
at_response("SUCCESS\n");
}
}
const command_action_t commands[] = {
// Name of command user types, function that implements the command.
// TODO add other commands, some day
{"at+blekeyboardcode", sendBLEKeyboardCode},
{"at+blehidmousemove", sendBLEMouseMove},
{"at+blehidmousebutton", sendBLEMouseButton},
{"at+blecurrentdevicename", sendBleSendCurrentDeviceName},
{"at+bleaddnewdevice", addNewBleDevice},
{"at+bleremovedevice", removeBleDevice},
{"at+switchconn", switchBleConnection},
{"at+printdevlist", printBleDevList},
{"at+blemaxdevlistsize", setBleMaxDevListSize},
{"at+resetdevlist", deleteDevList}
};
void update_connections(uint16_t conn_handle){
target_ble_conn = 1;
response_ble_conn = 0;
if(conn_handle == 1) {
target_ble_conn = 0;
response_ble_conn = 1;
}
}
void execute(uint16_t conn_handle, char *myLine)
{
if (myLine == NULL || *myLine == '\0')
return;
char cmdTemp[100];
memcpy(cmdTemp, myLine, 100);
char *cmd = strtok(cmdTemp, "=");
if (cmd == NULL || *cmd == '\0')
return;
toLower(cmd);
if(ble_mode) {
update_connections(conn_handle);
}
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++)
{
if (strcmp(cmd, commands[i].command) == 0)
{
commands[i].action(myLine); //strtok(NULL, "=")
if(!ble_mode) {
delay(30);
}
return;
}
}
// Command not found so just send OK. Should send ERROR at some point.
at_response("OK\n");
}
void receive_char(uint16_t conn_handle, char receive_char)
{
static uint8_t bytesIn;
static char receive_buffer[256];
switch (receive_char)
{
case '\n':
break;
case '\r':
receive_buffer[bytesIn] = '\0';
execute(conn_handle, receive_buffer);
bytesIn = 0;
case '\b': // backspace
if (bytesIn > 0)
{
bytesIn--;
}
break;
default:
receive_buffer[bytesIn++] = receive_char;
if (bytesIn >= sizeof(receive_buffer) - 1)
{
receive_buffer[bytesIn] = '\0';
execute(conn_handle, receive_buffer);
bytesIn = 0;
}
break;
}
}
void at_response(char *msg) {
int len = strlen(msg);
if(ble_mode) {
bleuart.write(response_ble_conn, (const uint8_t*)msg, len);
} else {
Serial.write(msg, len);
}
}
void prph_bleuart_rx_callback(uint16_t conn_handle)
{
if(ble_mode) {
while (bleuart.available())
{
receive_char(conn_handle, bleuart.read());
}
}
}
void loop()
{
if(!ble_mode) {
if(Serial.available() > 0) {
receive_char(0, Serial.read());
}
}
if(digitalRead(USER_SW) == false) {
if(detect_click() == 1){
addNewBleDevice("");
} else {
change_mode();
}
}
if (flag_saveListToFile)
{
flag_saveListToFile = false;
save_devList_toFile();
}
if (flag_addDevProsStarted)
{
if (millis() - addDevProsStartTicks >= ADD_NEW_DEV_PROCESS_TIMEOUT)
{
flag_addDevProsStarted = 0;
updateStatusLED();
#ifdef DEBUG
Serial.println("ERROR: Timeout");
#endif
}
}
if (flag_bleSwapConnProsStarted == 1)
{
if (millis() - swapConnProsStartTicks >= SWAP_CONN_PROCESS_TIMEOUT)
{
#ifdef DEBUG
Serial.println("ERROR: Timeout");
#endif
switchBleConnCurrIndex++;
if (switchBleConnCurrIndex > bleDeviceNameListIndex)