Make EPD colors changable without restart

This commit is contained in:
Djuri Baars 2023-10-21 14:49:42 +02:00
parent da4719c046
commit 088677872f
6 changed files with 114 additions and 24 deletions

View file

@ -0,0 +1,22 @@
int fgColor;
int bgColor;
int getBgColor()
{
return bgColor;
}
int getFgColor()
{
return fgColor;
}
void setBgColor(int color)
{
bgColor = color;
}
void setFgColor(int color)
{
fgColor = color;
}

View file

@ -0,0 +1,4 @@
int getBgColor();
int getFgColor();
void setBgColor(int color);
void setFgColor(int color);

View file

@ -11,8 +11,7 @@ const int MCP_INT_PIN = 8;
#endif #endif
bool timerRunning = true; bool timerRunning = true;
int fgColor;
int bgColor;
uint wifiConnectionLostCount = 0; uint wifiConnectionLostCount = 0;
#ifdef WITH_RGB_LED #ifdef WITH_RGB_LED
@ -157,8 +156,8 @@ void setupPreferences()
timerSeconds = preferences.getUInt("timerSeconds", 1800); timerSeconds = preferences.getUInt("timerSeconds", 1800);
currentScreen = preferences.getUInt("currentScreen", 0); currentScreen = preferences.getUInt("currentScreen", 0);
// handleScreenTasks(currentScreen); // handleScreenTasks(currentScreen);
fgColor = preferences.getUInt("fgColor", DEFAULT_FG_COLOR); setFgColor(preferences.getUInt("fgColor", DEFAULT_FG_COLOR));
bgColor = preferences.getUInt("bgColor", DEFAULT_BG_COLOR); setBgColor(preferences.getUInt("bgColor", DEFAULT_BG_COLOR));
preferences.getBool("ledFlashOnUpd", false); preferences.getBool("ledFlashOnUpd", false);
screenNameMap = {{SCREEN_BLOCK_HEIGHT, "Block Height"}, screenNameMap = {{SCREEN_BLOCK_HEIGHT, "Block Height"},
@ -300,15 +299,6 @@ void toggleScreenTimer()
} }
} }
int getBgColor()
{
return bgColor;
}
int getFgColor()
{
return fgColor;
}
void timebasedChangeTask(void *parameter) void timebasedChangeTask(void *parameter)
{ {

View file

@ -7,6 +7,7 @@
#include "config.h" #include "config.h"
#include "shared.hpp" #include "shared.hpp"
#include "Adafruit_GFX.h" #include "Adafruit_GFX.h"
#include "lib/epd.hpp"
#include "tasks/blocknotify.hpp" #include "tasks/blocknotify.hpp"
#include "tasks/minute.hpp" #include "tasks/minute.hpp"
#include "tasks/get_price.hpp" #include "tasks/get_price.hpp"
@ -31,8 +32,6 @@ void toggleScreenTimer();
void timebasedChangeTask(void *parameter); void timebasedChangeTask(void *parameter);
GFXfont getFont(uint size); GFXfont getFont(uint size);
int getBgColor();
int getFgColor();
void setupSoftAP(); void setupSoftAP();
void nextScreen(); void nextScreen();

View file

@ -26,6 +26,7 @@ void setupWebserver()
server.on("/api/action/update", HTTP_GET, onApiActionUpdate); server.on("/api/action/update", HTTP_GET, onApiActionUpdate);
server.on("/api/full_refresh", HTTP_GET, onApiFullRefresh); server.on("/api/full_refresh", HTTP_GET, onApiFullRefresh);
server.on("/api/status", HTTP_GET, onApiStatus); server.on("/api/status", HTTP_GET, onApiStatus);
server.on("/api/system_status", HTTP_GET, onApiSystemStatus);
server.on("/api/settings", HTTP_GET, onApiSettingsGet); server.on("/api/settings", HTTP_GET, onApiSettingsGet);
server.on("/api/settings", HTTP_POST, onApiSettingsPost); server.on("/api/settings", HTTP_POST, onApiSettingsPost);
@ -67,6 +68,10 @@ void setupWebserver()
Serial.println("Webserver should be running"); Serial.println("Webserver should be running");
} }
/**
* @Api
* @Path("/api/status")
*/
void onApiStatus(AsyncWebServerRequest *request) void onApiStatus(AsyncWebServerRequest *request)
{ {
AsyncResponseStream *response = request->beginResponseStream("application/json"); AsyncResponseStream *response = request->beginResponseStream("application/json");
@ -101,6 +106,10 @@ void onApiStatus(AsyncWebServerRequest *request)
request->send(response); request->send(response);
} }
/**
* @Api
* @Path("/api/action/pause")
*/
void onApiActionPause(AsyncWebServerRequest *request) void onApiActionPause(AsyncWebServerRequest *request)
{ {
timerRunning = false; timerRunning = false;
@ -109,6 +118,10 @@ void onApiActionPause(AsyncWebServerRequest *request)
request->send(200); request->send(200);
}; };
/**
* @Api
* @Path("/api/action/full_refresh")
*/
void onApiFullRefresh(AsyncWebServerRequest *request) void onApiFullRefresh(AsyncWebServerRequest *request)
{ {
@ -117,6 +130,10 @@ void onApiFullRefresh(AsyncWebServerRequest *request)
request->send(200); request->send(200);
} }
/**
* @Api
* @Path("/api/action/timer_restart")
*/
void onApiActionTimerRestart(AsyncWebServerRequest *request) void onApiActionTimerRestart(AsyncWebServerRequest *request)
{ {
// moment = millis(); // moment = millis();
@ -126,6 +143,11 @@ void onApiActionTimerRestart(AsyncWebServerRequest *request)
request->send(200); request->send(200);
} }
/**
* @Api
* @Path("/api/action/update")
* @Parameter int rate Time in minutes
*/
void onApiActionUpdate(AsyncWebServerRequest *request) void onApiActionUpdate(AsyncWebServerRequest *request)
{ {
if (request->hasParam("rate")) if (request->hasParam("rate"))
@ -140,6 +162,11 @@ void onApiActionUpdate(AsyncWebServerRequest *request)
request->send(200); request->send(200);
} }
/**
* @Api
* @Method GET
* @Path("/api/settings")
*/
void onApiSettingsGet(AsyncWebServerRequest *request) void onApiSettingsGet(AsyncWebServerRequest *request)
{ {
DynamicJsonDocument root(1024); DynamicJsonDocument root(1024);
@ -164,12 +191,12 @@ void onApiSettingsGet(AsyncWebServerRequest *request)
root["ledFlashOnUpdate"] = preferences.getBool("ledFlashOnUpd", false); root["ledFlashOnUpdate"] = preferences.getBool("ledFlashOnUpd", false);
root["ledBrightness"] = preferences.getUInt("ledBrightness", 128); root["ledBrightness"] = preferences.getUInt("ledBrightness", 128);
#ifdef GIT_REV #ifdef GIT_REV
root["gitRev"] = String(GIT_REV); root["gitRev"] = String(GIT_REV);
#endif #endif
#ifdef LAST_BUILD_TIME #ifdef LAST_BUILD_TIME
root["lastBuildTime"] = String(LAST_BUILD_TIME); root["lastBuildTime"] = String(LAST_BUILD_TIME);
#endif #endif
JsonArray screens = root.createNestedArray("screens"); JsonArray screens = root.createNestedArray("screens");
for (int i = 0; i < screenNameMap.size(); i++) for (int i = 0; i < screenNameMap.size(); i++)
@ -186,14 +213,14 @@ void onApiSettingsGet(AsyncWebServerRequest *request)
request->send(200, "application/json", responseText); request->send(200, "application/json", responseText);
} }
void onApiSettingsPost(AsyncWebServerRequest *request) bool processEpdColorSettings(AsyncWebServerRequest *request)
{ {
int params = request->params();
bool settingsChanged = false; bool settingsChanged = false;
if (request->hasParam("fgColor", true)) if (request->hasParam("fgColor", true))
{ {
AsyncWebParameter *fgColor = request->getParam("fgColor", true); AsyncWebParameter *fgColor = request->getParam("fgColor", true);
preferences.putUInt("fgColor", strtol(fgColor->value().c_str(), NULL, 16)); preferences.putUInt("fgColor", strtol(fgColor->value().c_str(), NULL, 16));
setFgColor(int(strtol(fgColor->value().c_str(), NULL, 16)));
Serial.print("Setting foreground color to "); Serial.print("Setting foreground color to ");
Serial.println(fgColor->value().c_str()); Serial.println(fgColor->value().c_str());
settingsChanged = true; settingsChanged = true;
@ -203,11 +230,37 @@ void onApiSettingsPost(AsyncWebServerRequest *request)
AsyncWebParameter *bgColor = request->getParam("bgColor", true); AsyncWebParameter *bgColor = request->getParam("bgColor", true);
preferences.putUInt("bgColor", strtol(bgColor->value().c_str(), NULL, 16)); preferences.putUInt("bgColor", strtol(bgColor->value().c_str(), NULL, 16));
setBgColor(int(strtol(bgColor->value().c_str(), NULL, 16)));
Serial.print("Setting background color to "); Serial.print("Setting background color to ");
Serial.println(bgColor->value().c_str()); Serial.println(bgColor->value().c_str());
settingsChanged = true; settingsChanged = true;
} }
return settingsChanged;
}
void onApiEpdSettingsPost(AsyncWebServerRequest *request)
{
bool settingsChanged = false;
settingsChanged = processEpdColorSettings(request);
request->send(200);
if (settingsChanged)
{
flashTemporaryLights(0, 255, 0);
Serial.println("Settings changed");
}
}
void onApiSettingsPost(AsyncWebServerRequest *request)
{
int params = request->params();
bool settingsChanged = false;
settingsChanged = processEpdColorSettings(request);
if (request->hasParam("ledFlashOnUpd", true)) if (request->hasParam("ledFlashOnUpd", true))
{ {
AsyncWebParameter *ledFlashOnUpdate = request->getParam("ledFlashOnUpd", true); AsyncWebParameter *ledFlashOnUpdate = request->getParam("ledFlashOnUpd", true);
@ -313,12 +366,15 @@ void onApiSettingsPost(AsyncWebServerRequest *request)
{ {
AsyncWebParameter *pv = request->getParam(v, true); AsyncWebParameter *pv = request->getParam(v, true);
// Don't store an empty password, probably new settings save // Don't store an empty password, probably new settings save
if (!(v.equals("rpcPass") && pv->value().length() == 0)) { if (!(v.equals("rpcPass") && pv->value().length() == 0))
{
preferences.putString(v.c_str(), pv->value().c_str()); preferences.putString(v.c_str(), pv->value().c_str());
} }
} }
} }
} else { }
else
{
preferences.putBool("useNode", false); preferences.putBool("useNode", false);
settingsChanged = true; settingsChanged = true;
} }
@ -326,7 +382,7 @@ void onApiSettingsPost(AsyncWebServerRequest *request)
request->send(200); request->send(200);
if (settingsChanged) if (settingsChanged)
{ {
flashTemporaryLights(0,255,0); flashTemporaryLights(0, 255, 0);
Serial.println("Settings changed"); Serial.println("Settings changed");
} }
@ -452,4 +508,20 @@ void onApiLightsSetColor(AsyncWebServerRequest *request)
sscanf(rgbColor.c_str(), "%02x%02x%02x", &r, &g, &b); sscanf(rgbColor.c_str(), "%02x%02x%02x", &r, &g, &b);
setLights(r, g, b); setLights(r, g, b);
request->send(200, "text/plain", rgbColor); request->send(200, "text/plain", rgbColor);
}
void onApiSystemStatus(AsyncWebServerRequest *request)
{
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonDocument root(1024);
root["espFreeHeap"] = ESP.getFreeHeap();
root["espHeapSize"] = ESP.getHeapSize();
root["espFreePsram"] = ESP.getFreePsram();
root["espPsramSize"] = ESP.getPsramSize();
String responseText;
serializeJson(root, responseText);
request->send(200, "application/json", responseText);
} }

View file

@ -6,12 +6,15 @@
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include "lib/functions.hpp" #include "lib/functions.hpp"
#include "lib/epd.hpp"
#include <string> #include <string>
#include "tasks/epd.hpp" #include "tasks/epd.hpp"
#include "shared.hpp" #include "shared.hpp"
void setupWebserver(); void setupWebserver();
void onApiStatus(AsyncWebServerRequest *request); void onApiStatus(AsyncWebServerRequest *request);
void onApiSystemStatus(AsyncWebServerRequest *request);
void onApiActionPause(AsyncWebServerRequest *request); void onApiActionPause(AsyncWebServerRequest *request);
void onApiActionTimerRestart(AsyncWebServerRequest *request); void onApiActionTimerRestart(AsyncWebServerRequest *request);
void onApiActionUpdate(AsyncWebServerRequest *request); void onApiActionUpdate(AsyncWebServerRequest *request);