From ad8ba619869e05771162eb01c06bff0f2a58e520 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 17 Sep 2018 15:34:24 +0200 Subject: [PATCH] moar topics --- .vscode/settings.json | 6 +++++- README.md | 4 ++++ lib/NeoPattern/NeoPattern.cpp | 14 ++++++++------ lib/NeoPattern/NeoPatternDto.h | 34 ++++++++++++++++++++++++++++++++++ platformio.ini | 2 +- src/IlluCat.h | 16 ++++++++++++---- src/PixelPlugin.h | 27 +++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 12 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1d4ecef..feb10db 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,10 @@ "array": "cpp", "initializer_list": "cpp", "*.tcc": "cpp", - "sstream": "cpp" + "sstream": "cpp", + "deque": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "vector": "cpp" } } \ No newline at end of file diff --git a/README.md b/README.md index 5b021e4..cbed7c2 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,10 @@ - [20%] easy to flash single binary file - [0%] audio output +## Bugs +- when new connections arrive, it somehow switches to defaultAnimation +- animations don't seem to work properly anymore + ## Enduser Setup 1. Scan for access points 1. connect to illucat-mesh diff --git a/lib/NeoPattern/NeoPattern.cpp b/lib/NeoPattern/NeoPattern.cpp index eabd2db..8a507a7 100644 --- a/lib/NeoPattern/NeoPattern.cpp +++ b/lib/NeoPattern/NeoPattern.cpp @@ -30,11 +30,12 @@ class NeoPattern : public Adafruit_NeoPixel pattern ActivePattern; // which pattern is running direction Direction; // direction to run the pattern - unsigned long Interval; // milliseconds between updates - unsigned long lastUpdate; // last update of position + unsigned long Interval = 150; // milliseconds between updates + unsigned long lastUpdate = 0; // last update of position - uint32_t Color1, Color2; // What colors are in use - uint16_t TotalSteps; // total number of steps in the pattern + uint32_t Color1 = 0; + uint32_t Color2 = 0; // What colors are in use + uint16_t TotalSteps = 255; // total number of steps in the pattern uint16_t Index; // current step within the pattern uint16_t completed = 0; @@ -45,19 +46,20 @@ class NeoPattern : public Adafruit_NeoPixel :Adafruit_NeoPixel(pixels, pin, type) { OnComplete = callback; + TotalSteps = numPixels(); } NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type) :Adafruit_NeoPixel(pixels, pin, type) { - + TotalSteps = numPixels(); } void onCompleteDefault(int pixels) { if(ActivePattern != RAINBOW_CYCLE){ //Serial.println("reversing"); - Reverse(); } + Reverse(); //Serial.println("pattern completed"); } diff --git a/lib/NeoPattern/NeoPatternDto.h b/lib/NeoPattern/NeoPatternDto.h index fcbfb6d..15897ef 100644 --- a/lib/NeoPattern/NeoPatternDto.h +++ b/lib/NeoPattern/NeoPatternDto.h @@ -32,6 +32,38 @@ struct NeoPixelConfig : public JsonStruct { } }; +struct NeoPatternMsg : public JsonStruct { + String pattern; + String payload; + String topic; + uint color; + uint color2; + uint totalSteps; + void mapJsonObject(JsonObject& root) { + root["topic"] = topic; + root["pattern"] = pattern; + root["payload"] = payload; + root["color"] = color; + root["color2"] = color2; + root["totalSteps"] = totalSteps; + } + // Map a json object to this struct. + void fromJsonObject(JsonObject& json){ + if(!verifyJsonObject(json)){ + PRINT_MSG(Serial, "fromJsonObject", "cannot parse JSON"); + valid = 0; + return; + } + topic = getAttrFromJson(json, "topic"); + color = getIntAttrFromJson(json, "color"); + color2 = getIntAttrFromJson(json, "color2"); + pattern = getAttrFromJson(json, "pattern"); + payload = getAttrFromJson(json, "payload"); + totalSteps = getIntAttrFromJson(json, "totalSteps", 255); + valid = 1; + }; +}; + struct NeoPatternDto : public JsonStruct { uint mode; uint value; @@ -51,12 +83,14 @@ struct NeoPatternDto : public JsonStruct { void fromJsonObject(JsonObject& json){ if(!verifyJsonObject(json)){ PRINT_MSG(Serial, "fromJsonObject", "cannot parse JSON"); + valid = 0; return; } mode = atoi(json[JSON_MODE_NODE]); mode = mode < ARRAY_LENGTH(PIXEL_FNCS) ? mode : 0; value = json[JSON_VALUE]; valueStr = json[JSON_VALUE]; + valid = 1; }; }; diff --git a/platformio.ini b/platformio.ini index 8d5acb5..9971116 100644 --- a/platformio.ini +++ b/platformio.ini @@ -39,4 +39,4 @@ lib_deps = ${common.lib_deps} ESPAsyncTCP Adafruit NeoPixel DNSServer - https://gitlab.com/wirelos/sprocket-core.git#develop \ No newline at end of file + https://gitlab.com/wirelos/sprocket-core.git#mediator \ No newline at end of file diff --git a/src/IlluCat.h b/src/IlluCat.h index 8487e3b..dd59af8 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -53,6 +53,7 @@ class IlluCat : public MeshSprocket { pixelConfig.defaultColor = 100; } + // TDOO remove virtual void scanningAnimation() { pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval); //pixels->Fade(0, pixels->Color(255,255,255), 4, pixelConfig.updateInterval, FORWARD); @@ -97,7 +98,8 @@ class IlluCat : public MeshSprocket { server->addHandler(ws); // FIXME OnDisable is triggered after last scan, aprx. 10 sec - net->mesh.stationScan.task.setOnDisable(bind(&IlluCat::defaultAnimation,this)); + // FIXME chck if this is also triggered when new connection arrives + //net->mesh.stationScan.task.setOnDisable(bind(&IlluCat::defaultAnimation,this)); return MeshSprocket::activate(scheduler, network); } using MeshSprocket::activate; @@ -113,9 +115,15 @@ class IlluCat : public MeshSprocket { 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); - net->mesh.sendBroadcast(frame); - client->text(String(millis())); + //onMessage(0, frame); + NeoPatternMsg msg; + msg.fromJsonString(frame); + if(msg.valid){ + // TODO convert message to send to mesh + //net->mesh.sendBroadcast(frame); + publish(msg.topic, msg.payload); + client->text(String(millis())); + } } } diff --git a/src/PixelPlugin.h b/src/PixelPlugin.h index 8345da0..99579d4 100644 --- a/src/PixelPlugin.h +++ b/src/PixelPlugin.h @@ -26,11 +26,38 @@ class PixelPlugin : public Plugin { pixels->setBrightness(pixelConfig.brightness); } void activate(Scheduler* userScheduler, Network* network){ + 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)); + animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this)); userScheduler->addTask(animation); animation.enable(); Serial.println("NeoPixels activated"); } + 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->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->ActivePattern = (pattern)atoi(msg.c_str()); + } void animate(){ pixels->Update(); }