diff --git a/lib/NeoPattern/NeoPatternDto.cpp b/lib/NeoPattern/NeoPatternDto.h similarity index 68% rename from lib/NeoPattern/NeoPatternDto.cpp rename to lib/NeoPattern/NeoPatternDto.h index 7e4d274..297982b 100644 --- a/lib/NeoPattern/NeoPatternDto.cpp +++ b/lib/NeoPattern/NeoPatternDto.h @@ -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 { diff --git a/platformio.ini b/platformio.ini index f65c106..09cc2af 100644 --- a/platformio.ini +++ b/platformio.ini @@ -34,6 +34,7 @@ lib_deps = ${common.lib_deps} painlessMesh ESP8266mDNS ArduinoOTA + ArduinoJson ESP Async WebServer ESPAsyncTCP Adafruit NeoPixel diff --git a/src/IlluCat.h b/src/IlluCat.h index e228ca5..2ae54af 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -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(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); diff --git a/src/PixelPlugin.h b/src/PixelPlugin.h index 09d33b8..8345da0 100644 --- a/src/PixelPlugin.h +++ b/src/PixelPlugin.h @@ -7,6 +7,7 @@ #include "TaskSchedulerDeclarations.h" #include "MeshNet.h" #include "Plugin.h" +#include "NeoPatternDto.h" #include "NeoPattern.cpp" using namespace std; diff --git a/src/main.cpp b/src/main.cpp index f2fff0f..440847f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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() {