udpated n,e,s modeswitch

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v6 by Pacemaker

Revision:
1:ea01c3232c4a
Parent:
0:3afa00a23ce2
Child:
2:3773afd2256c
diff -r 3afa00a23ce2 -r ea01c3232c4a main.cpp
--- a/main.cpp	Wed Dec 03 21:20:04 2014 +0000
+++ b/main.cpp	Wed Dec 03 22:38:41 2014 +0000
@@ -26,6 +26,14 @@
 Timer ta;   // time since a event
 Timer tv;   // time since v event
 
+// mutexes
+Mutex t_mutex;          // protect reading of ta, tv
+Mutex status_mutex;     // protect reading of
+Mutex input_mutex;      // protects reading input
+
+// input stuff
+char input;
+
 // heart rate global vars
 int HR = 0;
 int beats = 0;
@@ -40,6 +48,22 @@
 const int N_UB    = 100;    // 100ppm
 const int N_LB    = 40;     // 40ppm
 
+// Exercise Values
+const int E_PVARP = 175;    // ms
+const int E_VRP   = 150;    // ms
+const int E_LRI   = 428;    // ms (= about 140ppm)
+const int E_AVI   = 30;     // ms
+const int E_UB    = 175;    // 175ppm
+const int E_LB    = 100;    // 100ppm
+
+// Sleep Values
+const int S_PVARP = 500;    // ms
+const int S_VRP   = 475;    // ms
+const int S_LRI   = 1333;   // ms (= about 45ppm)
+const int S_AVI   = 100;    // ms
+const int S_UB    = 60;     // 60ppm
+const int S_LB    = 30;     // 30ppm
+
 // Heart Values - Normal Mode is default
 int PVARP = N_PVARP;
 int VRP = N_VRP;
@@ -59,8 +83,6 @@
 void VS_func(void const *args);
 void AS_func(void const *args);
 void manage_flags(void const *i);
-void calcHR(void const *args);
-void disp(void const *args);
 void send_Apace();
 void send_Vpace();
 void listen_Aget(void const *args);
@@ -68,6 +90,10 @@
 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);
 
 // threads
 Thread * VS_thread;
@@ -76,6 +102,7 @@
 Thread * AP_thread;
 Thread * VG_thread;
 Thread * AG_thread;
+Thread * input_thread;
 Thread * disp_thread;
 
 // rtos timers
@@ -91,12 +118,14 @@
     tv.stop();
     
     // 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);
     
     // init timers
     VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
@@ -113,6 +142,17 @@
     }
 }
 
+void input_func(void const *args) {
+    while (1) {
+        input_mutex.lock();
+        input=pc.getc();
+        if (input == 'n') setVals('n');
+        if (input == 's') setVals('s');
+        if (input == 'e') setVals('e');
+        input_mutex.unlock();
+    }
+}
+
 void calcHR(void const *args) {
     if (firstSample == 1) {
         HR = beats*(60000/sampleRate);
@@ -133,50 +173,62 @@
 }
 
 void manage_flags(void const *i) {
+    status_mutex.lock();
     if ((int)i==1) isVRP = 0;
     if ((int)i==2) isPVARP = 0;
+    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(4);
+        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);
         }
     }
@@ -185,7 +237,9 @@
 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);
         }
     }
@@ -201,29 +255,61 @@
     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++;
-    waitingForV = 0;
+    
 }
 
 void Aevent() {
     ta.start();
     tv.reset();
     tv.stop();
+    status_mutex.lock();
     waitingForV = 1;
+    status_mutex.unlock();
 }
 
 void send_Apace() {
     APace = 1;
-    Thread::wait(50);
+    Thread::wait(2);
     APace = 0;
 }
 
 void send_Vpace() {
     VPace = 1;
-    Thread::wait(50);
+    Thread::wait(2);
     VPace = 0;
 }
+
+void setVals(char c) {
+    if (c == 'n') {
+        PVARP = N_PVARP;
+        VRP = N_VRP;
+        LRI = N_LRI;
+        AVI = N_AVI;
+        UB = N_UB;
+        LB = N_LB;
+    }
+    if (c == 's') {
+        PVARP = S_PVARP;
+        VRP = S_VRP;
+        LRI = S_LRI;
+        AVI = S_AVI;
+        UB = S_UB;
+        LB = S_LB;
+    }
+    if (c == 'e') {
+        PVARP = E_PVARP;
+        VRP = E_VRP;
+        LRI = E_LRI;
+        AVI = E_AVI;
+        UB = E_UB;
+        LB = E_LB;
+    }
+}