-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathVetSim.cpp
More file actions
2364 lines (2192 loc) · 69.7 KB
/
Copy pathVetSim.cpp
File metadata and controls
2364 lines (2192 loc) · 69.7 KB
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* VetSim.cpp
*
* SimMgr applicatiopn
*
* This file is part of the sim-mgr distribution.
*
* Copyright (c) 2019-2026 VetSim, Cornell University College of Veterinary Medicine Ithaca, NY
* Copyright (c) 2019-2026 ITown Design, Ithaca, NY
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vetsim.h"
using namespace std;
// FOR WIN32 SIGNAL HANDLING
// compile with: /EHsc /W4
// Use signal to attach a signal handler to the abort routine
#include <signal.h>
char buf[1024];
int scenarioPid = -1;
char sesid[1024] = { 0, };
char c_msgbuf[MSG_LENGTH];
char sessionsPath[1088];
// Time values, to track start time and elapsed time
// This is the "absolute" time
std::time_t scenario_start_time;
std::time_t now;
std::time_t scenario_run_time;
std::time_t nibp_next_time;
std::time_t nibp_run_complete_time;
#define SCENARIO_LOOP_COUNT 19 // Run the scenario every SCENARIO_LOOP_COUNT iterations of the 10 msec loop
#define SCENARIO_PROCCHECK 1
#define SCENARIO_COMMCHECK 5
#define SCENARIO_EVENTCHECK 10
#define SCENARIO_CPRCHECK 13
#define SCENARIO_SHOCKCHECK 14
#define SCENARIO_AWRRCHECK 15
#define SCENARIO_TIMECHECK 18
int scenarioCount = 0;
struct simmgr_shm shmSpace;
struct localConfiguration localConfig;
#define BUF_SIZE 2048
char msg_buf[BUF_SIZE];
int vs_iiLockTaken = 0;
int hrCheckCount = 0;
bool currentIsPulsed = FALSE;
bool currentIsRegular = FALSE;
void simmgrInitialize(void);
void resetAllParameters(void);
void clearAllTrends(void);
void hrcheck_handler(void);
int updateScenarioState(ScenarioState new_state);
ScenarioState scenario_state = ScenarioState::ScenarioStopped;
NibpState nibp_state = NibpState::NibpIdle;
void SignalHandler(int signal)
{
switch (signal)
{
case SIGABRT:
printf("sig: Abort\n");
break;
case SIGFPE:
printf("sig: Floating Point Error\n");
break;
case SIGILL:
printf("sig: Illegal Instruction\n");
break;
case SIGINT:
printf("sig: CTRL+C\n");
break;
case SIGSEGV:
printf("sig: Segmentation Fault\n");
break;
case SIGTERM:
printf("sig: Termination Request\n");
break;
}
stopPHPServer();
}
extern char WVSversion[];
HANDLE hComm = NULL;
int pulseState = 0;
int pulseStateCount = 0;;
void
setPulseState(int val)
{
if (hComm)
{
switch (val)
{
case 1:
EscapeCommFunction(hComm, SETRTS);
pulseState = 1;
break;
case 0:
EscapeCommFunction(hComm, CLRRTS);
pulseState = 0;
break;
default: // Toggle
if (pulseState == 1)
setPulseState(0);
else
setPulseState(1);
break;
}
}
}
extern char phpPath[];
int vetsim()
{
int last = -1;
int count = 0;
char cc;
extern struct obsData obsd;
setvbuf(stdout, NULL, _IONBF, 0);
char cmd[BUF_SIZE];
initSHM();
simmgrInitialize();
log_message_init();
(void)start_task("pluseTask", pulseTask);
(void)start_task("simstatusMain", simstatusMain);
(void)start_task("bcastReply", bcastReply);
printf("Hostname: %s\n", simmgr_shm->server.name);
sprintf_s(msg_buf, BUF_SIZE, "simmgrInitialization %s", "Done");
log_message("", msg_buf);
if (startPHPServer())
{
printf("Could not start PHP Server\n" );
sprintf_s(msg_buf, BUF_SIZE, "%s", "Could not start PHP Server");
log_message("", msg_buf);
exit(202);
}
else
{
printf("PHP Server OK! %s\n", phpPath);
sprintf_s(msg_buf, BUF_SIZE, "%s", "PHP Server OK!!");
log_message("", msg_buf);
}
// Open web page
sprintf_s(cmd, BUF_SIZE, "cmd /c start http://%s:%d/sim-ii", PHP_SERVER_ADDR, PHP_SERVER_PORT );
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
BOOL created = CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (!created)
{
DWORD error = GetLastError();
// Log the command line that was attempted
log_message("", cmd);
sprintf_s(msg_buf, BUF_SIZE, "CreateProcess failed with error %lu: %s", error, std::system_category().message(error).c_str());
printf("%s\n", msg_buf);
log_message("", msg_buf);
}
else
{
// We don't need the handles; close them.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
obsd.obsWnd = NULL;
while (1)
{
Sleep(10);
if (last != simmgr_shm->status.cardiac.pulseCount)
{
last = simmgr_shm->status.cardiac.pulseCount;
}
if (count++ == 200)
{
// For testing, load up the default scenario
//sprintf_s(simmgr_shm->status.scenario.active, STR_SIZE, "default");
//simmgr_shm->status.scenario.record = 0;
//start_scenario();
}
if (_kbhit())
{
cc = _getch();
if (cc == 'q' || cc == 'Q')
{
break;
}
}
simmgrRun();
}
#ifdef DEBUG
printf("Close window to exit\n" );
while (1)
{
Sleep(10);
}
#endif
stopPHPServer();
exit(201);
}
void
simmgrInitialize(void)
{
char* ptr;
typedef void (*SignalHandlerPointer)(int);
SignalHandlerPointer previousHandler;
previousHandler = signal(SIGABRT, SignalHandler);
previousHandler = signal(SIGFPE, SignalHandler);
previousHandler = signal(SIGILL, SignalHandler);
previousHandler = signal(SIGINT, SignalHandler);
previousHandler = signal(SIGSEGV, SignalHandler);
previousHandler = signal(SIGTERM, SignalHandler);
// Zero out the shared memory and reinit the values
memset(simmgr_shm, 0, sizeof(struct simmgr_shm));
// hdr
sprintf_s(simmgr_shm->hdr.version, STR_SIZE, "%s", WVSversion );
simmgr_shm->hdr.size = sizeof(struct simmgr_shm);
// server
do_command_read("hostname.exe", simmgr_shm->server.name, sizeof(simmgr_shm->server.name) - 1);
char buffer[256];
sprintf_s(buffer, 256, "Hostname is %s", simmgr_shm->server.name);
#ifdef NDEBUG
append_text_to_edit(buffer);
#endif
ptr = getETH0_IP();
sprintf_s(simmgr_shm->server.ip_addr, STR_SIZE, "%s", ptr);
// server_time and msec_time are updated in the loop
resetAllParameters();
// status/scenario
sprintf_s(simmgr_shm->status.scenario.active, STR_SIZE, "%s", "default");
sprintf_s(simmgr_shm->status.scenario.state, STR_SIZE, "%s", "Stopped");
simmgr_shm->status.scenario.record = 0;
sprintf_s(simmgr_shm->status.scenario.error_message, LONG_STR_SIZE, "%s", "");
simmgr_shm->status.scenario.error_flag = 0;
// instructor/sema
simmgr_shm->instructor.sema = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
vs_iiLockTaken = 0;
// instructor/scenario
sprintf_s(simmgr_shm->instructor.scenario.active, STR_SIZE, "%s", "");
sprintf_s(simmgr_shm->instructor.scenario.state, STR_SIZE, "%s", "");
simmgr_shm->instructor.scenario.record = -1;
sprintf_s(simmgr_shm->instructor.scenario.error_message, LONG_STR_SIZE, "%s", "");
simmgr_shm->instructor.scenario.error_flag = -1;
// Log File
simmgr_shm->logfile.sema = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
if (!simmgr_shm->logfile.sema)
{
printf("CreateMutex error: %d\n", GetLastError());
exit(203);
}
simmgr_shm->logfile.active = 0;
sprintf_s(simmgr_shm->logfile.filename, FILENAME_SIZE, "%s", "");
simmgr_shm->logfile.lines_written = 0;
// Event List
simmgr_shm->eventListNextWrite = 0;
simmgr_shm->eventListNextRead = 0;
simmgr_shm->lastEventLogged = 0;
// Comment List
simmgr_shm->commentListNext = 0;
simmgr_shm->lastCommentLogged = 0;
// instructor/cpr
simmgr_shm->instructor.cpr.compression = -1;
simmgr_shm->instructor.cpr.last = -1;
simmgr_shm->instructor.cpr.release = -1;
simmgr_shm->instructor.cpr.duration = -1;
// instructor/defibrillation
simmgr_shm->instructor.defibrillation.last = -1;
simmgr_shm->instructor.defibrillation.energy = -1;
simmgr_shm->instructor.defibrillation.shock = -1;
clearAllTrends();
timer_start(hrcheck_handler, 5 );
timer_start(awrr_check, 10);
}
void
resetAllParameters(void)
{
// status/cardiac
sprintf_s(simmgr_shm->status.cardiac.rhythm, STR_SIZE, "%s", "sinus");
sprintf_s(simmgr_shm->status.cardiac.vpc, STR_SIZE, "%s", "none");
sprintf_s(simmgr_shm->status.cardiac.vfib_amplitude, STR_SIZE, "%s", "high");
simmgr_shm->status.cardiac.vpc_freq = 0;
simmgr_shm->status.cardiac.vpc_delay = 0;
resetVpc();
simmgr_shm->status.cardiac.pea = 0;
simmgr_shm->status.cardiac.rate = 80;
simmgr_shm->status.cardiac.nibp_rate = 80;
simmgr_shm->status.cardiac.nibp_read = -1;
simmgr_shm->status.cardiac.nibp_linked_hr = 1;
simmgr_shm->status.cardiac.nibp_freq = 0;
sprintf_s(simmgr_shm->status.cardiac.pwave, STR_SIZE, "%s", "none");
simmgr_shm->status.cardiac.pr_interval = 140; // Good definition at http://lifeinthefastlane.com/ecg-library/basics/pr-interval/
simmgr_shm->status.cardiac.qrs_interval = 85;
simmgr_shm->status.cardiac.bps_sys = 105;
simmgr_shm->status.cardiac.bps_dia = 70;
simmgr_shm->status.cardiac.right_dorsal_pulse_strength = 2;
simmgr_shm->status.cardiac.right_femoral_pulse_strength = 2;
simmgr_shm->status.cardiac.left_dorsal_pulse_strength = 2;
simmgr_shm->status.cardiac.left_femoral_pulse_strength = 2;
sprintf_s(simmgr_shm->status.cardiac.heart_sound, STR_SIZE, "%s", "none");
simmgr_shm->status.cardiac.heart_sound_volume = 10;
simmgr_shm->status.cardiac.heart_sound_mute = 0;
simmgr_shm->status.cardiac.ecg_indicator = 0;
simmgr_shm->status.cardiac.bp_cuff = 0;
simmgr_shm->status.cardiac.arrest = 0;
// status/respiration
sprintf_s(simmgr_shm->status.respiration.left_lung_sound, STR_SIZE, "%s", "normal");
sprintf_s(simmgr_shm->status.respiration.left_sound_in, STR_SIZE, "%s", "normal");
sprintf_s(simmgr_shm->status.respiration.left_sound_out, STR_SIZE, "%s", "normal");
sprintf_s(simmgr_shm->status.respiration.left_sound_back, STR_SIZE, "%s", "normal");
sprintf_s(simmgr_shm->status.respiration.right_lung_sound, STR_SIZE, "%s", "normal");
sprintf_s(simmgr_shm->status.respiration.right_sound_in, STR_SIZE, "%s", "normal");
sprintf_s(simmgr_shm->status.respiration.right_sound_out, STR_SIZE, "%s", "normal");
sprintf_s(simmgr_shm->status.respiration.right_sound_back, STR_SIZE, "%s", "normal");
simmgr_shm->status.respiration.left_lung_sound_volume = 10;
simmgr_shm->status.respiration.left_lung_sound_mute = 1;
simmgr_shm->status.respiration.right_lung_sound_volume = 10;
simmgr_shm->status.respiration.right_lung_sound_mute = 0;
simmgr_shm->status.respiration.inhalation_duration = 1350;
simmgr_shm->status.respiration.exhalation_duration = 1050;
simmgr_shm->status.respiration.rate = 20;
simmgr_shm->status.respiration.spo2 = 95;
simmgr_shm->status.respiration.etco2 = 34;
simmgr_shm->status.respiration.etco2_indicator = 0;
simmgr_shm->status.respiration.spo2_indicator = 0;
simmgr_shm->status.respiration.chest_movement = 0;
simmgr_shm->status.respiration.manual_breath = 0;
simmgr_shm->status.respiration.manual_count = 0;
awrr_restart();
// status/vocals
sprintf_s(simmgr_shm->status.vocals.filename, STR_SIZE, "%s", "");
simmgr_shm->status.vocals.repeat = 0;
simmgr_shm->status.vocals.volume = 10;
simmgr_shm->status.vocals.play = 0;
simmgr_shm->status.vocals.mute = 0;
// status/auscultation
simmgr_shm->status.auscultation.side = 0;
simmgr_shm->status.auscultation.row = 0;
simmgr_shm->status.auscultation.col = 0;
// status/pulse
simmgr_shm->status.pulse.right_dorsal = 0;
simmgr_shm->status.pulse.left_dorsal = 0;
simmgr_shm->status.pulse.right_femoral = 0;
simmgr_shm->status.pulse.left_femoral = 0;
// status/cpr
simmgr_shm->status.cpr.last = 0;
simmgr_shm->status.cpr.compression = 0;
simmgr_shm->status.cpr.release = 0;
simmgr_shm->status.cpr.duration = 0;
// status/defibrillation
simmgr_shm->status.defibrillation.last = 0;
simmgr_shm->status.defibrillation.energy = 100;
simmgr_shm->status.defibrillation.shock = 0;
// status/general
simmgr_shm->status.general.temperature = 1017;
simmgr_shm->status.general.temperature_enable = 0;
//sprintf_s(simmgr_shm->status.general.temperature_units, STR_SIZE, "%s", "F" );
// status/media
sprintf_s(simmgr_shm->status.media.filename, FILENAME_SIZE, "%s", "");
simmgr_shm->status.media.play = 0;
// status/telesim
// simmgr_shm->status.telesim.enable = 0;
sprintf_s(simmgr_shm->status.telesim.vid[0].name, STR_SIZE, "%s", "none");
simmgr_shm->status.telesim.vid[0].command = 0;
simmgr_shm->status.telesim.vid[0].param = 0;
simmgr_shm->status.telesim.vid[0].next = 0;
sprintf_s(simmgr_shm->status.telesim.vid[1].name, STR_SIZE, "%s", "");
simmgr_shm->status.telesim.vid[1].command = 0;
simmgr_shm->status.telesim.vid[1].param = 0;
simmgr_shm->status.telesim.vid[1].next = 0;
// instructor/cardiac
sprintf_s(simmgr_shm->instructor.cardiac.rhythm, STR_SIZE, "%s", "");
simmgr_shm->instructor.cardiac.rate = -1;
simmgr_shm->instructor.cardiac.nibp_rate = -1;
simmgr_shm->instructor.cardiac.nibp_read = -1;
simmgr_shm->instructor.cardiac.nibp_linked_hr = -1;
simmgr_shm->instructor.cardiac.nibp_freq = -1;
sprintf_s(simmgr_shm->instructor.cardiac.pwave, STR_SIZE, "%s", "");
simmgr_shm->instructor.cardiac.pr_interval = -1;
simmgr_shm->instructor.cardiac.qrs_interval = -1;
simmgr_shm->instructor.cardiac.bps_sys = -1;
simmgr_shm->instructor.cardiac.bps_dia = -1;
simmgr_shm->instructor.cardiac.pea = -1;
simmgr_shm->instructor.cardiac.vpc_freq = -1;
simmgr_shm->instructor.cardiac.vpc_delay = -1;
sprintf_s(simmgr_shm->instructor.cardiac.vpc, STR_SIZE, "%s", "");
sprintf_s(simmgr_shm->instructor.cardiac.vfib_amplitude, STR_SIZE, "%s", "");
simmgr_shm->instructor.cardiac.right_dorsal_pulse_strength = -1;
simmgr_shm->instructor.cardiac.right_femoral_pulse_strength = -1;
simmgr_shm->instructor.cardiac.left_dorsal_pulse_strength = -1;
simmgr_shm->instructor.cardiac.left_femoral_pulse_strength = -1;
sprintf_s(simmgr_shm->instructor.cardiac.heart_sound, STR_SIZE, "%s", "");
simmgr_shm->instructor.cardiac.heart_sound_volume = -1;
simmgr_shm->instructor.cardiac.heart_sound_mute = -1;
simmgr_shm->instructor.cardiac.ecg_indicator = -1;
simmgr_shm->instructor.cardiac.bp_cuff = -1;
simmgr_shm->instructor.cardiac.arrest = -1;
// instructor/respiration
sprintf_s(simmgr_shm->instructor.respiration.left_lung_sound, STR_SIZE, "%s", "");
sprintf_s(simmgr_shm->instructor.respiration.left_sound_in, STR_SIZE, "%s", "");
sprintf_s(simmgr_shm->instructor.respiration.left_sound_out, STR_SIZE, "%s", "");
sprintf_s(simmgr_shm->instructor.respiration.left_sound_back, STR_SIZE, "%s", "");
simmgr_shm->instructor.respiration.left_lung_sound_volume = -1;
simmgr_shm->instructor.respiration.left_lung_sound_mute = -1;
simmgr_shm->instructor.respiration.right_lung_sound_volume = -1;
simmgr_shm->instructor.respiration.right_lung_sound_mute = -1;
sprintf_s(simmgr_shm->instructor.respiration.right_lung_sound, STR_SIZE, "%s", "");
sprintf_s(simmgr_shm->instructor.respiration.right_sound_in, STR_SIZE, "%s", "");
sprintf_s(simmgr_shm->instructor.respiration.right_sound_out, STR_SIZE, "%s", "");
sprintf_s(simmgr_shm->instructor.respiration.right_sound_back, STR_SIZE, "%s", "");
simmgr_shm->instructor.respiration.inhalation_duration = -1;
simmgr_shm->instructor.respiration.exhalation_duration = -1;
simmgr_shm->instructor.respiration.rate = -1;
simmgr_shm->instructor.respiration.spo2 = -1;
simmgr_shm->instructor.respiration.etco2 = -1;
simmgr_shm->instructor.respiration.etco2_indicator = -1;
simmgr_shm->instructor.respiration.spo2_indicator = -1;
simmgr_shm->instructor.respiration.chest_movement = -1;
simmgr_shm->instructor.respiration.manual_breath = -1;
simmgr_shm->instructor.respiration.manual_count = -1;
// instructor/media
sprintf_s(simmgr_shm->instructor.media.filename, FILENAME_SIZE, "%s", "");
simmgr_shm->instructor.media.play = -1;
// instructor/telesim
simmgr_shm->instructor.telesim.enable = -1;
sprintf_s(simmgr_shm->instructor.telesim.vid[0].name, STR_SIZE, "%s", "");
simmgr_shm->instructor.telesim.vid[0].command = -1;
simmgr_shm->instructor.telesim.vid[0].param = -1;
simmgr_shm->instructor.telesim.vid[0].next = -1;
sprintf_s(simmgr_shm->instructor.telesim.vid[1].name, STR_SIZE, "%s", "");
simmgr_shm->instructor.telesim.vid[1].command = -1;
simmgr_shm->instructor.telesim.vid[1].param = -1;
simmgr_shm->instructor.telesim.vid[1].next = -1;
// instructor/general
simmgr_shm->instructor.general.temperature = -1;
simmgr_shm->instructor.general.temperature_enable = -1;
sprintf_s(simmgr_shm->instructor.general.temperature_units, sizeof(simmgr_shm->instructor.general.temperature_units), "%s", "");
// instructor/vocals
sprintf_s(simmgr_shm->instructor.vocals.filename, FILENAME_SIZE, "%s", "");
simmgr_shm->instructor.vocals.repeat = -1;
simmgr_shm->instructor.vocals.volume = -1;
simmgr_shm->instructor.vocals.play = -1;
simmgr_shm->instructor.vocals.mute = -1;
// instructor/defibrillation
simmgr_shm->instructor.defibrillation.energy = -1;
simmgr_shm->instructor.defibrillation.shock = -1;
clearAllTrends();
}
/*
* awrr_check
*
* Calculate awrr based on count of breaths, both manual and 'normal'
*
* 1 - Detect and log breath when either natural or manual start
* 2 - If no breaths are recorded in the past 20 seconds (excluding the past 2 seconds) report AWRR as zero
* 3 - Calculate AWRR based on the average time of the recorded breaths within the past 90 seconds, excluding the past 2 seconds
*
*/
#define BREATH_CALC_LIMIT 4 // Max number of recorded breaths to count in calculation
#define BREATH_LOG_LEN 128
ULONGLONG breathLog[BREATH_LOG_LEN] = { 0, };
int breathLogNext = 0;
#define BREATH_LOG_STATE_IDLE 0
#define BREATH_LOG_STATE_DETECT 1
int breathLogState = BREATH_LOG_STATE_IDLE;
ULONGLONG breathLogLastNatural = 0; // breathCount, last natural
ULONGLONG breathLogLastManual = 0; // manual_count, last manual
ULONGLONG breathLogLast = 0; // Time of last breath
#define BREATH_LOG_DELAY (2)
int breathLogDelay = 0;
#define BREATH_LOG_CHANGE_LOOPS 1
int breathLogReportLoops = 0;
void
awrr_restart(void)
{
breathLogLastNatural = 0;
breathLogLastManual = 0;
breathLogNext = 0;
breathLogLast = 0;
breathLogDelay = 0;
breathLogReportLoops = 0;
ULONGLONG now = simmgr_shm->server.msec_time; // time(NULL); // Current sec time
breathLog[breathLogNext] = now - 40000;
breathLogNext += 1;
breathLog[breathLogNext] = now - 39000;
breathLogNext += 1;
breathLog[breathLogNext] = now - 38000;
breathLogNext += 1;
}
void
awrr_check(void)
{
ULONGLONG now = simmgr_shm->server.msec_time; // time(NULL); // Current sec time
int prev;
int breaths;
ULONGLONG totalTime;
ULONGLONG lastTime;
ULONGLONG firstTime;
ULONGLONG diff;
float awRR;
int i;
int intervals;
int oldRate;
int newRate;
//int setRate;
oldRate = simmgr_shm->status.respiration.awRR;
// Breath Detect and Log
if (breathLogState == BREATH_LOG_STATE_DETECT)
{
// After detect, wait 400 msec (two calls) before another detect allowed
if (breathLogDelay++ >= BREATH_LOG_DELAY)
{
breathLogState = BREATH_LOG_STATE_IDLE;
breathLogLastNatural = simmgr_shm->status.respiration.breathCount;
breathLogLastManual = simmgr_shm->status.respiration.manual_count;
}
}
else if (breathLogState == BREATH_LOG_STATE_IDLE)
{
if (breathLogLastNatural != simmgr_shm->status.respiration.breathCount)
{
breathLogState = BREATH_LOG_STATE_DETECT;
breathLogLastNatural = simmgr_shm->status.respiration.breathCount;
breathLogLast = now;
breathLog[breathLogNext] = now;
breathLogDelay = 0;
breathLogNext += 1;
if (breathLogNext >= BREATH_LOG_LEN)
{
breathLogNext = 0;
}
}
else if (breathLogLastManual != simmgr_shm->status.respiration.manual_count)
{
breathLogState = BREATH_LOG_STATE_DETECT;
breathLogLastManual = simmgr_shm->status.respiration.manual_count;
breathLogLast = now;
breathLog[breathLogNext] = now;
breathLogDelay = 0;
breathLogNext += 1;
if (breathLogNext >= BREATH_LOG_LEN)
{
breathLogNext = 0;
}
}
}
// AWRR Calculation - Look at no more than BREATH_CALC_LIMIT breaths - Skip if no breaths within 20 seconds
lastTime = 0;
firstTime = 0;
if (breathLogNext == 0)
{
prev = (BREATH_LOG_LEN - 1);
}
else
{
prev = breathLogNext - 1;
}
breaths = 0;
intervals = 0;
lastTime = breathLog[prev];
if (lastTime <= 0) // Don't look at empty logs
{
simmgr_shm->status.respiration.awRR = 0;
}
else
{
diff = now - lastTime;
if (diff > 20000)
{
simmgr_shm->status.respiration.awRR = 0;
}
else
{
prev -= 1;
if (prev < 0)
{
prev = BREATH_LOG_LEN - 1;
}
for (i = 0; i < BREATH_CALC_LIMIT; i++)
{
diff = now - breathLog[prev];
if (diff > 47000) // Over Limit seconds since this recorded breath
{
break;
}
else
{
firstTime = breathLog[prev]; // Recorded start of the first breath
breaths += 1;
intervals += 1;
prev -= 1;
if (prev < 0)
{
prev = BREATH_LOG_LEN - 1;
}
}
}
}
if (intervals > 2)
{
totalTime = lastTime - firstTime;
if (totalTime == 0)
{
awRR = 0;
}
else
{
awRR = ((((float)intervals / (float)totalTime)) * 60000);
}
if (awRR < 0)
{
awRR = 0;
}
else if (awRR > 60)
{
awRR = 60;
}
}
else
{
awRR = 0;
}
if (breathLogReportLoops++ == BREATH_LOG_CHANGE_LOOPS)
{
breathLogReportLoops = 0;
newRate = (int)roundf(awRR);
if (oldRate != newRate)
{
// setRespirationPeriods(oldRate, newRate );
/*
setRate = simmgr_shm->status.respiration.rate;
if ((newRate <= (setRate + 3)) &&
(newRate >= (setRate - 3)))
{
simmgr_shm->status.respiration.awRR = setRate;
}
else */
{
simmgr_shm->status.respiration.awRR = newRate;
}
}
}
}
}
ULONGLONG cprLast = 0;
ULONGLONG cprRunTime = 0;
ULONGLONG cprDuration = 2000;
void
cpr_check(void)
{
ULONGLONG now = simmgr_shm->server.msec_time;
ULONGLONG cprCurrent = simmgr_shm->status.cpr.last;
if (cprCurrent != cprLast)
{
if (cprCurrent == 0)
{
simmgr_shm->status.cpr.running = 0;
cprLast = cprCurrent;
}
else
{
cprRunTime = now;
simmgr_shm->status.cpr.running = 1;
cprLast = cprCurrent;
}
}
if (simmgr_shm->status.cpr.running > 0)
{
if (simmgr_shm->status.cpr.compression > 0)
{
cprRunTime = now;
}
else if ((cprRunTime + cprDuration) < now)
{
simmgr_shm->status.cpr.running = 0;
}
}
}
ULONGLONG shockLast = 0;
ULONGLONG shockStartTime = 0;
ULONGLONG shockDuration = 2000;
void
shock_check(void)
{
ULONGLONG now = simmgr_shm->server.msec_time;
ULONGLONG shockCurrent = simmgr_shm->status.defibrillation.last;
if (shockCurrent != shockLast)
{
if (shockCurrent == 0)
{
simmgr_shm->status.defibrillation.shock = 0;
shockLast = shockCurrent;
}
else
{
shockStartTime = now;
simmgr_shm->status.defibrillation.shock = 1;
shockLast = shockCurrent;
}
}
if (simmgr_shm->status.defibrillation.shock > 0)
{
if ((shockStartTime + shockDuration) < now)
{
simmgr_shm->status.defibrillation.shock = 0;
}
// Check for a shock time that seems out of order
if (shockStartTime > now)
{
snprintf(msg_buf, sizeof(msg_buf), "shockStartTime (%llu) is greater than now (%llu)", shockStartTime, now );
lockAndComment(msg_buf);
simmgr_shm->status.defibrillation.shock = 0;
shockStartTime = 0;
}
}
}
/*
* hrcheck_handler
*
* Calculate heart rate based on count of beats, (normal, CPR and VPC)
*
* 1 - Detect and log beat when any of natural, vpc or CPR
* 2 - If no beats are recorded in the past 20 seconds (excluding the past 2 seconds) report rate as zero
* 3 - Calculate beats based on the average time of the recorded beats within the past 90 seconds, excluding the past 2 seconds
*
*/
#define HR_CALC_LIMIT 10 // Max number of recorded beats to count in calculation
#define HR_CALC_LIMIT_FAST 40 // Beats to cound with fast heart rate (over 160 BPM)
#define HR_LOG_LEN 64
ULONGLONG hrLog[HR_LOG_LEN] = { 0, };
int hrLogNext = 0;
ULONGLONG hrLogLastNatural = 0; // beatCount, last natural
ULONGLONG hrLogLastVPC = 0; // VPC count, last VPC
int hrLogDelay = 0;
#define HR_LOG_CHANGE_LOOPS 10 // hr_check is called every 5 msec. So this will cause a recalc every 50 msec
int hrLogReportLoops = 0;
void hrLogBeat(void)
{
hrLog[hrLogNext] = msec_time_update();
hrLogNext += 1;
if (hrLogNext >= HR_LOG_LEN)
{
hrLogNext = 0;
}
}
void hrcheck_handler(void ) // current system time ) // additional information
{
ULONGLONG now; // Current msec time
int prev;
int beats;
ULONGLONG totalTime;
ULONGLONG lastTime;
ULONGLONG firstTime;
ULONGLONG diff = 0;
float avg_rate = -10;
float seconds;
float minutes;
int i;
int intervals = -1;
// int oldRate;
int newRate;
int setRate = simmgr_shm->status.cardiac.rate;
int newBeat = 0;
static int reports = 0;
int calcLimit;
hrCheckCount++;
now = msec_time_update();
if (simmgr_shm->status.cpr.running)
{
hrLogLastNatural = simmgr_shm->status.cardiac.pulseCount;
hrLogLastVPC = simmgr_shm->status.cardiac.pulseCountVpc;
simmgr_shm->status.cardiac.avg_rate = 0;
firstTime = now - 30000;
for (i = 0; i < HR_LOG_LEN; i++)
{
hrLog[i] = firstTime;
}
return;
}
else if (hrLogLastNatural != simmgr_shm->status.cardiac.pulseCount)
{
hrLogLastNatural = simmgr_shm->status.cardiac.pulseCount;
newBeat = 1;
}
else if (hrLogLastVPC != simmgr_shm->status.cardiac.pulseCountVpc)
{
hrLogLastVPC = simmgr_shm->status.cardiac.pulseCountVpc;
newBeat = 1;
}
if (hrLogReportLoops++ >= HR_LOG_CHANGE_LOOPS)
{
avg_rate = -9;
hrLogReportLoops = 0;
// AVG Calculation - Look at no more than 10 beats - Skip if no beats within 20 seconds
lastTime = 0;
firstTime = 0;
beats = 0;
intervals = 0;
prev = hrLogNext - 1;
if (prev < 0)
{
prev = (HR_LOG_LEN - 1);
}
lastTime = hrLog[prev];
if (lastTime <= 0) // Don't look at empty logs
{
avg_rate = 0;
}
else
{
diff = now - lastTime;
if (diff > 20000)
{
simmgr_shm->status.cardiac.avg_rate = 4;
}
else
{
if (prev <= 0)
{
prev = (HR_LOG_LEN - 1);
}
else
{
prev -= 1;
}
if ( setRate > 160 )
{
calcLimit = HR_CALC_LIMIT_FAST;
}
else
{
calcLimit = HR_CALC_LIMIT;
}
for (i = 0; i < calcLimit; i++)
{
diff = now - hrLog[prev];
if (diff > 20000) // Over Limit seconds since this recorded beat
{
break;
}
else
{
firstTime = hrLog[prev]; // Recorded start of the first beat
beats += 1;
intervals += 1;
prev -= 1;
if (prev < 0)
{
prev = (HR_LOG_LEN - 1);
}
}
}
}
if (intervals > 0)
{
totalTime = lastTime - firstTime;
if (totalTime == 0)
{
avg_rate = -7;
}
else
{
seconds = (float)totalTime / 1000;
minutes = seconds / 60;
avg_rate = (float)intervals / minutes;
}
if (avg_rate < 0)
{
avg_rate = -6;
}
else if (avg_rate > 360)
{
avg_rate = 360;
}
}
else
{
avg_rate = -5;
}
}
if (avg_rate < 0)
{
simmgr_shm->status.cardiac.avg_rate = 0;
}
else
{
newRate = (int)round(avg_rate);
/*
if (currentIsRegular)
{
if ((newRate <= (setRate + 5)) &&
(newRate >= (setRate - 5)))
{
newRate = setRate;
}
}