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
|
|
|
|
|
|
|
|
Player::Player() {
|
2019-08-04 11:42:07 +00:00
|
|
|
pinMode(XRESET, OUTPUT);
|
|
|
|
digitalWrite(XRESET, HIGH);
|
|
|
|
pinMode(DREQ, INPUT);
|
|
|
|
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_reset() {
|
|
|
|
digitalWrite(XRESET, LOW);
|
2019-08-06 18:50:11 +00:00
|
|
|
delay(100);
|
2019-08-04 11:42:07 +00:00
|
|
|
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-04 11:42:07 +00:00
|
|
|
_reset();
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
uint16_t result = _read_control_register(SCI_MODE);
|
|
|
|
Serial.printf("SCI_MODE: 0x%04X\n", result);
|
|
|
|
if (result != 0x4800) {
|
|
|
|
Serial.printf("Error: SCI_MODE was 0x%04X, expected was 0x4800.\n", result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = _read_control_register(SCI_STATUS);
|
|
|
|
Serial.printf("SCI_STATUS: 0x%04X\n", result);
|
|
|
|
if (result != 0x0040 && result != 0x0048) {
|
|
|
|
Serial.printf("Error: SCI_STATUS was 0x%04X, expected was 0x0040 or 0x0048.\n", result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = _read_control_register(SCI_CLOCKF);
|
|
|
|
Serial.printf("SCI_CLOCKF: 0x%04X\n", result);
|
2019-08-04 11:42:07 +00:00
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
Serial.println("VS1053 Init looking good.");
|
|
|
|
Serial.println("Upping VS1053 multiplier...");
|
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);
|
|
|
|
Serial.printf("SCI_CLOCKF: 0x%04X\n", result);
|
|
|
|
if (result != 0x6000) {
|
|
|
|
Serial.printf("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
|
|
|
}
|
|
|
|
|
|
|
|
_end_byte = (int8_t)(_read_wram(ADDR_ENDBYTE) & 0xFF);
|
|
|
|
|
|
|
|
set_volume(VOLUME_DEFAULT);
|
|
|
|
|
|
|
|
Serial.println("VS1053 initialization completed.");
|
|
|
|
_state = idle;
|
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) {
|
|
|
|
Serial.printf("Reading WRAM address 0x%04X... ", address);
|
|
|
|
_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);
|
|
|
|
Serial.printf("Got 0x%04X and 0x%04X.\n", r1, r2);
|
|
|
|
return r1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::set_volume(uint8_t vol, bool save) {
|
|
|
|
if (save) {
|
|
|
|
_volume = vol;
|
|
|
|
}
|
|
|
|
Serial.printf("Setting volume to 0x%02X\n", vol);
|
|
|
|
vol = 0xFF - vol;
|
|
|
|
if (vol==0xFF) vol=0xFE;
|
|
|
|
uint16_t value = (vol<<8)|vol;
|
|
|
|
Serial.printf("Setting volume register to 0x%04X\n", value);
|
|
|
|
_write_control_register(SCI_VOL, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::vol_up() {
|
|
|
|
if (_volume + VOLUME_STEP > VOLUME_MAX) set_volume(VOLUME_MAX);
|
|
|
|
else set_volume(_volume + VOLUME_STEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::vol_down() {
|
|
|
|
if (_volume >= VOLUME_MIN + VOLUME_STEP) set_volume(_volume - VOLUME_STEP);
|
|
|
|
else set_volume(VOLUME_MIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_mute() {
|
|
|
|
Serial.println("Muting.");
|
|
|
|
set_volume(0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_unmute() {
|
|
|
|
Serial.println("Unmuting.");
|
|
|
|
set_volume(_volume, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::track_next() {
|
|
|
|
if (_state != playing) return;
|
|
|
|
stop();
|
|
|
|
play_song(_playing_album, _playing_index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::track_prev() {
|
|
|
|
if (_state != playing) return;
|
|
|
|
if (_playing_index == 0) _playing_index=1;
|
|
|
|
stop();
|
|
|
|
play_song(_playing_album, _playing_index - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<String> Player::_files_in_dir(String path) {
|
|
|
|
Serial.printf("Examining folder %s...\n", path.c_str());
|
|
|
|
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();
|
|
|
|
if (!entry.isDirectory() && !filename.startsWith(".") && filename.endsWith(".mp3")) {
|
|
|
|
//Serial.printf("Adding file %s\n", filename.c_str());
|
|
|
|
result.push_back(path + filename);
|
|
|
|
} else {
|
|
|
|
//Serial.printf("Ignoring entry %s\n", filename.c_str());
|
|
|
|
}
|
|
|
|
entry.close();
|
|
|
|
}
|
|
|
|
dir.close();
|
|
|
|
result.sort();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Player::play_album(String album) {
|
|
|
|
album_state s = _last_tracks[album.c_str()];
|
|
|
|
Serial.printf("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 != idle) return false;
|
|
|
|
Serial.printf("Playing song at index %d, offset %d of album %s\n", index, skip_to, album.c_str());
|
|
|
|
std::list<String> files = _files_in_dir(album);
|
|
|
|
Serial.printf("Found %d songs in album\n", files.size());
|
|
|
|
if (index >= files.size()) {
|
|
|
|
Serial.println("No matching file found - not playing.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//std::list<String>::iterator it = files.begin();
|
|
|
|
//std::advance(it, index);
|
|
|
|
String file = *(std::next(files.begin(), index));
|
|
|
|
Serial.printf("Opening file %s for playback...\n", file.c_str());
|
|
|
|
|
|
|
|
_file = SD.open(file);
|
|
|
|
|
|
|
|
Serial.println("Resetting SCI_DECODE_TIME...");
|
|
|
|
_write_control_register(SCI_DECODE_TIME, 0);
|
|
|
|
Serial.println("Resetting SS_DO_NOT_JUMP...");
|
|
|
|
_write_control_register(SCI_STATUS, _read_control_register(SCI_STATUS) & ~SS_DO_NOT_JUMP);
|
|
|
|
delay(100);
|
|
|
|
_state = playing;
|
|
|
|
_playing_album = album;
|
|
|
|
_playing_index = index;
|
|
|
|
_refills = 0;
|
|
|
|
_skip_to = skip_to;
|
|
|
|
if (_skip_to>0) _mute();
|
|
|
|
Serial.println("Now playing.");
|
|
|
|
_set_last_track(album.c_str(), index, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_flush(uint bytes) {
|
|
|
|
SPIMaster::enable(XDCS);
|
|
|
|
SPI.beginTransaction(*_spi_settings);
|
|
|
|
for(uint i=0; i<bytes; i++) {
|
|
|
|
_wait();
|
|
|
|
SPI.transfer(_end_byte);
|
|
|
|
}
|
|
|
|
SPI.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_finish_playing() {
|
|
|
|
_flush(2052);
|
|
|
|
_write_control_register(SCI_MODE, _read_control_register(SCI_MODE) | SM_CANCEL);
|
|
|
|
for (int i=0; i<64; i++) {
|
|
|
|
_flush(32);
|
|
|
|
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::_flush_and_cancel() {
|
|
|
|
Serial.println("In flush_and_cancel()");
|
|
|
|
//_flush(2052);
|
|
|
|
for (int i=0; i<64; i++) {
|
|
|
|
_write_control_register(SCI_MODE, _read_control_register(SCI_MODE) | SM_CANCEL);
|
|
|
|
}
|
|
|
|
_flush(2052);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::stop() {
|
|
|
|
if (_state != playing) return;
|
|
|
|
Serial.println("Stopping.");
|
|
|
|
_set_last_track(_playing_album.c_str(), _playing_index, (uint32_t)_file.position());
|
|
|
|
_state = stopping;
|
|
|
|
_stop_delay = 0;
|
|
|
|
_write_control_register(SCI_MODE, _read_control_register(SCI_MODE) | SM_CANCEL);
|
|
|
|
while (true) {
|
|
|
|
_refill();
|
|
|
|
uint16_t mode = _read_control_register(SCI_MODE);
|
|
|
|
if ((mode & SM_CANCEL) == 0) {
|
|
|
|
_flush(2052);
|
|
|
|
_finish_stopping();
|
|
|
|
break;
|
|
|
|
} else if (_stop_delay > 2048) {
|
|
|
|
init();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_stop_delay++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_finish_stopping() {
|
|
|
|
_state = idle;
|
|
|
|
if (_file) {
|
|
|
|
_file.close();
|
|
|
|
}
|
|
|
|
Serial.println("Stopped.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_refill() {
|
|
|
|
SPIMaster::enable(PIN_SD_CS);
|
|
|
|
_refills++;
|
|
|
|
if (_refills % 1000 == 0) Serial.print(".");
|
|
|
|
uint8_t result = _file.read(_buffer, sizeof(_buffer));
|
|
|
|
if (result == 0) {
|
|
|
|
// File is over.
|
|
|
|
Serial.println("EOF reached.");
|
|
|
|
_finish_playing();
|
|
|
|
_finish_stopping();
|
|
|
|
bool result = play_song(_playing_album, _playing_index + 1);
|
|
|
|
if (!result) {
|
|
|
|
_set_last_track(_playing_album.c_str(), 0, 0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_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) {
|
|
|
|
Serial.printf("Skipping to %d.\n", _skip_to);
|
|
|
|
_flush(2048);
|
|
|
|
_file.seek(_skip_to);
|
|
|
|
_skip_to = 0;
|
|
|
|
_unmute();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_skip_to = 0;
|
|
|
|
_unmute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Player::loop() {
|
|
|
|
if (digitalRead(DREQ) && (_state==playing || _state==stopping)) {
|
|
|
|
_refill();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_set_last_track(const char* album, uint8_t index, uint32_t position) {
|
|
|
|
Serial.printf("Setting _last_track[%s]=%d,%d.\n", album, index, position);
|
|
|
|
_last_tracks[album] = {index, position};
|
2019-08-04 11:42:07 +00:00
|
|
|
}
|