diff --git a/main.c b/main.c index 9d046f0..218a021 100644 --- a/main.c +++ b/main.c @@ -7,9 +7,8 @@ #include #include -pthread_cond_t isr_cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t isr_mtx = PTHREAD_MUTEX_INITIALIZER; -unsigned int pin_pressed= 0; +volatile sig_atomic_t pin_pressed= 0; unsigned int last_pressed_time = 0; #define FASTFORWARD_PIN 5 @@ -29,7 +28,6 @@ } else { pin_pressed = 0; } - pthread_cond_signal(&isr_cond); pthread_mutex_unlock(&isr_mtx); } @@ -74,74 +72,73 @@ bool playing = true; for(;;) { - int last_pin_pressed = 0; - pthread_mutex_lock(&isr_mtx); - while (pin_pressed == 0) { - pthread_cond_wait(&isr_cond, &isr_mtx); - } - last_pin_pressed = pin_pressed; - pin_pressed = 0; - pthread_mutex_unlock(&isr_mtx); bool success = false; while(!success) { - switch(last_pin_pressed) { + switch(pin_pressed) { case PLAYPAUSE_PIN: - success = mpd_run_toggle_pause(conn); - printf("play/pause %d\n", success); - break; + success = mpd_run_toggle_pause(conn); + printf("play/pause %d\n", success); + break; case ONOFF_PIN: - if (playing) { - success = mpd_run_stop(conn); - printf("stop %d\n", success); - } else { - success = mpd_run_play(conn); - printf("start %d\n", success); - } - if (success) { - playing = !playing; - } - break; + if (playing) { + success = mpd_run_stop(conn); + printf("stop %d\n", success); + } else { + success = mpd_run_play(conn); + printf("start %d\n", success); + } + if (success) { + playing = !playing; + } + break; case FASTFORWARD_PIN: - success = mpd_run_next(conn); - printf("ff %d\n", success); - break; + success = mpd_run_next(conn); + printf("ff %d\n", success); + break; case REWIND_PIN: - success = mpd_run_previous(conn); - printf("rw %d\n", success); - break; + success = mpd_run_previous(conn); + printf("rw %d\n", success); + break; case VOLUMEDOWN_PIN: - success = mpd_run_change_volume(conn, -10); - printf("vol- %d\n", success); - break; + success = mpd_run_change_volume(conn, -10); + printf("vol- %d\n", success); + break; case VOLUMEUP_PIN: - success = mpd_run_change_volume(conn, 10); - printf("vol+ %d\n", success); - break; + success = mpd_run_change_volume(conn, 10); + printf("vol+ %d\n", success); + break; default: - success = true; - break; + success = true; + break; } + + if (pin_pressed != 0) { + pthread_mutex_lock(&isr_mtx); + pin_pressed = 0; + pthread_mutex_unlock(&isr_mtx); + } + if (!success) { - switch(mpd_connection_get_error(conn)) { - case MPD_ERROR_SERVER: - case MPD_ERROR_ARGUMENT: - case MPD_ERROR_STATE: - case MPD_ERROR_MALFORMED: - mpd_connection_clear_error(conn); - printf("Clear error, don't try again.\n"); - success = true; - break; - default: - printf("Error code %d\n", mpd_connection_get_error(conn)); - mpd_connection_free(conn); - conn = mpd_connection_new("/run/mpd.socket", 0, 0); - if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { - mpd_connection_free(conn); - return EXIT_FAILURE; - } - printf("Reconnected, try again.\n"); - break; - } + switch(mpd_connection_get_error(conn)) { + case MPD_ERROR_SERVER: + case MPD_ERROR_ARGUMENT: + case MPD_ERROR_STATE: + case MPD_ERROR_MALFORMED: + mpd_connection_clear_error(conn); + printf("Clear error, don't try again.\n"); + success = true; + break; + default: + printf("Error code %d\n", mpd_connection_get_error(conn)); + mpd_connection_free(conn); + conn = mpd_connection_new("/run/mpd.socket", 0, 0); + if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { + mpd_connection_free(conn); + return EXIT_FAILURE; + } + printf("Reconnected, try again.\n"); + break; + } } } }