From 2b8d75a4752e1565ba49b38b693ce44a42b2854e Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Tue, 24 Dec 2019 18:11:09 +0100 Subject: [PATCH] WebServer: API for colored scrolling text messages --- include/WebServer.h | 1 + src/Animation/AnimationController.cpp | 6 +- src/WebServer.cpp | 110 +++++++++++++++++++------- 3 files changed, 84 insertions(+), 33 deletions(-) diff --git a/include/WebServer.h b/include/WebServer.h index 6c3a08e..054fe28 100644 --- a/include/WebServer.h +++ b/include/WebServer.h @@ -47,6 +47,7 @@ class WebServer static void handleColor(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); static void handleSetAnim(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); static void handleListAnim(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res); + static void handleText(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); diff --git a/src/Animation/AnimationController.cpp b/src/Animation/AnimationController.cpp index 19844a9..4a23da0 100644 --- a/src/Animation/AnimationController.cpp +++ b/src/Animation/AnimationController.cpp @@ -51,10 +51,10 @@ void AnimationController::changeAnimation(AnimationController::DefaultAnimation case FONT_TEST: { - Bitmap bmp(16, 16); - Font::textToBitmap("Hello World!", &bmp, Fader::Color{0, 0, 32, 32}); + Bitmap bmp(0, 0); + Font::textToBitmap("Hello World! ABCDEFGHIJKLMNOPQRSTUVWXYZ", &bmp, Fader::Color{0, 0, 32, 32}); - changeAnimation(std::unique_ptr(new ImageScrollerAnimation(m_fader, &bmp)), transition); + changeAnimation(std::unique_ptr(new ImageScrollerAnimation(m_fader, &bmp, 5)), transition); } break; diff --git a/src/WebServer.cpp b/src/WebServer.cpp index 1157886..3dbb75e 100644 --- a/src/WebServer.cpp +++ b/src/WebServer.cpp @@ -8,6 +8,7 @@ #include "Config.h" #include "Fader.h" +#include "Font.h" #include "Animation/AllAnimations.h" #include "Animation/AnimationController.h" @@ -75,6 +76,39 @@ static void error400(httpsserver::HTTPResponse *res, const std::string &reason) res->setStatusCode(400); } +static bool parseColor(const std::string &colorstr, Fader::Color *color, std::string *error) +{ + if(colorstr.length() != 8) { + *error = "Wrong length of color string (expected 8 hex digits)."; + return false; + } + + uint32_t colorval = 0; + + for(size_t i = 0; i < 8; i++) { + colorval <<= 4; + + char c = colorstr[i]; + if(c >= 'A' && c <= 'F') { + colorval |= c - 'A' + 10; + } else if(c >= 'a' && c <= 'f') { + colorval |= c - 'a' + 10; + } else if(c >= '0' && c <= '9') { + colorval |= c - '0'; + } else { + *error = "Invalid character in color string (allowed: 0-9, a-f, A-F)"; + return false; + } + } + + color->r = (colorval & 0xFF000000) >> 24; + color->g = (colorval & 0x00FF0000) >> 16; + color->b = (colorval & 0x0000FF00) >> 8; + color->w = (colorval & 0x000000FF) >> 0; + + return true; +} + void WebServer::handleColor(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res) { res->setHeader("Content-Type", "text/plain"); @@ -88,46 +122,25 @@ void WebServer::handleColor(httpsserver::HTTPRequest *req, httpsserver::HTTPResp std::string colorstr = params->getRequestParameter("color"); - if(colorstr.length() != 8) { - error400(res, "Wrong length of color string (expected 8 hex digits)."); + Fader::Color color; + std::string errorMessage; + if(!parseColor(colorstr, &color, &errorMessage)) { + error400(res, errorMessage); return; } - uint32_t color = 0; - - for(size_t i = 0; i < 8; i++) { - color <<= 4; - - char c = colorstr[i]; - if(c >= 'A' && c <= 'F') { - color |= c - 'A' + 10; - } else if(c >= 'a' && c <= 'f') { - color |= c - 'a' + 10; - } else if(c >= '0' && c <= '9') { - color |= c - '0'; - } else { - error400(res, "Invalid character in color string (allowed: 0-9, a-f, A-F)"); - return; - } - } - - uint8_t r = (color & 0xFF000000) >> 24; - uint8_t g = (color & 0x00FF0000) >> 16; - uint8_t b = (color & 0x0000FF00) >> 8; - uint8_t w = (color & 0x000000FF) >> 0; - res->print("Color changed to r:"); - res->print(r); + res->print(color.r); res->print(" g:"); - res->print(g); + res->print(color.g); res->print(" b:"); - res->print(b); + res->print(color.b); res->print(" w:"); - res->println(w); + res->println(color.w); WebServer::instance().m_animController->changeAnimation( std::unique_ptr( - new FadeToColorAnimation(WebServer::instance().m_fader, Fader::Color{r,g,b,w})), + new FadeToColorAnimation(WebServer::instance().m_fader, color)), false); } @@ -183,6 +196,40 @@ void WebServer::handleListAnim(httpsserver::HTTPRequest *req, httpsserver::HTTPR res->print("]"); } +void WebServer::handleText(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res) +{ + res->setHeader("Content-Type", "text/plain"); + + httpsserver::ResourceParameters * params = req->getParams(); + + if(!params->isRequestParameterSet("text")) { + error400(res, "Required parameter 'text' not set."); + return; + } + + Fader::Color color; + + if(params->isRequestParameterSet("color")) { + std::string colorstr = params->getRequestParameter("color"); + + std::string errorMessage; + if(!parseColor(colorstr, &color, &errorMessage)) { + error400(res, errorMessage); + return; + } + } else { + color = Fader::Color{0, 0, 0, 32}; + } + + Bitmap bmp; + Font::textToBitmap(params->getRequestParameter("text").c_str(), &bmp, color); + + WebServer::instance().m_animController->changeAnimation( + std::unique_ptr( + new ImageScrollerAnimation(WebServer::instance().m_fader, &bmp, 5)), + false); +} + void WebServer::handleUpdate(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res) { res->setHeader("Content-Type", "text/plain"); @@ -286,6 +333,8 @@ void WebServer::serverTask(void *arg) new httpsserver::ResourceNode("/api/setanim", "GET", WebServer::handleSetAnim); httpsserver::ResourceNode *nodeAPIListAnim = new httpsserver::ResourceNode("/api/listanim", "GET", WebServer::handleListAnim); + httpsserver::ResourceNode *nodeAPIText = + new httpsserver::ResourceNode("/api/text", "GET", WebServer::handleText); httpsserver::ResourceNode *nodeAPIUpdate = new httpsserver::ResourceNode("/api/update", "POST", WebServer::handleUpdate); httpsserver::ResourceNode *nodeAPIChallenge = @@ -303,6 +352,7 @@ void WebServer::serverTask(void *arg) server.m_server->registerNode(nodeAPIColor); server.m_server->registerNode(nodeAPISetAnim); server.m_server->registerNode(nodeAPIListAnim); + server.m_server->registerNode(nodeAPIText); server.m_server->registerNode(nodeAPIUpdate); server.m_server->registerNode(nodeAPIChallenge); server.m_server->registerNode(nodeAPIAuthTest);