From a8d5cae284f39323f6cb94e704c51af1134e4045 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Wed, 17 Oct 2018 12:08:15 +0200 Subject: [PATCH] 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_