From 5758940063241da9d7d59a6c8aca079ba0e4fd91 Mon Sep 17 00:00:00 2001 From: John Janus Date: Mon, 28 Aug 2017 11:17:54 +0200 Subject: [PATCH] try cond variables --- simple_try.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/simple_try.c b/simple_try.c index f53943e..1f05c6e 100644 --- a/simple_try.c +++ b/simple_try.c @@ -1,8 +1,10 @@ #include #include +#include #include #include #include +#include #define PWRLED 8 #define CHNL1LED 9 @@ -15,22 +17,27 @@ 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"); -- 2.47.0