mirror of
https://gitlab.com/zwirbel/illucat.git
synced 2025-12-16 10:04:30 +01:00
fix some stuff
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
#include <painlessMesh.h>
|
||||
#include <base/MeshSprocket.h>
|
||||
#include <MeshNet.h>
|
||||
#include <DNSServer.h>
|
||||
|
||||
|
||||
#include "config.h"
|
||||
#include "NeoPattern.cpp"
|
||||
@@ -21,14 +23,16 @@
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
const byte DNS_PORT = 53;
|
||||
|
||||
class IlluCat : public MeshSprocket {
|
||||
public:
|
||||
NeoPattern* pixels;
|
||||
NeoPatternDto defaultState;
|
||||
NeoPatternDto state;
|
||||
Task animation;
|
||||
AsyncWebServer* server;
|
||||
AsyncWebSocket* ws;
|
||||
DNSServer* dnsServer;
|
||||
|
||||
NeoPixelConfig pixelConfig;
|
||||
SprocketConfig sprocketConfig;
|
||||
@@ -49,13 +53,14 @@ class IlluCat : public MeshSprocket {
|
||||
pixelConfig.defaultColor = 100;
|
||||
|
||||
}
|
||||
void scanningAnimation() {
|
||||
virtual void scanningAnimation() {
|
||||
pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval);
|
||||
//pixels->Fade(0, pixels->Color(255,255,255), 4, pixelConfig.updateInterval, FORWARD);
|
||||
}
|
||||
void defaultAnimation() {
|
||||
virtual void defaultAnimation() {
|
||||
String defaultStr = String(defaultState.value);
|
||||
PIXEL_FNCS[defaultState.mode](pixels, defaultStr.c_str());
|
||||
//pixels->RainbowCycle(150);
|
||||
}
|
||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
||||
|
||||
@@ -71,30 +76,40 @@ class IlluCat : public MeshSprocket {
|
||||
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
|
||||
server = new AsyncWebServer(80);
|
||||
ws = new AsyncWebSocket("/pixel");
|
||||
ws->onEvent([=](AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
|
||||
this->onWsEvent(server, client, type, arg, data, len);
|
||||
});
|
||||
dnsServer = new DNSServer();
|
||||
|
||||
addPlugin(new OtaTcpPlugin(otaConfig));
|
||||
addPlugin(new WebServerPlugin(webConfig, server));
|
||||
addPlugin(new WebConfigPlugin(server));
|
||||
addPlugin(new PixelPlugin(pixelConfig, pixels));
|
||||
|
||||
scanningAnimation();
|
||||
|
||||
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
||||
dnsServer->start(DNS_PORT, "*", WiFi.softAPIP());
|
||||
|
||||
//scanningAnimation();
|
||||
defaultAnimation();
|
||||
|
||||
// setup web stuff
|
||||
server->on("/pixel/pattern", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1));
|
||||
ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||
server->addHandler(ws);
|
||||
server->on("/pixel/pattern", HTTP_GET, [this](AsyncWebServerRequest *request){
|
||||
Serial.println("POST /pixel/pattern");
|
||||
if(request->hasParam("state", true)) {
|
||||
String inStr = request->getParam("state", true)->value();
|
||||
onMessage(0, inStr);
|
||||
}
|
||||
request->send(200, "text/plain", "OK");
|
||||
});
|
||||
|
||||
// FIXME OnDisable is triggered after last scan, aprx. 10 sec
|
||||
net->mesh.stationScan.task.setOnDisable(bind(&IlluCat::defaultAnimation,this));
|
||||
return MeshSprocket::activate(scheduler, network);
|
||||
} using MeshSprocket::activate;
|
||||
|
||||
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
||||
void patternWebRequestHandler(AsyncWebServerRequest *request) {
|
||||
Serial.println("POST /pixel/pattern");
|
||||
if(request->hasParam("state", true)) {
|
||||
String inStr = request->getParam("state", true)->value();
|
||||
onMessage(0, inStr);
|
||||
}
|
||||
request->send(200, "text/plain", "OK");
|
||||
}
|
||||
|
||||
virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
||||
if(type == WS_EVT_DATA){
|
||||
String frame = WsUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||
onMessage(0, frame);
|
||||
@@ -103,22 +118,28 @@ class IlluCat : public MeshSprocket {
|
||||
}
|
||||
}
|
||||
|
||||
void onMessage( uint32_t from, String &msg ) {
|
||||
virtual void onMessage( uint32_t from, String &msg ) {
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, "msg from %u = %s\n", from, msg.c_str());
|
||||
state.fromJsonString(msg);
|
||||
PIXEL_FNCS[state.mode](pixels, state.valueStr);
|
||||
}
|
||||
|
||||
void onNewConnection(uint32_t nodeId){
|
||||
virtual void onNewConnection(uint32_t nodeId){
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, "connected to %u", nodeId);
|
||||
defaultAnimation();
|
||||
// TODO publish current state to pixel/color on new node
|
||||
}
|
||||
void onConnectionChanged(){
|
||||
virtual void onConnectionChanged(){
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, "connection changed");
|
||||
if(!net->mesh.getNodeList().size()){
|
||||
defaultAnimation();
|
||||
}
|
||||
}
|
||||
|
||||
void loop(){
|
||||
MeshSprocket::loop();
|
||||
dnsServer->processNextRequest();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user