Improved Volume button handling, fix for music stopping

This commit is contained in:
Stefan Ostermann 2025-08-18 23:00:21 +02:00
parent 0a08709160
commit 22f8d3eec3
1 changed files with 33 additions and 9 deletions

View File

@ -78,6 +78,9 @@ DirectoryNode *currentNode = nullptr;
volatile bool newRfidInt = false;
volatile bool playButtonDown = false;
// Track if play button hold is active and if volume was adjusted during this hold
volatile bool playHoldActive = false;
volatile bool volumeAdjustedDuringHold = false;
volatile uint8_t sd_lock_flag = 0;
/* Simple spinlock using older GCC sync builtins (no libatomic required).
@ -1509,7 +1512,7 @@ void loop()
server.begin();
}
if (audio.isRunning() && webreq_cnt == 0)
if (audio.isRunning())
{
if (asyncStop)
{
@ -1583,7 +1586,7 @@ void loop()
asyncTogglePlayPause = false;
togglePlayPause();
}
else if (asyncNext && webreq_cnt == 0)
else if (asyncNext)
{
asyncNext = false;
// If the play/start button is held, treat NEXT as volume up
@ -1595,6 +1598,7 @@ void loop()
vol++;
audio.setVolume(vol);
volume = vol; // update stored volume for mute/unmute
volumeAdjustedDuringHold = true;
}
// do not play the startup sound when changing volume while holding play
}
@ -1629,6 +1633,7 @@ void loop()
vol--;
audio.setVolume(vol);
volume = vol; // update stored volume for mute/unmute
volumeAdjustedDuringHold = true;
}
// do not play the startup sound when changing volume while holding play
}
@ -1704,9 +1709,32 @@ void loop2(void *parameter)
for (;;)
{
// Track whether the play/start button is currently held down so the main loop
// can interpret NEXT/PREV as volume changes while play is held.
playButtonDown = (digitalRead(BTN_START_STOP) == LOW);
// Track whether the play/start button is currently held down and detect press/release
bool currentDown = (digitalRead(BTN_START_STOP) == LOW);
static bool prevDown = false;
playButtonDown = currentDown;
// On press: start hold tracking and reset volume-change marker
if (currentDown && !prevDown)
{
playHoldActive = true;
volumeAdjustedDuringHold = false;
lastInteraction = millis();
}
// On release: toggle only if no volume change occurred during hold
if (!currentDown && prevDown)
{
if (playHoldActive)
{
if (!volumeAdjustedDuringHold)
{
asyncTogglePlayPause = true;
}
playHoldActive = false;
volumeAdjustedDuringHold = false;
}
}
prevDown = currentDown;
if (buttonPressed(BTN_NEXT))
{
@ -1716,10 +1744,6 @@ void loop2(void *parameter)
{
asyncPrev = true;
}
if (buttonPressed(BTN_START_STOP))
{
asyncTogglePlayPause = true;
}
if (!loggingDone)
{
Serial.println("loop2 started");