371 lines
8.6 KiB
C++
371 lines
8.6 KiB
C++
#include <Wire.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <Ticker.h>
|
|
|
|
#include "icons.h"
|
|
|
|
#define SCREEN_WIDTH 128 // OLED display width, in pixels
|
|
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
|
|
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
|
|
|
#define MPDHOST "music.yannik.intern.yannikenss.de"
|
|
#define MPDPORT 6600
|
|
|
|
#define BUTTON_NEXT 14
|
|
#define BUTTON_PAUSE 12
|
|
#define BUTTON_VOLUP 13
|
|
#define BUTTON_VOLDN 0
|
|
|
|
const char* ssid = "hiddennetworkforchromecast";
|
|
const char* password = "raisingelephantsissoutterlyboring";
|
|
|
|
WiFiClient espClient;
|
|
|
|
struct song_info {
|
|
String title;
|
|
String artist;
|
|
};
|
|
|
|
struct player_info {
|
|
int volume;
|
|
bool is_playing;
|
|
};
|
|
|
|
enum btn_flag { BTN_NONE, BTN_VOLUP, BTN_VOLDN, BTN_PAUSE, BTN_NEXT };
|
|
|
|
btn_flag flag = BTN_NONE;
|
|
|
|
bool inhibit = false;
|
|
Ticker inhibit_ticker;
|
|
const int debounce_wait = 200;
|
|
|
|
//ISRs
|
|
|
|
void reset_inhibit() {
|
|
inhibit = false;
|
|
}
|
|
|
|
void set_flag_pause() {
|
|
if (!inhibit) {
|
|
flag = BTN_PAUSE;
|
|
inhibit = true;
|
|
inhibit_ticker.once_ms(debounce_wait, reset_inhibit);
|
|
}
|
|
}
|
|
void set_flag_next() {
|
|
if (!inhibit) {
|
|
flag = BTN_NEXT;
|
|
inhibit = true;
|
|
inhibit_ticker.once_ms(debounce_wait, reset_inhibit);
|
|
}
|
|
}
|
|
void set_flag_volup() {
|
|
if (!inhibit) {
|
|
flag = BTN_VOLUP;
|
|
inhibit = true;
|
|
inhibit_ticker.once_ms(debounce_wait, reset_inhibit);
|
|
}
|
|
}
|
|
void set_flag_voldn() {
|
|
if (!inhibit) {
|
|
flag = BTN_VOLDN;
|
|
inhibit = true;
|
|
inhibit_ticker.once_ms(debounce_wait, reset_inhibit);
|
|
}
|
|
}
|
|
|
|
//end ISRs
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
|
|
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
|
|
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
|
|
Serial.println(F("SSD1306 allocation failed"));
|
|
for(;;); // Don't proceed, loop forever
|
|
}
|
|
|
|
// Clear the buffer
|
|
display.clearDisplay();
|
|
|
|
display.display();
|
|
|
|
setup_wifi();
|
|
|
|
delay(1000);
|
|
|
|
Serial.println("Start main program");
|
|
|
|
pinMode(BUTTON_NEXT, INPUT_PULLUP);
|
|
pinMode(BUTTON_PAUSE, INPUT_PULLUP);
|
|
pinMode(BUTTON_VOLUP, INPUT_PULLUP);
|
|
pinMode(BUTTON_VOLDN, INPUT_PULLUP);
|
|
attachInterrupt(digitalPinToInterrupt(BUTTON_NEXT), set_flag_next, FALLING);
|
|
attachInterrupt(digitalPinToInterrupt(BUTTON_PAUSE), set_flag_pause, FALLING);
|
|
attachInterrupt(digitalPinToInterrupt(BUTTON_VOLUP), set_flag_volup, FALLING);
|
|
attachInterrupt(digitalPinToInterrupt(BUTTON_VOLDN), set_flag_voldn, FALLING);
|
|
|
|
|
|
display_song_info();
|
|
|
|
}
|
|
|
|
void loop() {
|
|
Serial.println("loop");
|
|
|
|
for (int i = 0; i<10; i++) {
|
|
delay(100);
|
|
|
|
switch (flag) {
|
|
case BTN_NEXT:
|
|
Serial.println("next flag");
|
|
next_song();
|
|
flag = BTN_NONE;
|
|
break;
|
|
case BTN_PAUSE:
|
|
Serial.println("pause flag");
|
|
toggle_pause();
|
|
flag = BTN_NONE;
|
|
break;
|
|
case BTN_VOLUP:
|
|
Serial.println("volup flag");
|
|
display_volume();
|
|
volume_up();
|
|
delay(10);
|
|
display_volume();
|
|
delay(500);
|
|
while (!digitalRead(BUTTON_VOLUP)) {
|
|
volume_up();
|
|
delay(10);
|
|
display_volume();
|
|
delay(250);
|
|
}
|
|
flag = BTN_NONE;
|
|
break;
|
|
case BTN_VOLDN:
|
|
Serial.println("voldn flag");
|
|
display_volume();
|
|
delay(10);
|
|
volume_down();
|
|
display_volume();
|
|
delay(500);
|
|
while (!digitalRead(BUTTON_VOLDN)) {
|
|
volume_down();
|
|
delay(10);
|
|
display_volume();
|
|
delay(250);
|
|
}
|
|
flag = BTN_NONE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
display_song_info();
|
|
}
|
|
|
|
void display_error(char* error) {
|
|
Serial.println("Displaying error:");
|
|
Serial.println(error);
|
|
display.clearDisplay();
|
|
display.setCursor(0, 0);
|
|
display.println("Error:");
|
|
display.println(error);
|
|
display.display();
|
|
}
|
|
|
|
void send_command(String command) {
|
|
WiFiClient client;
|
|
|
|
if (client.connect(MPDHOST, MPDPORT)) {
|
|
client.println(command);
|
|
while (client.available()) {
|
|
Serial.write(client.read());
|
|
}
|
|
client.stop();
|
|
}
|
|
else {
|
|
Serial.print("send command \"");
|
|
Serial.print(command);
|
|
Serial.println("\" failed");
|
|
display_error("mpd connect failed");
|
|
client.stop();
|
|
}
|
|
}
|
|
|
|
void next_song() {
|
|
display.clearDisplay();
|
|
display.drawBitmap(0, 0, fastforward_icon, 128 ,32, WHITE);
|
|
display.display();
|
|
|
|
send_command("next");
|
|
|
|
delay(250);
|
|
}
|
|
|
|
void toggle_pause() {
|
|
display.clearDisplay();
|
|
display.drawBitmap(0, 0, playpause_icon, 128 ,32, WHITE);
|
|
display.display();
|
|
|
|
send_command("pause");
|
|
|
|
delay(250);
|
|
}
|
|
|
|
void volume_up() {
|
|
send_command("volume 1");
|
|
}
|
|
|
|
void volume_down() {
|
|
send_command("volume -1");
|
|
}
|
|
|
|
int get_player_info(player_info* player_info) {
|
|
WiFiClient client;
|
|
String vol_str;
|
|
String state_str;
|
|
|
|
if (client.connect(MPDHOST, MPDPORT)) {
|
|
Serial.println("connected");
|
|
client.print("status\n");
|
|
|
|
while (client.connected() || client.available())
|
|
{
|
|
if (client.available())
|
|
{
|
|
String line = client.readStringUntil('\n');
|
|
if (line.startsWith("volume: ")) {
|
|
vol_str = line.substring(8);
|
|
}
|
|
else if (line.startsWith("state: ")) {
|
|
state_str = line.substring(7);
|
|
}
|
|
else if (line.equals("OK")) {
|
|
client.stop();
|
|
}
|
|
}
|
|
}
|
|
|
|
client.stop();
|
|
Serial.println("connection done");
|
|
}
|
|
else {
|
|
display_error("mpd connect failed");
|
|
client.stop();
|
|
return 1;
|
|
}
|
|
|
|
player_info->volume = vol_str.toInt();
|
|
player_info->is_playing = state_str.equals("play");
|
|
|
|
return 0;
|
|
}
|
|
|
|
int get_song_info(song_info* song_info) {
|
|
WiFiClient client;
|
|
|
|
if (client.connect(MPDHOST, MPDPORT)) {
|
|
Serial.println("connected");
|
|
client.print("currentsong\n");
|
|
|
|
while (client.connected() || client.available())
|
|
{
|
|
if (client.available())
|
|
{
|
|
String line = client.readStringUntil('\n');
|
|
if (line.startsWith("Title: ")) {
|
|
song_info->title = line.substring(7);
|
|
}
|
|
else if (line.startsWith("Artist: ")) {
|
|
song_info->artist = line.substring(8);
|
|
}
|
|
else if (line.equals("OK")) {
|
|
client.stop();
|
|
}
|
|
}
|
|
}
|
|
|
|
client.stop();
|
|
Serial.println("connection done");
|
|
}
|
|
else {
|
|
display_error("mpd connect failed");
|
|
client.stop();
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void display_volume() {
|
|
player_info pi;
|
|
if (get_player_info(&pi))
|
|
return;
|
|
|
|
int volume = pi.volume;
|
|
|
|
display.clearDisplay();
|
|
display.drawBitmap(0, 0, volume_icon, 32, 32, WHITE);
|
|
|
|
display.setTextSize(2); // Draw 2X-scale text
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(40, 8);
|
|
display.setTextWrap(false);
|
|
display.println(volume);
|
|
display.display(); // Show initial text
|
|
}
|
|
|
|
void display_song_info() {
|
|
WiFiClient client;
|
|
|
|
song_info song_info;
|
|
|
|
if (get_song_info(&song_info)) {
|
|
Serial.println("Error in get_song_info");
|
|
return;
|
|
}
|
|
|
|
display.clearDisplay();
|
|
|
|
display.setTextSize(2); // Draw 2X-scale text
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(0, 0);
|
|
display.setTextWrap(false);
|
|
display.println(song_info.title);
|
|
display.println(song_info.artist);
|
|
display.display(); // Show initial text
|
|
}
|
|
|
|
|
|
void setup_wifi() {
|
|
display.clearDisplay();
|
|
display.setTextColor(WHITE);
|
|
display.setTextSize(1);
|
|
display.setCursor(0, 0);
|
|
display.setTextWrap(true);
|
|
|
|
delay(10);
|
|
// We start by connecting to a WiFi network
|
|
display.print("Network: ");
|
|
display.print(ssid);
|
|
display.display();
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
display.print(".");
|
|
display.display();
|
|
}
|
|
|
|
display.println("");
|
|
display.println("WiFi connected");
|
|
display.print("IP: ");
|
|
display.println(WiFi.localIP());
|
|
display.display();
|
|
}
|