2019-08-04 11:42:07 +00:00
|
|
|
// Based on https://github.com/mpflaga/Arduino_Library-vs1053_for_SdFat/blob/master/src/vs1053_SdFat.cpp
|
|
|
|
|
|
|
|
#include "player.h"
|
|
|
|
#include "spi_master.h"
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
//Player::_spi_settings
|
|
|
|
|
2019-08-11 15:15:22 +00:00
|
|
|
Player::Player(MCP* m) {
|
|
|
|
_mcp = m;
|
|
|
|
_mcp->pinMode(XRESET, OUTPUT);
|
|
|
|
_mcp->digitalWrite(XRESET, HIGH);
|
2019-08-04 11:42:07 +00:00
|
|
|
pinMode(DREQ, INPUT);
|
|
|
|
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_reset() {
|
2019-08-11 15:15:22 +00:00
|
|
|
_mcp->digitalWrite(XRESET, LOW);
|
2019-08-06 18:50:11 +00:00
|
|
|
delay(100);
|
2019-08-11 15:15:22 +00:00
|
|
|
_mcp->digitalWrite(XRESET, HIGH);
|
2019-08-06 18:50:11 +00:00
|
|
|
delay(100);
|
|
|
|
_state = uninitialized;
|
|
|
|
_spi_settings = &_spi_settings_slow; // After reset, communication has to be slow
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_init() {
|
2019-08-06 18:50:11 +00:00
|
|
|
SPIMaster::disable();
|
2019-08-11 15:15:22 +00:00
|
|
|
DEBUG("Resetting VS1053...\n");
|
2019-08-04 11:42:07 +00:00
|
|
|
_reset();
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
uint16_t result = _read_control_register(SCI_MODE);
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("SCI_MODE: 0x%04X\n", result);
|
2019-08-06 18:50:11 +00:00
|
|
|
if (result != 0x4800) {
|
2019-08-09 04:27:33 +00:00
|
|
|
ERROR("SCI_MODE was 0x%04X, expected was 0x4800.\n", result);
|
2019-08-06 18:50:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = _read_control_register(SCI_STATUS);
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("SCI_STATUS: 0x%04X\n", result);
|
2019-08-06 18:50:11 +00:00
|
|
|
if (result != 0x0040 && result != 0x0048) {
|
2019-08-09 04:27:33 +00:00
|
|
|
ERROR("SCI_STATUS was 0x%04X, expected was 0x0040 or 0x0048.\n", result);
|
2019-08-06 18:50:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = _read_control_register(SCI_CLOCKF);
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("SCI_CLOCKF: 0x%04X\n", result);
|
2019-08-04 11:42:07 +00:00
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("VS1053 Init looking good.\n");
|
|
|
|
DEBUG("Upping VS1053 multiplier...\n");
|
2019-08-04 11:42:07 +00:00
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
_write_control_register(SCI_CLOCKF, 0x6000);
|
2019-08-04 11:42:07 +00:00
|
|
|
delay(10);
|
2019-08-06 18:50:11 +00:00
|
|
|
|
|
|
|
_spi_settings = &_spi_settings_fast;
|
|
|
|
|
|
|
|
result = _read_control_register(SCI_CLOCKF);
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("SCI_CLOCKF: 0x%04X\n", result);
|
2019-08-06 18:50:11 +00:00
|
|
|
if (result != 0x6000) {
|
2019-08-09 04:27:33 +00:00
|
|
|
ERROR("Error: SCI_CLOCKF was 0x%04X, expected was 0x6000.\n", result);
|
2019-08-04 11:42:07 +00:00
|
|
|
return;
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_volume(VOLUME_DEFAULT);
|
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("VS1053 initialization completed.\n");
|
2019-08-11 15:15:22 +00:00
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Checking system sounds...\n");
|
|
|
|
SPIMaster::enable(PIN_SD_CS);
|
|
|
|
_check_system_sound("no_prev_song.mp3");
|
|
|
|
_check_system_sound("no_next_song.mp3");
|
|
|
|
_check_system_sound("volume_max.mp3");
|
|
|
|
_check_system_sound("volume_min.mp3");
|
2019-08-11 15:15:22 +00:00
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
_state = idle;
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
void Player::_check_system_sound(String filename) {
|
|
|
|
String path = String("/system/") + filename;
|
|
|
|
if (!SD.exists(path)) {
|
|
|
|
ERROR("System sound %s is missing on the sd card!\n", path.c_str());
|
|
|
|
} else {
|
|
|
|
DEBUG("%s found.\n", path.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 11:42:07 +00:00
|
|
|
inline void Player::_wait() {
|
|
|
|
while(!digitalRead(DREQ));
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
uint16_t Player::_read_control_register(uint8_t address) {
|
|
|
|
_wait();
|
|
|
|
SPIMaster::enable(XCS);
|
|
|
|
SPI.beginTransaction(*_spi_settings);
|
2019-08-04 11:42:07 +00:00
|
|
|
SPI.transfer(CMD_READ);
|
|
|
|
SPI.transfer(address);
|
|
|
|
uint8_t b1 = SPI.transfer(0xFF);
|
2019-08-06 18:50:11 +00:00
|
|
|
_wait();
|
2019-08-04 11:42:07 +00:00
|
|
|
uint8_t b2 = SPI.transfer(0xFF);
|
2019-08-06 18:50:11 +00:00
|
|
|
_wait();
|
|
|
|
SPI.endTransaction();
|
|
|
|
SPIMaster::disable();
|
2019-08-04 11:42:07 +00:00
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
return (b1 << 8) | b2;
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
void Player::_write_control_register(uint8_t address, uint16_t value) {
|
|
|
|
uint8_t b1 = value >> 8;
|
|
|
|
uint8_t b2 = value & 0xFF;
|
|
|
|
_wait();
|
|
|
|
SPIMaster::enable(XCS);
|
|
|
|
SPI.beginTransaction(*_spi_settings);
|
2019-08-04 11:42:07 +00:00
|
|
|
SPI.transfer(CMD_WRITE);
|
|
|
|
SPI.transfer(address);
|
2019-08-06 18:50:11 +00:00
|
|
|
SPI.transfer(b1);
|
|
|
|
SPI.transfer(b2);
|
2019-08-04 11:42:07 +00:00
|
|
|
_wait();
|
2019-08-06 18:50:11 +00:00
|
|
|
SPI.endTransaction();
|
2019-08-04 11:42:07 +00:00
|
|
|
SPIMaster::disable();
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_write_data(uint8_t* buffer) {
|
|
|
|
SPIMaster::enable(XDCS);
|
|
|
|
SPI.beginTransaction(*_spi_settings);
|
|
|
|
for (uint i=0; i<sizeof(_buffer); i++) {
|
|
|
|
SPI.transfer(_buffer[i]);
|
|
|
|
}
|
|
|
|
SPI.endTransaction();
|
|
|
|
SPIMaster::disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Player::_read_wram(uint16_t address) {
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Reading WRAM address 0x%04X...\n", address);
|
2019-08-06 18:50:11 +00:00
|
|
|
_write_control_register(SCI_WRAMADDR, address);
|
|
|
|
uint16_t r1 = _read_control_register(SCI_WRAM);
|
|
|
|
_write_control_register(SCI_WRAMADDR, address);
|
|
|
|
uint16_t r2 = _read_control_register(SCI_WRAM);
|
2019-08-09 04:27:33 +00:00
|
|
|
if (r1 == r2) return r1;
|
|
|
|
DEBUG("Reading WRAM resulted in different values: 0x%04X and 0x%04X.\n", r1, r2);
|
|
|
|
_write_control_register(SCI_WRAMADDR, address);
|
|
|
|
r1 = _read_control_register(SCI_WRAM);
|
|
|
|
if (r1 == r2) return r1;
|
|
|
|
DEBUG("Reading WRAM resulted in different values: 0x%04X and 0x%04X.\n", r1, r2);
|
|
|
|
_write_control_register(SCI_WRAMADDR, address);
|
|
|
|
r2 = _read_control_register(SCI_WRAM);
|
|
|
|
if (r1 == r2) return r1;
|
|
|
|
DEBUG("Reading WRAM resulted in different values: 0x%04X and 0x%04X.\n", r1, r2);
|
|
|
|
DEBUG("Returning last value (0x%04X)...\n", r2);
|
|
|
|
return r2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t Player::_get_endbyte() {
|
|
|
|
int8_t endbyte = _read_wram(ADDR_ENDBYTE) & 0xFF;
|
|
|
|
DEBUG("Endbyte is 0x%02X.\n", endbyte);
|
|
|
|
return endbyte;
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
void Player::set_volume(uint8_t vol, bool save) {
|
|
|
|
if (save) {
|
|
|
|
_volume = vol;
|
|
|
|
}
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Setting volume to %d\n", vol);
|
2019-08-06 18:50:11 +00:00
|
|
|
vol = 0xFF - vol;
|
|
|
|
if (vol==0xFF) vol=0xFE;
|
|
|
|
uint16_t value = (vol<<8)|vol;
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Setting volume register to 0x%04X\n", value);
|
2019-08-06 18:50:11 +00:00
|
|
|
_write_control_register(SCI_VOL, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::vol_up() {
|
2019-08-09 04:27:33 +00:00
|
|
|
if (_volume == VOLUME_MAX) play_system_sound("volume_max.mp3");
|
|
|
|
else if (_volume + VOLUME_STEP > VOLUME_MAX) set_volume(VOLUME_MAX);
|
2019-08-06 18:50:11 +00:00
|
|
|
else set_volume(_volume + VOLUME_STEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::vol_down() {
|
|
|
|
if (_volume >= VOLUME_MIN + VOLUME_STEP) set_volume(_volume - VOLUME_STEP);
|
2019-08-09 04:27:33 +00:00
|
|
|
else if (_volume == VOLUME_MIN) play_system_sound("volume_min.mp3");
|
2019-08-06 18:50:11 +00:00
|
|
|
else set_volume(VOLUME_MIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_mute() {
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Muting.");
|
2019-08-06 18:50:11 +00:00
|
|
|
set_volume(0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_unmute() {
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Unmuting.");
|
2019-08-06 18:50:11 +00:00
|
|
|
set_volume(_volume, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::track_next() {
|
|
|
|
if (_state != playing) return;
|
2019-08-09 04:27:33 +00:00
|
|
|
if (_playing_index + 1 >= _playing_album_songs) {
|
|
|
|
play_system_sound("no_next_song.mp3");
|
|
|
|
return;
|
|
|
|
}
|
2019-08-06 18:50:11 +00:00
|
|
|
stop();
|
|
|
|
play_song(_playing_album, _playing_index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::track_prev() {
|
|
|
|
if (_state != playing) return;
|
2019-08-09 04:27:33 +00:00
|
|
|
if (_current_play_position > 100000) {
|
|
|
|
stop();
|
|
|
|
play_song(_playing_album, _playing_index);
|
|
|
|
} else {
|
|
|
|
if (_playing_index == 0) {
|
|
|
|
play_system_sound("no_prev_song.mp3");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stop();
|
|
|
|
play_song(_playing_album, _playing_index - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<String> Player::ls(String path) {
|
|
|
|
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()) {
|
|
|
|
String filename = entry.name();
|
|
|
|
if (entry.isDirectory()) filename.concat("/");
|
|
|
|
result.push_back(filename);
|
|
|
|
}
|
|
|
|
return result;
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 03:31:27 +00:00
|
|
|
String Player::_find_album_dir(String id) {
|
|
|
|
String id_with_divider = id + " - ";
|
|
|
|
File root = SD.open("/");
|
|
|
|
File entry;
|
|
|
|
String result = String("");
|
|
|
|
while ((result.length()==0) && (entry = root.openNextFile())) {
|
|
|
|
String name = entry.name();
|
|
|
|
if (entry.isDirectory() && (name.startsWith(id_with_divider) || name.equals(id))) {
|
|
|
|
result = name;
|
|
|
|
}
|
|
|
|
entry.close();
|
|
|
|
}
|
|
|
|
root.close();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
std::list<String> Player::_files_in_dir(String path) {
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Examining folder %s...\n", path.c_str());
|
2019-08-06 18:50:11 +00:00
|
|
|
if (!path.startsWith("/")) path = String("/") + path;
|
|
|
|
if (!path.endsWith("/")) path.concat("/");
|
|
|
|
std::list<String> result;
|
|
|
|
if (!SD.exists(path)) return result;
|
|
|
|
File dir = SD.open(path);
|
|
|
|
File entry;
|
|
|
|
while (entry = dir.openNextFile()) {
|
|
|
|
String filename = entry.name();
|
2019-08-08 03:31:27 +00:00
|
|
|
if (!entry.isDirectory() &&
|
2019-08-08 04:49:35 +00:00
|
|
|
!filename.startsWith(".") &&
|
2019-08-08 03:31:27 +00:00
|
|
|
( filename.endsWith(".mp3") ||
|
|
|
|
filename.endsWith(".ogg") ||
|
|
|
|
filename.endsWith(".wma") ||
|
|
|
|
filename.endsWith(".mp4") ||
|
2019-08-08 04:49:35 +00:00
|
|
|
filename.endsWith(".mpa"))) {
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG(" Adding entry %s\n", filename.c_str());
|
2019-08-06 18:50:11 +00:00
|
|
|
result.push_back(path + filename);
|
|
|
|
} else {
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG(" Ignoring entry %s\n", filename.c_str());
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
entry.close();
|
|
|
|
}
|
|
|
|
dir.close();
|
|
|
|
result.sort();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Player::play_album(String album) {
|
2019-08-08 04:49:35 +00:00
|
|
|
//if (_state==playing) stop();
|
2019-08-06 18:50:11 +00:00
|
|
|
album_state s = _last_tracks[album.c_str()];
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Last index for album %s was %d,%d\n", album.c_str(), s.index, s.position);
|
2019-08-06 18:50:11 +00:00
|
|
|
return play_song(album, s.index, s.position);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Player::play_song(String album, uint8_t index, uint32_t skip_to) {
|
|
|
|
if (_state != idle) return false;
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Trying to play song at index %d, offset %d of album %s\n", index, skip_to, album.c_str());
|
2019-08-08 03:31:27 +00:00
|
|
|
String path = _find_album_dir(album);
|
|
|
|
if (path.length()==0) {
|
2019-08-09 04:27:33 +00:00
|
|
|
ERROR("Could not find album.\n");
|
2019-08-08 03:31:27 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Found album in dir '%s'.\n", path.c_str());
|
2019-08-08 03:31:27 +00:00
|
|
|
}
|
|
|
|
std::list<String> files = _files_in_dir(path);
|
2019-08-09 04:27:33 +00:00
|
|
|
_playing_album_songs = files.size();
|
|
|
|
DEBUG("Found %d songs in album\n", files.size());
|
2019-08-06 18:50:11 +00:00
|
|
|
if (index >= files.size()) {
|
2019-08-09 04:27:33 +00:00
|
|
|
ERROR("No matching file found - not playing.\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//std::list<String>::iterator it = files.begin();
|
|
|
|
//std::advance(it, index);
|
|
|
|
String file = *(std::next(files.begin(), index));
|
2019-08-08 03:31:27 +00:00
|
|
|
_state = playing;
|
|
|
|
_playing_album = album;
|
|
|
|
_playing_index = index;
|
|
|
|
_set_last_track(album.c_str(), index, skip_to);
|
|
|
|
_play_file(file, skip_to);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::play_system_sound(String filename) {
|
|
|
|
//String file = String("/system/") + filename;
|
|
|
|
String file = filename;
|
|
|
|
if (!SD.exists(file)) {
|
2019-08-09 04:27:33 +00:00
|
|
|
ERROR("File %s does not exist!\n", file.c_str());
|
2019-08-08 03:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_state == playing) {
|
|
|
|
stop();
|
|
|
|
_state = system_sound_while_playing;
|
|
|
|
} else {
|
|
|
|
_state = system_sound_while_stopped;
|
|
|
|
}
|
|
|
|
_play_file(file, 0);
|
|
|
|
}
|
2019-08-06 18:50:11 +00:00
|
|
|
|
2019-08-08 03:31:27 +00:00
|
|
|
void Player::_play_file(String file, uint32_t file_offset) {
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("play_file('%s', %d)\n", file.c_str(), file_offset);
|
2019-08-06 18:50:11 +00:00
|
|
|
_file = SD.open(file);
|
2019-08-08 03:31:27 +00:00
|
|
|
if (!_file) return;
|
2019-08-06 18:50:11 +00:00
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Resetting SCI_DECODE_TIME...\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
_write_control_register(SCI_DECODE_TIME, 0);
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Resetting SS_DO_NOT_JUMP...\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
_write_control_register(SCI_STATUS, _read_control_register(SCI_STATUS) & ~SS_DO_NOT_JUMP);
|
|
|
|
delay(100);
|
2019-08-08 04:49:35 +00:00
|
|
|
|
2019-08-12 04:20:38 +00:00
|
|
|
if (file_offset == 0) {
|
|
|
|
_file.seek(_id3_tag_offset(_file));
|
|
|
|
}
|
2019-08-06 18:50:11 +00:00
|
|
|
_refills = 0;
|
2019-08-12 04:20:38 +00:00
|
|
|
_current_play_position = _file.position();
|
2019-08-08 03:31:27 +00:00
|
|
|
_skip_to = file_offset;
|
2019-08-06 18:50:11 +00:00
|
|
|
if (_skip_to>0) _mute();
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Now playing.\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 04:20:38 +00:00
|
|
|
uint32_t Player::_id3_tag_offset(File f) {
|
|
|
|
uint32_t original_position = f.position();
|
|
|
|
uint32_t offset = 0;
|
|
|
|
if (f.read()=='I' && f.read()=='D' && f.read()=='3') {
|
|
|
|
DEBUG("ID3 tag found\n");
|
|
|
|
// Skip ID3 tag version
|
|
|
|
f.read(); f.read();
|
|
|
|
byte tags = f.read();
|
|
|
|
bool footer_present = tags & 0x10;
|
|
|
|
DEBUG("ID3 footer found: %d\n", footer_present);
|
|
|
|
for (byte i=0; i<4; i++) {
|
|
|
|
offset <<= 7;
|
|
|
|
offset |= (0x7F & f.read());
|
|
|
|
}
|
|
|
|
offset += 10;
|
|
|
|
if (footer_present) offset += 10;
|
|
|
|
DEBUG("ID3 tag length is %d bytes.\n", offset);
|
|
|
|
} else {
|
|
|
|
DEBUG("No ID3 tag found\n");
|
|
|
|
}
|
|
|
|
f.seek(original_position);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
void Player::_flush(uint count, int8_t byte) {
|
2019-08-06 18:50:11 +00:00
|
|
|
SPIMaster::enable(XDCS);
|
|
|
|
SPI.beginTransaction(*_spi_settings);
|
2019-08-09 04:27:33 +00:00
|
|
|
for(uint i=0; i<count; i++) {
|
2019-08-06 18:50:11 +00:00
|
|
|
_wait();
|
2019-08-09 04:27:33 +00:00
|
|
|
SPI.transfer(byte);
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
SPI.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_finish_playing() {
|
2019-08-09 04:27:33 +00:00
|
|
|
uint8_t endbyte = _get_endbyte();
|
|
|
|
_flush(2052, endbyte);
|
2019-08-06 18:50:11 +00:00
|
|
|
_write_control_register(SCI_MODE, _read_control_register(SCI_MODE) | SM_CANCEL);
|
|
|
|
for (int i=0; i<64; i++) {
|
2019-08-09 04:27:33 +00:00
|
|
|
_flush(32, endbyte);
|
2019-08-06 18:50:11 +00:00
|
|
|
uint16_t mode = _read_control_register(SCI_MODE);
|
|
|
|
if ((mode & SM_CANCEL) == 0) return;
|
|
|
|
}
|
|
|
|
// If we reached this, the Chip didn't stop. That should not happen.
|
|
|
|
// (That's written in the manual.)
|
|
|
|
// Reset the chip.
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::stop() {
|
2019-08-08 03:31:27 +00:00
|
|
|
if (_state != playing /* && _state != system_sound_while_playing && _state != system_sound_while_stopped*/) return;
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Stopping...\n");
|
2019-08-08 03:31:27 +00:00
|
|
|
if (_state == playing) {
|
|
|
|
_set_last_track(_playing_album.c_str(), _playing_index, (uint32_t)_file.position());
|
|
|
|
}
|
2019-08-06 18:50:11 +00:00
|
|
|
_state = stopping;
|
|
|
|
_stop_delay = 0;
|
|
|
|
_write_control_register(SCI_MODE, _read_control_register(SCI_MODE) | SM_CANCEL);
|
2019-08-09 04:27:33 +00:00
|
|
|
uint8_t endbyte = _get_endbyte();
|
2019-08-06 18:50:11 +00:00
|
|
|
while (true) {
|
|
|
|
_refill();
|
|
|
|
uint16_t mode = _read_control_register(SCI_MODE);
|
|
|
|
if ((mode & SM_CANCEL) == 0) {
|
2019-08-09 04:27:33 +00:00
|
|
|
_flush(2052, endbyte);
|
2019-08-06 18:50:11 +00:00
|
|
|
_finish_stopping();
|
|
|
|
break;
|
|
|
|
} else if (_stop_delay > 2048) {
|
|
|
|
init();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_stop_delay++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_finish_stopping() {
|
|
|
|
_state = idle;
|
|
|
|
if (_file) {
|
|
|
|
_file.close();
|
|
|
|
}
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Stopped.\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_refill() {
|
|
|
|
SPIMaster::enable(PIN_SD_CS);
|
|
|
|
_refills++;
|
2019-08-09 04:27:33 +00:00
|
|
|
if (_refills % 1000 == 0) DEBUG(".");
|
2019-08-06 18:50:11 +00:00
|
|
|
uint8_t result = _file.read(_buffer, sizeof(_buffer));
|
|
|
|
if (result == 0) {
|
|
|
|
// File is over.
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("EOF reached.\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
_finish_playing();
|
2019-08-08 03:31:27 +00:00
|
|
|
if (_state == system_sound_while_playing) {
|
|
|
|
_finish_stopping();
|
|
|
|
play_album(_playing_album);
|
|
|
|
return;
|
|
|
|
} else if (_state == system_sound_while_stopped) {
|
|
|
|
_finish_stopping();
|
|
|
|
return;
|
|
|
|
}
|
2019-08-08 04:49:35 +00:00
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
_finish_stopping();
|
|
|
|
bool result = play_song(_playing_album, _playing_index + 1);
|
|
|
|
if (!result) {
|
|
|
|
_set_last_track(_playing_album.c_str(), 0, 0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-08-09 04:27:33 +00:00
|
|
|
_current_play_position+=result;
|
2019-08-06 18:50:11 +00:00
|
|
|
_write_data(_buffer);
|
|
|
|
|
|
|
|
if (_skip_to > 0) {
|
|
|
|
if (_skip_to > _file.position()) {
|
|
|
|
uint16_t status = _read_control_register(SCI_STATUS);
|
|
|
|
if ((status & SS_DO_NOT_JUMP) == 0) {
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Skipping to %d.\n", _skip_to);
|
|
|
|
_flush(2048, _get_endbyte());
|
2019-08-06 18:50:11 +00:00
|
|
|
_file.seek(_skip_to);
|
|
|
|
_skip_to = 0;
|
|
|
|
_unmute();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_skip_to = 0;
|
|
|
|
_unmute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 03:31:27 +00:00
|
|
|
bool Player::_refill_needed() {
|
|
|
|
return _state==playing ||
|
2019-08-08 04:49:35 +00:00
|
|
|
_state==stopping ||
|
|
|
|
_state==system_sound_while_playing ||
|
2019-08-08 03:31:27 +00:00
|
|
|
_state==system_sound_while_stopped;
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
bool Player::loop() {
|
2019-08-08 03:31:27 +00:00
|
|
|
if (digitalRead(DREQ) && _refill_needed()) {
|
2019-08-06 18:50:11 +00:00
|
|
|
_refill();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_set_last_track(const char* album, uint8_t index, uint32_t position) {
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Setting _last_track[%s]=%d,%d.\n", album, index, position);
|
2019-08-06 18:50:11 +00:00
|
|
|
_last_tracks[album] = {index, position};
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|