comm with heart

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v3 by Pacemaker

Files at this revision

API Documentation at this revision

Comitter:
jfields
Date:
Wed Dec 03 02:32:50 2014 +0000
Parent:
1:9d463bc2b7b9
Commit message:
code for dual mbed mode

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Dec 02 22:20:46 2014 +0000
+++ b/main.cpp	Wed Dec 03 02:32:50 2014 +0000
@@ -6,12 +6,15 @@
 
 #define RUN 0x1
 
-// pins 5,6 for AGet
-// pins 7,8 for Vpace
-
 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
 Serial pc (USBTX, USBRX);
 
+// 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 = AS, 4 = VS
 
@@ -25,6 +28,12 @@
 int AG = 0;
 int VG = 0;
 
+// heart rate global vars
+int HR = 0;
+int beats = 0;
+int sampleRate = 10000; // default 10 seconds
+int firstSample = 1;
+
 // Normal Values
 const int N_PVARP = 325;    // ms
 const int N_VRP   = 300;    // ms
@@ -58,19 +67,25 @@
 void flashLED(int i);
 void event_out(char *s, int between_t);
 void blind();
-void rand_heart_func(void const *args);
+//void rand_heart_func(void const *args);
+void calcHR(void const *args);
+void disp(void const *args);
+void send_Apace();
+void send_Vpace();
 
 // threads
 Thread * VS_thread;
 Thread * AS_thread;
 Thread * PM_monitor_thread;
-Thread * rand_heart_thread; // just for testing until mbed connection is made
+Thread * disp_thread;
+//Thread * rand_heart_thread; // just for testing until mbed connection is made
 
 // rtos timers
 RtosTimer * VP_timer;
 RtosTimer * AP_timer;
 RtosTimer * VRP_timer;
 RtosTimer * PVARP_timer;
+RtosTimer * HR_timer;
 //RtosTimer * exit_timer; // for log file
 
 int main() {
@@ -81,18 +96,22 @@
     // init threads
     VS_thread = new Thread(VS_func);
     AS_thread = new Thread(AS_func);
+    disp_thread = new Thread(disp);
     PM_monitor_thread = new Thread(PM_monitor_func);
-    rand_heart_thread = new Thread(rand_heart_func);    // just for testing until mbed connection is made
+    //rand_heart_thread = new Thread(rand_heart_func);    // just for testing until mbed connection is made
     
     // init timers
     VP_timer = new RtosTimer(VP_func, osTimerOnce, (void *)0);
     AP_timer = new RtosTimer(AP_func, osTimerOnce, (void *)0);
+    HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0);
     VRP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)1);
     PVARP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)2);
     //exit_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)99);
     
-    // init global time, VP thread, exit thread
+    // init VP thread, HR timer, display timer
     VP_timer->start(AVI);
+    HR_timer->start(sampleRate);
+    disp_thread->signal_set(RUN);
     //exit_timer->start(60000);
     
     // main thread
@@ -101,6 +120,21 @@
     }
 }
 
+void calcHR(void const *args) {
+    
+    // calc
+    if (firstSample == 1) {
+        HR = beats*(60000/sampleRate);
+        firstSample = 0;
+    }
+    else {
+        HR = (beats*60000/sampleRate+HR)/2;
+    }
+    
+    // display
+    disp_thread->signal_set(RUN);
+}
+
 void manage_signals(void const *i) {
     if ((int)i==1) isVRP = 0;
     if ((int)i==2) isPVARP = 0;
@@ -117,6 +151,9 @@
     // start VP timer
     VP_timer->start(AVI);
     
+    // send Apace
+    send_Apace();
+    
     // update state
     waitingForV = 1;
     
@@ -133,10 +170,13 @@
     // start AP timer
     AP_timer->start(LRI-AVI);
     
+    // send Vpace
+    send_Vpace();
+    
     // update state
     waitingForV = 0;
     
-    // set VRP, PVARP
+    // set VRP, PVARP, update beats
     blind();
     
     // output
@@ -194,31 +234,36 @@
     }
 }
 
+/*
  void rand_heart_func(void const *args) {
-     int interval;
-     srand(time(NULL));
-     while (1) {
-         interval = rand()%5000+10;
-         //fprintf(fp,"interval = %d\n",interval);
-         Thread::wait(interval);
-         if (interval%2) AG = 1;
-         else
-             VG = 1;
-     }
+ int interval;
+ srand(time(NULL));
+ while (1) {
+ interval = rand()%5000+10;
+ //fprintf(fp,"interval = %d\n",interval);
+ Thread::wait(interval);
+ if (interval%2) AG = 1;
+ else
+ VG = 1;
  }
- 
+ }
+ */
 
 void PM_monitor_func(void const *args) {
     while (1) {
-        if (AG == 1) {
+        if (AGet == 1) {
             //fprintf(fp,"%f\tAget\t%d\n",global_t.read_ms(),isPVARP);
-            AG = 0;
+            //AG = 0;
             if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN);
+            wait(0.1);
+            while(AGet == 1);
         }
-        if (VG == 1) {
+        if (VGet == 1) {
             //fprintf(fp,"%f\tVget\t%d\n",global_t.read_ms(),isVRP);
-            VG = 0;
+            //VG = 0;
             if (!isVRP && waitingForV) VS_thread->signal_set(RUN);
+            wait(0.1);
+            while(VGet == 1);
         }
     }
 }
@@ -230,13 +275,34 @@
 }
 
 void event_out(char *s, int between_t) {
-    lcd.printf("%d\t%s\t%d\n",global_t.read_ms(),s,global_t.read_ms()-between_t);
+    //lcd.printf("%d\t%s\t%d\n",global_t.read_ms(),s,global_t.read_ms()-between_t);
     //fprintf(fp, "%f\t%s\t%f\n",global_t.read_ms(),s,global_t.read_ms()-between_t);
 }
 
 void blind() {
+    beats++;
     isVRP = 1;
     isPVARP = 1;
     VRP_timer->start(VRP);
     PVARP_timer->start(PVARP);
 }
+
+void disp(void const *args) {
+    while (1) {
+        Thread::signal_wait(RUN,osWaitForever);
+        lcd.printf("HR = %d ppm\nCyle = %d s\n",HR,sampleRate/1000);
+        beats = 0;
+    }
+}
+
+void send_Apace() {
+    APace = 1;
+    Thread::wait(50);
+    APace = 0;
+}
+
+void send_Vpace() {
+    VPace = 1;
+    Thread::wait(50);
+    VPace = 0;
+}