simpler func

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v7 by Pacemaker

Revision:
0:3afa00a23ce2
Child:
1:ea01c3232c4a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 03 21:20:04 2014 +0000
@@ -0,0 +1,229 @@
+#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);
+
+// 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
+
+// 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
+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
+
+// 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;
+
+// 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 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);
+void listen_Vget(void const *args);
+void flashLED(int i);
+void blind();
+void Aevent();
+
+// threads
+Thread * VS_thread;
+Thread * AS_thread;
+Thread * VP_thread;
+Thread * AP_thread;
+Thread * VG_thread;
+Thread * AG_thread;
+Thread * disp_thread;
+
+// rtos timers
+RtosTimer * VRP_timer;
+RtosTimer * PVARP_timer;
+RtosTimer * HR_timer;
+
+int main() {
+    
+    // start global timer
+    tv.start();
+    ta.start();
+    tv.stop();
+    
+    // init threads
+    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);
+    
+    // init timers
+    VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
+    PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2);
+    HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0);
+    
+    // start display and heart rate sample
+    HR_timer->start(sampleRate);
+    disp_thread->signal_set(RUN);
+    
+    // main thread
+    while (1) {
+        
+    }
+}
+
+void calcHR(void const *args) {
+    if (firstSample == 1) {
+        HR = beats*(60000/sampleRate);
+        firstSample = 0;
+    }
+    else {
+        HR = (beats*60000/sampleRate+HR)/2;
+    }
+    disp_thread->signal_set(RUN);
+}
+
+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 manage_flags(void const *i) {
+    if ((int)i==1) isVRP = 0;
+    if ((int)i==2) isPVARP = 0;
+}
+
+void AP_func(void const *args) {
+    while (1) {
+        if (tv.read_ms() >= (LRI-AVI)) {
+            Aevent();
+            send_Apace();
+            flashLED(2);
+        }
+    }
+}
+
+void VP_func(void const *args) {
+    while (1) {
+        if (ta.read_ms() >= AVI) {
+            blind();
+            send_Vpace();
+            flashLED(1);
+        }
+    }
+}
+
+void AS_func(void const *args) {
+    while (1) {
+        Thread::signal_wait(RUN,osWaitForever);
+        Aevent();
+        flashLED(4);
+    }
+}
+
+void VS_func(void const *args) {
+    while (1) {
+        Thread::signal_wait(RUN,osWaitForever);
+        blind();
+        flashLED(4);
+    }
+}
+
+void listen_Vget(void const *args) {
+    while (1) {
+        if (VGet==1) {
+            if (!isVRP && waitingForV) VS_thread->signal_set(RUN);
+            while(VGet == 1);
+        }
+    }
+}
+
+void listen_Aget(void const *args) {
+    while (1) {
+        if (AGet == 1) {
+            if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN);
+            while(AGet == 1);
+        }
+    }
+}
+
+void flashLED(int i) {
+    leds[i-1] = 1;
+    wait(0.01);
+    leds[i-1] = 0;
+}
+
+void blind() {
+    tv.start();
+    ta.reset();
+    ta.stop();
+    isVRP = 1;
+    isPVARP = 1;
+    VRP_timer->start(VRP);
+    PVARP_timer->start(PVARP);
+    beats++;
+    waitingForV = 0;
+}
+
+void Aevent() {
+    ta.start();
+    tv.reset();
+    tv.stop();
+    waitingForV = 1;
+}
+
+void send_Apace() {
+    APace = 1;
+    Thread::wait(50);
+    APace = 0;
+}
+
+void send_Vpace() {
+    VPace = 1;
+    Thread::wait(50);
+    VPace = 0;
+}