-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.ino
44 lines (40 loc) · 1.58 KB
/
config.ino
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
bool loadConfig() {
Serial.println("Loading configuration");
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
parseJsonConfig(configFile);
}
/**
* Parse the configuration JSON and Update the global instance of Configuration struct
*/
void parseJsonConfig(File configFile){
String string_config = configFile.readString();
configFile.close();
Serial.println("CONFIG: ---> " + string_config);
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.parseObject(string_config);
JsonArray &nodes = jsonBuffer.parseArray(string_config);
JsonArray &fanSpeedConfig = root["fan_speed_config"];
int length = fanSpeedConfig.size();
for(int i = 0; i < length; i++){
conf.fanSpeed[i].envTemp = double(fanSpeedConfig[i]["temp"]);
conf.fanSpeed[i].fanSpeed = double(fanSpeedConfig[i]["speed"]);
// Serial.println("[CONFIG] Env. temp: " + String(conf.fanSpeed[i].envTemp) + "°C --> Fan speed: " + String(conf.fanSpeed[i].fanSpeed)+"%");
}
qsort(conf.fanSpeed, length, sizeof(struct FanSpeed), compareFanSpeedConfig);
for(int i = 0; i < length; i++){
Serial.println("[CONFIG] SORT Env. temp: " + String(conf.fanSpeed[i].envTemp) + "°C --> Fan speed: " + String(conf.fanSpeed[i].fanSpeed)+"%");
}
}
/**
* fan speed comparing function for qsort
*/
int compareFanSpeedConfig(const void *fsc1, const void *fsc2)
{
struct FanSpeed *fanSpeed1 = (struct FanSpeed *)fsc1;
struct FanSpeed *fanSpeed2 = (struct FanSpeed *)fsc2;
return fanSpeed1->envTemp - fanSpeed2->envTemp;
}