diff --git a/main.c b/main.c index 5dd15af..f754f46 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include pthread_cond_t isr_cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t isr_mtx = PTHREAD_MUTEX_INITIALIZER; @@ -33,12 +35,19 @@ int main() { struct mpd_connection *conn; + unsigned int tries = 0; conn = mpd_connection_new(NULL, 0, 0); + while ((mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) && (tries < 60)) { + mpd_connection_free(conn); + conn = mpd_connection_new(NULL, 0, 0); + tries++; + sleep(1); + } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { mpd_connection_free(conn); return EXIT_FAILURE; } - + wiringPiSetupGpio(); pullUpDnControl(FASTFORWARD_PIN, PUD_UP); pullUpDnControl(PLAYPAUSE_PIN, PUD_UP); @@ -53,6 +62,7 @@ wiringPiISR(ONOFF_PIN, INT_EDGE_FALLING, &onOffInterrupt); wiringPiISR(VOLUMEUP_PIN, INT_EDGE_FALLING, &volumeUpInterrupt); + bool playing = true; for(;;) { int last_pin_pressed = 0; pthread_mutex_lock(&isr_mtx); @@ -64,9 +74,17 @@ pthread_mutex_unlock(&isr_mtx); switch(last_pin_pressed) { case PLAYPAUSE_PIN: - case ONOFF_PIN: mpd_run_toggle_pause(conn); break; + case ONOFF_PIN: + if (playing) { + mpd_run_stop(conn); + playing = false; + } else { + mpd_run_play(conn); + playing = true; + } + break; case FASTFORWARD_PIN: mpd_run_next(conn); break; @@ -85,5 +103,6 @@ } + mpd_connection_free(conn); return 0; }