feat: rewrite NeoPattern example
This commit is contained in:
59
examples/neopattern/NeoPatternState.h
Normal file
59
examples/neopattern/NeoPatternState.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef __NEOPATTERN_STATE__
|
||||
#define __NEOPATTERN_STATE__
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
struct NeoPatternState {
|
||||
// Default values
|
||||
static constexpr uint DEFAULT_PATTERN = 0;
|
||||
static constexpr uint DEFAULT_COLOR = 0xFF0000; // Red
|
||||
static constexpr uint DEFAULT_COLOR2 = 0x0000FF; // Blue
|
||||
static constexpr uint DEFAULT_TOTAL_STEPS = 16;
|
||||
static constexpr uint DEFAULT_BRIGHTNESS = 64;
|
||||
|
||||
uint pattern = DEFAULT_PATTERN;
|
||||
uint color = DEFAULT_COLOR;
|
||||
uint color2 = DEFAULT_COLOR2;
|
||||
uint totalSteps = DEFAULT_TOTAL_STEPS;
|
||||
uint brightness = DEFAULT_BRIGHTNESS;
|
||||
|
||||
NeoPatternState() = default;
|
||||
|
||||
NeoPatternState(uint p, uint c, uint c2, uint steps, uint b)
|
||||
: pattern(p), color(c), color2(c2), totalSteps(steps), brightness(b) {}
|
||||
|
||||
void mapJsonObject(JsonObject& root) const {
|
||||
root["pattern"] = pattern;
|
||||
root["color"] = color;
|
||||
root["color2"] = color2;
|
||||
root["totalSteps"] = totalSteps;
|
||||
root["brightness"] = brightness;
|
||||
}
|
||||
|
||||
void fromJsonObject(JsonObject& json) {
|
||||
pattern = json["pattern"] | pattern;
|
||||
color = json["color"] | color;
|
||||
color2 = json["color2"] | color2;
|
||||
totalSteps = json["totalSteps"] | totalSteps;
|
||||
brightness = json["brightness"] | brightness;
|
||||
}
|
||||
|
||||
// Helper methods for JSON string conversion
|
||||
String toJsonString() const {
|
||||
JsonDocument doc;
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
mapJsonObject(root);
|
||||
String result;
|
||||
serializeJson(doc, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void fromJsonString(const String& jsonStr) {
|
||||
JsonDocument doc;
|
||||
deserializeJson(doc, jsonStr);
|
||||
JsonObject root = doc.as<JsonObject>();
|
||||
fromJsonObject(root);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user