From a8d5cae284f39323f6cb94e704c51af1134e4045 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Wed, 17 Oct 2018 12:08:15 +0200 Subject: [PATCH 1/9] separate declarations --- include/readme.txt | 39 +++++++++++++ src/PixelPlugin.cpp | 94 ++++++++++++++++++++++++++++++++ src/PixelPlugin.h | 84 +++++----------------------- src/wifi/{WebCat.h => WebCat.h_} | 0 4 files changed, 146 insertions(+), 71 deletions(-) create mode 100644 include/readme.txt create mode 100644 src/PixelPlugin.cpp rename src/wifi/{WebCat.h => WebCat.h_} (100%) diff --git a/include/readme.txt b/include/readme.txt new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/readme.txt @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/src/PixelPlugin.cpp b/src/PixelPlugin.cpp new file mode 100644 index 0000000..8869362 --- /dev/null +++ b/src/PixelPlugin.cpp @@ -0,0 +1,94 @@ +#include "PixelPlugin.h" + +PixelPlugin::PixelPlugin(NeoPixelConfig cfg, NeoPattern *neoPattern) +{ + pixelConfig = cfg; + pixels = neoPattern; + pixels->begin(); + pixels->setBrightness(pixelConfig.brightness); +} + +void PixelPlugin::activate(Scheduler *userScheduler, Network *network) +{ + 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)); + + animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this)); + userScheduler->addTask(animation); + animation.enable(); + PRINT_MSG(Serial, SPROCKET_TYPE, "NeoPixels activated"); +} + +void PixelPlugin::setState(String msg) +{ + PRINT_MSG(Serial, SPROCKET_TYPE, 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(); +} + +void PixelPlugin::enable() +{ + animation.enable(); +} + +void PixelPlugin::disable() +{ + animation.disable(); +} \ No newline at end of file diff --git a/src/PixelPlugin.h b/src/PixelPlugin.h index ae9f331..937ec2c 100644 --- a/src/PixelPlugin.h +++ b/src/PixelPlugin.h @@ -9,6 +9,7 @@ #include "Plugin.h" #include "NeoPatternDto.h" #include "NeoPattern.cpp" +#include "config.h" using namespace std; using namespace std::placeholders; @@ -20,77 +21,18 @@ class PixelPlugin : public Plugin { 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){ - 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)); - - animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this)); - userScheduler->addTask(animation); - animation.enable(); - PRINT_MSG(Serial, SPROCKET_TYPE, "NeoPixels activated"); - } - - void setState(String msg) { - PRINT_MSG(Serial, SPROCKET_TYPE, 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 colorWheel(String msg){ - int color = atoi(msg.c_str()); - pixels->ActivePattern = NONE; - pixels->ColorSet(pixels->Wheel(color)); - } - void setTotalSteps(String msg){ - pixels->TotalSteps = atoi(msg.c_str()); - } - void setBrightness(String msg){ - int inVal = atoi(msg.c_str()); - pixels->setBrightness(inVal); - pixels->show(); - } - void setColor(String msg){ - pixels->ActivePattern = NONE; - pixels->Color1 = atoi(msg.c_str()); - //if(pixels->ActivePattern == NONE){ - pixels->ColorSet(pixels->Color1); - //} - } - void setColor2(String msg){ - pixels->Color2 = atoi(msg.c_str()); - } - void setPattern(String msg){ - pixels->Index = 0; - pixels->Direction = FORWARD; - pixels->ActivePattern = (pattern)atoi(msg.c_str()); - } - void animate(){ - pixels->Update(); - } - void enable(){ - animation.enable(); - } - void disable(){ - animation.disable(); - } + PixelPlugin(NeoPixelConfig cfg, NeoPattern* neoPattern); + void activate(Scheduler* userScheduler, Network* network); + 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 \ No newline at end of file diff --git a/src/wifi/WebCat.h b/src/wifi/WebCat.h_ similarity index 100% rename from src/wifi/WebCat.h rename to src/wifi/WebCat.h_ From 7fa3683ca6c27fba26d04b24455c20b93b186bba Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Sun, 28 Oct 2018 03:21:26 +0100 Subject: [PATCH 2/9] uncouple generic web stuff into api plugin --- data/www/index.html | 2 +- data/www/script.js | 2 +- lib/NeoPattern/NeoPatternDto.h | 21 +-- .../{utils_ws.h => utils_web.h} | 12 +- src/IlluCat.cpp_ | 126 ++++++++++++++++++ src/IlluCat.h | 79 ++--------- src/WebApi.cpp | 74 ++++++++++ src/wifi/WebCat.h_ | 6 +- 8 files changed, 237 insertions(+), 85 deletions(-) rename lib/sprocket-utils/{utils_ws.h => utils_web.h} (93%) create mode 100644 src/IlluCat.cpp_ create mode 100644 src/WebApi.cpp diff --git a/data/www/index.html b/data/www/index.html index 508a322..2862670 100644 --- a/data/www/index.html +++ b/data/www/index.html @@ -2,7 +2,7 @@ - ESP Kit + IlluCat diff --git a/data/www/script.js b/data/www/script.js index 424603a..15b4f74 100644 --- a/data/www/script.js +++ b/data/www/script.js @@ -10784,7 +10784,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); -let endpoint = '/pixel'; +let endpoint = '/ws'; // gradients // https://uigradients.com diff --git a/lib/NeoPattern/NeoPatternDto.h b/lib/NeoPattern/NeoPatternDto.h index 145d402..99a1e7a 100644 --- a/lib/NeoPattern/NeoPatternDto.h +++ b/lib/NeoPattern/NeoPatternDto.h @@ -11,11 +11,12 @@ #define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0]) struct NeoPixelConfig : public JsonStruct { - int pin; - int length; - int brightness; - int updateInterval; - int defaultColor; + // FIXME constants! + int pin = 4; + int length = 8; + int brightness = 100; + int updateInterval = 100; + int defaultColor = 100; // FIXME remove unused void mapJsonObject(JsonObject& root) { root["pin"] = pin; root["length"] = length; @@ -24,11 +25,11 @@ struct NeoPixelConfig : public JsonStruct { 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"); + pin = getIntAttrFromJson(json, "pin", pin); + length = getIntAttrFromJson(json, "length", length); + brightness = getIntAttrFromJson(json, "brightness", brightness); + updateInterval = getIntAttrFromJson(json, "updateInterval", updateInterval); + defaultColor = getIntAttrFromJson(json, "defaultColor", defaultColor); } }; diff --git a/lib/sprocket-utils/utils_ws.h b/lib/sprocket-utils/utils_web.h similarity index 93% rename from lib/sprocket-utils/utils_ws.h rename to lib/sprocket-utils/utils_web.h index 33ea353..0ff8d4a 100644 --- a/lib/sprocket-utils/utils_ws.h +++ b/lib/sprocket-utils/utils_web.h @@ -1,13 +1,19 @@ -#ifndef __WSUTILS_H___ -#define __WSUTILS_H___ +#ifndef __WebUtils_H___ +#define __WebUtils_H___ #include #include #include #include -class WsUtils { +class WebUtils { public: + static String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){ + if(request->hasParam(param, isPost)) { + return request->getParam(param, isPost)->value(); + } + return defaultValue; + } static String parseFrame(AwsEventType type, void * arg, uint8_t *data, size_t len) { String msg = ""; if(type == WS_EVT_DATA){ diff --git a/src/IlluCat.cpp_ b/src/IlluCat.cpp_ new file mode 100644 index 0000000..890fab4 --- /dev/null +++ b/src/IlluCat.cpp_ @@ -0,0 +1,126 @@ +#include "IlluCat.h" + +IlluCat::IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) +{ + Serial.println("illucat ctr"); + sprocketConfig = cfg; + otaConfig = otaCfg; + webConfig = webCfg; + pixelConfig.pin = 4; + pixelConfig.length = 8; + pixelConfig.brightness = 32; + pixelConfig.updateInterval = 100; + pixelConfig.defaultColor = 100; + catScheduler = new Scheduler(); +} + +void IlluCat::scanningAnimation() +{ + pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval); +} +void IlluCat::defaultAnimation() +{ + pixels->RainbowCycle(pixelConfig.updateInterval); +} + +/* Sprocket *IlluCat::activate(Scheduler *scheduler, Network *network) +{ + + // load config files from SPIFFS + if (SPIFFS.begin()) + { + pixelConfig.fromFile("/pixelConfig.json"); + defaultState.fromFile("/pixelState.json"); + state = defaultState; + } + + // initialize services + pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800); + server = new AsyncWebServer(80); + ws = new AsyncWebSocket("/pixel"); + //wsStream = new AsyncWebSocket("/stream"); + defaultAnimation(); + + // add plugins + // TODO add HTTP OTA instead of TCP + //addPlugin(new OtaTcpPlugin(otaConfig)); + addPlugin(new WebServerPlugin(webConfig, server)); + addPlugin(new WebConfigPlugin(server)); + addPlugin(new PixelPlugin(pixelConfig, pixels)); + + // FIXME move to networking + //String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString(); + //PRINT_MSG(Serial, SPROCKET_TYPE, softApPrt.c_str()); + + // TODO move to plugin + // setup web stuff + server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); + server->on("/pixel/api", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1)); + ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6)); + server->addHandler(ws); + //wsStream->onEvent(bind(&IlluCat::onStream, this, _1, _2, _3, _4, _5, _6)); + //server->addHandler(wsStream); + + return Sprocket::activate(scheduler, network); +} using Sprocket::activate; */ + +// TODO move to utils +/* String IlluCat::getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost) +{ + if (request->hasParam(param, isPost)) + { + return request->getParam(param, isPost)->value(); + } + return defaultValue; +} + +void IlluCat::onStream(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) +{ + if (type == WS_EVT_DATA) + { + PRINT_MSG(Serial, SPROCKET_TYPE, WebUtils::parseFrameAsString(type, arg, data, len, 0).c_str()); + pixels->ActivePattern = NONE; + pixels->handleStream(data, len); + } +} + +void IlluCat::patternWebRequestHandler(AsyncWebServerRequest *request) +{ + PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api"); + currentMessage.topic = getRequestParameterOrDefault(request, "topic", ""); + currentMessage.payload = getRequestParameterOrDefault(request, "payload", ""); + currentMessage.broadcast = atoi(getRequestParameterOrDefault(request, "broadcast", "0").c_str()); + String msg = currentMessage.toJsonString(); + publish(currentMessage.topic, currentMessage.payload); + if (currentMessage.broadcast) + { + network.broadcast(msg); + } + request->send(200, "text/plain", msg); +} + +void IlluCat::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) +{ + if (type == WS_EVT_DATA) + { + String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0); + dispatch(0, frame); + network.broadcast(frame); + } +} + +void IlluCat::dispatch(uint32_t from, String &msg) +{ + currentMessage.fromJsonString(msg); + if (currentMessage.valid) + { + currentMessage.from = from; + publish(currentMessage.topic, currentMessage.payload); + } +} + +void IlluCat::loop() +{ + Sprocket::loop(); + yield(); +} */ \ No newline at end of file diff --git a/src/IlluCat.h b/src/IlluCat.h index 9ab820d..4baadcf 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -11,11 +11,12 @@ #include "NeoPattern_api_json.h" #include "NeoPattern_api_modes.cpp" #include "utils_print.h" -#include "utils_ws.h" +#include "utils_web.h" #include #include #include #include +#include "WebApi.cpp" #include "PixelPlugin.h" using namespace std; @@ -28,8 +29,6 @@ class IlluCat : public Sprocket { NeoPatternDto defaultState; NeoPatternDto state; AsyncWebServer* server; - AsyncWebSocket* ws; - //AsyncWebSocket* wsStream; NeoPixelConfig pixelConfig; SprocketConfig sprocketConfig; @@ -42,24 +41,14 @@ class IlluCat : public Sprocket { sprocketConfig = cfg; otaConfig = otaCfg; webConfig = webCfg; - pixelConfig.pin = 4; - pixelConfig.length = 8; - pixelConfig.brightness = 32; - pixelConfig.updateInterval = 100; - pixelConfig.defaultColor = 100; } - virtual void scanningAnimation() { - pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval); - } - virtual void defaultAnimation() { - pixels->RainbowCycle(pixelConfig.updateInterval); - } Sprocket* activate(Scheduler* scheduler, Network* network) { // load config files from SPIFFS if(SPIFFS.begin()){ pixelConfig.fromFile("/pixelConfig.json"); + // FIXME actualy store and load state to use as initial animation defaultState.fromFile("/pixelState.json"); state = defaultState; } @@ -67,72 +56,32 @@ class IlluCat : public Sprocket { // initialize services pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800); server = new AsyncWebServer(80); - ws = new AsyncWebSocket("/pixel"); - //wsStream = new AsyncWebSocket("/stream"); defaultAnimation(); // add plugins + // TODO how can any type of API be linked to another one? // TODO add HTTP OTA instead of TCP //addPlugin(new OtaTcpPlugin(otaConfig)); addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebConfigPlugin(server)); + addPlugin(new WebApi(server, 1)); + // TODO pixel streaming addPlugin(new PixelPlugin(pixelConfig, pixels)); - - // FIXME move to networking - //String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString(); - //PRINT_MSG(Serial, SPROCKET_TYPE, softApPrt.c_str()); - - // TODO move to plugin // setup web stuff server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); - server->on("/pixel/api", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1)); - ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6)); - server->addHandler(ws); - //wsStream->onEvent(bind(&IlluCat::onStream, this, _1, _2, _3, _4, _5, _6)); - //server->addHandler(wsStream); return Sprocket::activate(scheduler, network); } using Sprocket::activate; - // TODO move to utils - String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){ - if(request->hasParam(param, isPost)) { - return request->getParam(param, isPost)->value(); - } - return defaultValue; - } - - - void onStream(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { - if(type == WS_EVT_DATA){ - PRINT_MSG(Serial, SPROCKET_TYPE, WsUtils::parseFrameAsString(type, arg, data, len, 0).c_str()); - pixels->ActivePattern = NONE; - pixels->handleStream(data, len); - } - } - - void patternWebRequestHandler(AsyncWebServerRequest *request) { - PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api"); - currentMessage.topic = getRequestParameterOrDefault(request, "topic", ""); - currentMessage.payload = getRequestParameterOrDefault(request, "payload", ""); - currentMessage.broadcast = atoi(getRequestParameterOrDefault(request, "broadcast", "0").c_str()); - String msg = currentMessage.toJsonString(); - publish(currentMessage.topic, currentMessage.payload); - if(currentMessage.broadcast){ - network.broadcast(msg); - } - request->send(200, "text/plain", msg); - } - - 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); - dispatch(0, frame); - network.broadcast(frame); - } + virtual void scanningAnimation() { + pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval); + } + virtual void defaultAnimation() { + pixels->RainbowCycle(pixelConfig.updateInterval); } + // TODO move to Sprocket virtual void dispatch( uint32_t from, String &msg ) { currentMessage.fromJsonString(msg); if(currentMessage.valid){ @@ -141,10 +90,6 @@ class IlluCat : public Sprocket { } } - void loop(){ - Sprocket::loop(); - yield(); - } }; #endif \ No newline at end of file diff --git a/src/WebApi.cpp b/src/WebApi.cpp new file mode 100644 index 0000000..1c5860c --- /dev/null +++ b/src/WebApi.cpp @@ -0,0 +1,74 @@ +#ifndef __WEBAPI_PLUGIN__ +#define __WEBAPI_PLUGIN__ + +#include +#include + +#include "config.h" +#include "utils_print.h" +#include "utils_web.h" +#include +#include + +using namespace std; +using namespace std::placeholders; + +// TODO headerfile + +class WebApi : public Plugin { + private: + Network* network; + public: + AsyncWebServer* server; + AsyncWebSocket* ws; + SprocketMessage currentMessage; + + int broadcast; + + WebApi(AsyncWebServer* _server, int _broadcast = 0){ + server = _server; + broadcast = _broadcast; + } + + void activate(Scheduler* _scheduler, Network* _network) { + network = _network; + ws = new AsyncWebSocket("/ws"); // FIXME constant /ws + ws->onEvent(bind(&WebApi::onWsEvent, this, _1, _2, _3, _4, _5, _6)); + server->addHandler(ws); + server->on("/api", HTTP_POST, bind(&WebApi::postRequestHandler, this, _1)); + } + + void postRequestHandler(AsyncWebServerRequest *request) { + PRINT_MSG(Serial, SPROCKET_TYPE, "POST WebApi"); + currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", ""); + currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", ""); + currentMessage.broadcast = atoi(WebUtils::getRequestParameterOrDefault(request, "broadcast", "0").c_str()); + String msg = currentMessage.toJsonString(); + publish(currentMessage.topic, currentMessage.payload); + if(currentMessage.broadcast){ + network->broadcast(msg); + } + request->send(200, "text/plain", msg); + } + void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { + // FIXME to limitted + if(type == WS_EVT_DATA){ + String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0); + dispatch(0, frame); + } + } + + void dispatch( uint32_t from, String &msg ) { + currentMessage.fromJsonString(msg); + if(currentMessage.valid){ + currentMessage.from = from; + publish(currentMessage.topic, currentMessage.payload); + if(broadcast){ + network->broadcast(msg); + } + } + } + +}; + +#endif \ No newline at end of file diff --git a/src/wifi/WebCat.h_ b/src/wifi/WebCat.h_ index 54fb09d..83f6ee8 100644 --- a/src/wifi/WebCat.h_ +++ b/src/wifi/WebCat.h_ @@ -11,7 +11,7 @@ #include "NeoPattern_api_json.h" #include "NeoPattern_api_modes.cpp" #include "utils_print.h" -#include "utils_ws.h" +#include "utils_web.h" #include #include #include @@ -74,7 +74,7 @@ class WebCat : public IlluCat { void onStream(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { if(type == WS_EVT_DATA){ - PRINT_MSG(Serial, SPROCKET_TYPE, WsUtils::parseFrameAsString(type, arg, data, len, 0).c_str()); + PRINT_MSG(Serial, SPROCKET_TYPE, WebUtils::parseFrameAsString(type, arg, data, len, 0).c_str()); pixels->ActivePattern = NONE; pixels->handleStream(data, len); } @@ -95,7 +95,7 @@ class WebCat : public IlluCat { 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); + String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0); dispatch(0, frame); network.broadcast(frame); } From d4d40428cc583020debc18d0668145e85c41120e Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 29 Oct 2018 21:00:27 +0100 Subject: [PATCH 3/9] fire animation added --- data/www/index.html | 4 +- lib/NeoPattern/NeoPattern.cpp | 100 +++++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 11 deletions(-) diff --git a/data/www/index.html b/data/www/index.html index 2862670..4bb3ac4 100644 --- a/data/www/index.html +++ b/data/www/index.html @@ -2,7 +2,7 @@ - IlluCat + ESP Kit @@ -39,7 +39,7 @@ data-name="pattern" data-topic="pixels/pattern" data-default="0" - data-entries='[{"text": "None", "value": "0"}, {"text": "Rainbow", "value": "1"}, {"text": "TheaterChase", "value": "2"}, {"text": "Color Wipe", "value": "3"}, {"text": "Scanner", "value": "4"}, {"text": "Fade", "value": "5"}]' + data-entries='[{"text": "None", "value": "0"}, {"text": "Rainbow", "value": "1"}, {"text": "TheaterChase", "value": "2"}, {"text": "Color Wipe", "value": "3"}, {"text": "Scanner", "value": "4"}, {"text": "Fade", "value": "5"}, {"text": "Fire", "value": "6"}]' >
  • 0){ + if (bufferSize > 0) + { drawFrameBuffer(TotalSteps, frameBuffer, bufferSize); } break; @@ -379,6 +386,81 @@ class NeoPattern : public Adafruit_NeoPixel return Color(WheelPos * 3, 255 - WheelPos * 3, 0); } } + void Fire(int Cooling, int Sparking) + { + byte heat[numPixels()]; + int cooldown; + + // Step 1. Cool down every cell a little + for (int i = 0; i < numPixels(); i++) + { + cooldown = random(0, ((Cooling * 10) / numPixels()) + 2); + + if (cooldown > heat[i]) + { + heat[i] = 0; + } + else + { + heat[i] = heat[i] - cooldown; + } + } + + // Step 2. Heat from each cell drifts 'up' and diffuses a little + for (int k = numPixels() - 1; k >= 2; k--) + { + heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3; + } + + // Step 3. Randomly ignite new 'sparks' near the bottom + if (random(255) < Sparking) + { + int y = random(7); + heat[y] = heat[y] + random(160, 255); + //heat[y] = random(160,255); + } + + // Step 4. Convert heat to LED colors + for (int j = 0; j < numPixels(); j++) + { + setPixelHeatColor(j, heat[j]); + } + + showStrip(); + } + + void setPixelHeatColor(int Pixel, byte temperature) + { + // Scale 'heat' down from 0-255 to 0-191 + byte t192 = round((temperature / 255.0) * 191); + + // calculate ramp up from + byte heatramp = t192 & 0x3F; // 0..63 + heatramp <<= 2; // scale up to 0..252 + + // figure out which third of the spectrum we're in: + if (t192 > 0x80) + { // hottest + setPixel(Pixel, 255, 255, heatramp); + } + else if (t192 > 0x40) + { // middle + setPixel(Pixel, 255, heatramp, 0); + } + else + { // coolest + setPixel(Pixel, heatramp, 0, 0); + } + } + + void setPixel(int Pixel, byte red, byte green, byte blue) + { + setPixelColor(Pixel, Color(red, green, blue)); + } + void showStrip() + { + show(); + } }; #endif \ No newline at end of file From 99dbd8379b9097bd19fde2634e63b5faf6da1aaf Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 5 Nov 2018 14:57:49 +0100 Subject: [PATCH 4/9] externalize pixel stuff, add pixel instance to plugin, remove old code --- data/www/gradients.json | 4 ++++ lib/NeoPattern/NeoPattern.cpp | 5 +++++ src/IlluCat.h | 31 ++----------------------------- src/PixelPlugin.cpp | 34 +++++++++++++++++++++++++++++++++- src/PixelPlugin.h | 10 ++++++++++ src/config.h | 2 ++ src/wifi/main.cpp | 1 + src/wifiMesh/main.cpp | 1 + 8 files changed, 58 insertions(+), 30 deletions(-) diff --git a/data/www/gradients.json b/data/www/gradients.json index d184f8d..2b8556f 100644 --- a/data/www/gradients.json +++ b/data/www/gradients.json @@ -3,6 +3,10 @@ "text": "None", "value": ["#000000", "#000000"] }, + { + "text": "Stadler", + "value": ["#0B3F75", "#0B3F75"] + }, { "text": "Blu", "value": ["#00416A", "#E4E5E6"] diff --git a/lib/NeoPattern/NeoPattern.cpp b/lib/NeoPattern/NeoPattern.cpp index d72042f..aa61c85 100644 --- a/lib/NeoPattern/NeoPattern.cpp +++ b/lib/NeoPattern/NeoPattern.cpp @@ -66,6 +66,7 @@ class NeoPattern : public Adafruit_NeoPixel frameBuffer = (uint8_t *)malloc(768); OnComplete = callback; TotalSteps = numPixels(); + begin(); } NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type) @@ -73,6 +74,7 @@ class NeoPattern : public Adafruit_NeoPixel { frameBuffer = (uint8_t *)malloc(768); TotalSteps = numPixels(); + begin(); } void handleStream(uint8_t *data, size_t len) @@ -386,6 +388,9 @@ class NeoPattern : public Adafruit_NeoPixel return Color(WheelPos * 3, 255 - WheelPos * 3, 0); } } + /** + * Effects from https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/ + */ void Fire(int Cooling, int Sparking) { byte heat[numPixels()]; diff --git a/src/IlluCat.h b/src/IlluCat.h index 4baadcf..a6b2b95 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -25,12 +25,7 @@ using namespace std::placeholders; class IlluCat : public Sprocket { public: - NeoPattern* pixels; - NeoPatternDto defaultState; - NeoPatternDto state; AsyncWebServer* server; - - NeoPixelConfig pixelConfig; SprocketConfig sprocketConfig; OtaConfig otaConfig; WebServerConfig webConfig; @@ -45,42 +40,20 @@ class IlluCat : public Sprocket { Sprocket* activate(Scheduler* scheduler, Network* network) { - // load config files from SPIFFS - if(SPIFFS.begin()){ - pixelConfig.fromFile("/pixelConfig.json"); - // FIXME actualy store and load state to use as initial animation - defaultState.fromFile("/pixelState.json"); - state = defaultState; - } - - // initialize services - pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800); server = new AsyncWebServer(80); - defaultAnimation(); - - // add plugins // TODO how can any type of API be linked to another one? // TODO add HTTP OTA instead of TCP //addPlugin(new OtaTcpPlugin(otaConfig)); addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebConfigPlugin(server)); addPlugin(new WebApi(server, 1)); - // TODO pixel streaming - addPlugin(new PixelPlugin(pixelConfig, pixels)); - + // setup web stuff - server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); + server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json"); return Sprocket::activate(scheduler, network); } using Sprocket::activate; - virtual void scanningAnimation() { - pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval); - } - virtual void defaultAnimation() { - pixels->RainbowCycle(pixelConfig.updateInterval); - } - // TODO move to Sprocket virtual void dispatch( uint32_t from, String &msg ) { currentMessage.fromJsonString(msg); diff --git a/src/PixelPlugin.cpp b/src/PixelPlugin.cpp index 8869362..f9dab05 100644 --- a/src/PixelPlugin.cpp +++ b/src/PixelPlugin.cpp @@ -4,10 +4,42 @@ PixelPlugin::PixelPlugin(NeoPixelConfig cfg, NeoPattern *neoPattern) { pixelConfig = cfg; pixels = neoPattern; - pixels->begin(); + applyConfig(pixelConfig); + defaultAnimation(); +} + +PixelPlugin::PixelPlugin(NeoPattern *neoPattern) +{ + pixels = neoPattern; + loadConfigFromFile(); + 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, Network *network) { subscribe("pixels/colorWheel", bind(&PixelPlugin::colorWheel, this, _1)); diff --git a/src/PixelPlugin.h b/src/PixelPlugin.h index 937ec2c..e1a727d 100644 --- a/src/PixelPlugin.h +++ b/src/PixelPlugin.h @@ -14,6 +14,10 @@ using namespace std; using namespace std::placeholders; +#ifndef PIXEL_CONFIG_FILE +#define PIXEL_CONFIG_FILE "/pixelConfig.json" +#endif + class PixelPlugin : public Plugin { private: NeoPixelConfig pixelConfig; @@ -22,7 +26,13 @@ class PixelPlugin : public Plugin { public: Task animation; PixelPlugin(NeoPixelConfig cfg, NeoPattern* neoPattern); + PixelPlugin(NeoPattern *neoPattern); + PixelPlugin(); + void loadConfigFromFile(); + void applyConfig(NeoPixelConfig cfg); + void applyConfigFromFile(); void activate(Scheduler* userScheduler, Network* network); + void defaultAnimation(); void setState(String msg); void colorWheel(String msg); void setTotalSteps(String msg); diff --git a/src/config.h b/src/config.h index b8025ea..ba28385 100644 --- a/src/config.h +++ b/src/config.h @@ -25,6 +25,8 @@ #define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION //ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE +#define PIXEL_CONFIG_FILE "/pixelConfig.json" + // OTA config #define OTA_PORT 8266 #define OTA_PASSWORD "" diff --git a/src/wifi/main.cpp b/src/wifi/main.cpp index 2b751c7..4f22af0 100644 --- a/src/wifi/main.cpp +++ b/src/wifi/main.cpp @@ -18,6 +18,7 @@ IlluCat sprocket( ); void setup() { + sprocket.addPlugin(new PixelPlugin()); sprocket.join(net); } diff --git a/src/wifiMesh/main.cpp b/src/wifiMesh/main.cpp index a9b7f0a..4e9e5c4 100644 --- a/src/wifiMesh/main.cpp +++ b/src/wifiMesh/main.cpp @@ -15,6 +15,7 @@ MeshCat sprocket( ); void setup() { + sprocket.addPlugin(new PixelPlugin()); sprocket.join(net); } From 299f7dae65b6c6d0f8bb6b080ce4775a0da09b1a Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 5 Nov 2018 15:11:45 +0100 Subject: [PATCH 5/9] move pixel plugin back to illucat, introduce setup method for better encapsulation of plugins in order to remove activation method --- src/IlluCat.h | 15 ++++----------- src/wifi/main.cpp | 2 +- src/wifiMesh/main.cpp | 2 +- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/IlluCat.h b/src/IlluCat.h index a6b2b95..daa9c7d 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -36,23 +36,16 @@ class IlluCat : public Sprocket { sprocketConfig = cfg; otaConfig = otaCfg; webConfig = webCfg; + server = new AsyncWebServer(80); } - Sprocket* activate(Scheduler* scheduler, Network* network) { - - server = new AsyncWebServer(80); - // TODO how can any type of API be linked to another one? - // TODO add HTTP OTA instead of TCP - //addPlugin(new OtaTcpPlugin(otaConfig)); + void setup(){ + addPlugin(new PixelPlugin()); addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebConfigPlugin(server)); addPlugin(new WebApi(server, 1)); - - // setup web stuff server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json"); - - return Sprocket::activate(scheduler, network); - } using Sprocket::activate; + } // TODO move to Sprocket virtual void dispatch( uint32_t from, String &msg ) { diff --git a/src/wifi/main.cpp b/src/wifi/main.cpp index 4f22af0..05f873b 100644 --- a/src/wifi/main.cpp +++ b/src/wifi/main.cpp @@ -18,7 +18,7 @@ IlluCat sprocket( ); void setup() { - sprocket.addPlugin(new PixelPlugin()); + sprocket.setup(); sprocket.join(net); } diff --git a/src/wifiMesh/main.cpp b/src/wifiMesh/main.cpp index 4e9e5c4..6dfc6a4 100644 --- a/src/wifiMesh/main.cpp +++ b/src/wifiMesh/main.cpp @@ -15,7 +15,7 @@ MeshCat sprocket( ); void setup() { - sprocket.addPlugin(new PixelPlugin()); + sprocket.setup(); sprocket.join(net); } From a940903bd3b8db8adaf80daf1deb93d8853871ae Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 5 Nov 2018 15:34:14 +0100 Subject: [PATCH 6/9] reorder fnc calls --- src/IlluCat.h | 13 +------------ src/PixelPlugin.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/IlluCat.h b/src/IlluCat.h index daa9c7d..39be7f8 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -30,13 +30,12 @@ class IlluCat : public Sprocket { OtaConfig otaConfig; WebServerConfig webConfig; - SprocketMessage currentMessage; - IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) { sprocketConfig = cfg; otaConfig = otaCfg; webConfig = webCfg; server = new AsyncWebServer(80); + server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json"); } void setup(){ @@ -44,16 +43,6 @@ class IlluCat : public Sprocket { addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebConfigPlugin(server)); addPlugin(new WebApi(server, 1)); - server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json"); - } - - // TODO move to Sprocket - virtual void dispatch( uint32_t from, String &msg ) { - currentMessage.fromJsonString(msg); - if(currentMessage.valid){ - currentMessage.from = from; - publish(currentMessage.topic, currentMessage.payload); - } } }; diff --git a/src/PixelPlugin.cpp b/src/PixelPlugin.cpp index f9dab05..d6ad2f0 100644 --- a/src/PixelPlugin.cpp +++ b/src/PixelPlugin.cpp @@ -42,6 +42,10 @@ void PixelPlugin::defaultAnimation() { void PixelPlugin::activate(Scheduler *userScheduler, Network *network) { + 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)); @@ -50,9 +54,6 @@ void PixelPlugin::activate(Scheduler *userScheduler, Network *network) subscribe("pixels/brightness", bind(&PixelPlugin::setBrightness, this, _1)); subscribe("pixels/state", bind(&PixelPlugin::setState, this, _1)); - animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this)); - userScheduler->addTask(animation); - animation.enable(); PRINT_MSG(Serial, SPROCKET_TYPE, "NeoPixels activated"); } From a039e591daca5a616a128f4457e834ea67584859 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 5 Nov 2018 16:19:19 +0100 Subject: [PATCH 7/9] remove unused --- src/IlluCat.cpp_ | 122 +++-------------------------------------------- src/IlluCat.h | 8 +--- 2 files changed, 9 insertions(+), 121 deletions(-) diff --git a/src/IlluCat.cpp_ b/src/IlluCat.cpp_ index 890fab4..cab9551 100644 --- a/src/IlluCat.cpp_ +++ b/src/IlluCat.cpp_ @@ -2,125 +2,17 @@ IlluCat::IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) { - Serial.println("illucat ctr"); sprocketConfig = cfg; otaConfig = otaCfg; webConfig = webCfg; - pixelConfig.pin = 4; - pixelConfig.length = 8; - pixelConfig.brightness = 32; - pixelConfig.updateInterval = 100; - pixelConfig.defaultColor = 100; - catScheduler = new Scheduler(); -} - -void IlluCat::scanningAnimation() -{ - pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval); -} -void IlluCat::defaultAnimation() -{ - pixels->RainbowCycle(pixelConfig.updateInterval); -} - -/* Sprocket *IlluCat::activate(Scheduler *scheduler, Network *network) -{ - - // load config files from SPIFFS - if (SPIFFS.begin()) - { - pixelConfig.fromFile("/pixelConfig.json"); - defaultState.fromFile("/pixelState.json"); - state = defaultState; - } - - // initialize services - pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800); server = new AsyncWebServer(80); - ws = new AsyncWebSocket("/pixel"); - //wsStream = new AsyncWebSocket("/stream"); - defaultAnimation(); + server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json"); +} - // add plugins - // TODO add HTTP OTA instead of TCP - //addPlugin(new OtaTcpPlugin(otaConfig)); +void IlluCat::setup() +{ + addPlugin(new PixelPlugin()); addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebConfigPlugin(server)); - addPlugin(new PixelPlugin(pixelConfig, pixels)); - - // FIXME move to networking - //String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString(); - //PRINT_MSG(Serial, SPROCKET_TYPE, softApPrt.c_str()); - - // TODO move to plugin - // setup web stuff - server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); - server->on("/pixel/api", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1)); - ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6)); - server->addHandler(ws); - //wsStream->onEvent(bind(&IlluCat::onStream, this, _1, _2, _3, _4, _5, _6)); - //server->addHandler(wsStream); - - return Sprocket::activate(scheduler, network); -} using Sprocket::activate; */ - -// TODO move to utils -/* String IlluCat::getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost) -{ - if (request->hasParam(param, isPost)) - { - return request->getParam(param, isPost)->value(); - } - return defaultValue; -} - -void IlluCat::onStream(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) -{ - if (type == WS_EVT_DATA) - { - PRINT_MSG(Serial, SPROCKET_TYPE, WebUtils::parseFrameAsString(type, arg, data, len, 0).c_str()); - pixels->ActivePattern = NONE; - pixels->handleStream(data, len); - } -} - -void IlluCat::patternWebRequestHandler(AsyncWebServerRequest *request) -{ - PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api"); - currentMessage.topic = getRequestParameterOrDefault(request, "topic", ""); - currentMessage.payload = getRequestParameterOrDefault(request, "payload", ""); - currentMessage.broadcast = atoi(getRequestParameterOrDefault(request, "broadcast", "0").c_str()); - String msg = currentMessage.toJsonString(); - publish(currentMessage.topic, currentMessage.payload); - if (currentMessage.broadcast) - { - network.broadcast(msg); - } - request->send(200, "text/plain", msg); -} - -void IlluCat::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) -{ - if (type == WS_EVT_DATA) - { - String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0); - dispatch(0, frame); - network.broadcast(frame); - } -} - -void IlluCat::dispatch(uint32_t from, String &msg) -{ - currentMessage.fromJsonString(msg); - if (currentMessage.valid) - { - currentMessage.from = from; - publish(currentMessage.topic, currentMessage.payload); - } -} - -void IlluCat::loop() -{ - Sprocket::loop(); - yield(); -} */ \ No newline at end of file + addPlugin(new WebApi(server, 1)); +} \ No newline at end of file diff --git a/src/IlluCat.h b/src/IlluCat.h index 39be7f8..4143776 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -1,5 +1,5 @@ -#ifndef __MESH_APP__ -#define __MESH_APP__ +#ifndef __ILLUCAT__ +#define __ILLUCAT__ #include #include @@ -8,10 +8,6 @@ #include "config.h" #include "NeoPattern.cpp" #include "NeoPatternDto.h" -#include "NeoPattern_api_json.h" -#include "NeoPattern_api_modes.cpp" -#include "utils_print.h" -#include "utils_web.h" #include #include #include From 5a0225b0404ab52145eec6fc9fc0d83ad556f53f Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 5 Nov 2018 16:25:38 +0100 Subject: [PATCH 8/9] add dispatch method again to ensure compatibility with meshcat --- src/IlluCat.h | 12 +++++ src/wifi/WebCat.h_ | 118 --------------------------------------------- 2 files changed, 12 insertions(+), 118 deletions(-) delete mode 100644 src/wifi/WebCat.h_ diff --git a/src/IlluCat.h b/src/IlluCat.h index 4143776..72605f9 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -25,6 +25,8 @@ class IlluCat : public Sprocket { SprocketConfig sprocketConfig; OtaConfig otaConfig; WebServerConfig webConfig; + + SprocketMessage currentMessage; IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) { sprocketConfig = cfg; @@ -41,6 +43,16 @@ class IlluCat : public Sprocket { addPlugin(new WebApi(server, 1)); } + // TODO move to core + virtual void dispatch( uint32_t from, String &msg ) { + currentMessage.fromJsonString(msg); + if(currentMessage.valid){ + currentMessage.from = from; + publish(currentMessage.topic, currentMessage.payload); + } + } + + }; #endif \ No newline at end of file diff --git a/src/wifi/WebCat.h_ b/src/wifi/WebCat.h_ deleted file mode 100644 index 83f6ee8..0000000 --- a/src/wifi/WebCat.h_ +++ /dev/null @@ -1,118 +0,0 @@ -#ifndef __WEB_CAT__ -#define __WEB_CAT__ - -#include -#include -#include - -#include "config.h" -#include "NeoPattern.cpp" -#include "NeoPatternDto.h" -#include "NeoPattern_api_json.h" -#include "NeoPattern_api_modes.cpp" -#include "utils_print.h" -#include "utils_web.h" -#include -#include -#include -#include -#include "PixelPlugin.h" -#include "IlluCat.h" - -using namespace std; -using namespace std::placeholders; - - -class WebCat : public IlluCat { - public: - AsyncWebServer* server; - AsyncWebSocket* ws; - //AsyncWebSocket* wsStream; - WebServerConfig webConfig; - - SprocketMessage currentMessage; - - WebCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : IlluCat(cfg, otaCfg, webCfg) { - webConfig = webCfg; - } - - Sprocket* activate(Scheduler* scheduler, Network* network) { - - Serial.println("SETUP SERVER"); - // initialize services - server = new AsyncWebServer(80); - ws = new AsyncWebSocket("/pixel"); - //wsStream = new AsyncWebSocket("/stream"); - Serial.println("init handlers"); - // TODO move to plugin - // setup web stuff - server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); - server->on("/pixel/api", HTTP_POST, bind(&WebCat::patternWebRequestHandler, this, _1)); - ws->onEvent(bind(&WebCat::onWsEvent, this, _1, _2, _3, _4, _5, _6)); - server->addHandler(ws); - //wsStream->onEvent(bind(&WebCat::onStream, this, _1, _2, _3, _4, _5, _6)); - //server->addHandler(wsStream); - Serial.println("add plugins"); - // add plugins - // TODO add HTTP OTA instead of TCP - //addPlugin(new OtaTcpPlugin(otaConfig)); - addPlugin(new WebServerPlugin(webConfig, server)); - addPlugin(new WebConfigPlugin(server)); - - return IlluCat::activate(scheduler, network);; - }; - - - // TODO move to utils - String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){ - if(request->hasParam(param, isPost)) { - return request->getParam(param, isPost)->value(); - } - return defaultValue; - } - - - void onStream(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { - if(type == WS_EVT_DATA){ - PRINT_MSG(Serial, SPROCKET_TYPE, WebUtils::parseFrameAsString(type, arg, data, len, 0).c_str()); - pixels->ActivePattern = NONE; - pixels->handleStream(data, len); - } - } - - void patternWebRequestHandler(AsyncWebServerRequest *request) { - PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api"); - currentMessage.topic = getRequestParameterOrDefault(request, "topic", ""); - currentMessage.payload = getRequestParameterOrDefault(request, "payload", ""); - currentMessage.broadcast = atoi(getRequestParameterOrDefault(request, "broadcast", "0").c_str()); - String msg = currentMessage.toJsonString(); - publish(currentMessage.topic, currentMessage.payload); - if(currentMessage.broadcast){ - network.broadcast(msg); - } - request->send(200, "text/plain", msg); - } - - virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { - if(type == WS_EVT_DATA){ - String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0); - dispatch(0, frame); - network.broadcast(frame); - } - } - - virtual void dispatch( uint32_t from, String &msg ) { - currentMessage.fromJsonString(msg); - if(currentMessage.valid){ - currentMessage.from = from; - publish(currentMessage.topic, currentMessage.payload); - } - } - - void loop(){ - Sprocket::loop(); - yield(); - } -}; - -#endif \ No newline at end of file From 2ea9c0257119d326c4f0e1fd883c2e01b6039954 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 5 Nov 2018 16:57:43 +0100 Subject: [PATCH 9/9] remove dispatch method as it is now supported by core --- src/IlluCat.h | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/IlluCat.h b/src/IlluCat.h index 72605f9..4143776 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -25,8 +25,6 @@ class IlluCat : public Sprocket { SprocketConfig sprocketConfig; OtaConfig otaConfig; WebServerConfig webConfig; - - SprocketMessage currentMessage; IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) { sprocketConfig = cfg; @@ -43,16 +41,6 @@ class IlluCat : public Sprocket { addPlugin(new WebApi(server, 1)); } - // TODO move to core - virtual void dispatch( uint32_t from, String &msg ) { - currentMessage.fromJsonString(msg); - if(currentMessage.valid){ - currentMessage.from = from; - publish(currentMessage.topic, currentMessage.payload); - } - } - - }; #endif \ No newline at end of file