]> Johnzone git - FakeRadio.git/commitdiff
play music on button
authorJohn Janus <mail@johnzone.org>
Sat, 16 Sep 2017 21:08:13 +0000 (23:08 +0200)
committerJohn Janus <mail@johnzone.org>
Sat, 16 Sep 2017 21:08:13 +0000 (23:08 +0200)
.gitignore
dansk01.mp3 [new file with mode: 0644]
mp3player.c
simple_try.c

index 874c63cfa699b0cb28ada8b48e0f107ee5716f85..7c1e38ed6c3eeadfa87f6c4230b40e8ce64f70ff 100644 (file)
@@ -1,2 +1,2 @@
 *.o
-
+*swp
diff --git a/dansk01.mp3 b/dansk01.mp3
new file mode 100644 (file)
index 0000000..fa67535
Binary files /dev/null and b/dansk01.mp3 differ
index 5c69abe31b9992a47afb00e63c9a35beb7754cc1..15339209b0de850dde27ed7d18f684316d3b63bf 100644 (file)
@@ -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");
-}
+}*/
 
index af8476cc4a2bdf75d3c90416d6103a5d239140da..70269ee5c4e01f9c798adf69cfc8eb92f8351ac2 100644 (file)
@@ -5,6 +5,7 @@
 #include <signal.h>
 #include <unistd.h>
 #include <pthread.h>
+#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);