Massive changes. Added a quite nice webinterface with live data using WebSockets. Removed the FTP server (wasn't that useful anyways). JSON creating using ArduinoJson instead of String concatenation. Ans, and, and.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "player.h"
|
||||
#include "spi_master.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
//Player::_spi_settings
|
||||
|
||||
@@ -15,7 +16,11 @@ Player::Player(SPIMaster* s) {
|
||||
_spi->disable();
|
||||
PIN_VS1053_DREQ_SETUP();
|
||||
|
||||
_init();
|
||||
init();
|
||||
}
|
||||
|
||||
void Player::register_controller(Controller* c) {
|
||||
_controller = c;
|
||||
}
|
||||
|
||||
void Player::_reset() {
|
||||
@@ -27,7 +32,7 @@ void Player::_reset() {
|
||||
_spi_settings = &_spi_settings_slow; // After reset, communication has to be slow
|
||||
}
|
||||
|
||||
void Player::_init() {
|
||||
void Player::init() {
|
||||
DEBUG("Resetting VS1053...\n");
|
||||
_reset();
|
||||
|
||||
@@ -91,6 +96,7 @@ void Player::_sleep() {
|
||||
_write_control_register(SCI_AUDATA, 0x0010);
|
||||
set_volume(0, false);
|
||||
_state = sleeping;
|
||||
TRACE("VS1053 is sleeping now.\n");
|
||||
}
|
||||
|
||||
void Player::_wakeup() {
|
||||
@@ -506,6 +512,12 @@ void Player::track_prev() {
|
||||
}
|
||||
}
|
||||
|
||||
void Player::set_track(uint8_t id) {
|
||||
stop();
|
||||
_current_playlist->set_track(id);
|
||||
play();
|
||||
}
|
||||
|
||||
bool Player::is_playing() {
|
||||
return _state == playing;
|
||||
}
|
||||
@@ -518,10 +530,13 @@ bool Player::play(Playlist* p) {
|
||||
bool Player::play() {
|
||||
if (_state == sleeping || _state == recording) _wakeup();
|
||||
if (_state != idle) return false;
|
||||
if (_current_playlist == NULL) return false;
|
||||
_current_playlist->start();
|
||||
String file = _current_playlist->get_current_file();
|
||||
uint32_t position = _current_playlist->get_position();
|
||||
_state = playing;
|
||||
_play_file(file, position);
|
||||
_controller->send_player_status();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -529,6 +544,7 @@ void Player::_play_file(String file, uint32_t file_offset) {
|
||||
INFO("play_file('%s', %d)\n", file.c_str(), file_offset);
|
||||
_spi->select_sd();
|
||||
_file = SD.open(file);
|
||||
_file_size = _file.size();
|
||||
_spi->select_sd(false);
|
||||
if (!_file) {
|
||||
DEBUG("Could not open file %s", file.c_str());
|
||||
@@ -552,6 +568,7 @@ void Player::_play_file(String file, uint32_t file_offset) {
|
||||
if (_skip_to>0) _mute();
|
||||
else _speaker_on();
|
||||
INFO("Now playing.\n");
|
||||
_controller->send_player_status();
|
||||
}
|
||||
|
||||
uint32_t Player::_id3_tag_offset(File f) {
|
||||
@@ -601,7 +618,7 @@ void Player::_finish_playing() {
|
||||
// If we reached this, the Chip didn't stop. That should not happen.
|
||||
// (That's written in the manual.)
|
||||
// Reset the chip.
|
||||
_init();
|
||||
init();
|
||||
}
|
||||
|
||||
void Player::stop(bool turn_speaker_off) {
|
||||
@@ -635,7 +652,10 @@ void Player::_finish_stopping(bool turn_speaker_off) {
|
||||
if (_file) {
|
||||
_file.close();
|
||||
}
|
||||
_current_play_position = 0;
|
||||
_file_size = 0;
|
||||
INFO("Stopped.\n");
|
||||
_controller->send_player_status();
|
||||
}
|
||||
|
||||
void Player::_refill() {
|
||||
@@ -655,7 +675,9 @@ void Player::_refill() {
|
||||
play();
|
||||
} else {
|
||||
_current_playlist->reset();
|
||||
_controller->send_player_status();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
_current_play_position+=result;
|
||||
@@ -672,6 +694,7 @@ void Player::_refill() {
|
||||
_spi->select_sd(false);
|
||||
_skip_to = 0;
|
||||
_unmute();
|
||||
_controller->send_position();
|
||||
}
|
||||
} else {
|
||||
_skip_to = 0;
|
||||
@@ -712,3 +735,29 @@ bool Player::loop() {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String Player::json() {
|
||||
DynamicJsonDocument json(10240);
|
||||
json["_type"] = "player";
|
||||
json["playing"] = is_playing();
|
||||
if (_current_playlist) {
|
||||
JsonObject playlist = json.createNestedObject("playlist");
|
||||
_current_playlist->json(playlist);
|
||||
} else {
|
||||
json["playlist"] = nullptr;
|
||||
}
|
||||
JsonObject volume = json.createNestedObject("volume");
|
||||
volume["current"] = _volume;
|
||||
volume["min"] = VOLUME_MIN;
|
||||
volume["max"] = VOLUME_MAX;
|
||||
volume["step"] = VOLUME_STEP;
|
||||
return json.as<String>();
|
||||
}
|
||||
|
||||
String Player::position_json() {
|
||||
DynamicJsonDocument json(200);
|
||||
json["_type"] = "position";
|
||||
json["position"] = _current_play_position;
|
||||
json["file_size"] = _file_size;
|
||||
return json.as<String>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user