stateBufferUpdateRequest

This commit is contained in:
Patrick Haßel 2025-06-06 12:52:17 +02:00
parent 8d4d888957
commit 1e304979c5
3 changed files with 40 additions and 20 deletions

View File

@ -7,12 +7,15 @@
#include <SPI.h> #include <SPI.h>
#include <SD.h> #include <SD.h>
#include <SnapClient.h>
#include "SnapClient.h" #include "player.h"
#include "wifi.h" #include "wifi.h"
bool audioMute = false; bool audioMute = false;
bool audioMute2 = true;
float audioVolume = 0.1; float audioVolume = 0.1;
int copyErrors = 0; int copyErrors = 0;
@ -195,12 +198,17 @@ void audioSetup() {
} }
bool audioLoop() { bool audioLoop() {
audioVolume = max(0.0f, min(audioVolume, 1.0f));
if (abs(board.volume() - audioVolume) >= 0.01f) { if (abs(board.volume() - audioVolume) >= 0.01f) {
Serial.printf("[AUDIO ] writing volume = %.2f\n", audioVolume); Serial.printf("[AUDIO ] volume = %.2f\n", audioVolume);
board.setVolume(audioVolume); board.setVolume(audioVolume);
stateBufferUpdateRequest();
}
if (audioMute2 != audioMute) {
audioMute2 = audioMute;
Serial.printf("[AUDIO ] mute = %s\n", audioMute ? "MUTED" : "no");
board.setMute(audioMute);
stateBufferUpdateRequest();
} }
board.setMute(audioMute);
if (copier.copy() > 0) { if (copier.copy() > 0) {
copyErrors = 0; copyErrors = 0;
return true; return true;

View File

@ -23,6 +23,8 @@ char stateBuffer1[200];
char *stateBuffer = stateBuffer0; char *stateBuffer = stateBuffer0;
bool stateBufferUpdateNeeded = true;
const char *getPlayerStateString() { const char *getPlayerStateString() {
switch (playerState) { switch (playerState) {
case PLAYER_STOP: case PLAYER_STOP:
@ -36,7 +38,15 @@ const char *getPlayerStateString() {
} }
void updateStateBuffer() { void stateBufferUpdateRequest() {
stateBufferUpdateNeeded = true;
}
void updateStateBufferIfNeeded() {
if (!stateBufferUpdateNeeded) {
return;
}
stateBufferUpdateNeeded = false;
auto json = JsonDocument(); auto json = JsonDocument();
const auto entry = playlistCurrent(); const auto entry = playlistCurrent();
json["state"] = getPlayerStateString(); json["state"] = getPlayerStateString();
@ -69,6 +79,7 @@ void playerStart(const Entry &entry) {
delayMs = min(DELAY_MS_MAX, delayMs + DELAY_MS_ADD); delayMs = min(DELAY_MS_MAX, delayMs + DELAY_MS_ADD);
Serial.printf("[PLAYER ] Retry delay: %d ms\n", delayMs); Serial.printf("[PLAYER ] Retry delay: %d ms\n", delayMs);
} }
stateBufferUpdateRequest();
} }
void playerLoop() { void playerLoop() {
@ -76,20 +87,19 @@ void playerLoop() {
playerState = PLAYER_PLAY; playerState = PLAYER_PLAY;
playerStart(playlistNext(playerSkip)); playerStart(playlistNext(playerSkip));
playerSkip = 0; playerSkip = 0;
return; } else {
} const auto playing = audioLoop();
if (playing) {
const auto playing = audioLoop(); errorMs = 0;
if (playing) { }
errorMs = 0; if (playing && playerState == PLAYER_STOP) {
} errorMs = 0;
if (playing && playerState == PLAYER_STOP) { audioStop();
errorMs = 0; stateBufferUpdateRequest();
audioStop(); }
updateStateBuffer(); if (!playing && playerState == PLAYER_PLAY && (errorMs == 0 || millis() - errorMs >= delayMs)) {
} playerStart(playlistNextOrRepeatOneIfEnabled());
if (!playing && playerState == PLAYER_PLAY && (errorMs == 0 || millis() - errorMs >= delayMs)) { }
playerStart(playlistNextOrRepeatOneIfEnabled());
updateStateBuffer();
} }
updateStateBufferIfNeeded();
} }

View File

@ -15,4 +15,6 @@ extern char *stateBuffer;
void playerLoop(); void playerLoop();
void stateBufferUpdateRequest();
#endif #endif