
comm with heart
Dependencies: TextLCD mbed-rtos mbed
Fork of pacemaker_v3 by
main.cpp
- Committer:
- jfields
- Date:
- 2014-12-03
- Revision:
- 2:1e42b5fdf9e9
- Parent:
- 1:9d463bc2b7b9
File content as of revision 2:1e42b5fdf9e9:
#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 = AS, 4 = VS // create log file and debugging vars //LocalFileSystem local("local"); //FILE *fp = fopen("/local/out.txt", "w"); int between_a = 0; int between_v = 0; // Heart Signals int AG = 0; int VG = 0; // 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 default_LRI = N_LRI; int AVI = N_AVI; int UB = N_UB; int LB = N_LB; // time vars Timer global_t; 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 PM_monitor_func(void const *args); void manage_signals(void const *i); void flashLED(int i); void event_out(char *s, int between_t); void blind(); //void rand_heart_func(void const *args); void calcHR(void const *args); void disp(void const *args); void send_Apace(); void send_Vpace(); // threads Thread * VS_thread; Thread * AS_thread; Thread * PM_monitor_thread; Thread * disp_thread; //Thread * rand_heart_thread; // just for testing until mbed connection is made // rtos timers RtosTimer * VP_timer; RtosTimer * AP_timer; RtosTimer * VRP_timer; RtosTimer * PVARP_timer; RtosTimer * HR_timer; //RtosTimer * exit_timer; // for log file int main() { // start global timer global_t.start(); // init threads VS_thread = new Thread(VS_func); AS_thread = new Thread(AS_func); disp_thread = new Thread(disp); PM_monitor_thread = new Thread(PM_monitor_func); //rand_heart_thread = new Thread(rand_heart_func); // just for testing until mbed connection is made // init timers VP_timer = new RtosTimer(VP_func, osTimerOnce, (void *)0); AP_timer = new RtosTimer(AP_func, osTimerOnce, (void *)0); HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0); VRP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)1); PVARP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)2); //exit_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)99); // init VP thread, HR timer, display timer VP_timer->start(AVI); HR_timer->start(sampleRate); disp_thread->signal_set(RUN); //exit_timer->start(60000); // main thread while (1) { } } void calcHR(void const *args) { // calc if (firstSample == 1) { HR = beats*(60000/sampleRate); firstSample = 0; } else { HR = (beats*60000/sampleRate+HR)/2; } // display disp_thread->signal_set(RUN); } void manage_signals(void const *i) { if ((int)i==1) isVRP = 0; if ((int)i==2) isPVARP = 0; // for debuggging //if ((int)i==99) { //fclose(fp); //exit(1); //} } void AP_func(void const *args) { // start VP timer VP_timer->start(AVI); // send Apace send_Apace(); // update state waitingForV = 1; // output event_out("AP",between_a); between_a = global_t.read_ms(); // flash LED flashLED(2); } void VP_func(void const *args) { // start AP timer AP_timer->start(LRI-AVI); // send Vpace send_Vpace(); // update state waitingForV = 0; // set VRP, PVARP, update beats blind(); // output event_out("VP", between_v); between_v = global_t.read_ms(); // flash LED flashLED(1); } void AS_func(void const *args) { while (1) { // wait for event Thread::signal_wait(RUN,osWaitForever); // update state waitingForV = 1; // stop AP timer and start VP timer AP_timer->stop(); VP_timer->start(AVI); // output event_out("AS", between_a); between_a = global_t.read_ms(); // flash LED flashLED(3); } } void VS_func(void const *args) { while (1) { // wait for event Thread::signal_wait(RUN,osWaitForever); // update state waitingForV = 0; // stop VP timer and start AP timer VP_timer->stop(); AP_timer->start(LRI-AVI); // set VRP, PVARP blind(); // output event_out("VS", between_v); between_v = global_t.read_ms(); // flash LED flashLED(4); } } /* void rand_heart_func(void const *args) { int interval; srand(time(NULL)); while (1) { interval = rand()%5000+10; //fprintf(fp,"interval = %d\n",interval); Thread::wait(interval); if (interval%2) AG = 1; else VG = 1; } } */ void PM_monitor_func(void const *args) { while (1) { if (AGet == 1) { //fprintf(fp,"%f\tAget\t%d\n",global_t.read_ms(),isPVARP); //AG = 0; if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN); wait(0.1); while(AGet == 1); } if (VGet == 1) { //fprintf(fp,"%f\tVget\t%d\n",global_t.read_ms(),isVRP); //VG = 0; if (!isVRP && waitingForV) VS_thread->signal_set(RUN); wait(0.1); while(VGet == 1); } } } void flashLED(int i) { leds[i-1] = 1; wait(0.01); leds[i-1] = 0; } void event_out(char *s, int between_t) { //lcd.printf("%d\t%s\t%d\n",global_t.read_ms(),s,global_t.read_ms()-between_t); //fprintf(fp, "%f\t%s\t%f\n",global_t.read_ms(),s,global_t.read_ms()-between_t); } void blind() { beats++; isVRP = 1; isPVARP = 1; VRP_timer->start(VRP); PVARP_timer->start(PVARP); } 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 send_Apace() { APace = 1; Thread::wait(50); APace = 0; } void send_Vpace() { VPace = 1; Thread::wait(50); VPace = 0; }