Made player and playlist_manager pubilc members of Controller.

This commit is contained in:
Fabian Schlenz 2019-11-19 20:46:04 +01:00
parent 5fad39ee0e
commit 68ecc05712
2 changed files with 39 additions and 38 deletions

View File

@ -18,7 +18,6 @@ class Controller {
private:
MFRC522* _rfid;
HTTPServer* _http_server;
PlaylistManager* _pm;
ControllerState _state = NORMAL;
bool _rfid_enabled = true;
void _check_rfid();
@ -30,7 +29,7 @@ private:
bool _rfid_present = false;
String _last_rfid_uid = "";
String _last_rfid_data = "";
Player* _player;
unsigned long _last_rfid_scan_at = 0;
unsigned long _last_position_info_at = 0;
String _serial_buffer = String();
@ -42,6 +41,8 @@ private:
bool _check_button(uint8_t btn);
public:
Controller(Player* p, PlaylistManager* pm);
PlaylistManager* pm;
Player* player;
void register_http_server(HTTPServer* h);
void loop();
void send_controller_status();

View File

@ -5,12 +5,12 @@
#include "http_server.h"
#include <ArduinoJson.h>
Controller::Controller(Player* p, PlaylistManager* pm) {
_player = p;
_pm = pm;
Controller::Controller(Player* p, PlaylistManager* playlist_manager) {
player = p;
pm = playlist_manager;
_rfid = new MFRC522(17, MFRC522::UNUSED_PIN);
_player->register_controller(this);
player->register_controller(this);
BTN_NEXT_SETUP();
BTN_PREV_SETUP();
@ -87,7 +87,7 @@ void Controller::_check_rfid() {
_rfid_present = false;
INFO("No more RFID card.\n");
if (_state != LOCKED) {
_player->stop();
player->stop();
}
send_controller_status();
} else {
@ -106,7 +106,7 @@ void Controller::_check_rfid() {
String data = _read_rfid_data();
_last_rfid_data = data;
Playlist* pl = _pm->get_playlist_for_id(s_uid);
Playlist* pl = pm->get_playlist_for_id(s_uid);
if (data.indexOf("[lock]") != -1) {
if (_state == LOCKED) {
_state = NORMAL;
@ -152,7 +152,7 @@ void Controller::_check_rfid() {
DEBUG("ControllerState is now LOCKED.\n");
}
_player->play(pl);
player->play(pl);
//send_playlist_manager_status();
send_controller_status();
}
@ -248,38 +248,38 @@ bool Controller::process_message(String cmd) {
DEBUG("Executing command: %s\n", cmd.c_str());
if (cmd.startsWith("play ")) {
Playlist* p = _pm->get_playlist_for_folder(cmd.substring(5));
_player->play(p);
Playlist* p = pm->get_playlist_for_folder(cmd.substring(5));
player->play(p);
//} else if (cmd.equals("ls")) {
// _execute_command_ls("/");
//} else if (cmd.startsWith("ls ")) {
// _execute_command_ls(cmd.substring(3));
} else if (cmd.equals("play")) {
_player->play();
player->play();
} else if (cmd.equals("stop")) {
_player->stop();
player->stop();
} else if (cmd.equals("help")) {
_execute_command_help();
} else if (cmd.equals("-")) {
_player->vol_down();
player->vol_down();
} else if (cmd.equals("+")) {
_player->vol_up();
player->vol_up();
} else if (cmd.startsWith("volume=")) {
uint8_t vol = cmd.substring(7).toInt();
_player->set_volume(vol);
player->set_volume(vol);
} else if (cmd.equals("track_prev")) {
_player->track_prev();
player->track_prev();
} else if (cmd.equals("track_next")) {
_player->track_next();
player->track_next();
} else if (cmd.startsWith("track=")) {
uint8_t track = cmd.substring(6).toInt();
_player->set_track(track);
player->set_track(track);
} else if (cmd.equals("ids")) {
_pm->dump_ids();
pm->dump_ids();
} else if (cmd.equals("reset_vs1053")) {
_player->stop();
_player->init();
player->stop();
player->init();
} else if (cmd.equals("reboot")) {
ESP.restart();
} else if (cmd.startsWith("add_mapping=")) {
@ -287,7 +287,7 @@ bool Controller::process_message(String cmd) {
uint8_t idx = rest.indexOf('=');
String id = rest.substring(0, idx);
String folder = rest.substring(idx + 1);
_pm->add_mapping(id, folder);
pm->add_mapping(id, folder);
send_playlist_manager_status();
} else {
ERROR("Unknown command: %s\n", cmd.c_str());
@ -299,7 +299,7 @@ bool Controller::process_message(String cmd) {
void Controller::_execute_command_ls(String path) {
INFO("Listing contents of %s:\n", path.c_str());
// TODO
//std::list<String> files = _player->ls(path);
//std::list<String> files = player->ls(path);
//for(std::list<String>::iterator it=files.begin(); it!=files.end(); ++it) {
// INFO(" %s\n", (*it).c_str());
//}
@ -321,17 +321,17 @@ void Controller::_check_buttons() {
if (BTN_PREV() && _debounce_button(0)) {
if (_state == NORMAL) {
_player->track_prev();
player->track_prev();
} else {
DEBUG("Ignoring btn_prev because state is LOCKED.\n");
}
} else if (BTN_VOL_UP() && _debounce_button(1)) {
_player->vol_up();
player->vol_up();
} else if (BTN_VOL_DOWN() && _debounce_button(2)) {
_player->vol_down();
player->vol_down();
} else if (BTN_NEXT() && _debounce_button(3)) {
if (_state == NORMAL) {
_player->track_next();
player->track_next();
} else {
DEBUG("Ignoring btn_next because state is LOCKED.\n");
}
@ -367,22 +367,22 @@ void Controller::send_player_status() {
TRACE("In send_player_status()...\n");
if (_http_server->ws->count() > 0) {
_http_server->ws->textAll(_player->json());
_http_server->ws->textAll(_player->position_json());
_http_server->ws->textAll(player->json());
_http_server->ws->textAll(player->position_json());
}
}
void Controller::send_playlist_manager_status() {
TRACE("In send_playlist_manager_status()...\n");
if (_http_server->ws->count() > 0) {
_http_server->ws->textAll(_pm->json());
_http_server->ws->textAll(pm->json());
}
}
void Controller::send_position() {
TRACE("In send_position()...\n");
if (_http_server->ws->count() > 0 && _player->is_playing()) {
_http_server->ws->textAll(_player->position_json());
if (_http_server->ws->count() > 0) {
_http_server->ws->textAll(player->position_json());
}
_last_position_info_at = millis();
}
@ -396,11 +396,11 @@ void Controller::send_controller_status() {
void Controller::inform_new_client(AsyncWebSocketClient* client) {
String s;
s += _pm->json();
s += pm->json();
s += '\n';
s += _player->json();
s += player->json();
s += '\n';
s += _player->position_json();
s += player->position_json();
s += '\n';
s += json();
client->text(s);
@ -412,6 +412,6 @@ void Controller::queue_command(String s) {
}
void Controller::update_playlist_manager() {
_pm->scan_files();
pm->scan_files();
send_playlist_manager_status();
}