local timers

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_FINAL_v2 by Pacemaker

Revision:
4:584d9e2e4fae
Parent:
3:c60266d9ce0b
Child:
5:8cd62e3c3c3a
--- a/main.cpp	Thu Dec 04 01:02:18 2014 +0000
+++ b/main.cpp	Thu Dec 04 03:03:56 2014 +0000
@@ -1,83 +1,15 @@
-#include "mbed.h"
-#include "rtos.h"
-#include "TextLCD.h"
-#include <stdio.h>
-#include <stdlib.h>
-
-#define RUN 0x1
-
-TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
-Serial pc (USBTX, USBRX);
+#include "constants.h"
 
-// ports
-DigitalIn VGet(p11);
-DigitalIn AGet(p12);
-DigitalOut VPace(p13);
-DigitalOut APace(p14);
-
-// LEDs
-DigitalOut leds[] = {LED1, LED2, LED3, LED4};
-// 1 = VP
-// 2 = AP
-// 3 = VS
-// 4 = AS
 
 // global clocks
-Timer ta;   // time since a event
-Timer tv;   // time since v event
+Timer t;   // global time
+int waitingForV = 1;
 
 // 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;
-int sampleRate = 10000; // default 10 seconds
-int firstSample = 1;
-int Omode = 0;
-
-// Normal Values
-const int N_PVARP = 325;    // ms
-const int N_VRP   = 300;    // ms
-const int N_LRI   = 857;    // ms (= about 70ppm)
-const int N_AVI   = 65;     // ms
-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;
-int LRI = N_LRI;
-int AVI = N_AVI;
-int UB = N_UB;
-int LB = N_LB;
-
-// status flags
-int isVRP = 0;
-int isPVARP = 0;
-int waitingForV = 1;
-int inManual = 0;
+Mutex man_mutex;        // protects manual
+Mutex state_mutex;      // protects waitingForV
 
 // functions
 void A_func(void const *args);
@@ -91,6 +23,7 @@
 void makeManual();
 void blind();
 void get_listener(void const *args);
+void updateSR();
 
 // threads
 Thread * A_thread;
@@ -107,9 +40,7 @@
 int main() {
     
     // start global timer
-    tv.start();
-    ta.start();
-    tv.stop();
+    t.start();
     
     // init threads
     disp_thread = new Thread(disp);
@@ -135,49 +66,60 @@
 
 void A_func(void const *args) {
     while (1) {
+        man_mutex.lock();
         if (!inManual) {
-            while (tv.read_ms() <= (LRI-AVI)) {
-                t_mutex.unlock();
-                if (AGet==1 && !isPVARP && !waitingForV) {
-                    tv.reset();
-                    tv.stop();
-                    ta.start();
+            
+            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);
-                    while (AGet == 1);
                 }
-                t_mutex.lock();
+
             }
-            APace = 1;
-            Thread::wait(2);
-            APace = 0;
-            tv.reset();
-            tv.stop();
-            ta.start();
-            waitingForV = 1;
-            flashLED(2);
+            state_mutex.unlock();
         }
+        man_mutex.unlock();
     }
 }
 
 void V_func(void const *args) {
     while (1) {
+        man_mutex.lock();
         if (!inManual) {
-            while (ta.read_ms() <= AVI) {
-                t_mutex.unlock();
-                if (VGet==1 && !isVRP && waitingForV) {
+            
+            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);
-                    while (VGet == 1);
                 }
-                t_mutex.lock();
+
+                
             }
-            VPace = 1;
-            Thread::wait(2);
-            VPace = 0;
-            blind();
-            flashLED(1);
+            state_mutex.unlock();
         }
+        man_mutex.unlock();
     }
 }
 
@@ -193,22 +135,33 @@
             lcd.printf("Enter\n\n");
             Omode = 1;
             input = pc.getc();
-            if (input == '1') sampleRate = 10000;
-            if (input == '2') sampleRate = 20000;
-            if (input == '3') sampleRate = 30000;
-            if (input == '4') sampleRate = 60000;
-            if (input == '5') sampleRate = 100000;
-            beats = 0;
-            HR = 0;
-            Omode = 0;
-            firstSample = 1;
-            disp_thread->signal_set(RUN);
+            if (input == '1') {
+                sampleRate = 10000;
+                updateSR();
+            }
+            if (input == '2') {
+                sampleRate = 20000;
+                updateSR();
+            }
+            if (input == '3') {
+                sampleRate = 30000;
+                updateSR();
+            }
+            if (input == '4') {
+                sampleRate = 60000;
+                updateSR();
+            }
+            if (input == '5') {
+                sampleRate = 100000;
+                updateSR();
+            }
         }
         input_mutex.unlock();
     }
 }
 
 void calcHR(void const *args) {
+    status_mutex.lock();
     if (firstSample == 1) {
         HR = beats*(60000/sampleRate);
         firstSample = 0;
@@ -216,22 +169,26 @@
     else {
         HR = (beats*60000/sampleRate+HR)/2;
     }
+    if (HR>=UB || HR<=LB) {
+        speaker.period(1.0/500.0); // 500hz period
+        speaker =0.5;
+    }
+    else {
+        speaker=0.0;
+    }
+    status_mutex.unlock();
     disp_thread->signal_set(RUN);
-    
-    if (HR>=UB || HR<=LB) {
-        
-    }
-    
-    
 }
 
 void disp(void const *args) {
     while (1) {
         Thread::signal_wait(RUN,osWaitForever);
+        status_mutex.lock();
         if (!Omode) {
             lcd.printf("HR = %d ppm\nCycle = %d s\n",HR,sampleRate/1000);
         }
         beats = 0;
+        status_mutex.unlock();
     }
 }
 
@@ -249,23 +206,20 @@
 }
 
 void blind() {
-    tv.start();
-    ta.reset();
-    ta.stop();
+    status_mutex.lock();
     isVRP = 1;
     isPVARP = 1;
     waitingForV = 0;
     VRP_timer->start(VRP);
     PVARP_timer->start(PVARP);
     beats++;
+    status_mutex.unlock();
 }
 
 void makeManual() {
+    man_mutex.lock();
     inManual = 1;
-    ta.reset();
-    ta.stop();
-    tv.reset();
-    tv.stop();
+    man_mutex.unlock();
     UB = 175;
     LB = 30;
     int done = 0;
@@ -299,24 +253,34 @@
             lcd.printf("Enter\n\n");
             Omode = 1;
             input = pc.getc();
-            if (input == '1') sampleRate = 10000;
-            if (input == '2') sampleRate = 20000;
-            if (input == '3') sampleRate = 30000;
-            if (input == '4') sampleRate = 60000;
-            if (input == '5') sampleRate = 100000;
-            beats = 0;
-            HR = 0;
-            Omode = 0;
-            firstSample = 1;
-            disp_thread->signal_set(RUN);
-
+            if (input == '1') {
+                sampleRate = 10000;
+                updateSR();
+            }
+            if (input == '2') {
+                sampleRate = 20000;
+                updateSR();
+            }
+            if (input == '3') {
+                sampleRate = 30000;
+                updateSR();
+            }
+            if (input == '4') {
+                sampleRate = 60000;
+                updateSR();
+            }
+            if (input == '5') {
+                sampleRate = 100000;
+                updateSR();
+            }
         }
     }
-    tv.start();
     waitingForV = 1;
     VRP_timer->stop();
     PVARP_timer->stop();
+    man_mutex.lock();
     inManual = 0;
+    man_mutex.unlock();
 }
 
 void get_listener(void const *args) {
@@ -361,9 +325,19 @@
     }
 }
 
+void updateSR() {
+    status_mutex.lock();
+    beats = 0;
+    HR = 0;
+    Omode = 0;
+    firstSample = 1;
+    HR_timer->stop();
+    HR_timer->start(sampleRate);
+    status_mutex.unlock();
+    disp_thread->signal_set(RUN);
+}
 
 
 
 
 
-