From 3aeb8ffe5bd27bcf08cc8083fff735deb2145f7d Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Sat, 21 Dec 2024 21:28:35 +0100 Subject: [PATCH 1/5] added button action configuration and handling for reset and restart --- data/index.html | 16 ++++++++++++++++ src/main.cpp | 23 +++++++++++++++++++++++ src/routes/config.cpp | 14 ++++++++++++++ src/routes/config.h | 9 +++++++++ 4 files changed, 62 insertions(+) diff --git a/data/index.html b/data/index.html index c6f81ab..96ef84c 100644 --- a/data/index.html +++ b/data/index.html @@ -201,7 +201,23 @@ + +
diff --git a/src/main.cpp b/src/main.cpp index 40b1153..c0350f7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -87,6 +87,27 @@ void ledBlink(int ms) } } +void onButtonPress() +{ + ButtonAction action = static_cast(config.getUInt("button-action", DEFAULT_BUTTON_ACTION)); + Serial.print("Button pressed, action: "); + Serial.println(action); + + switch (action) + { + case ResetConfig: + config.begin("dmx", false); + config.clear(); + config.end(); + ESP.restart(); + break; + + case Restart: + ESP.restart(); + break; + } +} + void setup() { Serial.begin(9600); @@ -122,6 +143,8 @@ void setup() ledBlink(500); + attachInterrupt(PIN_BUTTON, onButtonPress, FALLING); + // wait for serial monitor delay(5000); Serial.println("Starting DMX-Interface..."); diff --git a/src/routes/config.cpp b/src/routes/config.cpp index d586feb..edd9e54 100644 --- a/src/routes/config.cpp +++ b/src/routes/config.cpp @@ -70,6 +70,16 @@ Direction parseDirection(uint8_t direction) throw ::std::invalid_argument("Invalid direction value: " + direction); } +ButtonAction parseButtonAction(uint8_t buttonAction) +{ + if (buttonAction > 0 || buttonAction < BUTTON_ACTION_SIZE) + { + return static_cast(buttonAction); + } + + throw ::std::invalid_argument("Invalid value for button action: " + buttonAction); +} + #pragma endregion void onGetConfig(AsyncWebServerRequest *request) @@ -93,6 +103,7 @@ void onGetConfig(AsyncWebServerRequest *request) doc["universe-2"] = config.getUInt("universe-2", DEFAULT_UNIVERSE2); doc["direction-2"] = config.getUInt("direction-2", DEFAULT_DIRECTION2); doc["led-brightness"] = config.getUInt("led-brightness", DEFAULT_LED_BRIGHTNESS); + doc["button-action"] = config.getUInt("button-action", DEFAULT_BUTTON_ACTION); config.end(); @@ -151,6 +162,9 @@ void onPutConfig(AsyncWebServerRequest *request, uint8_t *data, size_t len, size config.putUInt("led-brightness", doc["led-brightness"]); + ButtonAction buttonAction = parseButtonAction(doc["button-action"].as()); + config.putUInt("button-action", buttonAction); + config.end(); request->send(200); diff --git a/src/routes/config.h b/src/routes/config.h index bac228d..157292c 100644 --- a/src/routes/config.h +++ b/src/routes/config.h @@ -29,6 +29,14 @@ enum Direction }; const uint8_t DIRECTION_SIZE = 2; +enum ButtonAction +{ + None, + ResetConfig, + Restart +}; +const uint8_t BUTTON_ACTION_SIZE = 3; + const Connection DEFAULT_CONNECTION = WiFiAP; const IpMethod DEFAULT_IP_METHOD = DHCP; extern String DEFAULT_SSID; // initialized in setup because it depends on the mac address @@ -43,6 +51,7 @@ const uint8_t DEFAULT_UNIVERSE1 = 1; const uint8_t DEFAULT_UNIVERSE2 = 2; const uint8_t DEFAULT_LED_BRIGHTNESS = 25; +const ButtonAction DEFAULT_BUTTON_ACTION = Restart; void onGetConfig(AsyncWebServerRequest *request); From 4732231fa275eb2363c8cfc920ad56e01fd6955a Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Sat, 21 Dec 2024 22:02:01 +0100 Subject: [PATCH 2/5] added led blink code while resetting config --- src/main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index c0350f7..62b47e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -96,9 +96,13 @@ void onButtonPress() switch (action) { case ResetConfig: + ledBlink(100); + config.begin("dmx", false); config.clear(); config.end(); + delay(1000); + ESP.restart(); break; From ba9f8f50c28fec687adf2a03ba02c62965dd17d7 Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Sun, 16 Feb 2025 19:41:42 +0100 Subject: [PATCH 3/5] fixed loading button actions --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c9b92be..81e1c87 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -89,9 +89,9 @@ void ledBlink(int ms) void onButtonPress() { + config.begin("dmx", true); ButtonAction action = static_cast(config.getUInt("button-action", DEFAULT_BUTTON_ACTION)); - Serial.print("Button pressed, action: "); - Serial.println(action); + config.end(); switch (action) { From f649a14811143064680af68a3b753d443c10a374 Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Sun, 16 Feb 2025 21:22:10 +0100 Subject: [PATCH 4/5] added empty case for completeness --- src/main.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 81e1c87..9204603 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -109,6 +109,9 @@ void onButtonPress() case Restart: ESP.restart(); break; + case None: + // do nothing + break; } } From b1d3e6bd691de3d45936b317a63295a372562e14 Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Fri, 21 Feb 2025 22:26:18 +0100 Subject: [PATCH 5/5] fixed accidental reset when restarting the interface via button and keeping the button pressed --- src/main.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9204603..516d94d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -96,17 +96,18 @@ void onButtonPress() switch (action) { case ResetConfig: - ledBlink(100); - config.begin("dmx", false); config.clear(); config.end(); - delay(1000); ESP.restart(); break; case Restart: + config.begin("dmx", false); + config.putBool("restart-via-btn", true); + config.end(); + ESP.restart(); break; case None: @@ -125,12 +126,13 @@ void setup() // LED config.begin("dmx", true); brightness_led = config.getUInt("led-brightness", DEFAULT_LED_BRIGHTNESS); + bool restartViaButton = config.getBool("restart-via-btn", false); config.end(); analogWrite(PIN_LED, brightness_led); // Button pinMode(PIN_BUTTON, INPUT_PULLUP); - if (digitalRead(PIN_BUTTON) == LOW) + if (digitalRead(PIN_BUTTON) == LOW && !restartViaButton) { ledBlink(100); unsigned long startTime = millis(); @@ -148,6 +150,10 @@ void setup() } } + config.begin("dmx", false); + config.putBool("restart-via-btn", false); + config.end(); + ledBlink(500); attachInterrupt(PIN_BUTTON, onButtonPress, FALLING);