]> Johnzone git - FakeRadio.git/commitdiff
try cond variables
authorJohn Janus <mail@johnzone.org>
Mon, 28 Aug 2017 09:17:54 +0000 (11:17 +0200)
committerJohn Janus <mail@johnzone.org>
Mon, 28 Aug 2017 09:17:54 +0000 (11:17 +0200)
simple_try.c

index f53943e26be36bcf499331267eb07b3bb06d788a..1f05c6e9e8c7019f23c3b0f55f4898dcc635c220 100644 (file)
@@ -1,8 +1,10 @@
 #include <wiringPi.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <stdbool.h>
 #include <signal.h>
 #include <unistd.h>
+#include <pthread.h>
 
 #define PWRLED 8
 #define CHNL1LED 9
 static volatile bool running = true;
 static volatile bool pwrOn = false;
 static volatile bool chnl1 = true;
+pthread_cond_t* wakeup;
 
-void ctrlCHandler(int dummy) {
+void ctrlCHandler(int signum) {
     running = false;
+    pthread_cond_signal(wakeup);
     return;
 }
 
 void pwrBtn(void) {
     pwrOn = !pwrOn;
+    pthread_cond_signal(wakeup);
 }
 
 void chnl1Btn(void) {
     chnl1 = true;
+    pthread_cond_signal(wakeup);
 }
 
 void chnl2Btn(void) {
     chnl1 = false;
+    pthread_cond_signal(wakeup);
 }
 
 int main(void) {
@@ -55,13 +62,21 @@ int main(void) {
     pinMode(PWRLED, OUTPUT);
     pinMode(CHNL1LED, OUTPUT);
     pinMode(CHNL2LED, OUTPUT);
+    pthread_mutex_t* 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 (!pwrOn) {
             digitalWrite(PWRLED, HIGH);
             digitalWrite(CHNL1LED, HIGH);
             digitalWrite(CHNL2LED, HIGH);
-            sleep(0.3);
+            //sleep(0.3);
             continue;
         }
         digitalWrite(PWRLED, LOW);
@@ -72,8 +87,9 @@ int main(void) {
             digitalWrite(CHNL2LED, LOW);
             digitalWrite(CHNL1LED, HIGH);
         }
+        pthread_mutex_unlock(mut)
         //delay(200);
-        sleep(0.3);
+        //sleep(0.3);
 
     }
     printf("ShuttingDown\n");