From e1b4f4b275c92cb1cb2fba8d6c454da07ea77acb Mon Sep 17 00:00:00 2001 From: radex Date: Sat, 25 May 2024 15:26:55 +0200 Subject: [PATCH] auto playlist (badly) --- firmware/README.md | 4 ++-- firmware/src/gfx_decoder.cpp | 10 +++++----- firmware/src/gfx_decoder.h | 4 ++-- firmware/src/main.cpp | 16 ++++++++++++---- firmware/src/sd.cpp | 14 +++++++++----- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/firmware/README.md b/firmware/README.md index 5ba88f9..bbd2dee 100644 --- a/firmware/README.md +++ b/firmware/README.md @@ -36,10 +36,10 @@ python3 scripts/audio_convert.py ## play videos on device -videos/YOURNAME/files +create `video/YOURNAME/` folder, with files: (where files are audio.bin, gfx.bin, gfx_len.bin) NOTE: yourname MUST be at most 8 characters long -also `videos/playlist.txt` with names of videos in order +also `video/playlist.txt` with names of videos in order diff --git a/firmware/src/gfx_decoder.cpp b/firmware/src/gfx_decoder.cpp index eb6cb34..2b73355 100644 --- a/firmware/src/gfx_decoder.cpp +++ b/firmware/src/gfx_decoder.cpp @@ -8,11 +8,11 @@ uint16_t frameCount = 0; uint8_t gfxFrameBuffer[6400] = {0}; -bool gfx_decoder_loadNextFrame() { +int32_t gfx_decoder_loadNextFrame() { // load frame from SD card auto frameSize = sd_loadNextFrame(); if (frameSize < 0) { - return false; + return frameSize; } // decode PNG @@ -44,16 +44,16 @@ bool gfx_decoder_loadNextFrame() { // TODO: mutex? double buffer? or something... memcpy(framebuffer, buffer, ROW_COUNT * COL_COUNT); free(buffer); - return true; + return frameSize; } unsigned long frameLastChangedAt = 0; -bool gfx_decoder_handleLoop() { +int32_t gfx_decoder_handleLoop() { auto now = millis(); if (now - frameLastChangedAt > MS_PER_FRAME) { frameLastChangedAt = now; return gfx_decoder_loadNextFrame(); } - return true; + return 0; } diff --git a/firmware/src/gfx_decoder.h b/firmware/src/gfx_decoder.h index b218ac7..02389ae 100644 --- a/firmware/src/gfx_decoder.h +++ b/firmware/src/gfx_decoder.h @@ -8,7 +8,7 @@ extern uint16_t gfxFrameLengthsBuffer[24000]; extern uint16_t frameCount; extern uint8_t gfxFrameBuffer[6400]; -bool gfx_decoder_loadNextFrame(); -bool gfx_decoder_handleLoop(); +int32_t gfx_decoder_loadNextFrame(); +int32_t gfx_decoder_handleLoop(); #endif diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index d465498..305396e 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -52,11 +52,15 @@ void loadVideo(size_t index) { isLoaded = true; } +void nextSong() { + Serial.println("Next song!"); + currentVideoIndex = (currentVideoIndex + 1) % playlistSize; + loadVideo(currentVideoIndex); +} + void loop() { if (digitalRead(4) == LOW) { - Serial.println("Next song!"); - currentVideoIndex = (currentVideoIndex + 1) % playlistSize; - loadVideo(currentVideoIndex); + nextSong(); } // if (Serial.available() > 0) { @@ -75,8 +79,12 @@ void loop() { if (isLoaded) { sd_loadNextAudio(); - if (!gfx_decoder_handleLoop()) { + auto loopStatus = gfx_decoder_handleLoop(); + + if (loopStatus == -1) { Serial.println("Failed to load frame..."); + } else if (loopStatus == -2) { + nextSong(); } } } diff --git a/firmware/src/sd.cpp b/firmware/src/sd.cpp index c0930db..d8c961f 100644 --- a/firmware/src/sd.cpp +++ b/firmware/src/sd.cpp @@ -271,8 +271,10 @@ void sd_loadNextAudio() { auto bytesRead = audioFile.read(next_buffer, BUFFER_LEN); if (bytesRead < BUFFER_LEN) { - Serial.println("End of audio file, rewinding..."); - audioFile.seek(0); + // Serial.println("End of audio file, rewinding..."); + // audioFile.seek(0); + Serial.println("End of audio."); + audio_stop(); } else { /* Serial.print("Read "); @@ -374,9 +376,11 @@ int32_t sd_loadNextFrame() { // increment if (frameIdx == frameCount - 1) { - Serial.println("Last frame, rewinding..."); - gfxFile.seek(0); - frameIdx = 0; + // Serial.println("Last frame, rewinding..."); + // gfxFile.seek(0); + // frameIdx = 0; + Serial.println("Last frame, next video!"); + return -2; } else { frameIdx++; }