prepare for tracking volume and play/pause locally

This commit is contained in:
2019-07-13 21:33:43 +02:00
parent dbcf80ef25
commit ffc9accb6c

View File

@@ -29,6 +29,11 @@ struct song_info {
String artist; String artist;
}; };
struct player_info {
int volume;
bool is_playing;
};
enum btn_flag { BTN_NONE, BTN_VOLUP, BTN_VOLDN, BTN_PAUSE, BTN_NEXT }; enum btn_flag { BTN_NONE, BTN_VOLUP, BTN_VOLDN, BTN_PAUSE, BTN_NEXT };
btn_flag flag = BTN_NONE; btn_flag flag = BTN_NONE;
@@ -219,9 +224,10 @@ void volume_down() {
send_command("volume -1"); send_command("volume -1");
} }
int get_volume() { int get_player_info(player_info* player_info) {
WiFiClient client; WiFiClient client;
String vol_str; String vol_str;
String state_str;
if (client.connect(MPDHOST, MPDPORT)) { if (client.connect(MPDHOST, MPDPORT)) {
Serial.println("connected"); Serial.println("connected");
@@ -235,6 +241,9 @@ int get_volume() {
if (line.startsWith("volume: ")) { if (line.startsWith("volume: ")) {
vol_str = line.substring(8); vol_str = line.substring(8);
} }
else if (line.startsWith("state: ")) {
state_str = line.substring(7);
}
else if (line.equals("OK")) { else if (line.equals("OK")) {
client.stop(); client.stop();
} }
@@ -247,10 +256,13 @@ int get_volume() {
else { else {
display_error("mpd connect failed"); display_error("mpd connect failed");
client.stop(); client.stop();
return -1; return 1;
} }
return vol_str.toInt(); player_info->volume = vol_str.toInt();
player_info->is_playing = state_str.equals("play");
return 0;
} }
int get_song_info(song_info* song_info) { int get_song_info(song_info* song_info) {
@@ -290,7 +302,11 @@ int get_song_info(song_info* song_info) {
} }
void display_volume() { void display_volume() {
int volume = get_volume(); player_info pi;
if (get_player_info(&pi))
return;
int volume = pi.volume;
display.clearDisplay(); display.clearDisplay();
display.drawBitmap(0, 0, volume_icon, 32, 32, WHITE); display.drawBitmap(0, 0, volume_icon, 32, 32, WHITE);