latest pacemaker
Dependencies: TextLCD mbed-rtos mbed
Diff: main.cpp
- Revision:
- 1:e6f6471e2c00
- Parent:
- 0:dac187b013e0
- Child:
- 2:682a3ac9d7a3
--- a/main.cpp Fri Dec 02 23:54:09 2016 +0000 +++ b/main.cpp Mon Dec 05 03:14:51 2016 +0000 @@ -19,6 +19,7 @@ osThreadId senseTid; osThreadId displayTid; osThreadId pacemodeTid; +osThreadId alarmTid; TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); RawSerial pc(USBTX, USBRX); @@ -44,44 +45,54 @@ int lowerBound; //for mode changes int heart_beats = 0; // Heart-Beats (sensed or paced) since the last observation interval char mode = 'n'; -int isChangingObsInt = 0; char key = 'n'; char newObsInt[8]; int manual_mode = 0; +Mutex hr_mutex; //hr_mutex.lock()/unlock() Queue<char,256> mode_q; Queue<char,256> signal_q; +Queue<char,256> obsint_q; + volatile char c; volatile int mm = 0; +volatile int om = 0; int mm_flag = 0; void initialize_intervals() { LRI = 1000; URI = 700; - VRP = 200; - ARP = 50; - AVI = 150; - PVARP = 300; +// VRP = 200; +// ARP = 50; +// AVI = 150; +// PVARP = 300; } void Rx_interrupt() { while(pc.readable()) { c = pc.getc(); - if(c == 'm') { + if(c == 'm' && om != 1) { mode_q.put((char*)c); mm = 1; - } else if(c == 'n' || c == 'e' || c == 's') { + } else if(c == 'n' || c == 'e' || c == 's' && om != 1) { mode_q.put((char*)c); mm = 0; } else if((c == 'a' || c == 'v') && mm) { signal_q.put((char*)c); + } else if(c == 'o' && om != 1) { + mode_q.put((char*)c); + om = 1; + } else if (c == '\r' && om) { + obsint_q.put((char*)c); + om = 0; + } else if ((int)c > 47 && (int)c < 58 && om) { + obsint_q.put((char*)c); } } } - // Function to toggle the LEDs 1,2,3,4 void toggleLed(int led) { @@ -109,50 +120,48 @@ } } +void alarmThread(void const *args) +{ + while (1) + { + osEvent ext_signal = osSignalWait(0, osWaitForever); + int evt = ext_signal.value.signals; + + if (evt == 0xb){ + lcd.printf("%s", "\nALARM HIGH"); + } + else if (evt == 0xc) { + lcd.printf("%s", "\nALARM LOW"); + } + } +} + void displayThread(void const *args) { while (1) { Thread::wait(observation_interval); lcd.cls(); + + hr_mutex.lock(); int hr = (heart_beats*60) / (observation_interval / 1000); + heart_beats = 0; + hr_mutex.unlock(); + lcd.printf("%s%d%s","HR: ", hr, " bpm"); - - switch(mode) { - case('n'): - if (hr > 100) { - lcd.printf("%s", "\nALARM HIGH"); - } else if (hr < 40) { - lcd.printf("%s", "\nALARM LOW"); - } - break; - case('e'): - if (hr > 175) { - lcd.printf("%s", "\nALARM HIGH"); - } else if (hr < 100) { - lcd.printf("%s", "\nALARM LOW"); - } - break; - case('s'): - if (hr > 60) { - lcd.printf("%s", "\nALARM HIGH"); - } else if (hr < 30) { - lcd.printf("%s", "\nALARM LOW"); - } - break; - case('m'): - if (hr > 175) { - lcd.printf("%s", "\nALARM HIGH"); - } else if (hr < 30) { - lcd.printf("%s", "\nALARM LOW"); - } - break; + + if (hr > upperBound) + { + osSignalSet(alarmTid, 0xb); } - - heart_beats = 0; + else if (hr < lowerBound) + { + osSignalSet(alarmTid, 0xc); + } } } + // Incoming signal from the heart void asignal_irq() { @@ -169,27 +178,31 @@ // Timer-driven function to pace the Atrial void a_pace(void const*) { + Apace = 1; aClock.reset(); - Apace = 1; + apace_timer->stop(); toggleLed(3); Apace = 0; osSignalSet(signalTid, 0x3); - //osSignalSet(senseTid, 0x3); } // Timer-driven function to pace the ventrical void v_pace(void const*) { + Vpace = 1; + + vClock.reset(); - Vpace = 1; + vpace_timer->start(LRI); + apace_timer->start(LRI-AVI); + toggleLed(4); Vpace = 0; - vpace_timer->start(LRI); - apace_timer->start(LRI-AVI); -// vpace_timer2->start(AVI); osSignalSet(signalTid, 0x4); - //osSignalSet(senseTid, 0x4); + + hr_mutex.lock(); heart_beats++; + hr_mutex.unlock(); } @@ -200,7 +213,7 @@ int pFlag2 = 0; vClock.start(); aClock.start(); - if(!mm_flag){ + if(!mm_flag) { vpace_timer->start(LRI); apace_timer->start(LRI-AVI); } @@ -215,30 +228,28 @@ if (evt == 0x1 && vClock.read_ms() >= PVARP) { //aSignal osSignalSet(senseTid, 0x1); aClock.reset(); - if(!mm_flag){ - apace_timer->start(LRI-AVI); + if(!mm_flag) { + apace_timer->stop(); + int interval = (vClock.read_ms() + AVI >= URI) ? AVI : URI - vClock.read_ms(); + apace_timer->start(interval); } pFlag1 = 1; } else if(evt == 0x2 && vClock.read_ms() >= VRP) { //vSignal osSignalSet(senseTid, 0x2); vClock.reset(); - if(!mm_flag){ + if(!mm_flag) { vpace_timer->start(LRI); apace_timer->start(LRI-AVI); } -// vpace_timer2->start(AVI); + } else if (evt == 0x3) { //aPace - aClock.reset(); - if(!mm_flag){ - apace_timer->start(LRI-AVI); - } pFlag1 = 1; } } pFlag1 = 0; while(!pFlag2) { - + osEvent ext_signal = osSignalWait(0, osWaitForever); int evt = ext_signal.value.signals; @@ -247,26 +258,15 @@ if (evt == 0x1 && aClock.read_ms() >= ARP) { //aSignal osSignalSet(senseTid, 0x1); aClock.reset(); - if(!mm_flag){ - apace_timer->start(LRI-AVI); - } - Thread::wait(wait_period); } else if(evt == 0x2) { //vSignal osSignalSet(senseTid, 0x2); vClock.reset(); - if(!mm_flag){ + if(!mm_flag) { vpace_timer->start(LRI); apace_timer->start(LRI-AVI); } -// vpace_timer2->start(AVI); pFlag2 = 1; } else if (evt == 0x4) { //vPace - vClock.reset(); - if(!mm_flag){ - vpace_timer->start(LRI); - apace_timer->start(LRI-AVI); - } -// vpace_timer2->start(AVI); pFlag2 = 1; } } @@ -291,7 +291,9 @@ pFlag1 = 1; } else if(evt == 0x2) { //vSense toggleLed(evt); + hr_mutex.lock(); heart_beats++; + hr_mutex.unlock(); } else if (evt == 0x3) { //aPace pFlag1 = 1; } @@ -308,7 +310,9 @@ toggleLed(evt); } else if(evt == 0x2) { //vSignal toggleLed(evt); + hr_mutex.lock(); heart_beats++; + hr_mutex.unlock(); pFlag2 = 1; } else if (evt == 0x4) { //vPace pFlag2 = 1; @@ -325,6 +329,10 @@ lcd.printf("N"); upperBound = 100; //beats per msecond lowerBound = 40; //beats per msecond + //reset obs interval + hr_mutex.lock(); + heart_beats = 0; + hr_mutex.unlock(); vpace_timer->start(LRI); apace_timer->start(LRI-AVI); @@ -340,11 +348,11 @@ ratio = (175.00/100.00 + 100.00/40.00) / 2.00; LRI /= ratio; URI /= ratio; - VRP /= ratio; - ARP /= ratio; - AVI /= ratio; - PVARP /= ratio; - + //reset obs interval + hr_mutex.lock(); + heart_beats = 0; + hr_mutex.unlock(); + vpace_timer->start(LRI); apace_timer->start(LRI-AVI); } @@ -359,26 +367,29 @@ ratio = (60.00/100.00 + 30.00/40.00) / 2.00; LRI /= ratio; URI /= ratio; - VRP /= ratio; - ARP /= ratio; - AVI /= ratio; - PVARP /= ratio; - + //reset obs interval + hr_mutex.lock(); + heart_beats = 0; + hr_mutex.unlock(); + vpace_timer->start(LRI); apace_timer->start(LRI-AVI); - } -void m_vpace(){ +void m_vpace() +{ vClock.reset(); Vpace = 1; toggleLed(4); Vpace = 0; osSignalSet(signalTid, 0x4); + hr_mutex.lock(); heart_beats++; + hr_mutex.unlock(); } -void m_apace(){ +void m_apace() +{ aClock.reset(); Apace = 1; toggleLed(3); @@ -392,10 +403,9 @@ lowerBound = 30; //beats per msecond lcd.printf("M"); mode = 'm'; -// LRI = 1000; -// URI = 700; -// PVARP = 300; - + LRI = 2125; // max V-V (LRI) based on exercise mode + URI = 675; // min V-V (URI) based on sleep mode + while(1) { osEvent evt = signal_q.get(); if(evt.status == osEventMessage) { @@ -408,23 +418,54 @@ } } +void obsinterval() +{ + char newObsInt[8]; + int isChangingObsInt = 1; + int i = 0; + while(isChangingObsInt) { + osEvent evt = obsint_q.get(); + if(evt.status == osEventMessage) { + key = (char)evt.value.p; + if(key != '\r' && i < 7 ) { + newObsInt[i] = key; + i++; + } else if((key == '\r') && (i > 0)) { + heart_beats = 0; + int obsint; + newObsInt[i] = '\0'; + sscanf(newObsInt, "%d", &obsint); + observation_interval = (obsint > 0 ) ? obsint: 1; + isChangingObsInt = 0; + lcd.printf("%d", observation_interval); + } + } + } +} osThreadDef(PaceSignal, osPriorityNormal, DEFAULT_STACK_SIZE); osThreadDef(PaceSense, osPriorityNormal, DEFAULT_STACK_SIZE); +osThreadDef(alarmThread, osPriorityBelowNormal, DEFAULT_STACK_SIZE); //priority BelowNormal -osThreadDef(displayThread, osPriorityNormal, DEFAULT_STACK_SIZE); +osThreadDef(displayThread, osPriorityLow, DEFAULT_STACK_SIZE); //priority Low osThreadDef(manualmode, osPriorityNormal, DEFAULT_STACK_SIZE); osThreadDef(normalmode, osPriorityNormal, DEFAULT_STACK_SIZE); osThreadDef(exercisemode, osPriorityNormal, DEFAULT_STACK_SIZE); osThreadDef(sleepmode, osPriorityNormal, DEFAULT_STACK_SIZE); + +//////////////////////////////////////////// +//////////////////////////////////////////// +//////////////////////////////////////////// + int main() { + alarmTid = osThreadCreate(osThread(alarmThread), NULL); senseTid = osThreadCreate(osThread(PaceSense), NULL); signalTid = osThreadCreate(osThread(PaceSignal), NULL); displayTid = osThreadCreate(osThread(displayThread), NULL); pacemodeTid = osThreadCreate(osThread(normalmode), NULL); - + vsignal.rise(&vsignal_irq); //rising edge of timer asignal.rise(&asignal_irq); @@ -444,71 +485,47 @@ case('n'): mm_flag = 0; osThreadTerminate (pacemodeTid); + osThreadTerminate (displayTid); pacemodeTid = osThreadCreate(osThread(normalmode), NULL); + displayTid = osThreadCreate(osThread(displayThread), NULL); + lcd.printf("%d", observation_interval); break; case('s'): mm_flag = 0; - lcd.printf("testingS"); +// lcd.printf("testingS"); osThreadTerminate (pacemodeTid); + osThreadTerminate (displayTid); pacemodeTid = osThreadCreate(osThread(sleepmode), NULL); - + displayTid = osThreadCreate(osThread(displayThread), NULL); break; case('e'): mm_flag = 0; - lcd.printf("testingE"); +// lcd.printf("testingE"); osThreadTerminate (pacemodeTid); + osThreadTerminate (displayTid); pacemodeTid = osThreadCreate(osThread(exercisemode), NULL); - + displayTid = osThreadCreate(osThread(displayThread), NULL); + lcd.printf("%d", observation_interval); break; case('m'): mm_flag = 1; osThreadTerminate (pacemodeTid); + //osThreadTerminate (displayTid); apace_timer->stop(); vpace_timer->stop(); pacemodeTid = osThreadCreate(osThread(manualmode), NULL); +// displayTid = osThreadCreate(osThread(displayThread), NULL); manual_mode = 1; break; case('o'): - isChangingObsInt = 1; + lcd.printf("modeO"); + hr_mutex.lock(); + obsinterval(); + hr_mutex.unlock(); + osThreadTerminate (displayTid); + displayTid = osThreadCreate(osThread(displayThread), NULL); break; } - - - -///////observation interval -// int i = 0; -// while(isChangingObsInt){ -// // wait(1); -// key = pc.getc(); -//// lcd.printf("0"); -// if(key != '\r' && (int)key > 47 && (int)key < 58){ -// //use atoi - asci to integer to convert input key to 0 - 10 -// newObsInt[i] += key; -// i++; -// lcd.printf("1"); -// } -// else if(key == '\r'){ -// newObsInt[i] = '\0'; -// int obsint; -// sscanf(newObsInt, "%d", &obsint); -// observation_interval = obsint * 1000; -// lcd.printf("%s", obsint); -// isChangingObsInt = 0; -// // lcd.printf("2"); -// observation_interval = newObsInt * 1000; -// atoi(newObsInt[i]) -// strncat -// atoi -// !isChangingObsInt } } -// lcd.printf("3"); -// if (isChangingObsInt && (int)key > 47 && (int)key < 58){ -// newObsInt += key; -// lcd.printf("%lf", newObsInt); -// } -// else if (isChangingObsInt && key == '\r') { -// observation_interval = newObsInt * 1000; -// isChangingObsInt = 0; -// } }