-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.cpp
198 lines (138 loc) · 4.37 KB
/
json.cpp
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
////////////////////////////////////////////////////////////////////////////////
// INCLUDE FILES
////////////////////////////////////////////////////////////////////////////////
#include <arduino.h>
#include <FS.h>
#include "json.h"
#include "spider.h"
#include "lua.h"
#include "wifi.h"
////////////////////////////////////////////////////////////////////////////////
// INITIALIZE LED ARRAYS FROM JSON CONFIG
////////////////////////////////////////////////////////////////////////////////
void json_setup() {
json_init init[8];
memset(init, 0xff, sizeof(init));
json_parse(init, sizeof(init)/sizeof(json_init));
for (auto &item : init) {
switch (item.mode) {
case LED_RGB:
case LED_GRB:
case LED_WRGB:
case LED_WGRB:
break;
default:
item.mode = LED_GRB;
}
switch (item.type) {
case LED_ARRAY:
if (item.pin >= 64 || item.pin < 0) {
Serial.print(F("Invalid Array GPIO Pin: "));
Serial.println(item.pin);
continue;
}
if (item.length > 300 || item.length < 0) {
Serial.print(F("Invalid Array Length: "));
Serial.println(item.length);
continue;
}
ledset.add(new led_array(
item.pin,
item.length,
static_cast<LED_MODE>(item.mode)
));
break;
case LED_GRID:
if (item.pin >= 64 || item.pin < 0) {
Serial.print(F("Invalid Grid GPIO Pin: "));
Serial.println(item.pin);
}
if (item.width >= 256 || item.width < 0) {
Serial.print(F("Invalid Grid Width: "));
Serial.println(item.width);
continue;
}
if (item.height >= 256 || item.height < 0) {
Serial.print(F("Invalid Grid Width: "));
Serial.println(item.height);
continue;
}
ledset.add(new led_grid(
item.pin,
item.width,
item.height,
static_cast<LED_MODE>(item.mode)
));
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// LOAD AND PARSE JSON DATA
////////////////////////////////////////////////////////////////////////////////
void json_parse(json_init *init, int count) {
File json = SPIFFS.open(LUASTR("/config.json"), "r");
if (!json) {
Serial.println(F("Failed to locate config.json"));
return;
}
char *bytes = (char*) malloc(json.size()+1);
json.readBytes(bytes, json.size());
bytes[json.size()] = NULL;
DynamicJsonDocument root(4000);
DeserializationError error = deserializeJson(root, bytes);
// JsonObject root = config.parseObject(bytes);
if (error || root.isNull()) {
Serial.println(F("Error parsing JSON content"));
free(bytes);
return;
}
////////////////////////////////////////////////////////////////////////////
// PARSE JSON LED CONFIGURATION
////////////////////////////////////////////////////////////////////////////
int x = -1;
JsonArray leds = root["led"];
for (JsonObject led : leds) {
//IF THIS OBJECT DOESN'T HAVE THE TYPE PROPERTY, IGNORE IT
if (!led.containsKey("type")) continue;
//IF WE HAVE TOO MANY OBJECTS, BAIL
if (++x >= count) break;
String type = led["type"];
if (type == "array") {
init[x].type = LED_ARRAY;
if (led.containsKey("pin")) init[x].pin = led["pin"];
if (led.containsKey("mode")) init[x].mode = led["mode"];
if (led.containsKey("length")) init[x].length = led["length"];
} else if (type == "grid") {
init[x].type = LED_GRID;
if (led.containsKey("pin")) init[x].pin = led["pin"];
if (led.containsKey("mode")) init[x].mode = led["mode"];
if (led.containsKey("width")) init[x].width = led["width"];
if (led.containsKey("height")) init[x].height = led["height"];
} else {
Serial.print(F("Unknown type: "));
Serial.println(type);
}
}
////////////////////////////////////////////////////////////////////////////////
// PARSE WIFI CONFIGURATION
////////////////////////////////////////////////////////////////////////////////
String mode = root["wifi"];
const char *ssid = "Cosplay.Lighting";
const char *pass = "password";
if (root.containsKey("ssid")) ssid = root["ssid"];
if (root.containsKey("pass")) pass = root["pass"];
if (mode == "station") {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
} else {
WiFi.softAPConfig( // TODO: MAKE THIS JSON CONFIGURABLE
IPAddress(192,168,1,1), // IP ADDRESS
IPAddress(0,0,0,0), // DEFAULT GATEWAY
IPAddress(255,255,255,0) // SUBNET MASK
);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, pass);
}
free(bytes);
}