mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 21:18:21 +01:00
58 lines
1.8 KiB
C++
58 lines
1.8 KiB
C++
#ifndef __NEOPATTERN_STATE__
|
|
#define __NEOPATTERN_STATE__
|
|
|
|
#include <ArduinoJson.h>
|
|
#include "NeoPattern_api_json.h"
|
|
#include "NeoPattern_api_modes.cpp"
|
|
#include "utils_print.h"
|
|
|
|
// TODO move ARRAY_LENGTH to core lib
|
|
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0])
|
|
|
|
struct NeoPatternState {
|
|
uint mode;
|
|
uint value;
|
|
const char* valueStr;
|
|
// ------------------------------------------------------------------------------------------
|
|
//Check if given object is valid and contains fields: JSON_MODE_NODE, JSON_VALUE
|
|
int verifyJsonObject(JsonObject& json){
|
|
return json.success()
|
|
&& json.containsKey(JSON_MODE_NODE)
|
|
&& json.containsKey(JSON_VALUE);
|
|
};
|
|
JsonObject& toJsonObject() {
|
|
StaticJsonBuffer<200> jsonBuffer;
|
|
JsonObject& root = jsonBuffer.createObject();
|
|
root["mode"] = mode;
|
|
root["value"] = value;
|
|
return root;
|
|
}
|
|
String toJsonString(){
|
|
StaticJsonBuffer<200> jsonBuffer;
|
|
JsonObject& root = jsonBuffer.createObject();
|
|
root["mode"] = mode;
|
|
root["value"] = value;
|
|
String jsonString;
|
|
root.printTo(jsonString);
|
|
return jsonString;
|
|
}
|
|
// Map a json object to this struct.
|
|
void fromJsonObject(JsonObject& json){
|
|
if(!verifyJsonObject(json)){
|
|
PRINT_MSG(Serial, "PatternState.fromJsonObject", "cannot parse JSON");
|
|
return;
|
|
}
|
|
mode = atoi(json[JSON_MODE_NODE]);
|
|
mode = mode < ARRAY_LENGTH(PIXEL_FNCS) ? mode : 0;
|
|
value = json[JSON_VALUE];
|
|
valueStr = json[JSON_VALUE];
|
|
};
|
|
// Parse a json string and map parsed object
|
|
void fromJsonString(String& str){
|
|
StaticJsonBuffer<200> jsonBuffer;
|
|
JsonObject& json = jsonBuffer.parseObject(str);
|
|
fromJsonObject(json);
|
|
};
|
|
};
|
|
|
|
#endif |