From: John Janus Date: Sat, 16 Sep 2017 21:08:13 +0000 (+0200) Subject: play music on button X-Git-Url: https://git.johnzone.org/?a=commitdiff_plain;h=35fafdf65da7418be31c4535eecd6fbee926515b;p=FakeRadio.git play music on button --- diff --git a/.gitignore b/.gitignore index 874c63c..7c1e38e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *.o - +*swp diff --git a/dansk01.mp3 b/dansk01.mp3 new file mode 100644 index 0000000..fa67535 Binary files /dev/null and b/dansk01.mp3 differ diff --git a/mp3player.c b/mp3player.c index 5c69abe..1533920 100644 --- a/mp3player.c +++ b/mp3player.c @@ -32,8 +32,8 @@ void* playFunc(void* file) buffer_size = mpg123_outblock(mh); buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char)); - if (mpg123_open(mh, file) != MPG123_OK) return; - if (mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) return; + if (mpg123_open(mh, file) != MPG123_OK) return NULL; + if (mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) return NULL; format.bits = mpg123_encsize(encoding) * BITS; format.rate = rate; @@ -56,11 +56,11 @@ void* playFunc(void* file) } -int main (int argc, char** argv) +/*int main (int argc, char** argv) { pthread_t thread = startPlayThread("platt01.mp3"); void* status; pthread_join(thread, status); //playFunc("platt01.mp3"); -} +}*/ diff --git a/simple_try.c b/simple_try.c index af8476c..70269ee 100644 --- a/simple_try.c +++ b/simple_try.c @@ -5,6 +5,7 @@ #include #include #include +#include "mp3player.h" #define PWRLED 8 #define CHNL1LED 9 @@ -18,8 +19,11 @@ static volatile bool running = true; static volatile bool pwrOn = false; static volatile bool chnl1 = true; //static volatile bool sthchanged = false; -pthread_cond_t* wakeup; -pthread_mutex_t* mut; +static pthread_cond_t* wakeup; +static pthread_mutex_t* mut; +static pthread_t playThread; + + void ctrlCHandler(int signum) { pthread_mutex_lock(mut); @@ -68,20 +72,28 @@ int main(void) { if (wiringPiISR(PWRBTN, INT_EDGE_RISING, pwrBtn)) printf("failed to initialize pwrBtn\n"); if (wiringPiISR(CHNL1BTN, INT_EDGE_RISING, chnl1Btn)) printf("failed to initialize chnl1Btn\n"); if (wiringPiISR(CHNL2BTN, INT_EDGE_RISING, chnl2Btn)) printf("failed to initialize chnl2Btn\n"); + printf("Set LED pins to output\n"); - pinMode(PWRLED, OUTPUT); pinMode(CHNL1LED, OUTPUT); pinMode(CHNL2LED, OUTPUT); + + printf("initialize mutex and condition variable"); mut = malloc (sizeof(pthread_mutex_t)); pthread_mutex_init(mut, NULL); wakeup = malloc(sizeof(pthread_cond_t)); pthread_cond_init(wakeup, NULL); + printf("Start Main Loop\n"); while (running) { pthread_mutex_lock(mut); pthread_cond_wait(wakeup, mut); + if (playThread) { + printf("playing"); + pthread_cancel(playThread); + } + if (!pwrOn) { digitalWrite(PWRLED, HIGH); digitalWrite(CHNL1LED, HIGH); @@ -91,9 +103,11 @@ int main(void) { if (chnl1) { digitalWrite(CHNL1LED, LOW); digitalWrite(CHNL2LED, HIGH); + playThread = startPlayThread("platt01.mp3"); } else { digitalWrite(CHNL2LED, LOW); digitalWrite(CHNL1LED, HIGH); + playThread = startPlayThread("dansk01.mp3"); } } pthread_mutex_unlock(mut);