udpated

Dependencies:   TextLCD mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "TextLCD.h"
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 
00007 #define RUN 0x1
00008 
00009 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
00010 Serial pc (USBTX, USBRX);
00011 
00012 // ports
00013 DigitalIn VGet(p11);
00014 DigitalIn AGet(p12);
00015 DigitalOut VPace(p13);
00016 DigitalOut APace(p14);
00017 
00018 // LEDs
00019 DigitalOut leds[] = {LED1, LED2, LED3, LED4};
00020 // 1 = VP
00021 // 2 = AP
00022 // 3 = VS
00023 // 4 = AS
00024 
00025 // global clocks
00026 Timer ta;   // time since a event
00027 Timer tv;   // time since v event
00028 
00029 // heart rate global vars
00030 int HR = 0;
00031 int beats = 0;
00032 int sampleRate = 10000; // default 10 seconds
00033 int firstSample = 1;
00034 
00035 // Normal Values
00036 const int N_PVARP = 325;    // ms
00037 const int N_VRP   = 300;    // ms
00038 const int N_LRI   = 857;    // ms (= about 70ppm)
00039 const int N_AVI   = 65;     // ms
00040 const int N_UB    = 100;    // 100ppm
00041 const int N_LB    = 40;     // 40ppm
00042 
00043 // Heart Values - Normal Mode is default
00044 int PVARP = N_PVARP;
00045 int VRP = N_VRP;
00046 int LRI = N_LRI;
00047 int AVI = N_AVI;
00048 int UB = N_UB;
00049 int LB = N_LB;
00050 
00051 // status flags
00052 int isVRP = 0;
00053 int isPVARP = 0;
00054 int waitingForV = 1;
00055 
00056 // functions
00057 void VP_func(void const *args);
00058 void AP_func(void const *args);
00059 void VS_func(void const *args);
00060 void AS_func(void const *args);
00061 void manage_flags(void const *i);
00062 void calcHR(void const *args);
00063 void disp(void const *args);
00064 void send_Apace();
00065 void send_Vpace();
00066 void listen_Aget(void const *args);
00067 void listen_Vget(void const *args);
00068 void flashLED(int i);
00069 void blind();
00070 void Aevent();
00071 
00072 // threads
00073 Thread * VS_thread;
00074 Thread * AS_thread;
00075 Thread * VP_thread;
00076 Thread * AP_thread;
00077 Thread * VG_thread;
00078 Thread * AG_thread;
00079 Thread * disp_thread;
00080 
00081 // rtos timers
00082 RtosTimer * VRP_timer;
00083 RtosTimer * PVARP_timer;
00084 RtosTimer * HR_timer;
00085 
00086 int main() {
00087     
00088     // start global timer
00089     tv.start();
00090     ta.start();
00091     tv.stop();
00092     
00093     // init threads
00094     VS_thread = new Thread(VS_func);
00095     AS_thread = new Thread(AS_func);
00096     VP_thread = new Thread(VP_func);
00097     AP_thread = new Thread(AP_func);
00098     VG_thread = new Thread(listen_Vget);
00099     AG_thread = new Thread(listen_Aget);
00100     
00101     // init timers
00102     VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
00103     PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2);
00104     HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0);
00105     
00106     // start display and heart rate sample
00107     HR_timer->start(sampleRate);
00108     disp_thread->signal_set(RUN);
00109     
00110     // main thread
00111     while (1) {
00112         
00113     }
00114 }
00115 
00116 void calcHR(void const *args) {
00117     if (firstSample == 1) {
00118         HR = beats*(60000/sampleRate);
00119         firstSample = 0;
00120     }
00121     else {
00122         HR = (beats*60000/sampleRate+HR)/2;
00123     }
00124     disp_thread->signal_set(RUN);
00125 }
00126 
00127 void disp(void const *args) {
00128     while (1) {
00129         Thread::signal_wait(RUN,osWaitForever);
00130         lcd.printf("HR = %d ppm\nCyle = %d s\n",HR,sampleRate/1000);
00131         beats = 0;
00132     }
00133 }
00134 
00135 void manage_flags(void const *i) {
00136     if ((int)i==1) isVRP = 0;
00137     if ((int)i==2) isPVARP = 0;
00138 }
00139 
00140 void AP_func(void const *args) {
00141     while (1) {
00142         if (tv.read_ms() >= (LRI-AVI)) {
00143             Aevent();
00144             send_Apace();
00145             flashLED(2);
00146         }
00147     }
00148 }
00149 
00150 void VP_func(void const *args) {
00151     while (1) {
00152         if (ta.read_ms() >= AVI) {
00153             blind();
00154             send_Vpace();
00155             flashLED(1);
00156         }
00157     }
00158 }
00159 
00160 void AS_func(void const *args) {
00161     while (1) {
00162         Thread::signal_wait(RUN,osWaitForever);
00163         Aevent();
00164         flashLED(4);
00165     }
00166 }
00167 
00168 void VS_func(void const *args) {
00169     while (1) {
00170         Thread::signal_wait(RUN,osWaitForever);
00171         blind();
00172         flashLED(4);
00173     }
00174 }
00175 
00176 void listen_Vget(void const *args) {
00177     while (1) {
00178         if (VGet==1) {
00179             if (!isVRP && waitingForV) VS_thread->signal_set(RUN);
00180             while(VGet == 1);
00181         }
00182     }
00183 }
00184 
00185 void listen_Aget(void const *args) {
00186     while (1) {
00187         if (AGet == 1) {
00188             if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN);
00189             while(AGet == 1);
00190         }
00191     }
00192 }
00193 
00194 void flashLED(int i) {
00195     leds[i-1] = 1;
00196     wait(0.01);
00197     leds[i-1] = 0;
00198 }
00199 
00200 void blind() {
00201     tv.start();
00202     ta.reset();
00203     ta.stop();
00204     isVRP = 1;
00205     isPVARP = 1;
00206     VRP_timer->start(VRP);
00207     PVARP_timer->start(PVARP);
00208     beats++;
00209     waitingForV = 0;
00210 }
00211 
00212 void Aevent() {
00213     ta.start();
00214     tv.reset();
00215     tv.stop();
00216     waitingForV = 1;
00217 }
00218 
00219 void send_Apace() {
00220     APace = 1;
00221     Thread::wait(50);
00222     APace = 0;
00223 }
00224 
00225 void send_Vpace() {
00226     VPace = 1;
00227     Thread::wait(50);
00228     VPace = 0;
00229 }