load pixel config from json file

This commit is contained in:
2018-09-02 20:21:59 +02:00
parent 5538cffb20
commit b69a0937a0
5 changed files with 51 additions and 20 deletions

View File

@@ -10,12 +10,26 @@
// TODO move ARRAY_LENGTH to core lib
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0])
struct NeoPixelConfig {
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 {

View File

@@ -34,6 +34,7 @@ lib_deps = ${common.lib_deps}
painlessMesh
ESP8266mDNS
ArduinoOTA
ArduinoJson
ESP Async WebServer
ESPAsyncTCP
Adafruit NeoPixel

View File

@@ -7,7 +7,7 @@
#include "config.h"
#include "NeoPattern.cpp"
#include "NeoPatternDto.cpp"
#include "NeoPatternDto.h"
#include "NeoPattern_api_json.h"
#include "NeoPattern_api_modes.cpp"
#include "utils_print.h"
@@ -22,22 +22,29 @@ using namespace std::placeholders;
class IlluCat : public MeshSprocket {
public:
NeoPixelConfig pixelConfig;
NeoPattern* pixels;
NeoPatternDto state;
Task animation;
AsyncWebServer* server;
NeoPixelConfig pixelConfig;
SprocketConfig sprocketConfig;
OtaConfig otaConfig;
WebServerConfig webConfig;
IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg, NeoPixelConfig pixelCfg) : MeshSprocket(cfg) {
pixelConfig = pixelCfg;
pixels = new NeoPattern(pixelCfg.length, pixelCfg.pin, NEO_GRB + NEO_KHZ800);
server = new AsyncWebServer(80);
addPlugin(new OtaTcpPlugin(otaCfg));
addPlugin(new WebServerPlugin(webCfg, server));
addPlugin(new WebConfigPlugin(server));
addPlugin(new PixelPlugin(pixelConfig, pixels));
IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg/* , NeoPixelConfig pixelCfg */) : MeshSprocket(cfg) {
//pixelConfig = pixelCfg;
scanningAnimation();
sprocketConfig = cfg;
otaConfig = otaCfg;
webConfig = webCfg;
pixelConfig.pin = 4;
pixelConfig.length = 8;
pixelConfig.brightness = 32;
pixelConfig.updateInterval = 100;
pixelConfig.defaultColor = 100;
}
void scanningAnimation() {
pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval);
@@ -48,19 +55,27 @@ class IlluCat : public MeshSprocket {
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
// call parent method that enables dispatching and plugins
MeshSprocket::activate(scheduler, network);
net = static_cast<MeshNet*>(network);
net->mesh.onNewConnection(bind(&IlluCat::newConnection,this, _1));
net->mesh.onChangedConnections(bind(&IlluCat::connectionChanged,this));
if(SPIFFS.begin()){
pixelConfig.fromFile("/config.json");
}
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
server = new AsyncWebServer(80);
addPlugin(new OtaTcpPlugin(otaConfig));
addPlugin(new WebServerPlugin(webConfig, server));
addPlugin(new WebConfigPlugin(server));
addPlugin(new PixelPlugin(pixelConfig, pixels));
scanningAnimation();
// FIXME OnDisable is triggered after last scan, aprx. 10 sec
net->mesh.stationScan.task.setOnDisable(bind(&IlluCat::defaultAnimation,this));
return this;
return MeshSprocket::activate(scheduler, network);;
} using MeshSprocket::activate;
void animate(){
pixels->Update();
}
void onMessage( uint32_t from, String &msg ) {
PRINT_MSG(Serial, SPROCKET_TYPE, "msg from %u = %s\n", from, msg.c_str());
state.fromJsonString(msg);

View File

@@ -7,6 +7,7 @@
#include "TaskSchedulerDeclarations.h"
#include "MeshNet.h"
#include "Plugin.h"
#include "NeoPatternDto.h"
#include "NeoPattern.cpp"
using namespace std;

View File

@@ -11,8 +11,8 @@ MeshNet net({
IlluCat sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ OTA_PORT, OTA_PASSWORD },
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE },
{ LED_STRIP_PIN, LED_STRIP_LENGTH, LED_STRIP_BRIGHTNESS, LED_STRIP_UPDATE_INTERVAL, LED_STRIP_DEFAULT_COLOR }
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE }/* ,
{ LED_STRIP_PIN, LED_STRIP_LENGTH, LED_STRIP_BRIGHTNESS, LED_STRIP_UPDATE_INTERVAL, LED_STRIP_DEFAULT_COLOR } */
);
void setup() {