-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.h
193 lines (183 loc) · 5.64 KB
/
utilities.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
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
#include <string.h>
#include <ctype.h>
#include "configs.h"
/**
* Simple text output (refactor in progress).
**/
void printDebug(String msg)
{
if (DEBUG)
{
Serial.println(msg);
}
}
/**
* Wrap text to display, clearly pasted from StackOverflow :)
*/
void wrapTxtToScreen(LiquidCrystal_I2C &lcd, String msg)
{
byte offset = 0, charNum = 0, dataLength = strlen(msg.c_str());
if (dataLength > WIDTH * HEIGHT)
{
dataLength = WIDTH * HEIGHT + 3;
}
for (byte rowNum = 0; rowNum < HEIGHT; rowNum++)
{
lcd.setCursor(0, rowNum);
charNum = rowNum * WIDTH + offset;
while (charNum < ((rowNum + 1) * WIDTH) + offset)
{
if ((charNum - offset == rowNum * WIDTH) && (charNum < dataLength - offset) && (msg[charNum] == ' '))
{
charNum++;
offset++;
}
if (charNum - offset >= dataLength - offset)
{
lcd.write(254);
}
//if (charNum-offset >= dataLength-offset) {
// lcd.print("x");
// }
else if (charNum - offset < dataLength - offset)
{
lcd.print(msg[charNum]);
}
charNum++;
}
}
};
/**
* Wrap text to display, clearly pasted from StackOverflow :)
*/
char *wordWrap(LiquidCrystal_I2C &lcd, char *string, int line_width)
{
int i = 0, k, counter, row = 0;
char *buffer;
while (i < strlen(string))
{
lcd.setCursor(0, row);
// Copy string until the end of the line is reached.
for (counter = 1; counter <= line_width; counter++)
{
// Check if end of string reached.
if (i == strlen(string))
{
buffer[i] = 0;
return buffer;
}
buffer[i] = string[i];
i++;
}
// Check for not a whitespace.
if (!isspace(string[i]))
{
// Check for nearest whitespace back in string.
for (k = i; k > 0; k--)
{
if (isspace(string[k]))
{
lcd.print(buffer);
row++;
// Set string index back to character after this one.
i = k + 1;
break;
}
}
}
else
{
lcd.print(buffer);
row++;
}
}
buffer[i] = 0;
return buffer;
}
/**
* Debug Firebase client data objects.
*/
void printResult(FirebaseData &data)
{
if (data.dataType() == "int")
Serial.println(data.intData());
else if (data.dataType() == "float")
Serial.println(data.floatData(), 5);
else if (data.dataType() == "double")
printf("%.9lf\n", data.doubleData());
else if (data.dataType() == "boolean")
Serial.println(data.boolData() == 1 ? "true" : "false");
else if (data.dataType() == "string")
Serial.println(data.stringData());
else if (data.dataType() == "json")
{
Serial.println();
FirebaseJson &json = data.jsonObject();
//Print all object data
Serial.println("Pretty printed JSON data:");
String jsonStr;
json.toString(jsonStr, true);
Serial.println(jsonStr);
Serial.println();
Serial.println("Iterate JSON data:");
Serial.println();
size_t len = json.iteratorBegin();
String key, value = "";
int type = 0;
for (size_t i = 0; i < len; i++)
{
json.iteratorGet(i, type, key, value);
Serial.print(i);
Serial.print(", ");
Serial.print("Type: ");
Serial.print(type == FirebaseJson::JSON_OBJECT ? "object" : "array");
if (type == FirebaseJson::JSON_OBJECT)
{
Serial.print(", Key: ");
Serial.print(key);
}
Serial.print(", Value: ");
Serial.println(value);
}
json.iteratorEnd();
}
else if (data.dataType() == "array")
{
Serial.println();
//get array data from FirebaseData using FirebaseJsonArray object
FirebaseJsonArray &arr = data.jsonArray();
//Print all array values
Serial.println("Pretty printed Array:");
String arrStr;
arr.toString(arrStr, true);
Serial.println(arrStr);
Serial.println();
Serial.println("Iterate array values:");
Serial.println();
for (size_t i = 0; i < arr.size(); i++)
{
Serial.print(i);
Serial.print(", Value: ");
FirebaseJsonData &jsonData = data.jsonData();
//Get the result data from FirebaseJsonArray object
arr.get(jsonData, i);
if (jsonData.typeNum == FirebaseJson::JSON_BOOL)
Serial.println(jsonData.boolValue ? "true" : "false");
else if (jsonData.typeNum == FirebaseJson::JSON_INT)
Serial.println(jsonData.intValue);
else if (jsonData.typeNum == FirebaseJson::JSON_FLOAT)
Serial.println(jsonData.floatValue);
else if (jsonData.typeNum == FirebaseJson::JSON_DOUBLE)
printf("%.9lf\n", jsonData.doubleValue);
else if (jsonData.typeNum == FirebaseJson::JSON_STRING ||
jsonData.typeNum == FirebaseJson::JSON_NULL ||
jsonData.typeNum == FirebaseJson::JSON_OBJECT ||
jsonData.typeNum == FirebaseJson::JSON_ARRAY)
Serial.println(jsonData.stringValue);
}
}
else
{
Serial.println(data.payload());
}
}