From 30593fd2be7a50cd1f4eed7e2329465b0c1170b6 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Wed, 29 Aug 2018 15:13:24 +0200 Subject: [PATCH] improve pixel example --- .gitlab-ci.yml | 1 + data/config.json | 4 +-- lib/NeoPattern/NeoPattern.cpp | 10 +++++-- src/Plugin.h | 2 -- src/base/MeshSprocket.h | 12 ++------ src/examples/mesh/MeshApp.h | 11 +++++-- src/examples/meshPixel/MeshPixel.h | 31 ++++++++++++-------- src/examples/meshPixel/config.h | 8 +++--- src/plugins/OtaTcpPlugin.cpp | 3 +- src/plugins/PixelPlugin.cpp | 46 ++++++++++++++++++++++++++++++ src/plugins/WebServerPlugin.cpp | 2 -- 11 files changed, 91 insertions(+), 39 deletions(-) create mode 100644 src/plugins/PixelPlugin.cpp diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5ea9ae4..b005a81 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -37,6 +37,7 @@ firmware-build: - pio run -t clean - pio run --environment basic - pio run --environment mesh + - pio run --environment meshPixel - pio run --environment meshMqttBridge - pio run --environment ota artifacts: diff --git a/data/config.json b/data/config.json index 0bb8956..e20959f 100644 --- a/data/config.json +++ b/data/config.json @@ -1,8 +1,8 @@ { - "stationMode": 1, + "stationMode": 0, "channel": 11, "meshPort": 5555, - "meshSSID": "MyMesh", + "meshSSID": "WibblyWobbly", "meshPassword": "th3r31sn0sp00n", "stationSSID": "MyAP", "stationPassword": "myApPassword", diff --git a/lib/NeoPattern/NeoPattern.cpp b/lib/NeoPattern/NeoPattern.cpp index bff35c8..3a62bf8 100644 --- a/lib/NeoPattern/NeoPattern.cpp +++ b/lib/NeoPattern/NeoPattern.cpp @@ -57,7 +57,7 @@ class NeoPattern : public Adafruit_NeoPixel NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type) :Adafruit_NeoPixel(pixels, pin, type) { - bind(&NeoPattern::onCompleteDefault, this, pixels); + } void onCompleteDefault(int pixels) { @@ -111,10 +111,12 @@ class NeoPattern : public Adafruit_NeoPixel if (Index >= TotalSteps) { Index = 0; + completed = 1; if (OnComplete != NULL) { - completed = 1; OnComplete(numPixels()); // call the comlpetion callback + } else { + Reverse(); } } } @@ -124,10 +126,12 @@ class NeoPattern : public Adafruit_NeoPixel if (Index <= 0) { Index = TotalSteps-1; + completed = 1; if (OnComplete != NULL) { - completed = 1; OnComplete(numPixels()); // call the comlpetion callback + } else { + Reverse(); } } } diff --git a/src/Plugin.h b/src/Plugin.h index 4a1bea7..a8791ae 100644 --- a/src/Plugin.h +++ b/src/Plugin.h @@ -6,8 +6,6 @@ #include class Plugin { - protected: - Scheduler* scheduler; public: virtual void activate(Scheduler*, Network*); virtual void enable(){}; diff --git a/src/base/MeshSprocket.h b/src/base/MeshSprocket.h index 7a55dbf..fbb3ab5 100644 --- a/src/base/MeshSprocket.h +++ b/src/base/MeshSprocket.h @@ -8,25 +8,17 @@ #include #include #include -#include -#include -#include -#include #include "config.h" using namespace std; using namespace std::placeholders; -AsyncWebServer WEBSERVER(80); - class MeshSprocket : public Sprocket { public: MeshNet* net; - MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) { - addPlugin(new OtaTcpPlugin(otaCfg)); - addPlugin(new WebServerPlugin(webCfg, &WEBSERVER)); - addPlugin(new WebConfigPlugin(&WEBSERVER)); + MeshSprocket(SprocketConfig cfg) : Sprocket(cfg) { + } Sprocket* activate(Scheduler* scheduler, Network* network) { diff --git a/src/examples/mesh/MeshApp.h b/src/examples/mesh/MeshApp.h index dd1191b..8288b67 100644 --- a/src/examples/mesh/MeshApp.h +++ b/src/examples/mesh/MeshApp.h @@ -4,17 +4,24 @@ #include #include #include +#include +#include +#include +#include using namespace std; using namespace std::placeholders; +AsyncWebServer WEBSERVER(80); class MeshApp : public MeshSprocket { public: Task heartbeatTask; - MeshApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : MeshSprocket(cfg, otaCfg, webCfg) { - + MeshApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : MeshSprocket(cfg) { + addPlugin(new OtaTcpPlugin(otaCfg)); + addPlugin(new WebServerPlugin(webCfg, &WEBSERVER)); + addPlugin(new WebConfigPlugin(&WEBSERVER)); } Sprocket* activate(Scheduler* scheduler, Network* network) { diff --git a/src/examples/meshPixel/MeshPixel.h b/src/examples/meshPixel/MeshPixel.h index 3d1b1d7..e20bec0 100644 --- a/src/examples/meshPixel/MeshPixel.h +++ b/src/examples/meshPixel/MeshPixel.h @@ -11,34 +11,42 @@ #include "NeoPattern_api_json.h" #include "NeoPattern_api_modes.cpp" #include "utils_print.h" +#include +#include +#include +#include +#include using namespace std; using namespace std::placeholders; - class MeshPixel : public MeshSprocket { public: NeoPixelConfig pixelConfig; NeoPattern* pixels; NeoPatternState state; Task animation; + AsyncWebServer* server; - MeshPixel(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg, NeoPixelConfig pixelCfg) : MeshSprocket(cfg, otaCfg, webCfg) { + MeshPixel(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg, NeoPixelConfig pixelCfg) : MeshSprocket(cfg) { pixelConfig = pixelCfg; pixels = new NeoPattern(pixelCfg.length, pixelCfg.pin, NEO_GRB + NEO_KHZ800); - pixels->begin(); - pixels->setBrightness(pixelCfg.brightness); - pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelCfg.updateInterval); + server = new AsyncWebServer(80); + addPlugin(new OtaTcpPlugin(otaCfg)); + addPlugin(new WebServerPlugin(webCfg, server)); + addPlugin(new WebConfigPlugin(server)); + addPlugin(new PixelPlugin(pixelConfig, pixels)); + //pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelCfg.updateInterval); + startupAnimation(); + } + void startupAnimation() { + pixels->Fade(0, pixels->Color(255,255,255), 4, pixelConfig.updateInterval, FORWARD); } - Sprocket* activate(Scheduler* scheduler, Network* network) { // call parent method that enables dispatching and plugins MeshSprocket::activate(scheduler, network); net->mesh.onNewConnection(bind(&MeshPixel::newConnection,this, _1)); net->mesh.onChangedConnections(bind(&MeshPixel::connectionChanged,this)); - // pixel task - animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&MeshPixel::animate, this, pixels)); - addTask(animation); return this; } using MeshSprocket::activate; @@ -54,12 +62,11 @@ class MeshPixel : public MeshSprocket { void newConnection(uint32_t nodeId){ PRINT_MSG(Serial, SPROCKET_TYPE, "connected to %u", nodeId); - pixels->ActivePattern = NONE; - pixels->ColorSet(pixels->Wheel(COLOR_CONNECTED)); + pixels->RainbowCycle(pixelConfig.updateInterval); } void connectionChanged(){ if(!net->mesh.getNodeList().size()){ - pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval); + startupAnimation(); } } }; diff --git a/src/examples/meshPixel/config.h b/src/examples/meshPixel/config.h index 6c27eb9..3758926 100644 --- a/src/examples/meshPixel/config.h +++ b/src/examples/meshPixel/config.h @@ -32,10 +32,10 @@ #define WEB_DEFAULT_FILE "index.html" // NeoPixel -#define LED_STRIP_PIN D8 -#define LED_STRIP_LENGTH 24 -#define LED_STRIP_BRIGHTNESS 12 -#define LED_STRIP_UPDATE_INTERVAL 100 +#define LED_STRIP_PIN D2 +#define LED_STRIP_LENGTH 12 +#define LED_STRIP_BRIGHTNESS 32 +#define LED_STRIP_UPDATE_INTERVAL 200 #define LED_STRIP_DEFAULT_COLOR 100 #define COLOR_CONNECTED LED_STRIP_DEFAULT_COLOR #define COLOR_NOT_CONNECTED 254 diff --git a/src/plugins/OtaTcpPlugin.cpp b/src/plugins/OtaTcpPlugin.cpp index 4bbcebd..a0ab79e 100644 --- a/src/plugins/OtaTcpPlugin.cpp +++ b/src/plugins/OtaTcpPlugin.cpp @@ -49,11 +49,10 @@ class OtaTcpPlugin : public Plugin { //connectUpdateNetwork(network); net = static_cast(network); // setup task - scheduler = userScheduler; otaTask.set(TASK_MILLISECOND * 100, TASK_FOREVER, [](){ ArduinoOTA.handle(); }); - scheduler->addTask(otaTask); + userScheduler->addTask(otaTask); // configure OTA ArduinoOTA.setPort(config.port); diff --git a/src/plugins/PixelPlugin.cpp b/src/plugins/PixelPlugin.cpp new file mode 100644 index 0000000..be20af6 --- /dev/null +++ b/src/plugins/PixelPlugin.cpp @@ -0,0 +1,46 @@ +#ifndef __PIXEL_PLUGIN__ +#define __PIXEL_PLUGIN__ + +#define _TASK_SLEEP_ON_IDLE_RUN +#define _TASK_STD_FUNCTION + +#include "TaskSchedulerDeclarations.h" +#include "MeshNet.h" +#include "Plugin.h" +#include "NeoPattern.cpp" +#include "NeoPatternState.cpp" + +using namespace std; +using namespace std::placeholders; + +class PixelPlugin : public Plugin { + private: + NeoPixelConfig pixelConfig; + NeoPattern* pixels; + NeoPatternState state; + public: + Task animation; + PixelPlugin(NeoPixelConfig cfg, NeoPattern* neoPattern){ + pixelConfig = cfg; + pixels = neoPattern; + pixels->begin(); + pixels->setBrightness(pixelConfig.brightness); + } + void activate(Scheduler* userScheduler, Network* network){ + animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this)); + userScheduler->addTask(animation); + animation.enable(); + Serial.println("NeoPixels activated"); + } + void animate(){ + pixels->Update(); + } + void enable(){ + animation.enable(); + } + void disable(){ + animation.disable(); + } +}; + +#endif \ No newline at end of file diff --git a/src/plugins/WebServerPlugin.cpp b/src/plugins/WebServerPlugin.cpp index 3952e24..cf6c036 100644 --- a/src/plugins/WebServerPlugin.cpp +++ b/src/plugins/WebServerPlugin.cpp @@ -3,7 +3,6 @@ #include #include "TaskSchedulerDeclarations.h" -#include "ArduinoOTA.h" #include "MeshNet.h" #include "Plugin.h" #include @@ -22,7 +21,6 @@ class WebServerPlugin : public Plugin { server = webServer; } void activate(Scheduler* userScheduler, Network* network){ - //connectUpdateNetwork(network); net = static_cast(network); server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile); // TODO add auth if configured