Pacemaker / Mbed 2 deprecated pacemaker_v6

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v5 by Pacemaker

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 // mutexes
00030 Mutex t_mutex;          // protect reading of ta, tv
00031 Mutex status_mutex;     // protect reading of
00032 Mutex input_mutex;      // protects reading input
00033 
00034 // input stuff
00035 char input;
00036 
00037 // heart rate global vars
00038 int HR = 0;
00039 int beats = 0;
00040 int sampleRate = 10000; // default 10 seconds
00041 int firstSample = 1;
00042 
00043 // Normal Values
00044 const int N_PVARP = 325;    // ms
00045 const int N_VRP   = 300;    // ms
00046 const int N_LRI   = 857;    // ms (= about 70ppm)
00047 const int N_AVI   = 65;     // ms
00048 const int N_UB    = 100;    // 100ppm
00049 const int N_LB    = 40;     // 40ppm
00050 
00051 // Exercise Values
00052 const int E_PVARP = 175;    // ms
00053 const int E_VRP   = 150;    // ms
00054 const int E_LRI   = 428;    // ms (= about 140ppm)
00055 const int E_AVI   = 30;     // ms
00056 const int E_UB    = 175;    // 175ppm
00057 const int E_LB    = 100;    // 100ppm
00058 
00059 // Sleep Values
00060 const int S_PVARP = 500;    // ms
00061 const int S_VRP   = 475;    // ms
00062 const int S_LRI   = 1333;   // ms (= about 45ppm)
00063 const int S_AVI   = 100;    // ms
00064 const int S_UB    = 60;     // 60ppm
00065 const int S_LB    = 30;     // 30ppm
00066 
00067 // Heart Values - Normal Mode is default
00068 int PVARP = N_PVARP;
00069 int VRP = N_VRP;
00070 int LRI = N_LRI;
00071 int AVI = N_AVI;
00072 int UB = N_UB;
00073 int LB = N_LB;
00074 
00075 // status flags
00076 int isVRP = 0;
00077 int isPVARP = 0;
00078 int waitingForV = 1;
00079 
00080 // functions
00081 void VP_func(void const *args);
00082 void AP_func(void const *args);
00083 //void VS_func(void const *args);
00084 //void AS_func(void const *args);
00085 void manage_flags(void const *i);
00086 void send_Apace();
00087 void send_Vpace();
00088 void listen_Aget(void const *args);
00089 void listen_Vget(void const *args);
00090 void flashLED(int i);
00091 void blind();
00092 void Aevent();
00093 void calcHR(void const *args);
00094 void disp(void const *args);
00095 void input_func(void const *args);
00096 void setVals(char c);
00097 
00098 // threads
00099 Thread * VS_thread;
00100 Thread * AS_thread;
00101 Thread * VP_thread;
00102 Thread * AP_thread;
00103 Thread * VG_thread;
00104 Thread * AG_thread;
00105 Thread * input_thread;
00106 Thread * disp_thread;
00107 
00108 // rtos timers
00109 RtosTimer * VRP_timer;
00110 RtosTimer * PVARP_timer;
00111 RtosTimer * HR_timer;
00112 
00113 int main() {
00114     
00115     // start global timer
00116     tv.start();
00117     ta.start();
00118     tv.stop();
00119     
00120     // init threads
00121     disp_thread = new Thread(disp);
00122 //    VS_thread = new Thread(VS_func);
00123 //    AS_thread = new Thread(AS_func);
00124     VP_thread = new Thread(VP_func);
00125     AP_thread = new Thread(AP_func);
00126     VG_thread = new Thread(listen_Vget);
00127     AG_thread = new Thread(listen_Aget);
00128     input_thread = new Thread(input_func);
00129     
00130     // init timers
00131     VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
00132     PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2);
00133     HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0);
00134     
00135     // start display and heart rate sample
00136     HR_timer->start(sampleRate);
00137     disp_thread->signal_set(RUN);
00138     
00139     // main thread
00140     while (1) {
00141         
00142     }
00143 }
00144 
00145 void input_func(void const *args) {
00146     while (1) {
00147         input_mutex.lock();
00148         input=pc.getc();
00149         if (input == 'n') setVals('n');
00150         if (input == 's') setVals('s');
00151         if (input == 'e') setVals('e');
00152         input_mutex.unlock();
00153     }
00154 }
00155 
00156 void calcHR(void const *args) {
00157     if (firstSample == 1) {
00158         HR = beats*(60000/sampleRate);
00159         firstSample = 0;
00160     }
00161     else {
00162         HR = (beats*60000/sampleRate+HR)/2;
00163     }
00164     disp_thread->signal_set(RUN);
00165 }
00166 
00167 void disp(void const *args) {
00168     while (1) {
00169         Thread::signal_wait(RUN,osWaitForever);
00170         lcd.printf("HR = %d ppm\nCyle = %d s\n",HR,sampleRate/1000);
00171         beats = 0;
00172     }
00173 }
00174 
00175 void manage_flags(void const *i) {
00176     status_mutex.lock();
00177     if ((int)i==1) isVRP = 0;
00178     if ((int)i==2) isPVARP = 0;
00179     status_mutex.unlock();
00180 }
00181 
00182 void AP_func(void const *args) {
00183     while (1) {
00184         t_mutex.lock();
00185         if (tv.read_ms() >= (LRI-AVI)) {
00186             Aevent();
00187             send_Apace();
00188             flashLED(2);
00189         }
00190         t_mutex.unlock();
00191     }
00192 }
00193 
00194 void VP_func(void const *args) {
00195     while (1) {
00196         t_mutex.lock();
00197         if (ta.read_ms() >= AVI) {
00198             blind();
00199             send_Vpace();
00200             flashLED(1);
00201         }
00202         t_mutex.unlock();
00203     }
00204 }
00205 /*
00206 void AS_func(void const *args) {
00207     while (1) {
00208         Thread::signal_wait(RUN,osWaitForever);
00209         t_mutex.lock();
00210         Aevent();
00211         flashLED(4);
00212         t_mutex.unlock();
00213     }
00214 }*/
00215 /*
00216 void VS_func(void const *args) {
00217     while (1) {
00218         Thread::signal_wait(RUN,osWaitForever);
00219         t_mutex.lock();
00220         blind();
00221         flashLED(3);
00222         t_mutex.unlock();
00223     }
00224 }
00225 */
00226 void listen_Vget(void const *args) {
00227     while (1) {
00228         if (VGet==1) {
00229             
00230             if (!isVRP && waitingForV){
00231                 t_mutex.lock();
00232                 tv.start();
00233                 ta.reset();
00234                 ta.stop();
00235                 t_mutex.unlock();
00236                 status_mutex.lock();
00237                 isVRP = 1;
00238                 isPVARP = 1;
00239                 waitingForV = 0;
00240                 status_mutex.unlock();
00241                 VRP_timer->start(VRP);
00242                 PVARP_timer->start(PVARP);
00243                 beats++;
00244                 
00245                 flashLED(3);
00246             }
00247             while(VGet == 1);
00248         }
00249     }
00250 }
00251 
00252 void listen_Aget(void const *args) {
00253     while (1) {
00254         if (AGet == 1) {
00255             if (!isPVARP && !waitingForV) {
00256                 t_mutex.lock();
00257                 ta.start();
00258                 tv.reset();
00259                 tv.stop();
00260                 t_mutex.unlock();
00261                 status_mutex.lock();
00262                 waitingForV = 1;
00263                 status_mutex.unlock();
00264                 flashLED(4);
00265             }
00266             while(AGet == 1);
00267         }
00268     }
00269 }
00270 
00271 void flashLED(int i) {
00272     leds[i-1] = 1;
00273     wait(0.01);
00274     leds[i-1] = 0;
00275 }
00276 
00277 void blind() {
00278     tv.start();
00279     ta.reset();
00280     ta.stop();
00281     status_mutex.lock();
00282     isVRP = 1;
00283     isPVARP = 1;
00284     waitingForV = 0;
00285     status_mutex.unlock();
00286     VRP_timer->start(VRP);
00287     PVARP_timer->start(PVARP);
00288     beats++;
00289     
00290 }
00291 
00292 void Aevent() {
00293     ta.start();
00294     tv.reset();
00295     tv.stop();
00296     status_mutex.lock();
00297     waitingForV = 1;
00298     status_mutex.unlock();
00299 }
00300 
00301 void send_Apace() {
00302     APace = 1;
00303     Thread::wait(2);
00304     APace = 0;
00305 }
00306 
00307 void send_Vpace() {
00308     VPace = 1;
00309     Thread::wait(2);
00310     VPace = 0;
00311 }
00312 
00313 void setVals(char c) {
00314     if (c == 'n') {
00315         PVARP = N_PVARP;
00316         VRP = N_VRP;
00317         LRI = N_LRI;
00318         AVI = N_AVI;
00319         UB = N_UB;
00320         LB = N_LB;
00321     }
00322     if (c == 's') {
00323         PVARP = S_PVARP;
00324         VRP = S_VRP;
00325         LRI = S_LRI;
00326         AVI = S_AVI;
00327         UB = S_UB;
00328         LB = S_LB;
00329     }
00330     if (c == 'e') {
00331         PVARP = E_PVARP;
00332         VRP = E_VRP;
00333         LRI = E_LRI;
00334         AVI = E_AVI;
00335         UB = E_UB;
00336         LB = E_LB;
00337     }
00338 }