Added sleep mode for VS1053, HTTP server, tar upload, JSON status, RFID card removal debouncing, ...

This commit is contained in:
2019-08-12 20:15:00 +02:00
parent cd4251df86
commit 651a4e8510
8 changed files with 286 additions and 13 deletions

View File

@ -72,6 +72,27 @@ void Player::_init() {
_state = idle;
}
void Player::_sleep() {
DEBUG("VS1053 going to sleep.\n");
_write_control_register(SCI_CLOCKF, 0x0000);
_spi_settings = &_spi_settings_slow;
_write_control_register(SCI_AUDATA, 0x0010);
set_volume(0, false);
_state = sleeping;
}
void Player::_wakeup() {
if (_state != sleeping) return;
_stopped_at = millis();
DEBUG("Waking VS1053...\n");
set_volume(_volume, false);
_write_control_register(SCI_AUDATA, 0x0000);
_write_control_register(SCI_CLOCKF, 0x6000);
delay(10);
_spi_settings = &_spi_settings_fast;
_state = idle;
}
void Player::_check_system_sound(String filename) {
String path = String("/system/") + filename;
if (!SD.exists(path)) {
@ -159,7 +180,6 @@ void Player::set_volume(uint8_t vol, bool save) {
}
INFO("Setting volume to %d\n", vol);
vol = 0xFF - vol;
if (vol==0xFF) vol=0xFE;
uint16_t value = (vol<<8)|vol;
DEBUG("Setting volume register to 0x%04X\n", value);
_write_control_register(SCI_VOL, value);
@ -178,12 +198,12 @@ void Player::vol_down() {
}
void Player::_mute() {
INFO("Muting.");
set_volume(0, false);
INFO("Muting.\n");
set_volume(1, false);
}
void Player::_unmute() {
INFO("Unmuting.");
INFO("Unmuting.\n");
set_volume(_volume, false);
}
@ -212,21 +232,30 @@ void Player::track_prev() {
}
}
std::list<String> Player::ls(String path) {
bool Player::is_playing() {
return _state == playing;
}
std::list<String> Player::ls(String path, bool withFiles, bool withDirs, bool withHidden) {
SPIMaster::enable(PIN_SD_CS);
std::list<String> result;
if (!SD.exists(path)) return result;
File dir = SD.open(path);
File entry;
while (entry = dir.openNextFile()) {
if (!withDirs && entry.isDirectory()) continue;
if (!withFiles && !entry.isDirectory()) continue;
String filename = entry.name();
if (!withHidden && filename.startsWith(".")) continue;
if (entry.isDirectory()) filename.concat("/");
result.push_back(filename);
}
result.sort();
return result;
}
String Player::_find_album_dir(String id) {
if (id.endsWith("/")) id = id.substring(0, id.length() - 1);
String id_with_divider = id + " - ";
File root = SD.open("/");
File entry;
@ -259,10 +288,10 @@ std::list<String> Player::_files_in_dir(String path) {
filename.endsWith(".wma") ||
filename.endsWith(".mp4") ||
filename.endsWith(".mpa"))) {
DEBUG(" Adding entry %s\n", filename.c_str());
TRACE(" Adding entry %s\n", filename.c_str());
result.push_back(path + filename);
} else {
DEBUG(" Ignoring entry %s\n", filename.c_str());
TRACE(" Ignoring entry %s\n", filename.c_str());
}
entry.close();
}
@ -272,14 +301,26 @@ std::list<String> Player::_files_in_dir(String path) {
return result;
}
String Player::_random_album() {
std::list<String> albums = ls("/", false, true, false);
uint8_t rnd = random(albums.size());
std::list<String>::iterator it = albums.begin();
for (int i=0; i<rnd; i++) { it++; }
return *it;
}
void Player::play_random_album() {
play_album(_random_album());
}
bool Player::play_album(String album) {
//if (_state==playing) stop();
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);
return play_song(album, s.index, s.position);
}
bool Player::play_song(String album, uint8_t index, uint32_t skip_to) {
if (_state == sleeping) _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);
@ -420,6 +461,7 @@ void Player::stop() {
void Player::_finish_stopping() {
_state = idle;
_stopped_at = millis();
if (_file) {
_file.close();
}
@ -483,6 +525,10 @@ bool Player::loop() {
_refill();
return true;
}
if (_state == idle && _stopped_at < millis() - VS1053_SLEEP_DELAY) {
_sleep();
}
return false;
}