diff --git a/include/Animation/AnimationController.h b/include/Animation/AnimationController.h index 27f1c81..4f8e828 100644 --- a/include/Animation/AnimationController.h +++ b/include/Animation/AnimationController.h @@ -7,9 +7,18 @@ class AnimationController { public: + enum DefaultAnimation { + FIRE_HOT = 0, + FIRE_COLD = 1, + + NUM_DEFAULT_ANIMATIONS + }; + AnimationController(Fader *fader); void changeAnimation(std::unique_ptr anim, bool transition = true); + void changeAnimation(DefaultAnimation animation_id, bool transition = true); + void loop(void); void restart(void); diff --git a/include/WebServer.h b/include/WebServer.h index 63e3779..a83557d 100644 --- a/include/WebServer.h +++ b/include/WebServer.h @@ -7,6 +7,7 @@ #include "ChallengeResponse.h" class Fader; +class AnimationController; class WebServer { @@ -14,6 +15,7 @@ class WebServer bool start(void); void setFader(Fader *fader) { m_fader = fader;} + void setAnimationController(AnimationController *controller) { m_animController = controller;} static WebServer &instance(void) { @@ -26,18 +28,20 @@ class WebServer ChallengeResponse m_cr; Fader *m_fader; + AnimationController *m_animController; WebServer(void); static void serverTask(void *arg); - static void serveFile(String filename, httpsserver::HTTPResponse *res); + static bool serveFile(String filename, httpsserver::HTTPResponse *res); // handlers static void handleRoot(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); static void handleColor(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); + static void handleSetAnim(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); static void handleChallenge(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); static void handleAuthTest(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); static void handleUpdate(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); - static void handle404(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); -}; \ No newline at end of file + static void handleStatic(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); +}; diff --git a/src/Animation/AnimationController.cpp b/src/Animation/AnimationController.cpp index 75270e1..15fd7f3 100644 --- a/src/Animation/AnimationController.cpp +++ b/src/Animation/AnimationController.cpp @@ -1,5 +1,7 @@ #include "Animation/AnimationController.h" +#include "Animation/AllAnimations.h" + AnimationController::AnimationController(Fader *fader) : m_fader(fader), m_animation(nullptr), m_frame(0) {} @@ -17,6 +19,24 @@ void AnimationController::changeAnimation(std::unique_ptr anim, bool } } +void AnimationController::changeAnimation(AnimationController::DefaultAnimation animation_id, bool transition) +{ + std::unique_ptr anim(nullptr); + + switch(animation_id) { + case FIRE_HOT: + changeAnimation(std::unique_ptr(new FireAnimation(m_fader, false)), transition); + break; + + case FIRE_COLD: + changeAnimation(std::unique_ptr(new FireAnimation(m_fader, true)), transition); + break; + + default: + return; // unknown id, do nothing + } +} + void AnimationController::loop(void) { if(m_animation && !m_animation->finished()) { diff --git a/src/WebServer.cpp b/src/WebServer.cpp index 897b0ef..fed5684 100644 --- a/src/WebServer.cpp +++ b/src/WebServer.cpp @@ -8,6 +8,7 @@ #include "Config.h" #include "Fader.h" +#include "Animation/AnimationController.h" WebServer::WebServer(void) : m_cr(Config::instance().getCRPassword()), m_fader(NULL) @@ -108,6 +109,37 @@ void WebServer::handleColor(httpsserver::HTTPRequest *req, httpsserver::HTTPResp WebServer::instance().m_fader->fade_color({r,g,b,w}); } +void WebServer::handleSetAnim(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res) +{ + res->setHeader("Content-Type", "text/plain"); + + httpsserver::ResourceParameters * params = req->getParams(); + + if(!params->isRequestParameterSet("anim")) { + error400(res, "Parameter 'anim' not set."); + return; + } + + std::istringstream animstr(params->getRequestParameter("anim")); + + uint32_t anim_id; + animstr >> anim_id; + + if(animstr.bad() || animstr.fail()) { + error400(res, "Parameter 'anim' could not be parsed as number."); + return; + } + + if(anim_id >= AnimationController::NUM_DEFAULT_ANIMATIONS) { + error400(res, "Animation ID out of range."); + return; + } + + WebServer::instance().m_animController->changeAnimation( + static_cast(anim_id), + true); +} + void WebServer::handleUpdate(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res) { res->setHeader("Content-Type", "text/plain"); @@ -202,6 +234,8 @@ void WebServer::serverTask(void *arg) WebServer::handleRoot); httpsserver::ResourceNode *nodeColor = new httpsserver::ResourceNode("/color", "GET", WebServer::handleColor); + httpsserver::ResourceNode *nodeSetAnim = new httpsserver::ResourceNode("/setanim", "GET", + WebServer::handleSetAnim); httpsserver::ResourceNode *nodeUpdate = new httpsserver::ResourceNode("/update", "POST", WebServer::handleUpdate); httpsserver::ResourceNode *nodeChallenge = new httpsserver::ResourceNode("/challenge", "GET", @@ -217,6 +251,7 @@ void WebServer::serverTask(void *arg) server.m_server->registerNode(nodeRoot); server.m_server->registerNode(nodeColor); + server.m_server->registerNode(nodeSetAnim); server.m_server->registerNode(nodeUpdate); server.m_server->registerNode(nodeChallenge); server.m_server->registerNode(nodeAuthTest); diff --git a/src/main.cpp b/src/main.cpp index b7302ac..9c9410a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -244,6 +244,7 @@ void setup() // start the web server WebServer::instance().setFader(&ledFader); + WebServer::instance().setAnimationController(&animController); WebServer::instance().start(); // start the update server