Die Position in einer Playlist beilbt beibehalten, solange die Papabox nicht neu gestartet wird.

This commit is contained in:
Fabian Schlenz 2022-08-25 14:52:12 +02:00
parent b2cf9d6277
commit 913a64d465
7 changed files with 60 additions and 10 deletions

View File

@ -15,6 +15,7 @@ class Controller {
bool is_rfid_present = false;
unsigned long last_rfid_check = 0;
unsigned long last_button_check = 0;
unsigned long last_position_save = 0;
uint8_t button_pressed = 0;
unsigned long button_pressed_since = 0;
bool button_already_processed = false;

View File

@ -0,0 +1,9 @@
#pragma once
struct PersistedPlaylist {
String dir;
uint16_t file = 0;
uint32_t position = 0;
PersistedPlaylist(String s="") : dir(s) {}
};

View File

@ -2,17 +2,19 @@
#include <vector>
#include <Arduino.h>
#include "persisted_playlist.h"
class Playlist {
private:
std::vector<String> files;
uint8_t current_file = 0;
uint32_t current_time = 0;
void set_current_position(uint8_t file, uint32_t position=0);
String rfid_id;
PersistedPlaylist* pp;
public:
Playlist(String rfid_id="");
Playlist();
Playlist(String rfid_id, PersistedPlaylist* p);
void add_file(String filename);
void sort();
String get_rfid_id();
@ -23,4 +25,6 @@ class Playlist {
void set_current_time(uint32_t time);
uint32_t get_current_time();
void shuffle();
void set_current_position(uint8_t file, uint32_t position=0);
void save_current_position(uint32_t position=0);
};

View File

@ -4,6 +4,9 @@
#include <map>
#include <Arduino.h>
#include "playlist.h"
#include "persisted_playlist.h"
class Playlist;
class PlaylistManager {
private:
@ -13,9 +16,10 @@ class PlaylistManager {
public:
PlaylistManager();
std::map<String, String> map;
std::map<String, PersistedPlaylist> map;
Playlist get_playlist(String rfid_id);
bool has_playlist(String rfid_id);
Playlist current_playlist;
void set_audio_current_time(uint32_t time);
String pp_to_String();
};

View File

@ -10,6 +10,11 @@ void Controller::handle() {
handle_buttons();
last_button_check = millis();
}
if (last_position_save + 10000 < millis() || last_position_save > millis()) {
current_playlist.save_current_position(audio.getFilePos());
last_position_save = millis();
//Serial.println(pm->pp_to_String().c_str());
}
}
void Controller::handle_buttons() {

View File

@ -1,7 +1,10 @@
#include "playlist.h"
Playlist::Playlist(String id) {
Playlist::Playlist() {}
Playlist::Playlist(String id, PersistedPlaylist* p) {
rfid_id = id;
pp = p;
}
String Playlist::get_rfid_id() {
@ -16,9 +19,22 @@ void Playlist::sort() {
std::sort(files.begin(), files.end());
}
void Playlist::set_current_position(uint8_t file, uint32_t seconds) {
void Playlist::set_current_position(uint8_t file, uint32_t bytes) {
log_d("Setting position: File %d, bytes %d.", file, bytes);
current_file = file;
current_time = seconds;
current_time = bytes;
save_current_position();
}
void Playlist::save_current_position(uint32_t position) {
if (position==0) {
position = current_time;
}
log_d("Saving current position: File %d, bytes %d.", current_file, position);
if (pp != NULL) {
pp->file = current_file;
pp->position = position;
}
}
String Playlist::get_current_file_name() {
@ -54,7 +70,7 @@ void Playlist::restart() {
}
void Playlist::set_current_time(uint32_t pos) {
current_time = pos;
set_current_position(current_file, pos);
}
uint32_t Playlist::get_current_time() {

View File

@ -23,7 +23,7 @@ PlaylistManager::PlaylistManager() {
String rfid_id = data.substring(0, eq);
String folder = data.substring(eq + 1);
Serial.printf(" Adding mapping: %s=>%s\n", rfid_id.c_str(), folder.c_str());
map[rfid_id] = folder;
map[rfid_id] = PersistedPlaylist(folder);
}
}
f.close();
@ -38,8 +38,10 @@ Playlist PlaylistManager::get_playlist(String rfid_id) {
Serial.printf("No known playlist for id %s.\n", rfid_id);
return current_playlist;
} else {
current_playlist = Playlist(rfid_id);
String path = map[rfid_id];
PersistedPlaylist* ap = &(map[rfid_id]);
log_d("PP status is: File %d, bytes %d.", ap->file, ap->position);
current_playlist = Playlist(rfid_id, ap);
String path = ap->dir;
if (path.startsWith("/")) {
File dir = SD.open(path);
while(File entry = dir.openNextFile()) {
@ -54,6 +56,7 @@ Playlist PlaylistManager::get_playlist(String rfid_id) {
entry.close();
}
dir.close();
current_playlist.set_current_position(ap->file, ap->position);
} else if (path.startsWith("http")) {
Serial.printf("Adding URL %s to the list of files\n", path.c_str());
current_playlist.add_file(path);
@ -71,4 +74,12 @@ void PlaylistManager::set_audio_current_time(uint32_t time) {
bool PlaylistManager::has_playlist(String rfid_id) {
return map.count(rfid_id) == 1;
}
String PlaylistManager::pp_to_String() {
String s = "";
for(const auto& kv : map) {
s += kv.first + "=" + kv.second.file + "," + kv.second.position + '\n';
}
return s;
}