mirror of
https://gitlab.com/wirelos/sprocket-plugin-neopixel.git
synced 2025-12-17 06:46:40 +01:00
initial commit
This commit is contained in:
130
src/PixelPlugin.cpp
Normal file
130
src/PixelPlugin.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "PixelPlugin.h"
|
||||
|
||||
PixelPlugin::PixelPlugin(NeoPattern *neoPattern)
|
||||
{
|
||||
pixels = neoPattern;
|
||||
loadConfigFromFile();
|
||||
applyConfig(pixelConfig);
|
||||
defaultAnimation();
|
||||
}
|
||||
PixelPlugin::PixelPlugin(PixelConfig cfg)
|
||||
{
|
||||
pixelConfig.brightness = cfg.brightness;
|
||||
pixelConfig.pin = cfg.pin;
|
||||
pixelConfig.length = cfg.length;
|
||||
pixelConfig.updateInterval = cfg.updateInterval;
|
||||
loadConfigFromFile();
|
||||
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
|
||||
applyConfig(pixelConfig);
|
||||
defaultAnimation();
|
||||
}
|
||||
PixelPlugin::PixelPlugin()
|
||||
{
|
||||
loadConfigFromFile();
|
||||
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
|
||||
applyConfig(pixelConfig);
|
||||
defaultAnimation();
|
||||
}
|
||||
|
||||
void PixelPlugin::loadConfigFromFile(){
|
||||
if (SPIFFS.begin()){
|
||||
pixelConfig.fromFile(PIXEL_CONFIG_FILE);
|
||||
}
|
||||
}
|
||||
|
||||
void PixelPlugin::applyConfig(NeoPixelConfig cfg)
|
||||
{
|
||||
pixels->setBrightness(pixelConfig.brightness);
|
||||
}
|
||||
|
||||
void PixelPlugin::defaultAnimation() {
|
||||
pixels->RainbowCycle(pixelConfig.updateInterval);
|
||||
animate();
|
||||
}
|
||||
|
||||
void PixelPlugin::activate(Scheduler *userScheduler)
|
||||
{
|
||||
animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this));
|
||||
userScheduler->addTask(animation);
|
||||
animation.enable();
|
||||
|
||||
subscribe("pixels/colorWheel", bind(&PixelPlugin::colorWheel, this, _1));
|
||||
subscribe("pixels/color", bind(&PixelPlugin::setColor, this, _1));
|
||||
subscribe("pixels/color2", bind(&PixelPlugin::setColor2, this, _1));
|
||||
subscribe("pixels/pattern", bind(&PixelPlugin::setPattern, this, _1));
|
||||
subscribe("pixels/totalSteps", bind(&PixelPlugin::setTotalSteps, this, _1));
|
||||
subscribe("pixels/brightness", bind(&PixelPlugin::setBrightness, this, _1));
|
||||
subscribe("pixels/state", bind(&PixelPlugin::setState, this, _1));
|
||||
|
||||
PRINT_MSG(Serial, "PIXELS", "NeoPixels activated");
|
||||
}
|
||||
|
||||
void PixelPlugin::setState(String msg)
|
||||
{
|
||||
PRINT_MSG(Serial, "PIXELS", msg.c_str());
|
||||
state.fromJsonString(msg);
|
||||
//pixels->setBrightness(state.brightness);
|
||||
//pixels->ColorSet(state.color);
|
||||
pixels->Index = 0;
|
||||
pixels->Color1 = state.color;
|
||||
pixels->Color2 = state.color2;
|
||||
pixels->TotalSteps = state.totalSteps;
|
||||
pixels->ActivePattern = (pattern)state.pattern;
|
||||
pixels->Direction = FORWARD;
|
||||
}
|
||||
|
||||
void PixelPlugin::colorWheel(String msg)
|
||||
{
|
||||
int color = atoi(msg.c_str());
|
||||
pixels->ActivePattern = NONE;
|
||||
pixels->ColorSet(pixels->Wheel(color));
|
||||
}
|
||||
|
||||
void PixelPlugin::setTotalSteps(String msg)
|
||||
{
|
||||
pixels->TotalSteps = atoi(msg.c_str());
|
||||
}
|
||||
|
||||
void PixelPlugin::setBrightness(String msg)
|
||||
{
|
||||
int inVal = atoi(msg.c_str());
|
||||
pixels->setBrightness(inVal);
|
||||
pixels->show();
|
||||
}
|
||||
|
||||
void PixelPlugin::setColor(String msg)
|
||||
{
|
||||
pixels->ActivePattern = NONE;
|
||||
pixels->Color1 = atoi(msg.c_str());
|
||||
//if(pixels->ActivePattern == NONE){
|
||||
pixels->ColorSet(pixels->Color1);
|
||||
//}
|
||||
}
|
||||
|
||||
void PixelPlugin::setColor2(String msg)
|
||||
{
|
||||
pixels->Color2 = atoi(msg.c_str());
|
||||
}
|
||||
|
||||
void PixelPlugin::setPattern(String msg)
|
||||
{
|
||||
pixels->Index = 0;
|
||||
pixels->Direction = FORWARD;
|
||||
pixels->ActivePattern = (pattern)atoi(msg.c_str());
|
||||
}
|
||||
|
||||
void PixelPlugin::animate()
|
||||
{
|
||||
pixels->Update();
|
||||
yield();
|
||||
}
|
||||
|
||||
void PixelPlugin::enable()
|
||||
{
|
||||
animation.enable();
|
||||
}
|
||||
|
||||
void PixelPlugin::disable()
|
||||
{
|
||||
animation.disable();
|
||||
}
|
||||
59
src/PixelPlugin.h
Normal file
59
src/PixelPlugin.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#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 "NeoPixelConfig.h"
|
||||
#include "NeoPatternState.h"
|
||||
#include "NeoPattern.cpp"
|
||||
#include "config.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
#ifndef PIXEL_CONFIG_FILE
|
||||
#define PIXEL_CONFIG_FILE "/pixelConfig.json"
|
||||
#endif
|
||||
|
||||
struct PixelConfig
|
||||
{
|
||||
int pin;
|
||||
int length;
|
||||
int brightness;
|
||||
int updateInterval;
|
||||
};
|
||||
|
||||
class PixelPlugin : public Plugin
|
||||
{
|
||||
private:
|
||||
NeoPixelConfig pixelConfig;
|
||||
NeoPattern *pixels;
|
||||
NeoPatternState state;
|
||||
|
||||
public:
|
||||
Task animation;
|
||||
PixelPlugin(PixelConfig cfg);
|
||||
PixelPlugin(NeoPattern *neoPattern);
|
||||
PixelPlugin();
|
||||
void loadConfigFromFile();
|
||||
void applyConfig(NeoPixelConfig cfg);
|
||||
void applyConfigFromFile();
|
||||
void activate(Scheduler *userScheduler);
|
||||
void defaultAnimation();
|
||||
void setState(String msg);
|
||||
void colorWheel(String msg);
|
||||
void setTotalSteps(String msg);
|
||||
void setBrightness(String msg);
|
||||
void setColor(String msg);
|
||||
void setColor2(String msg);
|
||||
void setPattern(String msg);
|
||||
void animate();
|
||||
void enable();
|
||||
void disable();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "TemplatePlugin.h"
|
||||
|
||||
TemplatePlugin::TemplatePlugin(TemplateConfig cfg)
|
||||
{
|
||||
templateConfig = cfg;
|
||||
}
|
||||
|
||||
void TemplatePlugin::activate(Scheduler *scheduler)
|
||||
{
|
||||
PRINT_MSG(Serial, "TEMPLATE", "plugin activated");
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
#ifndef __TEMPLATE_PLUGIN__
|
||||
#define __TEMPLATE_PLUGIN__
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
|
||||
#include <Plugin.h>
|
||||
#include "utils/utils_print.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
struct TemplateConfig
|
||||
{
|
||||
const char *var;
|
||||
};
|
||||
|
||||
class TemplatePlugin : public Plugin
|
||||
{
|
||||
public:
|
||||
TemplateConfig templateConfig;
|
||||
TemplatePlugin(TemplateConfig cfg);
|
||||
void activate(Scheduler *scheduler);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -21,4 +21,11 @@
|
||||
#define HOSTNAME "sprocket"
|
||||
#define CONNECT_TIMEOUT 10000
|
||||
|
||||
// NeoPixel conig
|
||||
#define LED_STRIP_PIN D2
|
||||
#define LED_STRIP_LENGTH 8
|
||||
#define LED_STRIP_BRIGHTNESS 48
|
||||
#define LED_STRIP_UPDATE_INTERVAL 200
|
||||
#define LED_STRIP_DEFAULT_COLOR 100
|
||||
|
||||
#endif
|
||||
@@ -1,26 +1,18 @@
|
||||
#include "config.h"
|
||||
#include "WiFiNet.h"
|
||||
#include "Sprocket.h"
|
||||
#include "TemplatePlugin.h"
|
||||
#include "PixelPlugin.h"
|
||||
|
||||
WiFiNet *network;
|
||||
Sprocket *sprocket;
|
||||
|
||||
void setup()
|
||||
{
|
||||
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
|
||||
sprocket->addPlugin(new TemplatePlugin({"sprocket"}));
|
||||
|
||||
network = new WiFiNet(
|
||||
SPROCKET_MODE,
|
||||
STATION_SSID,
|
||||
STATION_PASSWORD,
|
||||
AP_SSID,
|
||||
AP_PASSWORD,
|
||||
HOSTNAME,
|
||||
CONNECT_TIMEOUT);
|
||||
network->connect();
|
||||
|
||||
sprocket = new Sprocket(
|
||||
{STARTUP_DELAY, SERIAL_BAUD_RATE});
|
||||
sprocket->addPlugin(new PixelPlugin(
|
||||
{LED_STRIP_PIN,
|
||||
LED_STRIP_LENGTH,
|
||||
LED_STRIP_BRIGHTNESS,
|
||||
LED_STRIP_UPDATE_INTERVAL}));
|
||||
sprocket->activate();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user