Pacemaker / Mbed 2 deprecated pacemaker_v2

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v2 by Jonathan Fields

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