-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPaceMaker.cpp
655 lines (622 loc) · 19.3 KB
/
PaceMaker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#include "mbed.h"
#include "rtos.h"
#include "Terminal.h"
#define LRI 0
#define AVI 1
#define VRP 2
#define PVARP 3
#define URI 4
#define PAVB 5
#define VSP 6
#define NR -1 //not running
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
DigitalOut buzzer(p9);
DigitalOut aPace(p7);
DigitalOut vPace(p8);
InterruptIn ASignal(p5);
InterruptIn VSignal(p6);
Terminal console(USBTX, USBRX);
bool expectingASignal, expectingVSignal, paceA, observationChange, digitOneReceived, modeChanged, canPaceV, paceVPending, ringAlarm, ringingAlarm, timerRunning, aSenseOccurred, digitTwoReceived;
int timeOutValue[7]; //timeout array that holds values for LRI, VRP, PVARP, AVI, URI, PAVB, VSP; PVARP >VRP
int heartRate, observationInterval, observationRate, timeOutStatus, waitCount, avgHeartRate, rateCoefficient, heartRateHeart, sec, avgHeartRateHeart;
int paceMakerMode=1; //1 - Normal, 2 - Exercise, 3 - Sleep, 4 - Manual
int uriTimeOutStatus=URI;
char ch;
char modeString[20];
const int nLRI=1500, nAVI = 60, nPVARP = 150, nURI = 600, nVRP = 100, nVSP = 0, nPAVB = 20;
const int sLRI=2000, sAVI = 60, sPVARP = 150, sURI = 1000, sVRP = 100, sVSP = 0, sPAVB = 20;
const int eLRI=1000, eAVI = 60, ePVARP = 150, eURI = 400, eVRP = 100, eVSP = 0, ePAVB = 20;
osMutexId displayMutex;
osMutexDef (displayMutex);
osMutexId observationChangeMutex;
osMutexDef (obserationChangeMutex);
osMutexId expectAMutex;
osMutexDef (expectAMutex);
osMutexId expectVMutex;
osMutexDef (expectVMutex);
osMutexId timeOutStatusMutex;
osMutexDef (timeOutStatusMutex);
osMutexId heartRateMutex;
osMutexDef (heartRateMutex);
Thread *SerialThreadPTR;
Thread *PacePTR;
Thread *ModeChangePTR;
Thread *PMSensePTR;
RtosTimer *TimeOutTimer;
RtosTimer *URITimeOutTimer;
RtosTimer *KeyTimeOutTimer;
RtosTimer *SecondsTimer;
void setTimeOutValues(int tLRI, int tAVI, int tPVARP, int tURI, int tVRP, int tVSP, int tPAVB)
{
timeOutValue[LRI]=tLRI;
timeOutValue[AVI]=tAVI;
timeOutValue[PVARP]=tPVARP;
timeOutValue[URI]=tURI;
timeOutValue[VRP]=tVRP;
timeOutValue[VSP]=tVSP;
timeOutValue[PAVB]=tPAVB;
}
void resetDisplay()
{
osMutexWait(displayMutex, osWaitForever);
console.cls();
console.locate(30, 4);
console.printf("Pace Maker Display");
console.locate(30, 8);
console.printf("Heart Rate : %04d bpm", avgHeartRate);
console.locate(30, 10);
console.printf("Observation Interval : %02d seconds", observationInterval/1000);
console.locate(30, 12);
console.printf("Mode : %s", modeString);
console.locate(30, 14);
console.printf("Heart Beat Rate : %04d bpm", (heartRateHeart*(60/sec)));
osMutexRelease(displayMutex);
ringingAlarm=false;
}
void updateDisplay()
{
osMutexWait(displayMutex, osWaitForever);
console.locate(44, 8);
console.printf("%04d", avgHeartRate);
console.locate(54, 10);
console.printf("%02d", observationInterval/1000);
console.locate(49, 14);
console.printf("%04d", (heartRateHeart*(60/sec)));
osMutexRelease(displayMutex);
}
void changeMode()
{
switch(paceMakerMode)
{
case 1: //Normal Mode
{
setTimeOutValues(nLRI, nAVI, nPVARP, nURI, nVRP, nVSP, nPAVB);
strcpy(modeString, "Normal");
break;
}
case 2: //Exercise Mode
{
setTimeOutValues(eLRI, eAVI, ePVARP, eURI, eVRP, eVSP, ePAVB);
strcpy(modeString, "Exercise");
break;
}
case 3: //Sleep Mode
{
setTimeOutValues(sLRI, sAVI, sPVARP, sURI, sVRP, sVSP, sPAVB);
strcpy(modeString, "Sleep");
break;
}
case 4: //Manual Mode; do not chnage the time out values
{
strcat(modeString, " + Manual");
break;
}
default: //Normal mode is any error occurs
{
setTimeOutValues(nLRI, nAVI, nPVARP, nURI, nVRP, nVSP, nPAVB);
strcpy(modeString, "Normal");
}
}
modeChanged=false;
resetDisplay();
}
void modeChange(const void *args)
{
while(1)
{
Thread::signal_wait(0x03);
if(modeChanged)
{
changeMode();
}
}
}
void aSense()
{
//ASignal received from heart
if(expectingASignal)
{
led4=1;
wait(0.001);
led4=0;
if(modeChanged)
{
(*ModeChangePTR).signal_set(0x03);
}
aSenseOccurred=true;
(*PMSensePTR).signal_set(0x04);
}
}
void vSense()
{
//VSignal received from heart
//reset timeout
//increment heart rate
heartRateHeart++;
if(expectingVSignal)
{
led3=1;
wait(0.001);
led3=0;
if(modeChanged)
{
(*ModeChangePTR).signal_set(0x03);
}
canPaceV=false;
aSenseOccurred=false;
(*PMSensePTR).signal_set(0x04);
}
}
void seconds(const void *args)
{
sec++;
if(sec>=60)
{
avgHeartRateHeart=heartRateHeart;
heartRateHeart=0;
sec=0;
}
}
void timeOut(const void *args)
{
// check which time out has occurred
// generate appropriate pace signal
// reset timer to new value
//led2=1;
if(timeOutStatus==AVI)
{
//generate VPace
if(canPaceV)
{
paceA=false;
(*PacePTR).signal_set(0x01);
}
else
{
canPaceV=true;
}
}
else if(timeOutStatus==PAVB)
{
osMutexWait(expectVMutex,osWaitForever);
expectingVSignal=true;
osMutexRelease(expectVMutex);
osMutexWait(timeOutStatusMutex, osWaitForever);
timeOutStatus=AVI;
osMutexRelease(timeOutStatusMutex);
timerRunning=true;
TimeOutTimer->start(timeOutValue[AVI]-timeOutValue[PAVB]);
}
else if(timeOutStatus==VRP)
{
//now we can sense a Ventrival event, but not an atrial event as PVARP is not over
//restart timer for PVARP
osMutexWait(expectVMutex,osWaitForever);
expectingVSignal=true;
osMutexRelease(expectVMutex);
osMutexWait(timeOutStatusMutex, osWaitForever);
timeOutStatus=PVARP;
osMutexRelease(timeOutStatusMutex);
timerRunning=true;
TimeOutTimer->start(timeOutValue[PVARP]-timeOutValue[VRP]);
}
else if(timeOutStatus==PVARP)
{
//now we can sense Atrial events as well
osMutexWait(expectAMutex,osWaitForever);
expectingASignal=true;
osMutexRelease(expectAMutex);
osMutexWait(timeOutStatusMutex, osWaitForever);
timeOutStatus=LRI;
osMutexRelease(timeOutStatusMutex);
timerRunning=true;
TimeOutTimer->start(timeOutValue[LRI]-timeOutValue[PVARP]-timeOutValue[AVI]);
}
else if(timeOutStatus==LRI)
{
//generate APace
paceA=true;
(*PacePTR).signal_set(0x01);
}
}
void uriTimeOut(const void *args)
{
// uri is over
//check is a vpace has to be generated; If yes then generate the pace; else enable a flag that lets the thread generate the pace
if(paceVPending || canPaceV)
{
//pace V as a pace occurred during URI
paceA=false;
paceVPending=false;
(*PacePTR).signal_set(0x01);
}
else
{
canPaceV=true; //allow the PM to pace V as URI is now over
}
}
void keyTimeOut(const void *args)
{
if(digitOneReceived)
{
observationChange=false;
}
else
{
observationChange=false;
}
resetDisplay();
}
void pmSense(const void *args)
{
while(1)
{
Thread::signal_wait(0x04);
if(timerRunning)
{
TimeOutTimer->stop();
timerRunning=false;
}
if(aSenseOccurred)
{
osMutexWait(expectAMutex,osWaitForever);
expectingASignal=false;
osMutexRelease(expectAMutex);
osMutexWait(expectVMutex,osWaitForever);
expectingVSignal=true;
osMutexRelease(expectVMutex);
timerRunning=true;
osMutexWait(timeOutStatusMutex, osWaitForever);
timeOutStatus=AVI;
osMutexRelease(timeOutStatusMutex);
TimeOutTimer->start(timeOutValue[AVI]);//500);
}
else
{
osMutexWait(heartRateMutex, osWaitForever);
heartRate++;
osMutexRelease(heartRateMutex);
osMutexWait(expectVMutex,osWaitForever);
expectingVSignal=false;
osMutexRelease(expectVMutex);
osMutexWait(expectAMutex,osWaitForever);
expectingASignal=false;
osMutexRelease(expectAMutex);
canPaceV=false;
URITimeOutTimer->start(timeOutValue[URI]);
timerRunning=true;
osMutexWait(timeOutStatusMutex, osWaitForever);
timeOutStatus=VRP;
osMutexRelease(timeOutStatusMutex);
TimeOutTimer->start(timeOutValue[VRP]);
}
}
}
void pace(const void *args)
{
while(1)
{
Thread::signal_wait(0x01);
if(paceA)
{
led2=1;
aPace=1;
Thread::wait(1);
aPace=0;
led2=0;
if(modeChanged)
{
changeMode();
}
// start AVI Timer
osMutexWait(expectAMutex,osWaitForever);
expectingASignal=false;
osMutexRelease(expectAMutex);
osMutexWait(expectVMutex,osWaitForever);
expectingVSignal=false;
osMutexRelease(expectVMutex);
osMutexWait(timeOutStatusMutex, osWaitForever);
timeOutStatus=PAVB;
osMutexRelease(timeOutStatusMutex);
timerRunning=true;
TimeOutTimer->start(timeOutValue[PAVB]);
//generate the APace pulse
}
else
{
led1=1;
vPace=1;
Thread::wait(1);
vPace=0;
led1=0;
if(modeChanged)
{
changeMode();
}
// start VRP and URI timers
osMutexWait(expectVMutex,osWaitForever);
expectingVSignal=false;
osMutexRelease(expectVMutex);
osMutexWait(expectAMutex,osWaitForever);
expectingASignal=false;
osMutexRelease(expectAMutex);
osMutexWait(heartRateMutex, osWaitForever);
heartRate++;
osMutexRelease(heartRateMutex);
canPaceV=false;
URITimeOutTimer->start(timeOutValue[URI]);
osMutexWait(timeOutStatusMutex, osWaitForever);
timeOutStatus=VRP;
osMutexRelease(timeOutStatusMutex);
timerRunning=true;
TimeOutTimer->start(timeOutValue[VRP]);
//generate the VPace pulse
}
}
}
void serialThread(const void *args)
{
while(1)
{
Thread::signal_wait(0x02);
if((((ch=='a')||(ch=='A')) && paceMakerMode==4) && !observationChange)
{
//fire A Pace
paceA=true;
(*PacePTR).signal_set(0x01);
}
else if((((ch=='v')||(ch=='V')) && paceMakerMode==4) && !observationChange)
{
//fire V Pace
if(canPaceV)
{
paceA=false;
paceVPending=false;
(*PacePTR).signal_set(0x01);
}
else
{
paceVPending=true;
}
}
else if(((((ch=='n')||(ch=='N'))&& paceMakerMode!=1) && !observationChange) && !modeChanged)
{
paceMakerMode=1; //change Mode to Normal
modeChanged=true;
}
else if(((((ch=='e')||(ch=='E')) && paceMakerMode!=2) && !observationChange) && !modeChanged)
{
paceMakerMode=2; //chnage mode to Exercise
modeChanged=true;
}
else if(((((ch=='s')||(ch=='S')) && paceMakerMode!=3) && !observationChange) && !modeChanged)
{
paceMakerMode=3; //change mode to Sleep
modeChanged=true;
}
else if(((((ch=='b')||(ch=='B')) ) && !observationChange) )
{
ringAlarm=!ringAlarm;
}
else if(((((ch=='m')||(ch=='M')) && paceMakerMode!=4) && !observationChange) && !modeChanged)
{
paceMakerMode=4; //change mode to Sleep
modeChanged=true;
}
else if(((((ch=='o')||(ch=='O')) && !observationChange) && !modeChanged))
{
observationChange=true;
digitOneReceived=false;
digitTwoReceived=false;
//spawn a timer for 3 seconds
osMutexWait(displayMutex, osWaitForever);
console.locate(30, 18);
console.printf("Observation Interval change : -- seconds");
osMutexRelease(displayMutex);
KeyTimeOutTimer=new RtosTimer(keyTimeOut, osTimerOnce, (void *)0);
KeyTimeOutTimer->start(3000);
}
else if((observationChange) && ((ch>=48) && (ch<=57)))
{
if(!digitOneReceived)
{
KeyTimeOutTimer->start(3000);
osMutexWait(observationChangeMutex, osWaitForever);
observationRate=ch-'0';
osMutexRelease(observationChangeMutex);
digitOneReceived=true;
osMutexWait(displayMutex, osWaitForever);
console.locate(60, 18);
console.printf("%02d", observationRate);
osMutexRelease(displayMutex);
}
else
{
KeyTimeOutTimer->stop();
digitTwoReceived=true;
osMutexWait(observationChangeMutex, osWaitForever);
observationRate=(observationRate*10)+(ch-'0');
osMutexRelease(observationChangeMutex);
observationChange=false;
osMutexWait(displayMutex, osWaitForever);
console.locate(60, 18);
console.printf("%02d", observationRate);
osMutexRelease(displayMutex);
}
}
}
}
void alarm(const void *args)
{
while(1)
{
Thread::wait(1000);
while(((heartRateHeart*(60/sec))>(60000/timeOutValue[URI])) && ringAlarm)
{
buzzer=1;
Thread::wait(5);
buzzer=0;
Thread::wait(5);
if(!ringingAlarm)
{
osMutexWait(displayMutex, osWaitForever);
console.locate(30, 16);
console.printf("Alarm : HeartRate too high");
osMutexRelease(displayMutex);
ringingAlarm=true;
}
}
while(((heartRateHeart*(60/sec))<(60000/timeOutValue[LRI])) && ringAlarm)
{
buzzer=1;
Thread::wait(10);
buzzer=0;
Thread::wait(10);
if(!ringingAlarm)
{
osMutexWait(displayMutex, osWaitForever);
console.locate(30, 16);
console.printf("Alarm : HeartRate too Low");
osMutexRelease(displayMutex);
ringingAlarm=true;
}
}
if(ringingAlarm)
{
ringingAlarm=false;
resetDisplay();
}
}
}
void display(const void *args)
{
while(1)
{
Thread::wait(observationInterval);
waitCount++;
if(!observationChange)
{
avgHeartRate=heartRate*rateCoefficient/waitCount;
osMutexWait(heartRateMutex, osWaitForever);
heartRate=0;
osMutexRelease(heartRateMutex);
if(observationRate!=(observationInterval/1000))
{
resetDisplay();
observationInterval=observationRate*1000;
rateCoefficient=(60000/observationInterval);
}
else
{
updateDisplay();
}
waitCount=0;
}
}
}
void Rx_interrupt()
{
if(console.readable())
{
ch=LPC_UART0->RBR; //read uart buffer data and clear the interrupt flag
(*SerialThreadPTR).signal_set(0x02); //initiate the serial thread to change the state of the timer
}
}
int main()
{
console.attach(&Rx_interrupt, Serial::RxIrq); //attach a uart interrupt for uart
console.cls();
console.locate(30, 4);
console.printf("Pace Maker Display");
console.locate(30, 8);
console.printf("Heart Rate : --- bpm");
console.locate(30, 10);
console.printf("Observation Interval : -- seconds");
console.locate(30, 12);
console.printf("Mode : Normal");
console.locate(30, 14);
console.printf("Heart Beat Rate : ---- bpm");
//initialize variables
osMutexWait(expectAMutex,osWaitForever);
expectingASignal=true;
osMutexRelease(expectAMutex);
osMutexWait(expectVMutex,osWaitForever);
expectingVSignal=true;
osMutexRelease(expectVMutex);
setTimeOutValues(nLRI, nAVI, nPVARP, nURI, nVRP, nVSP, nPAVB);
heartRate=0;
avgHeartRate=0;
paceMakerMode=1;
observationInterval=5000;
observationRate=5;
waitCount=0;
rateCoefficient=12;
paceA=false;
observationChange=false;
digitOneReceived=false;
digitTwoReceived=false;
modeChanged=false;
canPaceV=true; //assume at begining that URI has lapsed and start with an atrial event
paceVPending=false; //assume that a VPace request has not happened already
strcpy(modeString, "Normal");
buzzer=0;
ringAlarm=true;
heartRateHeart=0;
sec=0;
avgHeartRateHeart=0;
ringingAlarm=false;
timerRunning=false;
aSenseOccurred=true;
ASignal.fall(&aSense);
VSignal.fall(&vSense);
TimeOutTimer=new RtosTimer(timeOut, osTimerOnce, (void*)0);
URITimeOutTimer=new RtosTimer(uriTimeOut, osTimerOnce, (void *)0);
SecondsTimer=new RtosTimer(seconds, osTimerPeriodic, (void *)0); //timer that over runs every 1 second and is used to reset the heart rate count coming from the heart
SecondsTimer->start(1000); //start the timer to run for every 1 second
Thread Pace(pace);
PacePTR=&Pace;
Pace.set_priority(osPriorityHigh);
Thread PMSense(pmSense);
PMSensePTR=&PMSense;
PMSense.set_priority(osPriorityAboveNormal);
Thread Alarm(alarm);
Alarm.set_priority(osPriorityAboveNormal);
Thread ModeChange(modeChange);
ModeChangePTR=&ModeChange;
ModeChange.set_priority(osPriorityAboveNormal);
Thread Display(display);
Thread SerialThread(serialThread);
SerialThreadPTR=&SerialThread;
SerialThread.set_priority(osPriorityRealtime);
Display.set_priority(osPriorityAboveNormal);
osMutexWait(timeOutStatusMutex, osWaitForever);
timeOutStatus=VRP;
osMutexRelease(timeOutStatusMutex);
//test lines to start pacing without heart; heart starts immediately after an Atrial event
//timeOutStatus=AVI;
TimeOutTimer->start(timeOutValue[VRP]);
while(1);
}