Pacemaker / Mbed 2 deprecated pacemaker_v3

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v2 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 // pins 5,6 for AGet
00010 // pins 7,8 for Vpace
00011 
00012 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
00013 Serial pc (USBTX, USBRX);
00014 
00015 // LEDs
00016 DigitalOut leds[] = {LED1, LED2, LED3, LED4}; // 1 = VP, 2 = AP, 3 = AS, 4 = VS
00017 
00018 // create log file and debugging vars
00019 //LocalFileSystem local("local");
00020 //FILE *fp = fopen("/local/out.txt", "w");
00021 int between_a = 0;
00022 int between_v = 0;
00023 
00024 // Heart Signals
00025 int AG = 0;
00026 int VG = 0;
00027 
00028 // Normal Values
00029 const int N_PVARP = 325;    // ms
00030 const int N_VRP   = 300;    // ms
00031 const int N_LRI   = 857;    // ms (= about 70ppm)
00032 const int N_AVI   = 65;     // ms
00033 const int N_UB    = 100;    // 100ppm
00034 const int N_LB    = 40;     // 40ppm
00035 
00036 // Heart Values - Normal Mode is default
00037 int PVARP = N_PVARP;
00038 int VRP = N_VRP;
00039 int LRI = N_LRI;
00040 int default_LRI = N_LRI;
00041 int AVI = N_AVI;
00042 int UB = N_UB;
00043 int LB = N_LB;
00044 
00045 // time vars
00046 Timer global_t;
00047 int isVRP = 0;
00048 int isPVARP = 0;
00049 int waitingForV = 1;
00050 
00051 // functions
00052 void VP_func(void const *args);
00053 void AP_func(void const *args);
00054 void VS_func(void const *args);
00055 void AS_func(void const *args);
00056 void PM_monitor_func(void const *args);
00057 void manage_signals(void const *i);
00058 void flashLED(int i);
00059 void event_out(char *s, int between_t);
00060 void blind();
00061 void rand_heart_func(void const *args);
00062 
00063 // threads
00064 Thread * VS_thread;
00065 Thread * AS_thread;
00066 Thread * PM_monitor_thread;
00067 Thread * rand_heart_thread; // just for testing until mbed connection is made
00068 
00069 // rtos timers
00070 RtosTimer * VP_timer;
00071 RtosTimer * AP_timer;
00072 RtosTimer * VRP_timer;
00073 RtosTimer * PVARP_timer;
00074 //RtosTimer * exit_timer; // for log file
00075 
00076 int main() {
00077     
00078     // start global timer
00079     global_t.start();
00080     
00081     // init threads
00082     VS_thread = new Thread(VS_func);
00083     AS_thread = new Thread(AS_func);
00084     PM_monitor_thread = new Thread(PM_monitor_func);
00085     rand_heart_thread = new Thread(rand_heart_func);    // just for testing until mbed connection is made
00086     
00087     // init timers
00088     VP_timer = new RtosTimer(VP_func, osTimerOnce, (void *)0);
00089     AP_timer = new RtosTimer(AP_func, osTimerOnce, (void *)0);
00090     VRP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)1);
00091     PVARP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)2);
00092     //exit_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)99);
00093     
00094     // init global time, VP thread, exit thread
00095     VP_timer->start(AVI);
00096     //exit_timer->start(60000);
00097     
00098     // main thread
00099     while (1) {
00100         
00101     }
00102 }
00103 
00104 void manage_signals(void const *i) {
00105     if ((int)i==1) isVRP = 0;
00106     if ((int)i==2) isPVARP = 0;
00107     
00108     // for debuggging
00109     //if ((int)i==99) {
00110     //fclose(fp);
00111     //exit(1);
00112     //}
00113 }
00114 
00115 void AP_func(void const *args) {
00116     
00117     // start VP timer
00118     VP_timer->start(AVI);
00119     
00120     // update state
00121     waitingForV = 1;
00122     
00123     // output
00124     event_out("AP",between_a);
00125     between_a = global_t.read_ms();
00126     
00127     // flash LED
00128     flashLED(2);
00129 }
00130 
00131 void VP_func(void const *args) {
00132     
00133     // start AP timer
00134     AP_timer->start(LRI-AVI);
00135     
00136     // update state
00137     waitingForV = 0;
00138     
00139     // set VRP, PVARP
00140     blind();
00141     
00142     // output
00143     event_out("VP", between_v);
00144     between_v = global_t.read_ms();
00145     
00146     // flash LED
00147     flashLED(1);
00148 }
00149 
00150 void AS_func(void const *args) {
00151     while (1) {
00152         
00153         // wait for event
00154         Thread::signal_wait(RUN,osWaitForever);
00155         
00156         // update state
00157         waitingForV = 1;
00158         
00159         // stop AP timer and start VP timer
00160         AP_timer->stop();
00161         VP_timer->start(AVI);
00162         
00163         // output
00164         event_out("AS", between_a);
00165         between_a = global_t.read_ms();
00166         
00167         // flash LED
00168         flashLED(3);
00169     }
00170 }
00171 
00172 void VS_func(void const *args) {
00173     while (1) {
00174         
00175         // wait for event
00176         Thread::signal_wait(RUN,osWaitForever);
00177         
00178         // update state
00179         waitingForV = 0;
00180         
00181         // stop VP timer and start AP timer
00182         VP_timer->stop();
00183         AP_timer->start(LRI-AVI);
00184         
00185         // set VRP, PVARP
00186         blind();
00187         
00188         // output
00189         event_out("VS", between_v);
00190         between_v = global_t.read_ms();
00191         
00192         // flash LED
00193         flashLED(4);
00194     }
00195 }
00196 
00197  void rand_heart_func(void const *args) {
00198      int interval;
00199      srand(time(NULL));
00200      while (1) {
00201          interval = rand()%5000+10;
00202          //fprintf(fp,"interval = %d\n",interval);
00203          Thread::wait(interval);
00204          if (interval%2) AG = 1;
00205          else
00206              VG = 1;
00207      }
00208  }
00209  
00210 
00211 void PM_monitor_func(void const *args) {
00212     while (1) {
00213         if (AG == 1) {
00214             //fprintf(fp,"%f\tAget\t%d\n",global_t.read_ms(),isPVARP);
00215             AG = 0;
00216             if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN);
00217         }
00218         if (VG == 1) {
00219             //fprintf(fp,"%f\tVget\t%d\n",global_t.read_ms(),isVRP);
00220             VG = 0;
00221             if (!isVRP && waitingForV) VS_thread->signal_set(RUN);
00222         }
00223     }
00224 }
00225 
00226 void flashLED(int i) {
00227     leds[i-1] = 1;
00228     wait(0.01);
00229     leds[i-1] = 0;
00230 }
00231 
00232 void event_out(char *s, int between_t) {
00233     lcd.printf("%d\t%s\t%d\n",global_t.read_ms(),s,global_t.read_ms()-between_t);
00234     //fprintf(fp, "%f\t%s\t%f\n",global_t.read_ms(),s,global_t.read_ms()-between_t);
00235 }
00236 
00237 void blind() {
00238     isVRP = 1;
00239     isPVARP = 1;
00240     VRP_timer->start(VRP);
00241     PVARP_timer->start(PVARP);
00242 }