-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
115 lines (106 loc) · 2.28 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
/****
* 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 (DEBUG != true) {
return;
}
char buf[256]; // resulting string limited to 1000 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("> ");
Serial.print(buf);
}
/**
* Convert char string to upcase
*/
void upcase(char *str)
{
int i = 0;
if (strcasecmp(str, "") == 0) {
return;
}
while (str[i] != '\0') {
toupper(str[i]);
i++;
}
}
/**
* Convert array of char strings to comma-delimited string
*/
char *arrayToString(char result[], const char arr[][SETTING_ENTRY_MAX], int len)
{
for (int i = 0 ; i < len; i++) {
if (i == 0) {
strcpy(result, arr[i]);
} else {
strcat(result, ",");
strcat(result, arr[i]);
}
}
return result;
}
/**
* Convert array of char strings to comma-delimited char-based string
*/
char *arrayToStringJson(char result[], const char arr[][SETTING_ENTRY_MAX], 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;
}
/**
* Convert array of int to comma-delimited string
*/
char *arrayToString(char result[], int arr[], int len)
{
char buf[20];
for (int i = 0 ; i < len; i++) {
sprintf(buf, "%d", arr[i]);
if (i == 0) {
strcpy(result, buf);
} else {
strcat(result, ",");
strcat(result, buf);
}
memset(buf, 0, sizeof(buf));
}
return result;
}
/**
* Convert array of float to comma-delimited string
*/
char *arrayToString(char result[], float arr[], const int len)
{
char buf[20];
for (int i = 0 ; i < len; i++) {
dtostrf(arr[i], 0, 4, buf);
if (i == 0) {
strcpy(result, buf);
} else {
strcat(result, ",");
strcat(result, buf);
}
memset(buf, 0, sizeof(buf));
}
return result;
}