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

97 lines
3.1 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 {
// FIXME constants!
int pin = 4;
int length = 8;
int brightness = 100;
int updateInterval = 100;
int defaultColor = 100; // FIXME remove unused
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", pin);
length = getIntAttrFromJson(json, "length", length);
brightness = getIntAttrFromJson(json, "brightness", brightness);
updateInterval = getIntAttrFromJson(json, "updateInterval", updateInterval);
defaultColor = getIntAttrFromJson(json, "defaultColor", defaultColor);
}
};
struct NeoPatternState : public JsonStruct {
uint pattern = 0;
uint color= 0;
uint color2= 0;
uint totalSteps = 16;
uint brightness = 64;
void mapJsonObject(JsonObject& root) {
root["pattern"] = pattern;
root["color"] = color;
root["color2"] = color2;
root["totalSteps"] = totalSteps;
root["brightness"] = brightness;
}
// Map a json object to this struct.
void fromJsonObject(JsonObject& json){
if(!verifyJsonObject(json)){
PRINT_MSG(Serial, "fromJsonObject", "cannot parse JSON");
valid = 0;
return;
}
color = getIntAttrFromJson(json, "color", color);
color2 = getIntAttrFromJson(json, "color2", color2);
pattern = getIntAttrFromJson(json, "pattern", pattern);
brightness = getIntAttrFromJson(json, "brightness", brightness);
totalSteps = getIntAttrFromJson(json, "totalSteps", totalSteps);
valid = 1;
};
};
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, "fromJsonObject", "cannot parse JSON");
valid = 0;
return;
}
mode = atoi(json[JSON_MODE_NODE]);
mode = mode < ARRAY_LENGTH(PIXEL_FNCS) ? mode : 0;
value = json[JSON_VALUE];
valueStr = json[JSON_VALUE];
valid = 1;
};
};
#endif