Code style: define constants for settings subpage IDs

This commit is contained in:
Aircoookie 2023-06-15 23:58:22 +02:00
parent bb15e1d8ac
commit 264b3a785b
5 changed files with 90 additions and 73 deletions

View File

@ -333,12 +333,29 @@
#define ERR_OVERCURRENT 31 // An attached current sensor has measured a current above the threshold (not implemented)
#define ERR_UNDERVOLT 32 // An attached voltmeter has measured a voltage below the threshold (not implemented)
//Timer mode types
// Timer mode types
#define NL_MODE_SET 0 //After nightlight time elapsed, set to target brightness
#define NL_MODE_FADE 1 //Fade to target brightness gradually
#define NL_MODE_COLORFADE 2 //Fade to target brightness and secondary color gradually
#define NL_MODE_SUN 3 //Sunrise/sunset. Target brightness is set immediately, then Sunrise effect is started. Max 60 min.
// Settings sub page IDs
#define SUBPAGE_MENU 0
#define SUBPAGE_WIFI 1
#define SUBPAGE_LEDS 2
#define SUBPAGE_UI 3
#define SUBPAGE_SYNC 4
#define SUBPAGE_TIME 5
#define SUBPAGE_SEC 6
#define SUBPAGE_DMX 7
#define SUBPAGE_UM 8
#define SUBPAGE_UPDATE 9
#define SUBPAGE_2D 10
#define SUBPAGE_LOCK 251
#define SUBPAGE_PINREQ 252
#define SUBPAGE_CSS 253
#define SUBPAGE_JS 254
#define SUBPAGE_WELCOME 255
#define NTP_PACKET_SIZE 48

View File

@ -7,18 +7,17 @@
//called upon POST settings form submit
void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
{
// PIN code request
if (subPage == 252)
if (subPage == SUBPAGE_PINREQ)
{
checkSettingsPIN(request->arg(F("PIN")).c_str());
return;
}
//0: menu 1: wifi 2: leds 3: ui 4: sync 5: time 6: sec 7: DMX 8: usermods 9: N/A 10: 2D
if (subPage <1 || subPage >10 || !correctPIN) return;
if (subPage < 1 || subPage > 10 || !correctPIN) return;
//WIFI SETTINGS
if (subPage == 1)
if (subPage == SUBPAGE_WIFI)
{
strlcpy(clientSSID,request->arg(F("CS")).c_str(), 33);
@ -57,7 +56,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
}
//LED SETTINGS
if (subPage == 2)
if (subPage == SUBPAGE_LEDS)
{
int t = 0;
@ -261,7 +260,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
}
//UI
if (subPage == 3)
if (subPage == SUBPAGE_UI)
{
strlcpy(serverDescription, request->arg(F("DS")).c_str(), 33);
syncToggleReceive = request->hasArg(F("ST"));
@ -279,7 +278,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
}
//SYNC
if (subPage == 4)
if (subPage == SUBPAGE_SYNC)
{
int t = request->arg(F("UP")).toInt();
if (t > 0) udpPort = t;
@ -378,7 +377,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
}
//TIME
if (subPage == 5)
if (subPage == SUBPAGE_TIME)
{
ntpEnabled = request->hasArg(F("NT"));
strlcpy(ntpServerName, request->arg(F("NS")).c_str(), 33);
@ -452,7 +451,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
}
//SECURITY
if (subPage == 6)
if (subPage == SUBPAGE_SEC)
{
if (request->hasArg(F("RS"))) //complete factory reset
{
@ -501,7 +500,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
}
#ifdef WLED_ENABLE_DMX // include only if DMX is enabled
if (subPage == 7)
if (subPage == SUBPAGE_DMX)
{
int t = request->arg(F("PU")).toInt();
if (t >= 0 && t <= 63999) e131ProxyUniverse = t;
@ -531,7 +530,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
#endif
//USERMODS
if (subPage == 8)
if (subPage == SUBPAGE_UM)
{
if (!requestJSONBufferLock(5)) return;
@ -671,7 +670,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
#ifndef WLED_DISABLE_2D
//2D panels
if (subPage == 10)
if (subPage == SUBPAGE_2D)
{
strip.isMatrix = request->arg(F("SOMP")).toInt();
strip.panel.clear(); // release memory if allocated
@ -708,10 +707,10 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
lastEditTime = millis();
// do not save if factory reset or LED settings (which are saved after LED re-init)
doSerializeConfig = subPage != 2 && !(subPage == 6 && doReboot);
if (subPage == 8) doReboot = request->hasArg(F("RBT")); // prevent race condition on dual core system (set reboot here, after doSerializeConfig has been set)
doSerializeConfig = subPage != SUBPAGE_LEDS && !(subPage == SUBPAGE_SEC && doReboot);
if (subPage == SUBPAGE_UM) doReboot = request->hasArg(F("RBT")); // prevent race condition on dual core system (set reboot here, after doSerializeConfig has been set)
#ifndef WLED_DISABLE_ALEXA
if (subPage == 4) alexaInit();
if (subPage == SUBPAGE_SYNC) alexaInit();
#endif
}

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2306141
#define VERSION 2306150
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG

View File

@ -561,61 +561,62 @@ void serveSettings(AsyncWebServerRequest* request, bool post)
if (url.indexOf("sett") >= 0)
{
if (url.indexOf(".js") > 0) subPage = 254;
else if (url.indexOf(".css") > 0) subPage = 253;
else if (url.indexOf("wifi") > 0) subPage = 1;
else if (url.indexOf("leds") > 0) subPage = 2;
else if (url.indexOf("ui") > 0) subPage = 3;
else if (url.indexOf("sync") > 0) subPage = 4;
else if (url.indexOf("time") > 0) subPage = 5;
else if (url.indexOf("sec") > 0) subPage = 6;
else if (url.indexOf("dmx") > 0) subPage = 7;
else if (url.indexOf("um") > 0) subPage = 8;
else if (url.indexOf("2D") > 0) subPage = 10;
else if (url.indexOf("lock") > 0) subPage = 251;
if (url.indexOf(".js") > 0) subPage = SUBPAGE_JS;
else if (url.indexOf(".css") > 0) subPage = SUBPAGE_CSS;
else if (url.indexOf("wifi") > 0) subPage = SUBPAGE_WIFI;
else if (url.indexOf("leds") > 0) subPage = SUBPAGE_LEDS;
else if (url.indexOf("ui") > 0) subPage = SUBPAGE_UI;
else if (url.indexOf("sync") > 0) subPage = SUBPAGE_SYNC;
else if (url.indexOf("time") > 0) subPage = SUBPAGE_TIME;
else if (url.indexOf("sec") > 0) subPage = SUBPAGE_SEC;
else if (url.indexOf("dmx") > 0) subPage = SUBPAGE_DMX;
else if (url.indexOf("um") > 0) subPage = SUBPAGE_UM;
else if (url.indexOf("2D") > 0) subPage = SUBPAGE_2D;
else if (url.indexOf("lock") > 0) subPage = SUBPAGE_LOCK;
}
else if (url.indexOf("/update") >= 0) subPage = 9; // update page, for PIN check
else if (url.indexOf("/update") >= 0) subPage = SUBPAGE_UPDATE; // update page, for PIN check
//else if (url.indexOf("/edit") >= 0) subPage = 10;
else subPage = 255; // welcome page
else subPage = SUBPAGE_WELCOME;
if (!correctPIN && strlen(settingsPIN) > 0 && (subPage > 0 && subPage < 11)) {
originalSubPage = subPage;
subPage = 252; // require PIN
subPage = SUBPAGE_PINREQ; // require PIN
}
// if OTA locked or too frequent PIN entry requests fail hard
if ((subPage == 1 && wifiLock && otaLock) || (post && !correctPIN && millis()-lastEditTime < PIN_RETRY_COOLDOWN))
if ((subPage == SUBPAGE_WIFI && wifiLock && otaLock) || (post && !correctPIN && millis()-lastEditTime < PIN_RETRY_COOLDOWN))
{
serveMessage(request, 500, "Access Denied", FPSTR(s_unlock_ota), 254); return;
}
if (post) { //settings/set POST request, saving
if (subPage != 1 || !(wifiLock && otaLock)) handleSettingsSet(request, subPage);
if (subPage != SUBPAGE_WIFI || !(wifiLock && otaLock)) handleSettingsSet(request, subPage);
char s[32];
char s2[45] = "";
switch (subPage) {
case 1: strcpy_P(s, PSTR("WiFi")); strcpy_P(s2, PSTR("Please connect to the new IP (if changed)")); forceReconnect = true; break;
case 2: strcpy_P(s, PSTR("LED")); break;
case 3: strcpy_P(s, PSTR("UI")); break;
case 4: strcpy_P(s, PSTR("Sync")); break;
case 5: strcpy_P(s, PSTR("Time")); break;
case 6: strcpy_P(s, PSTR("Security")); if (doReboot) strcpy_P(s2, PSTR("Rebooting, please wait ~10 seconds...")); break;
case 7: strcpy_P(s, PSTR("DMX")); break;
case 8: strcpy_P(s, PSTR("Usermods")); break;
case 10: strcpy_P(s, PSTR("2D")); break;
case 252: strcpy_P(s, correctPIN ? PSTR("PIN accepted") : PSTR("PIN rejected")); break;
case SUBPAGE_WIFI : strcpy_P(s, PSTR("WiFi")); strcpy_P(s2, PSTR("Please connect to the new IP (if changed)")); forceReconnect = true; break;
case SUBPAGE_LEDS : strcpy_P(s, PSTR("LED")); break;
case SUBPAGE_UI : strcpy_P(s, PSTR("UI")); break;
case SUBPAGE_SYNC : strcpy_P(s, PSTR("Sync")); break;
case SUBPAGE_TIME : strcpy_P(s, PSTR("Time")); break;
case SUBPAGE_SEC : strcpy_P(s, PSTR("Security")); if (doReboot) strcpy_P(s2, PSTR("Rebooting, please wait ~10 seconds...")); break;
case SUBPAGE_DMX : strcpy_P(s, PSTR("DMX")); break;
case SUBPAGE_UM : strcpy_P(s, PSTR("Usermods")); break;
case SUBPAGE_2D : strcpy_P(s, PSTR("2D")); break;
case SUBPAGE_PINREQ : strcpy_P(s, correctPIN ? PSTR("PIN accepted") : PSTR("PIN rejected")); break;
}
if (subPage != 252) strcat_P(s, PSTR(" settings saved."));
if (subPage != SUBPAGE_PINREQ) strcat_P(s, PSTR(" settings saved."));
if (subPage == 252 && correctPIN) {
if (subPage == SUBPAGE_PINREQ && correctPIN) {
subPage = originalSubPage; // on correct PIN load settings page the user intended
} else {
if (!s2[0]) strcpy_P(s2, s_redirecting);
serveMessage(request, 200, s, s2, (subPage == 1 || ((subPage == 6 || subPage == 8) && doReboot)) ? 129 : (correctPIN ? 1 : 3));
bool redirectAfter9s = (subPage == SUBPAGE_WIFI || ((subPage == SUBPAGE_SEC || subPage == SUBPAGE_UM) && doReboot));
serveMessage(request, 200, s, s2, redirectAfter9s ? 129 : (correctPIN ? 1 : 3));
return;
}
}
@ -623,30 +624,30 @@ void serveSettings(AsyncWebServerRequest* request, bool post)
AsyncWebServerResponse *response;
switch (subPage)
{
case 1: response = request->beginResponse_P(200, "text/html", PAGE_settings_wifi, PAGE_settings_wifi_length); break;
case 2: response = request->beginResponse_P(200, "text/html", PAGE_settings_leds, PAGE_settings_leds_length); break;
case 3: response = request->beginResponse_P(200, "text/html", PAGE_settings_ui, PAGE_settings_ui_length); break;
case 4: response = request->beginResponse_P(200, "text/html", PAGE_settings_sync, PAGE_settings_sync_length); break;
case 5: response = request->beginResponse_P(200, "text/html", PAGE_settings_time, PAGE_settings_time_length); break;
case 6: response = request->beginResponse_P(200, "text/html", PAGE_settings_sec, PAGE_settings_sec_length); break;
case SUBPAGE_WIFI : response = request->beginResponse_P(200, "text/html", PAGE_settings_wifi, PAGE_settings_wifi_length); break;
case SUBPAGE_LEDS : response = request->beginResponse_P(200, "text/html", PAGE_settings_leds, PAGE_settings_leds_length); break;
case SUBPAGE_UI : response = request->beginResponse_P(200, "text/html", PAGE_settings_ui, PAGE_settings_ui_length); break;
case SUBPAGE_SYNC : response = request->beginResponse_P(200, "text/html", PAGE_settings_sync, PAGE_settings_sync_length); break;
case SUBPAGE_TIME : response = request->beginResponse_P(200, "text/html", PAGE_settings_time, PAGE_settings_time_length); break;
case SUBPAGE_SEC : response = request->beginResponse_P(200, "text/html", PAGE_settings_sec, PAGE_settings_sec_length); break;
#ifdef WLED_ENABLE_DMX
case 7: response = request->beginResponse_P(200, "text/html", PAGE_settings_dmx, PAGE_settings_dmx_length); break;
case SUBPAGE_DMX : response = request->beginResponse_P(200, "text/html", PAGE_settings_dmx, PAGE_settings_dmx_length); break;
#endif
case 8: response = request->beginResponse_P(200, "text/html", PAGE_settings_um, PAGE_settings_um_length); break;
case 9: response = request->beginResponse_P(200, "text/html", PAGE_update, PAGE_update_length); break;
case SUBPAGE_UM : response = request->beginResponse_P(200, "text/html", PAGE_settings_um, PAGE_settings_um_length); break;
case SUBPAGE_UPDATE : response = request->beginResponse_P(200, "text/html", PAGE_update, PAGE_update_length); break;
#ifndef WLED_DISABLE_2D
case 10: response = request->beginResponse_P(200, "text/html", PAGE_settings_2D, PAGE_settings_2D_length); break;
case SUBPAGE_2D : response = request->beginResponse_P(200, "text/html", PAGE_settings_2D, PAGE_settings_2D_length); break;
#endif
case 251: {
case SUBPAGE_LOCK : {
correctPIN = !strlen(settingsPIN); // lock if a pin is set
createEditHandler(correctPIN);
serveMessage(request, 200, strlen(settingsPIN) > 0 ? PSTR("Settings locked") : PSTR("No PIN set"), FPSTR(s_redirecting), 1);
return;
}
case 252: response = request->beginResponse_P(200, "text/html", PAGE_settings_pin, PAGE_settings_pin_length); break;
case 253: response = request->beginResponse_P(200, "text/css", PAGE_settingsCss, PAGE_settingsCss_length); break;
case 254: serveSettingsJS(request); return;
case 255: response = request->beginResponse_P(200, "text/html", PAGE_welcome, PAGE_welcome_length); break;
case SUBPAGE_PINREQ : response = request->beginResponse_P(200, "text/html", PAGE_settings_pin, PAGE_settings_pin_length); break;
case SUBPAGE_CSS : response = request->beginResponse_P(200, "text/css", PAGE_settingsCss, PAGE_settingsCss_length); break;
case SUBPAGE_JS : serveSettingsJS(request); return;
case SUBPAGE_WELCOME : response = request->beginResponse_P(200, "text/html", PAGE_welcome, PAGE_welcome_length); break;
default: response = request->beginResponse_P(200, "text/html", PAGE_settings, PAGE_settings_length); break;
}
response->addHeader(FPSTR(s_content_enc),"gzip");

View File

@ -285,7 +285,7 @@ void getSettingsJS(byte subPage, char* dest)
if (subPage <0 || subPage >10) return;
if (subPage == 0)
if (subPage == SUBPAGE_MENU)
{
#ifndef WLED_DISABLE_2D // include only if 2D is compiled in
oappend(PSTR("gId('2dbtn').style.display='';"));
@ -295,7 +295,7 @@ void getSettingsJS(byte subPage, char* dest)
#endif
}
if (subPage == 1)
if (subPage == SUBPAGE_WIFI)
{
sappends('s',SET_F("CS"),clientSSID);
@ -362,7 +362,7 @@ void getSettingsJS(byte subPage, char* dest)
}
}
if (subPage == 2)
if (subPage == SUBPAGE_LEDS)
{
char nS[32];
@ -493,7 +493,7 @@ void getSettingsJS(byte subPage, char* dest)
sappend('c',SET_F("MSO"),!irApplyToAllSelected);
}
if (subPage == 3)
if (subPage == SUBPAGE_UI)
{
sappends('s',SET_F("DS"),serverDescription);
sappend('c',SET_F("ST"),syncToggleReceive);
@ -504,7 +504,7 @@ void getSettingsJS(byte subPage, char* dest)
#endif
}
if (subPage == 4)
if (subPage == SUBPAGE_SYNC)
{
sappend('v',SET_F("UP"),udpPort);
sappend('v',SET_F("U2"),udpPort2);
@ -597,7 +597,7 @@ void getSettingsJS(byte subPage, char* dest)
sappend('v',SET_F("BD"),serialBaud);
}
if (subPage == 5)
if (subPage == SUBPAGE_TIME)
{
sappend('c',SET_F("NT"),ntpEnabled);
sappends('s',SET_F("NS"),ntpServerName);
@ -661,7 +661,7 @@ void getSettingsJS(byte subPage, char* dest)
}
}
if (subPage == 6)
if (subPage == SUBPAGE_SEC)
{
byte l = strlen(settingsPIN);
char fpass[l+1]; //fill PIN field with 0000
@ -683,7 +683,7 @@ void getSettingsJS(byte subPage, char* dest)
}
#ifdef WLED_ENABLE_DMX // include only if DMX is enabled
if (subPage == 7)
if (subPage == SUBPAGE_DMX)
{
sappend('v',SET_F("PU"),e131ProxyUniverse);
@ -710,7 +710,7 @@ void getSettingsJS(byte subPage, char* dest)
}
#endif
if (subPage == 8) //usermods
if (subPage == SUBPAGE_UM) //usermods
{
appendGPIOinfo();
oappend(SET_F("numM="));
@ -729,7 +729,7 @@ void getSettingsJS(byte subPage, char* dest)
usermods.appendConfigData();
}
if (subPage == 9) // update
if (subPage == SUBPAGE_UPDATE) // update
{
sappends('m',SET_F("(\"sip\")[0]"),(char*)F("WLED "));
olen -= 2; //delete ";
@ -745,7 +745,7 @@ void getSettingsJS(byte subPage, char* dest)
oappend(SET_F(")\";"));
}
if (subPage == 10) // 2D matrices
if (subPage == SUBPAGE_2D) // 2D matrices
{
sappend('v',SET_F("SOMP"),strip.isMatrix);
#ifndef WLED_DISABLE_2D