local timers

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_SINGLETIMER_v1 by Pacemaker

Revision:
5:8cd62e3c3c3a
Parent:
4:584d9e2e4fae
--- a/main.cpp	Thu Dec 04 03:03:56 2014 +0000
+++ b/main.cpp	Thu Dec 04 03:26:06 2014 +0000
@@ -1,11 +1,11 @@
 #include "constants.h"
 
-
-// global clocks
-Timer t;   // global time
-int waitingForV = 1;
+// global timer
+Timer tv;
+Timer ta;
 
 // mutexes
+Mutex t_mutex;          // protect reading of ta, tv
 Mutex status_mutex;     // protect reading of
 Mutex input_mutex;      // protects reading input
 Mutex man_mutex;        // protects manual
@@ -39,9 +39,6 @@
 
 int main() {
     
-    // start global timer
-    t.start();
-    
     // init threads
     disp_thread = new Thread(disp);
     input_thread = new Thread(input_func);
@@ -58,6 +55,11 @@
     HR_timer->start(sampleRate);
     disp_thread->signal_set(RUN);
     
+    // start pacemaker
+    ta.start();
+    tv.start();
+    V_thread->signal_set(RUN);
+    
     // main thread
     while (1) {
         
@@ -66,60 +68,49 @@
 
 void A_func(void const *args) {
     while (1) {
-        man_mutex.lock();
-        if (!inManual) {
-            
-            state_mutex.lock();
-            if (!waitingForV) {
-            
-                if (t.read_ms() >= LRI-AVI) {
-                    t.reset();
-                    waitingForV = 1;
-                    APace = 1;
-                    Thread::wait(2);
-                    APace = 0;
-                    flashLED(2);
-                }
-                if (AGet == 1 && !isPVARP) {
-                    t.reset();
-                    waitingForV = 1;
-                    flashLED(4);
-                }
-
+        Thread::signal_wait(RUN, osWaitForever);
+        int done = 0;
+        ta.reset();
+        while (!done) {
+            if (AGet == 1 && !isPVARP) {
+                V_thread->signal_set(RUN);
+                flashLED(4);
+                done = 1;
             }
-            state_mutex.unlock();
+            if (ta.read_ms() >= LRI-AVI) {
+                V_thread->signal_set(RUN);
+                APace = 1;
+                Thread::wait(2);
+                APace = 0;
+                flashLED(2);
+                done = 1;
+            }
         }
-        man_mutex.unlock();
     }
 }
 
 void V_func(void const *args) {
     while (1) {
-        man_mutex.lock();
-        if (!inManual) {
-            
-            state_mutex.lock();
-            if (waitingForV) {
-            
-                if (t.read_ms() >= AVI) {
-                    t.reset();
-                    VPace = 1;
-                    Thread::wait(2);
-                    VPace = 0;
-                    blind();
-                    flashLED(1);
-                }
-                if (VGet == 1 && !isVRP) {
-                    t.reset();
-                    blind();
-                    flashLED(3);
-                }
-
-                
+        Thread::signal_wait(RUN, osWaitForever);
+        int done = 0;
+        tv.reset();
+        while (!done) {
+            if (VGet == 1 && !isVRP) {
+                A_thread->signal_set(RUN);
+                blind();
+                flashLED(3);
+                done = 1;
             }
-            state_mutex.unlock();
+            if (tv.read_ms() >= AVI) {
+                A_thread->signal_set(RUN);
+                VPace = 1;
+                Thread::wait(2);
+                VPace = 0;
+                blind();
+                flashLED(1);
+                done = 1;
+            }
         }
-        man_mutex.unlock();
     }
 }
 
@@ -209,7 +200,6 @@
     status_mutex.lock();
     isVRP = 1;
     isPVARP = 1;
-    waitingForV = 0;
     VRP_timer->start(VRP);
     PVARP_timer->start(PVARP);
     beats++;
@@ -275,7 +265,6 @@
             }
         }
     }
-    waitingForV = 1;
     VRP_timer->stop();
     PVARP_timer->stop();
     man_mutex.lock();