Allow to switch animations via HTTP

This commit is contained in:
Thomas Kolb 2019-12-08 22:58:50 +01:00
parent 2c4f310395
commit f91b3c8cfb
5 changed files with 72 additions and 3 deletions

View file

@ -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<Animation> anim, bool transition = true);
void changeAnimation(DefaultAnimation animation_id, bool transition = true);
void loop(void);
void restart(void);

View file

@ -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);
};
static void handleStatic(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res);
};

View file

@ -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<Animation> anim, bool
}
}
void AnimationController::changeAnimation(AnimationController::DefaultAnimation animation_id, bool transition)
{
std::unique_ptr<Animation> anim(nullptr);
switch(animation_id) {
case FIRE_HOT:
changeAnimation(std::unique_ptr<Animation>(new FireAnimation(m_fader, false)), transition);
break;
case FIRE_COLD:
changeAnimation(std::unique_ptr<Animation>(new FireAnimation(m_fader, true)), transition);
break;
default:
return; // unknown id, do nothing
}
}
void AnimationController::loop(void)
{
if(m_animation && !m_animation->finished()) {

View file

@ -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<AnimationController::DefaultAnimation>(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);

View file

@ -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