Files
sprocket-device-illucat/lib/NeoPattern/NeoPatternDto.h

63 lines
2.0 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"
#include "JsonStruct.h"
// TODO move ARRAY_LENGTH to core lib
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0])
struct NeoPixelConfig : public JsonStruct {
int pin;
int length;
int brightness;
int updateInterval;
int defaultColor;
void mapJsonObject(JsonObject& root) {
root["pin"] = pin;
root["length"] = length;
root["brightness"] = brightness;
root["updateInterval"] = updateInterval;
root["defaultColor"] = defaultColor;
}
void fromJsonObject(JsonObject& json) {
pin = getIntAttrFromJson(json, "pin");
length = getIntAttrFromJson(json, "length");
brightness = getIntAttrFromJson(json, "brightness");
updateInterval = getIntAttrFromJson(json, "updateInterval");
defaultColor = getIntAttrFromJson(json, "defaultColor");
}
};
struct NeoPatternDto : public JsonStruct {
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);
};
void mapJsonObject(JsonObject& root) {
root[JSON_MODE_NODE] = mode;
root[JSON_VALUE] = value;
}
// 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];
};
};
#endif