ID of album to play now comes from a file ids.txt within the folder. This is as of yet untested.

This commit is contained in:
Fabian Schlenz 2019-11-12 19:29:55 +01:00
parent 46fb4c7615
commit 6d00474315
3 changed files with 81 additions and 13 deletions

View File

@ -31,6 +31,11 @@
#define SM_ADPCM 0x1000
#define SS_DO_NOT_JUMP 0x8000
struct ID_to_Folder_Map {
const char* id;
const char* folder;
};
class Player {
private:
enum state { uninitialized, idle, playing, stopping,
@ -70,6 +75,8 @@ private:
void _patch_adpcm();
void _speaker_off();
void _speaker_on();
void _fill_id_to_folder_map();
String _foldername_for_id(String id);
SPISettings _spi_settings_slow = SPISettings(250000, MSBFIRST, SPI_MODE0);
SPISettings _spi_settings_fast = SPISettings(4000000, MSBFIRST, SPI_MODE0);
@ -89,6 +96,7 @@ private:
uint32_t _skip_to;
SPIMaster* _spi;
unsigned long _stopped_at;
std::list<ID_to_Folder_Map> _id_to_folder_map;
public:
Player(SPIMaster* s);
void vol_up();
@ -97,6 +105,7 @@ public:
void track_prev();
bool is_playing();
bool play_id(String id);
bool play_album(String album);
void play_random_album();
bool play_song(String album, uint8_t song_index, uint32_t offset=0);

View File

@ -64,9 +64,14 @@ void Controller::_check_rfid() {
if (uid > 0) {
_mqtt_client->publish_rfid_uid(uid);
_no_rfid_card_count = 0;
INFO("New RFID card uid: %08x\n", uid);
String s_uid = String(uid, HEX);
_player->play_album(s_uid);
String temp = String(uid, HEX);
String s_uid = "";
for (int i=0; i<(8-s_uid.length()); i++) {
s_uid.concat("0");
}
s_uid.concat(temp);
INFO("New RFID card uid: %s\n", s_uid.c_str());
_player->play_id(s_uid);
} else {
if (_no_rfid_card_count >= 1) {
INFO("No more RFID card.\n");
@ -105,7 +110,7 @@ void Controller::_execute_serial_command(String cmd) {
} else if (cmd.equals("play")) {
_player->play_random_album();
} else if (cmd.startsWith("play ")) {
_player->play_album(cmd.substring(5));
_player->play_id(cmd.substring(5));
} else if (cmd.startsWith("sys ")) {
_player->play_system_sound(cmd.substring(4));
} else if (cmd.equals("stop")) {

View File

@ -14,6 +14,8 @@ Player::Player(SPIMaster* s) {
_speaker_off();
_spi->disable();
PIN_VS1053_DREQ_SETUP();
_fill_id_to_folder_map();
_init();
}
@ -389,6 +391,47 @@ std::list<String> Player::_files_in_dir(String path) {
return result;
}
void Player::_fill_id_to_folder_map() {
DEBUG("_fill_id_to_folder_map() running...");
_spi->select_sd();
File root = SD.open("/");
File entry;
while (entry = root.openNextFile()) {
String foldername = entry.name();
TRACE("Looking at %s...\n", foldername.c_str());
if (!entry.isDirectory() || foldername.startsWith("/.")) continue;
if (!SD.exists(foldername + "/id.txt")) {
TRACE("Folder %s does not contain id.txt -> ignoring\n", foldername.c_str());
continue;
}
TRACE("Reading contents of %s...\n", (foldername + "/id.txt").c_str());
File f = SD.open(foldername + "/id.txt");
String buffer = "";
while (f.available()) {
char c = f.read();
if (c=='\n' || c=='\r') {
if (buffer.length() > 0) {
ID_to_Folder_Map m = {buffer.c_str(), foldername.c_str()};
DEBUG("Adding mapping '%s'=>'%s'\n", m.id, m.folder);
_id_to_folder_map.push_back(m);
buffer = "";
}
}
}
f.close();
if (buffer.length() > 0) {
ID_to_Folder_Map m = {buffer.c_str(), foldername.c_str()};
DEBUG("Adding mapping '%s'=>'%s'\n", m.id, m.folder);
_id_to_folder_map.push_back(m);
}
entry.close();
}
root.close();
DEBUG("fill_id_to_folder_map done.\n");
_spi->select_sd(false);
}
String Player::_random_album() {
std::list<String> albums = ls("/", false, true, false);
uint8_t rnd = random(albums.size());
@ -401,9 +444,27 @@ void Player::play_random_album() {
play_album(_random_album());
}
bool Player::play_id(String id) {
String folder = _foldername_for_id(id);
if (folder.length()==0) return false;
return play_album(folder);
}
String Player::_foldername_for_id(String id) {
DEBUG("Searching for id %s...\n", id.c_str());
for(std::list<ID_to_Folder_Map>::iterator it=_id_to_folder_map.begin(); it!=_id_to_folder_map.end(); it++) {
if (id.equals((*it).id)) {
DEBUG("Found folder '%s' for id %s.\n", (*it).folder, id.c_str());
return (*it).folder;
}
}
DEBUG("No folder found for id %s.\n", id.c_str());
return "";
}
bool Player::play_album(String album) {
album_state s = _last_tracks[album.c_str()];
DEBUG("Last index for album %s was %d,%d\n", album.c_str(), s.index, s.position);
DEBUG("Last index for album '%s' was %d,%d\n", album.c_str(), s.index, s.position);
return play_song(album, s.index, s.position);
}
@ -411,14 +472,7 @@ bool Player::play_song(String album, uint8_t index, uint32_t skip_to) {
if (_state == sleeping || _state == recording) _wakeup();
if (_state != idle) return false;
DEBUG("Trying to play song at index %d, offset %d of album %s\n", index, skip_to, album.c_str());
String path = _find_album_dir(album);
if (path.length()==0) {
ERROR("Could not find album.\n");
return false;
} else {
INFO("Found album in dir '%s'.\n", path.c_str());
}
std::list<String> files = _files_in_dir(path);
std::list<String> files = _files_in_dir(album);
_playing_album_songs = files.size();
DEBUG("Found %d songs in album\n", files.size());
if (index >= files.size()) {