Merge pull request #66 from HendrikRauh/51-button-actions

Button actions
This commit is contained in:
Hendrik Rauh 2025-04-14 22:45:18 +02:00 committed by GitHub
commit 8fba7dcb29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 77 additions and 1 deletions

View file

@ -201,7 +201,23 @@
<span class="range-value"></span> <span class="range-value"></span>
</div> </div>
</label> </label>
<label>
<span>Aktion bei Knopfdruck:</span>
<select
name="button-action"
id="input-button-action"
title="Aktion bei Knopfdruck"
required
>
<option value="0">Nichts</option>
<option value="1">
Konfiguration zurücksetzen
</option>
<option value="2">Neustart</option>
</select>
</label>
</fieldset> </fieldset>
<div class="buttons"> <div class="buttons">
<button type="reset">Zurücksetzen</button> <button type="reset">Zurücksetzen</button>
<button type="submit">Speichern</button> <button type="submit">Speichern</button>

View file

@ -104,6 +104,36 @@ float getTemperature()
temp_sensor_read_celsius(&tempC); temp_sensor_read_celsius(&tempC);
return tempC; return tempC;
} }
void onButtonPress()
{
config.begin("dmx", true);
ButtonAction action = static_cast<ButtonAction>(config.getUInt("button-action", DEFAULT_BUTTON_ACTION));
config.end();
switch (action)
{
case ResetConfig:
config.begin("dmx", false);
config.clear();
config.end();
ESP.restart();
break;
case Restart:
config.begin("dmx", false);
config.putBool("restart-via-btn", true);
config.end();
ESP.restart();
break;
case None:
// do nothing
break;
}
}
void setup() void setup()
{ {
@ -132,12 +162,13 @@ void setup()
// LED // LED
config.begin("dmx", true); config.begin("dmx", true);
brightness_led = config.getUInt("led-brightness", DEFAULT_LED_BRIGHTNESS); brightness_led = config.getUInt("led-brightness", DEFAULT_LED_BRIGHTNESS);
bool restartViaButton = config.getBool("restart-via-btn", false);
config.end(); config.end();
analogWrite(PIN_LED, brightness_led); analogWrite(PIN_LED, brightness_led);
// Button // Button
pinMode(PIN_BUTTON, INPUT_PULLUP); pinMode(PIN_BUTTON, INPUT_PULLUP);
if (digitalRead(PIN_BUTTON) == LOW) if (digitalRead(PIN_BUTTON) == LOW && !restartViaButton)
{ {
// ledBlink(100); // ledBlink(100);
unsigned long startTime = millis(); unsigned long startTime = millis();
@ -155,8 +186,14 @@ void setup()
} }
} }
config.begin("dmx", false);
config.putBool("restart-via-btn", false);
config.end();
// ledBlink(500); // ledBlink(500);
attachInterrupt(PIN_BUTTON, onButtonPress, FALLING);
// wait for serial monitor // wait for serial monitor
delay(5000); delay(5000);
Serial.println("Starting DMX-Interface..."); Serial.println("Starting DMX-Interface...");

View file

@ -70,6 +70,16 @@ Direction parseDirection(uint8_t direction)
throw ::std::invalid_argument("Invalid direction value: " + 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>(buttonAction);
}
throw ::std::invalid_argument("Invalid value for button action: " + buttonAction);
}
#pragma endregion #pragma endregion
void onGetConfig(AsyncWebServerRequest *request) void onGetConfig(AsyncWebServerRequest *request)
@ -93,6 +103,7 @@ void onGetConfig(AsyncWebServerRequest *request)
doc["universe-2"] = config.getUInt("universe-2", DEFAULT_UNIVERSE2); doc["universe-2"] = config.getUInt("universe-2", DEFAULT_UNIVERSE2);
doc["direction-2"] = config.getUInt("direction-2", DEFAULT_DIRECTION2); doc["direction-2"] = config.getUInt("direction-2", DEFAULT_DIRECTION2);
doc["led-brightness"] = config.getUInt("led-brightness", DEFAULT_LED_BRIGHTNESS); doc["led-brightness"] = config.getUInt("led-brightness", DEFAULT_LED_BRIGHTNESS);
doc["button-action"] = config.getUInt("button-action", DEFAULT_BUTTON_ACTION);
config.end(); config.end();
@ -151,6 +162,9 @@ void onPutConfig(AsyncWebServerRequest *request, uint8_t *data, size_t len, size
config.putUInt("led-brightness", doc["led-brightness"]); config.putUInt("led-brightness", doc["led-brightness"]);
ButtonAction buttonAction = parseButtonAction(doc["button-action"].as<uint8_t>());
config.putUInt("button-action", buttonAction);
config.end(); config.end();
request->send(200); request->send(200);

View file

@ -28,6 +28,14 @@ enum Direction
}; };
const uint8_t DIRECTION_SIZE = 2; const uint8_t DIRECTION_SIZE = 2;
enum ButtonAction
{
None,
ResetConfig,
Restart
};
const uint8_t BUTTON_ACTION_SIZE = 3;
const Connection DEFAULT_CONNECTION = WiFiAP; const Connection DEFAULT_CONNECTION = WiFiAP;
const IpMethod DEFAULT_IP_METHOD = DHCP; const IpMethod DEFAULT_IP_METHOD = DHCP;
extern String DEFAULT_SSID; // initialized in setup because it depends on the mac address extern String DEFAULT_SSID; // initialized in setup because it depends on the mac address
@ -42,6 +50,7 @@ const uint8_t DEFAULT_UNIVERSE1 = 1;
const uint8_t DEFAULT_UNIVERSE2 = 2; const uint8_t DEFAULT_UNIVERSE2 = 2;
const uint8_t DEFAULT_LED_BRIGHTNESS = 25; const uint8_t DEFAULT_LED_BRIGHTNESS = 25;
const ButtonAction DEFAULT_BUTTON_ACTION = Restart;
void onGetConfig(AsyncWebServerRequest *request); void onGetConfig(AsyncWebServerRequest *request);