Files
sprocket-device-illucat/lib/NeoPattern/NeoPatternDto.h
2018-09-17 15:34:24 +02:00

97 lines
3.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 NeoPatternMsg : public JsonStruct {
String pattern;
String payload;
String topic;
uint color;
uint color2;
uint totalSteps;
void mapJsonObject(JsonObject& root) {
root["topic"] = topic;
root["pattern"] = pattern;
root["payload"] = payload;
root["color"] = color;
root["color2"] = color2;
root["totalSteps"] = totalSteps;
}
// Map a json object to this struct.
void fromJsonObject(JsonObject& json){
if(!verifyJsonObject(json)){
PRINT_MSG(Serial, "fromJsonObject", "cannot parse JSON");
valid = 0;
return;
}
topic = getAttrFromJson(json, "topic");
color = getIntAttrFromJson(json, "color");
color2 = getIntAttrFromJson(json, "color2");
pattern = getAttrFromJson(json, "pattern");
payload = getAttrFromJson(json, "payload");
totalSteps = getIntAttrFromJson(json, "totalSteps", 255);
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