local timers

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_FINAL_v2 by Pacemaker

Revision:
2:3773afd2256c
Parent:
1:ea01c3232c4a
Child:
3:c60266d9ce0b
--- a/main.cpp	Wed Dec 03 22:38:41 2014 +0000
+++ b/main.cpp	Wed Dec 03 23:57:02 2014 +0000
@@ -30,6 +30,7 @@
 Mutex t_mutex;          // protect reading of ta, tv
 Mutex status_mutex;     // protect reading of
 Mutex input_mutex;      // protects reading input
+Mutex VP_helper;
 
 // input stuff
 char input;
@@ -78,32 +79,24 @@
 int waitingForV = 1;
 
 // functions
-void VP_func(void const *args);
-void AP_func(void const *args);
-void VS_func(void const *args);
-void AS_func(void const *args);
+void A_func(void const *args);
+void V_func(void const *args);
 void manage_flags(void const *i);
-void send_Apace();
-void send_Vpace();
-void listen_Aget(void const *args);
-void listen_Vget(void const *args);
 void flashLED(int i);
-void blind();
-void Aevent();
 void calcHR(void const *args);
 void disp(void const *args);
 void input_func(void const *args);
 void setVals(char c);
+void makeManual();
+void blind();
 
 // threads
-Thread * VS_thread;
-Thread * AS_thread;
-Thread * VP_thread;
-Thread * AP_thread;
-Thread * VG_thread;
-Thread * AG_thread;
+Thread * A_thread;
+Thread * V_thread;
 Thread * input_thread;
 Thread * disp_thread;
+Thread * send_Vpace_thread;
+Thread * send_Apace_thread;
 
 // rtos timers
 RtosTimer * VRP_timer;
@@ -119,13 +112,9 @@
     
     // init threads
     disp_thread = new Thread(disp);
-    VS_thread = new Thread(VS_func);
-    AS_thread = new Thread(AS_func);
-    VP_thread = new Thread(VP_func);
-    AP_thread = new Thread(AP_func);
-    VG_thread = new Thread(listen_Vget);
-    AG_thread = new Thread(listen_Aget);
     input_thread = new Thread(input_func);
+    A_thread = new Thread(A_func);
+    V_thread = new Thread(V_func);
     
     // init timers
     VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
@@ -142,6 +131,46 @@
     }
 }
 
+void A_func(void const *args) {
+    while (1) {
+        while (tv.read_ms() <= (LRI-AVI)) {
+            if (AGet==1 && !isPVARP && !waitingForV) {
+                tv.reset();
+                tv.stop();
+                ta.start();
+                waitingForV = 1;
+                flashLED(4);
+                while (AGet == 1);
+            }
+        }
+        APace = 1;
+        Thread::wait(2);
+        APace = 0;
+        tv.reset();
+        tv.stop();
+        ta.start();
+        waitingForV = 1;
+        flashLED(2);
+    }
+}
+
+void V_func(void const *args) {
+    while (1) {
+        while (ta.read_ms() <= AVI) {
+            if (VGet==1 && !isVRP && waitingForV) {
+                blind();
+                flashLED(3);
+                while (VGet == 1);
+            }
+        }
+        VPace = 1;
+        Thread::wait(2);
+        VPace = 0;
+        blind();
+        flashLED(1);
+    }
+}
+
 void input_func(void const *args) {
     while (1) {
         input_mutex.lock();
@@ -149,6 +178,7 @@
         if (input == 'n') setVals('n');
         if (input == 's') setVals('s');
         if (input == 'e') setVals('e');
+        if (input == 'm') makeManual();
         input_mutex.unlock();
     }
 }
@@ -179,72 +209,6 @@
     status_mutex.unlock();
 }
 
-void AP_func(void const *args) {
-    while (1) {
-        t_mutex.lock();
-        if (tv.read_ms() >= (LRI-AVI)) {
-            Aevent();
-            send_Apace();
-            flashLED(2);
-        }
-        t_mutex.unlock();
-    }
-}
-
-void VP_func(void const *args) {
-    while (1) {
-        t_mutex.lock();
-        if (ta.read_ms() >= AVI) {
-            blind();
-            send_Vpace();
-            flashLED(1);
-        }
-        t_mutex.unlock();
-    }
-}
-
-void AS_func(void const *args) {
-    while (1) {
-        Thread::signal_wait(RUN,osWaitForever);
-        t_mutex.lock();
-        Aevent();
-        flashLED(4);
-        t_mutex.unlock();
-    }
-}
-
-void VS_func(void const *args) {
-    while (1) {
-        Thread::signal_wait(RUN,osWaitForever);
-        t_mutex.lock();
-        blind();
-        flashLED(3);
-        t_mutex.unlock();
-    }
-}
-
-void listen_Vget(void const *args) {
-    while (1) {
-        if (VGet==1) {
-            status_mutex.lock();
-            if (!isVRP && waitingForV) VS_thread->signal_set(RUN);
-            status_mutex.unlock();
-            while(VGet == 1);
-        }
-    }
-}
-
-void listen_Aget(void const *args) {
-    while (1) {
-        if (AGet == 1) {
-            status_mutex.lock();
-            if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN);
-            status_mutex.unlock();
-            while(AGet == 1);
-        }
-    }
-}
-
 void flashLED(int i) {
     leds[i-1] = 1;
     wait(0.01);
@@ -255,36 +219,38 @@
     tv.start();
     ta.reset();
     ta.stop();
-    status_mutex.lock();
     isVRP = 1;
     isPVARP = 1;
     waitingForV = 0;
-    status_mutex.unlock();
     VRP_timer->start(VRP);
     PVARP_timer->start(PVARP);
     beats++;
-    
 }
 
-void Aevent() {
-    ta.start();
+void makeManual() {
+    t_mutex.lock();
+    ta.reset();
     tv.reset();
+    ta.stop();
     tv.stop();
-    status_mutex.lock();
-    waitingForV = 1;
-    status_mutex.unlock();
-}
-
-void send_Apace() {
-    APace = 1;
-    Thread::wait(2);
-    APace = 0;
-}
-
-void send_Vpace() {
-    VPace = 1;
-    Thread::wait(2);
-    VPace = 0;
+    t_mutex.unlock();
+    while (1) {
+        input = pc.getc();
+        if (input == 'v') {
+            send_Vpace_thread->signal_set(RUN);
+            flashLED(1);
+        }
+        if (input == 'a') {
+            send_Apace_thread->signal_set(RUN);
+            flashLED(2);
+        }
+        t_mutex.lock();
+        ta.reset();
+        tv.reset();
+        ta.stop();
+        tv.stop();
+        t_mutex.unlock();
+    }
 }
 
 void setVals(char c) {
@@ -313,3 +279,10 @@
         LB = E_LB;
     }
 }
+
+
+
+
+
+
+