diff --git a/build_version.sh b/build_version.sh index 807fff1..c1fcaf1 100755 --- a/build_version.sh +++ b/build_version.sh @@ -1,2 +1,2 @@ #!/bin/bash -echo -n "-DVERSION=\\\"`git describe --tags --dirty`\\\"" +echo -n "-DVERSION=\\\"`git describe --tags --dirty`-`date +%Y%m%d-%H%M%S`\\\"" diff --git a/include/player.h b/include/player.h index c1b72f0..a6a9dae 100644 --- a/include/player.h +++ b/include/player.h @@ -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; 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); diff --git a/src/controller.cpp b/src/controller.cpp index 418c9ca..d156090 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -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")) { diff --git a/src/player.cpp b/src/player.cpp index 93921a7..ebbaccf 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -14,6 +14,8 @@ Player::Player(SPIMaster* s) { _speaker_off(); _spi->disable(); PIN_VS1053_DREQ_SETUP(); + + _fill_id_to_folder_map(); _init(); } @@ -400,6 +402,47 @@ std::list 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 albums = ls("/", false, true, false); uint8_t rnd = random(albums.size()); @@ -412,9 +455,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::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); } @@ -422,14 +483,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 files = _files_in_dir(path); + std::list files = _files_in_dir(album); _playing_album_songs = files.size(); DEBUG("Found %d songs in album\n", files.size()); if (index >= files.size()) {