improve pixel example

This commit is contained in:
2018-08-29 15:13:24 +02:00
parent 945bf78459
commit 30593fd2be
11 changed files with 91 additions and 39 deletions

View File

@@ -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:

View File

@@ -1,8 +1,8 @@
{
"stationMode": 1,
"stationMode": 0,
"channel": 11,
"meshPort": 5555,
"meshSSID": "MyMesh",
"meshSSID": "WibblyWobbly",
"meshPassword": "th3r31sn0sp00n",
"stationSSID": "MyAP",
"stationPassword": "myApPassword",

View File

@@ -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();
}
}
}

View File

@@ -6,8 +6,6 @@
#include <base/MeshMessage.h>
class Plugin {
protected:
Scheduler* scheduler;
public:
virtual void activate(Scheduler*, Network*);
virtual void enable(){};

View File

@@ -8,25 +8,17 @@
#include <MeshNet.h>
#include <base/MeshMessage.h>
#include <base/MeshSprocketConfig.h>
#include <plugins/WebSO.h>
#include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerPlugin.cpp>
#include <plugins/WebConfigPlugin.cpp>
#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) {

View File

@@ -4,17 +4,24 @@
#include <painlessMesh.h>
#include <base/MeshSprocket.h>
#include <MeshNet.h>
#include <plugins/WebSO.h>
#include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerPlugin.cpp>
#include <plugins/WebConfigPlugin.cpp>
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) {

View File

@@ -11,34 +11,42 @@
#include "NeoPattern_api_json.h"
#include "NeoPattern_api_modes.cpp"
#include "utils_print.h"
#include <plugins/WebSO.h>
#include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerPlugin.cpp>
#include <plugins/WebConfigPlugin.cpp>
#include <plugins/PixelPlugin.cpp>
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();
}
}
};

View File

@@ -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

View File

@@ -49,11 +49,10 @@ class OtaTcpPlugin : public Plugin {
//connectUpdateNetwork(network);
net = static_cast<MeshNet*>(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);

View File

@@ -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

View File

@@ -3,7 +3,6 @@
#include <FS.h>
#include "TaskSchedulerDeclarations.h"
#include "ArduinoOTA.h"
#include "MeshNet.h"
#include "Plugin.h"
#include <plugins/WebSO.h>
@@ -22,7 +21,6 @@ class WebServerPlugin : public Plugin {
server = webServer;
}
void activate(Scheduler* userScheduler, Network* network){
//connectUpdateNetwork(network);
net = static_cast<MeshNet*>(network);
server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
// TODO add auth if configured