forked from becauseinterwebs/tktalkie-v4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
135 lines (125 loc) · 2.39 KB
/
utils.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
/****
* General utils
*/
/**
* Uses the "F" FlashStringHelper (to help save memory)
* to output a formatted string.
* This code is adapted from http://playground.arduino.cc/Main/Printf
*/
void debug(const __FlashStringHelper *fmt, ... ) {
if (Config.debug == 0) {
return;
}
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);
Serial.print(F("[DEBUG] "));
Serial.print(F(buf));
}
/**
* Convert char string to upcase
*/
void upcase(char *str)
{
int i = 0;
if (strcasecmp(str, "") == 0) {
return;
}
while (str[i] != '\0') {
str[i] = toupper(str[i]);
i++;
}
}
/**
* Convert array of char strings to comma-delimited char-based string
*/
char *arrayToStringJson(char result[], const char arr[][14], int len)
{
strcpy(result, "[");
for (int i = 0 ; i < len; i++) {
strcat(result, "\"");
strcat(result, arr[i]);
strcat(result, "\"");
if (i < len-1) {
strcat(result, ",");
}
}
strcat(result, "]");
return result;
}
byte getCommand(const char cmd[14])
{
char commands[39][12] = {
"debug",
"echo",
"default",
"delete",
"load",
"play",
"play_effect",
"play_sound",
"play_glove",
"play_loop",
"stop_loop",
"config",
"mute",
"unmute",
"save",
"access_code",
"connect",
"disconnect",
"download",
"backup",
"restore",
"settings",
"files",
"sounds",
"effects",
"loops",
"glove",
"profiles",
"ls",
"help",
"calibrate",
"reset",
"sleep",
"baud",
"mem",
"beep",
"berp",
"show",
"profile_dir"
};
byte min = 0;
byte max = 37;
byte r = round((min+max)/2);
byte l = r-1;
byte index = 255;
while (1) {
if (strcasecmp(cmd, commands[l]) == 0) {
index = l;
break;
} else if (strcasecmp(cmd, commands[r]) == 0) {
index = r;
break;
} else {
if (l == min && r == max && index == 255) {
break;
}
if (l > min) {
l--;
}
if (r < max) {
r++;
}
}
}
//debug(F("cmd: %s index: %d\n"), cmd, index);
return (index);
}