-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble.h
151 lines (125 loc) · 3.79 KB
/
ble.h
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
/****
* Routines for BLE
*/
/**
* Send output to BLE
*/
void btprint(const char *str) {
if (ECHO == true) {
Serial.print("TX: ");
Serial.println(str);
}
if (BT_CONNECTED == true) {
Serial1.print(str);
}
}
/**
* Send output to BLE
*/
void btprintln(const char *str) {
if (ECHO == true) {
Serial.print("TX: ");
Serial.println(str);
}
if (BT_CONNECTED == true) {
Serial1.print(str);
Serial1.print("\n");
}
}
void btprint(const __FlashStringHelper *fmt, ... ) {
char buf[1025]; // resulting string limited to 1M chars
va_list args;
va_start (args, fmt);
#ifdef __AVR__
vsnprintf_P(buf, sizeof(buf), (const char *)fmt, args); // progmem for AVR
#else
vsnprintf(buf, sizeof(buf), (const char *)fmt, args); // for the rest of the world
#endif
va_end(args);
if (ECHO == true) {
Serial.print(buf);
}
Serial1.print(buf);
}
/**
* Shortcut to send output to in JSON format
*/
void sendToApp(const char *cmd, const char *value)
{
if (value[0] == '[') {
btprint(F("{\"cmd\":\"%s\",\"data\":%s}\n"), cmd, value);
} else {
btprint(F("{\"cmd\":\"%s\",\"data\":\"%s\"}\n"), cmd, value);
}
}
/**
* Sends config via Bluetooth Serial. Used for TKTalkie App
*/
void sendConfig()
{
debug(F("Sending config\n"));
btprint(F("{\"cmd\":\"config\", \"data\": { \"ver\":\"%s\","), VERSION);
// get sound files
char files[MAX_FILE_COUNT][SETTING_ENTRY_MAX];
int count = listFiles(SOUNDS_DIR, files, MAX_FILE_COUNT, SOUND_EXT, false, false);
// Add sound files
char buffer[1024];
char *sounds = arrayToStringJson(buffer, files, count);
btprint(F("\"sounds\":%s,"), sounds);
memset(buffer, 0, sizeof(buffer));
// Clear array
memset(files, 0, sizeof(files));
// Add effects
char *effects = arrayToStringJson(buffer, SOUND_EFFECTS, SOUND_EFFECTS_COUNT);
btprint(F("\"effects\":%s,"), effects);
memset(buffer, 0, sizeof(buffer));
// get loop files
count = listFiles(LOOP_DIR, files, MAX_FILE_COUNT, SOUND_EXT, false, false);
// Add loops
char *loops = arrayToStringJson(buffer, files, count);
btprint(F("\"loops\":%s,"), loops);
memset(buffer, 0, sizeof(buffer));
// Clear array
memset(files, 0, sizeof(files));
char *profile = getSettingValue(buffer, "profile");
btprint(F("\"default\":\"%s\","), profile);
memset(buffer, 0, sizeof(buffer));
btprint(F("\"current\": {\"name\":\"%s\",\"desc\":\"%s\"},"), PROFILE_FILE, PROFILE_NAME);
// This is already formatted correctly
char *json = settingsToJson(buffer);
btprint(F("%s,"), json);
memset(buffer, 0, sizeof(buffer));
btprint(F("\"profiles\":["));
// get config profile files
count = listFiles(PROFILES_DIR, files, MAX_FILE_COUNT, FILE_EXT, false, false);
for (int i = 0; i < count; i++) {
char entries[MAX_FILE_COUNT][SETTING_ENTRY_MAX];
char filename[SETTING_ENTRY_MAX];
strcpy(filename, PROFILES_DIR);
strcat(filename, files[i]);
int total = loadSettingsFile(filename, entries, MAX_FILE_COUNT);
for (int x = 0; x < total; x++) {
char *key, *value, *ptr;
char entry[SETTING_ENTRY_MAX];
strcpy(entry, entries[x]);
key = strtok_r(entry, "=", &ptr);
value = strtok_r(NULL, "=", &ptr);
if (strcasecmp(key, "name") == 0) {
btprint(F("{\"name\":\"%s\",\"desc\":\"%s\"}"), files[i], value);
if (i < count-1) {
btprint(F(","));
}
break;
}
}
}
btprint(F("],\"mute\":%s,"), MUTED == true ? "1" : "0");
btprint(F("\"bg\":%s,"), loopPlayer.isPlaying() ? "1" : "0");
// list directories on the card
memset(buffer, 0, sizeof(buffer));
count = listDirectories("/", files);
char *dirs = arrayToStringJson(buffer, files, count);
btprint(F("\"ls\":%s"), dirs);
// end
btprint("}}\n");
}