-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascriptForWork.js
2053 lines (1776 loc) · 65.4 KB
/
javascriptForWork.js
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
/* ABSTRACTION
Data Description:
- 500 Hz EKG data
- so should data be drawn at 500 fps? can device handle that?
Two parts:
I. Data processor - cycles through EKG data
II. Painter - paints the canvas
- painter should run at browser framerate (max for device) for smoothness
- data processor should run as fast as possible, but
width = 1000 pixels
60 bpm = 1 beat / 1000 msec
R-R should take one second to paint
- # data points per R-R = 500 Hz * 1 second = 500 data points
so 500 data points should be processed every second
paint will paint at Max Browser Framerate. So data processing should be 500/Max Browse Frame Rate and then paint
PHYSIOLOGY POINTS
- in typical conduction scenarios, SA node is NOT affected by ectopic impulses, including those coming from the ventricle.
- it can however sometimes be reset by ectopic impulses (non-compensatory pause)
- for simplicity, might just ignore this and do only compensatory pauses (consistent P clock throughout)
- so, any PVCs should NOT disturb the timing of the SA node
- this doesn't mean we will always see a P wave though (the SA can fire, but not cause atrial contraction if the surrounding tissue is refractory)
- so after a PVC/PJC/PAC, maybe no P wave, but the next P will be right on time with original P-P interval (compensatory pause)
- program relevance: there needs to be an absolute sinus interval/clock (P-P interval) that is not affected by ectopy (PVCs, pacing, etc.)
*/
// -------------------------- GLOBAL DEFINITIONS -----------------------------
var pacedBeatFlag = false;
var ventRefractoryTimer = 9999
var atrialRefractoryTimer = 9999
var afibPSenseTimer = 9999
// Wenkebach
var baseWenkPR = 150
var currentWenkPR = baseWenkPR
var wenkDegree = 3 // # of conducted beats before drop
var wenkCount = 0 // current count of conducted beats
var wenkPRincreaseAmount = 100 // by how much should PR interval increase?
var intermittentAVblock = false;
var AVBlockRandom = 1 // random factor of AV block
var lastBlocked = false;
var ratioBlockedPs = .20 // 20% of P's will be blocked
// a flutter vars
var baselineFlutterConductionRatio = 4; // default 4:1 conduction ratio
var currentFlutterConductionRatio = baselineFlutterConductionRatio; // this one can be varied for irregularity
var flutterAtrialRate = 300 // most flutters are around 300 atrial rate
var flutterAtrialMS = 1/(flutterAtrialRate/60000)
var flutterConductionIrregularity = 1.5 // 0=regular, up to 2=twice as many frequent blocks
var flutterVentRate = flutterAtrialRate/baselineFlutterConductionRatio
var flutterVentMS = 1/(flutterVentRate/60000)
var flutterAtrialTimer = 10000
// geminy vars
var geminyRatio = 1 // 1=bigeminy (one normal, one PVC), 2=trigeminy (one normal, two PVC)
var geminyCount = 0
var PVCtimer = -1
var drawNormalQRS = false
var PPtimer=0
//pacing intervals
var lowerRateLimitTimer // the rate of the pacemaker in ms
var maxTrackingRate = 150 // the highest rate pacer will pace V in response to sensed A's
var AVITimer = 200 // the set interval between a sensed/paced A and associated V (artificial PR interval)
var VAItimer = 1000 - 200 // the timer after sensed/paced V and next due A
var AAtimer = 1000
var VVtimer = 1000
var AVtimer = 200 //used by DOO logic (may be able to combine with AVITimer)
var noiseFlag = false;
var pacingFeedback=true;
var VRP // prevent ventricular sensing of immediate post-V noise (OPTIONAL)
var PVARP // prevent atrial sensing of immediate post-V noise (OPTIONAL)
var upperRateLimit //prevent atrial tracking of atrial tachyarrythmias (OPTIONAL)
var rNEW=0
var rOLD=0
var AVExtension=0
var PRInterval;
// a fib
var random = (1-((Math.random()-0.5)/1))
var aFibMS = 1000
var atrialAfibRate = 600;
var conductedAtimer=Math.round((1/(atrialAfibRate/60000))/2)*2
var afibRandomPtime = 1
//
var timeSincePGlobal=1;
var timeSinceVGlobal=1;
var timeSinceSensedPGlobal=1;
var timeSinceSensedVGlobal=1;
var HRadjustedPR=120;
var ventHeartRate = 40;
var atrialHeartRate = 80;
var conductionIntact = true;
var avgProcessSpeed = 2;
var goalMS=1000;
var dataCount=0;
var dataClock=0;
var testClock=0;
var setHR = 60;
var CHB = false;
var dataFeedLength=500;
var teleCanvas = document.getElementById("tele");
var teleCtx = teleCanvas.getContext("2d");
var HRCanvas = document.getElementById("HRLayer");
var HRctx = HRCanvas.getContext("2d");
var HRchanged = false;
var paceSpike=false;
var px=0;
h = tele.height
var processingSpeed = 570;
var realtimeProcessSpeed = 2;
var adjustRatio = 1;
var lastBrowserTime = Date.now();
dataHertz = 500, // in Hz (data points per second)
canvasBaseline = tele.height/2+25,
py = canvasBaseline;
var y = dataHertz/144;
var histPTimes=[0];
var histVentTimes = [0]; // each time implies a beat
var aPacerSensitivity = document.getElementById("aSensitivityBox").value; // default
var vPacerSensitivity = document.getElementById("vSensitivityBox").value; // default
var aPacerOutput = document.getElementById("aOutputBox").value;
var vPacerOutput = document.getElementById("vOutputBox").value;
var vCaptureThreshold = 5; // default V capture threshold (mA)
var aCaptureThreshold = 2; // default A capture threshold (mA)
// ------------------------- onload () ---------------------------------------
onload();
function onload() {
// --------------------- LOCAL DEFINITIONS ---------------------------------
dataFeed.length = 1000;
dataFeed.fill(0,0,1000);
//document.getElementById("tele").width = window.innerWidth;
//document.getElementById("HRLayer").width = window.innerWidth;
document.getElementById("tele").width = document.getElementById("tele").parentElement.offsetWidth
document.getElementById("HRLayer").width = document.getElementById("tele").parentElement.offsetWidth
PRInterval = parseInt(document.getElementById("PRbox").value)
var w = tele.width,
l = 0,
sec = 0,
avgRefresh = 1,
avgFPS = 144,
animateRatio = dataHertz / avgFPS,
dataVoltage = 10, // in mV
compressHfactor = 20,
opx = 0,
speed = .2, // speed of the cursor across the screen; affects "squeeze" of waves
isPainted = true;
timestamp = performance.now();
paintCount = 1;
(opy = py), (scanBarWidth = 1), (PVCflag = 0);
k = 0;
teleCtx.strokeStyle = "#00bd00";
teleCtx.lineWidth = 3;
// framelimiter code
var fpsInterval, startTime, now, then, elapsed;
// initialize the timer variables and start the animation
var p = 0;
var i = 0;
var j = 0;
var jold = 0;
// initiate animation (is looped)
function startAnimating() {
teleCtx.beginPath();
for (let i = 0; i < 1200 && isPainted; i++) {
loop();
}
}
setInterval(updateHertz, 1000);
startAnimating();
var lold = 0;
function updateHertz() {
sec++;
avgFPS = document.getElementById("demoTEXT").innerText = l - lold;
document.getElementById("demoTEXT3").innerText = processingSpeed = j - jold;
lold = l;
jold = j;
}
function parseData() {
py = -parseInt(dataFeed.shift() * 1000) / compressHfactor + canvasBaseline;
if (dataFeed.length < 1000) {
dataFeed.push(0);
if (noiseFlag)
{noiseFunction()}
}
j++;
i++;
dataCount++;
dataClock = dataClock + (1/dataHertz)*1000; // clock for the project
if (i%100==0) // every 100 data points (200 ms), calc realtime Hz
{
randomizeThresholds();
if (vPacerSensitivity < vOversenseThreshold) // is pacer oversensing?
{
sensedVentTimes.push(dataClock)
}
if (aPacerSensitivity < aOversenseThreshold)
{
sensedPTimes.push(dataClock)
}
avgProcessSpeed = calcRealtimeProcessingSpeed();
if (avgProcessSpeed<2)
{
y-=1;
}
if (avgProcessSpeed>2)
{
y+=1;
}
}
tickTimers() // advance all timers
masterRhythmFunction()
if (pacerOn)
{
pacingFunction();
}
// i=i+parseInt(animateRatio)-1;
if (dataFeed.length==0)
{
py=-(0*1000)/8+canvasBaseline;
i = 0;
}
}
function calcRealtimeProcessingSpeed() {
let browserTimeElapsed = Date.now()-lastBrowserTime;
lastBrowserTime=Date.now();
return browserTimeElapsed/100;
}
function loop() {
//ctx.canvas.width = window.innerWidth; // working on screen resizing
l++; //count # of times through loop
teleCtx.beginPath();
//for (let z = 0; z < dataHertz / avgFPS; z++)
// let y = dataHertz/avgFPS
for (let z = 0; z < y; z++)
{
parseData();
px += speed; // horizontal pixels per data point
//if (paceSpike)
//{drawPacingSpike();}
teleCtx.moveTo(opx, opy);
teleCtx.lineTo(px, py);
opx = px;
opy = py;
teleCtx.clearRect(px+10, 0, scanBarWidth, h);
if (opx > teleCanvas.width || opx > document.getElementById("canvasesdiv").offsetWidth) {
px = opx = 0; //-speed;
teleCtx.clearRect(px, 0, 10, h);
}
}
isPainted = false;
requestAnimationFrame(paint);
document.getElementById("demoTEXT2").innerText = i;
}
function paint() {
teleCtx.stroke();
isPainted = true;
paintCount++;
startAnimating();
}
document.getElementById("demoTEXT").onkeydown = function () {
i = 0;
PVCflag = 1;
};
//window.addEventListener('resize', function(event){
// do stuff here
//ctx.save();
// w = document.getElementById("demo").width = ctx.width = window.innerWidth;
//ctx.restore();
//});
input = document.getElementById('avgRateBox');
input.onchange = function(){setHR=input.value;HRchanged=true;};
pacerate = document.getElementById('pacingRate');
pacerate.onchange = function(){pacingRate=pacerate.value};
/*
document.getElementById("rhythmList").onchange = function() {
const value = document.getElementById("rhythmList").value;
if (value) {
currentRhythm=value;
} else {
currentRhythm='NSR';
}
}
document.getElementById('capturing').onchange = function ()
{
if (document.getElementById('capturing').checked)
{
captureOverride=true;
}
else
{
captureOverride=false;
}
}
*/
}
// --------------------- end onLoad() ------------------------------
function drawPWave(morphOnly,width,height,invert) { // morphOnly='morphOnly' or not, width:2x,3x etc. (integer only), height:1.5x, 2.8xm etc, (floatOK), invert:true or false
if (typeof width == 'undefined') {width = 0} // 0 means normal width
if (typeof height == 'undefined') {height = 1} // 1 means normal height
if (typeof invert == 'undefined') {invert = 0} // 1 means normal height
if (atrialRefractoryTimer > 100) // when atrium is depolarized, should be completely refractory for 100 ms (need to adjust?)
{
atrialRefractoryTimer = 0 // make atrium refractory
// RECORD KEEPING
if (morphOnly!='morphOnly') // not morphology only, so normal behaviour
{
histPTimes.push(dataClock); // add to absolute record of all P activity
if (histPTimes.length>10)
{
histPTimes.shift();
}
if (aPacerSensitivity <= aUndersenseThreshold || pacedBeatFlag) // not undersensed OR if a known paced beat
{
sensedPTimes.push(dataClock); // add to record of all sensed P activity
}
}
// MORPHOLOGY PORTION of draw function
i = 0;
var tempArray = widenWave(shortP80.slice(),width) // width 0 = normal, width 1 = double
var ogLength = tempArray.length
for (let j = 0; j < ogLength; j++)
{
if (invert==false)
{
dataFeed[j] = dataFeed[j]+tempArray.shift()*height; // add the voltages at each point (in case beats overlap)
}
if (invert==true)
{
dataFeed[j] = dataFeed[j]-tempArray.shift()*height; // add the voltages at each point (in case beats overlap)
}
}
}
}
function QRSClick() {
i = 0;
var tempArray=cleanQRS.slice();
for (let j = 0; j < cleanQRS.length; j++)
{
dataFeed[j] = dataFeed[j]+tempArray.shift(); // add the voltages at each point (in case beats overlap)
}
}
function TwaveClick() {
i = 0;
var tempArray=cleanT.slice();
for (let j = 0; j < cleanT.length; j++)
{
dataFeed[j] = dataFeed[j]+tempArray.shift(); // add the voltages at each point (in case beats overlap)
}
}
var avgVentRate=0;
var oldTime=performance.now();
var histVentBeats=0;
var sensedVentTimes = []; // each time implies a beat
var vAmplitude = 0.660; // default amplitude of R-wave
var sensedPTimes = []; // each time implies a beat
var aAmplitude = 0.290; // default amplitude of P-wave
var aOversenseThreshold = 1.5 // threhsold below which pacer will oversense (e.g. T wave)
var aUndersenseThreshold = 10 // threshold above which pacer will undersense (e.g. won't see P wave)
// The sensing threshold is the least sensitive mV setting at which the temporary pacemaker can detect a heartbeat
var vOversenseThreshold = 1.5 // threhsold below which pacer will oversense (e.g. T wave)
var vUndersenseThreshold = 10 // threshold above which pacer will undersense (e.g. won't see R wave)
function drawQRST(width, invertT, invertQRS) { // width: 0=normal, 1=double, 2=triple, etc.) invertT: 0=upright, 1=invert) invertQRS: 0=upright, 1=invert
if (typeof width == 'undefined')
{width=0;}
if (typeof invertT == 'undefined')
{invertT=0;}
if (typeof invertQRS == 'undefined')
{invertQRS=0;}
i = 0;
j = 0;
if (ventRefractoryTimer > 100) // when vent is depolarized, should be completely refractory for 100 ms (need to adjust?)
{
ventRefractoryTimer = 0 // make ventricle refractory
// mark event according to data clock
histVentTimes.push(dataClock);
if (histVentTimes.length>10)
{
histVentTimes.shift();
}
if (vPacerSensitivity <= vUndersenseThreshold || pacedBeatFlag) // not undersensing OR known paced beat-> mark real V
{
sensedVentTimes.push(dataClock); //mark sensed V
}
var tempArray=widenWave(cleanQRS,width).slice(); // widen the QRS with a factor of 1 (double the width)
/*
else
{
var tempArray=cleanQRS.slice();
}
*/
var ogLength = tempArray.length
for (j = 0; j < ogLength; j++)
{
if (invertQRS==0)
{
dataFeed[j] = dataFeed[j]+tempArray.shift(); // add the voltages at each point (in case beats overlap)
}
else if (invertQRS>0)
{
dataFeed[j] = dataFeed[j]-tempArray.shift(); // invert QRS
}
}
tempArray=cleanT.slice();
for (let i = 0; i < cleanT.length; i++)
{
if (invertT==0)
{
dataFeed[j] = dataFeed[j]+tempArray.shift(); // add the voltages at each point (in case beats overlap)
}
else if (invertT>0)
{
dataFeed[j] = dataFeed[j]-tempArray.shift(); // invert T wave if widened QRS
}
j++;
}
}
}
var currentRhythm = "NSR";
var drawQRS=false;
var PRtimer=-1;
function masterRhythmFunction()
{
/*
if (document.getElementById("CHBbox").checked==true)
{
//currentRhythm='CHB'
CHB=true;
document.getElementById("CHBstuff").hidden = false;
}
if (document.getElementById("CHBbox").checked==false)
{
//currentRhythm='NSR'
CHB=false;
document.getElementById("CHBstuff").hidden = true;
}
*/
if(dataClock%100 == 0)
{
currentRhythm = document.getElementById("rhythmList").value
PRInterval = parseInt(document.getElementById("PRbox").value) // native PR interval
setHR = document.getElementById("avgRateBox").value;
HRadjustedPR = PRInterval - 0.5*setHR + 30; // PR should decrease with increasing heart rates
if (HRadjustedPR<5){HRadjustedPR=5}
goalMS = (1/setHR)*60000
adjustRatio = realtimeProcessSpeed/((1/dataHertz)*1000);
}
if (currentRhythm=='flatline')
{
if (!CHB && timeSinceLastP() >= PRInterval && drawQRS)
{
drawQRST();
drawQRS=false;
}
}
if (currentRhythm=='NSR') // with this version, will incorporate a PR timer so that a V follows every P (paced or not) (unless CHB)
{
//let timeSinceV = timeSinceLastV();
let timeSinceP = timeSinceLastP();
let timeSinceV = timeSinceLastV();
if (!CHB) // not CHB, so normal behaviour
{
//if (timeSinceP >= goalMS && timeSinceV >= goalMS - HRadjustedPR) // this working 9/27
if (timeSinceP >= goalMS && timeSinceV >= goalMS - HRadjustedPR && timeSinceLastV() > 200 )
{
drawPWave();
timeSinceP=timeSinceLastP();
if (!CHB)
{
drawQRS = true; // flag that QRS should come follow sinus P
}
}
testClock = dataClock;
timeSinceP=timeSinceLastP()
timeSinceV=timeSinceLastV()
if (timeSinceP==0 || timeSinceP == 2)
{
PRtimer=0; // start P-R timer (QRS should follow a P wave, whether P is intrinsic or paced)
drawQRS=true;
}
if (PRtimer>=0)
{
PRtimer+=2;
}
// if (drawQRS && timeSinceLastV()>=goalMS && timeSinceLastP()>=HRadjustedPR && !CHB) // QRS should respond to any P's after a PR interval (unless CHB)
if (drawQRS && PRtimer >= HRadjustedPR && !CHB && timeSinceLastV() > 150) // !!! THIS PART CAUSING DOUBLE V-PACING -- built in minimum V-refractory 150 ms
{
drawQRST();
drawQRS=false;
PRtimer=-1; // stop PRtimer
}
else if (drawQRS && PRtimer >= HRadjustedPR && !CHB) // if above never runs, then clear QRS and PR timer
{
drawQRS=false;
PRtimer=-1;
}
}
/*
if (CHB) // complete heart block code
{
let timeSinceP = timeSinceLastP()
let timeSinceV = timeSinceLastV()
ventHeartRate = document.getElementById("ventRateBox").value;
atrialHeartRate = document.getElementById("atrialRateBox").value;
let goalVentMs = 1/(ventHeartRate/60000)
let goalAtrialMs = 1/(atrialHeartRate/60000)
if (timeSinceLastP() >= 1/(atrialHeartRate/60000))
{
drawPWave();
}
if (timeSinceLastV() >= 1/(ventHeartRate/60000))
{
drawQRST(1,1); //wide QRS due to idioventricular escape rhythm
}
}
*/
}
if (currentRhythm == "junctional")
{
// narrow QRS
let timeSinceV = timeSinceLastV()
ventHeartRate = document.getElementById("ventRateBox").value;
let goalVentMs = 1/(ventHeartRate/60000)
if (timeSinceLastV() >= 1/(ventHeartRate/60000))
{
drawQRST();
}
// if paced P appears, then QRS should follow (no block)
if (timeSinceLastP()==0 || timeSinceLastP() == 2)
{
PRtimer=0; //start timer
drawQRS=true;
}
if (PRtimer>=0)
{
PRtimer+=2;
}
if (drawQRS && PRtimer >= HRadjustedPR && !CHB && timeSinceLastSensedV() > 150) // !!! THIS PART CAUSING DOUBLE V-PACING -- built in minimum V-refractory 150 ms
{
drawQRST();
drawQRS=false;
PRtimer=-1; // stop PRtimer
}
else if (drawQRS && PRtimer >= HRadjustedPR && !CHB) // if above never runs, then clear QRS and PR timer
{
drawQRS=false;
PRtimer=-1;
}
}
if (currentRhythm == "ventEscape") // escape rhythm means sinus node is too slow or has stopped, but conduction still present (e.g. paced P should make a qrs)
{
// wide QRS (ventricular origin)
let timeSinceV = timeSinceLastV()
ventHeartRate = document.getElementById("avgRateBox").value;
let goalVentMs = 1/(ventHeartRate/60000)
if (timeSinceLastV() >= goalVentMs)
{
// *** insert wide QRS code here ***
if (ventHeartRate<120)
{
drawQRST(1,1); // draw wide QRS, inverted T
}
else (ventHeartRate >= 120) // v-tach
{
drawQRST(1,0); // draw wide QRS, upright T (looks more like true V-tach)
}
}
// if paced P appears, then *NARROW* QRS should follow (no block, should follow normal conduction)
if (timeSinceLastP()==0 || timeSinceLastP() == 2)
{
PRtimer=0; //start timer
drawQRS=true;
}
if (PRtimer>=0)
{
PRtimer+=2;
}
if (drawQRS && PRtimer >= HRadjustedPR && !CHB && timeSinceLastSensedV() > 150) // !!! THIS PART CAUSING DOUBLE V-PACING -- built in minimum V-refractory 150 ms
{
// *** insert *NARROW* QRS code here ***
drawQRST();
drawQRS=false;
PRtimer=-1; // stop PRtimer
}
else if (drawQRS && PRtimer >= HRadjustedPR && !CHB) // if above never runs, then clear QRS and PR timer
{
drawQRS=false;
PRtimer=-1;
}
}
// ----------- ATRIAL FIBRILLATION ---------------
if (currentRhythm == "aFib")
{
aCaptureThreshold=10000 // pacer should never be able to capture atrium in atrial fib
let timeSinceV = timeSinceLastV()
var afibVarianceFactor = 1; // the smaller the more varied
var morphoAtrialAfibRate = 800;
var ratioSensedPs = .50 // 50% of P's will be sensed by pacemaker
let afibPsensing = true;
goalMS = Math.round((1/(setHR/60000))/2)*2
if (timeSinceLastV() >= aFibMS)
{
drawQRST(); // this works, but maybe should allow for physiologic occasional V's capturing or being sensed
histPTimes.push(dataClock-AVInterval); // let a P "conduct" and be sensed
if (histPTimes.length>10)
{
histPTimes.shift();
}
random = (1-((Math.random()-0.5)/afibVarianceFactor))
aFibMS = (goalMS)*random
}
if (dataClock%goalMS==0)
{
random = (1-((Math.random()-0.5)/afibVarianceFactor))
aFibMS = (goalMS)*random
}
// throw in some sensed A's for the pacemaker to fuck with
if (afibPsensing && aPacerSensitivity < aOversenseThreshold*2) // raise the pacer oversense threshold while in afib
{
let senseMS = goalMS*ratioSensedPs*random
if (afibPSenseTimer > senseMS)
{
sensedPTimes.push(dataClock)
afibPSenseTimer = 0;
}
afibPSenseTimer += 2;
}
// draw p-wave at varying rate with varying amplitude
let morphoMSbaseline = 1/(morphoAtrialAfibRate/60000)
if (dataClock%parseInt((morphoMSbaseline/afibRandomPtime))==0)
{
drawPWave('morphOnly',1,(Math.random()*1.10))
afibRandomPtime = Math.random()/2+1
let testMS = morphoMSbaseline/afibRandomPtime
let testRate = (1/testMS)*60000
let blank = 0;
}
// noiseFlag=true;
// document.getElementById('noise').checked=true;
}
if (currentRhythm != "aFlutter") {document.getElementById("flutterStuff").hidden=true;}
if (currentRhythm == "aFlutter")
{
// show flutter options on page
document.getElementById("flutterStuff").hidden=false;
flutterAtrialRate = document.getElementById("flutterAtrialRate").value;
baselineFlutterConductionRatio = parseInt(document.getElementById("flutterConductionRatio").value);
let flutterVariableAV = document.getElementById("flutterVariableAV").checked;
let timeSinceV = timeSinceLastV()
//goalMS = Math.round((1/(setHR/60000))/2)*2
flutterAtrialMS = 1/(flutterAtrialRate/60000)
flutterVentMS=flutterAtrialMS*currentFlutterConductionRatio
if (timeSinceLastV() >= flutterVentMS)
{
drawQRST(); // this works, but maybe should allow for physiologic occasional V's capturing or being sensed
//random = (1-((Math.random()-0.5)/afibVarianceFactor))
let random = 0;
if (flutterVariableAV)
{
random = Math.random()*flutterConductionIrregularity
}
currentFlutterConductionRatio = baselineFlutterConductionRatio + Math.round(random)
}
if (flutterAtrialTimer >= flutterAtrialMS)
{
drawPWave('no',1,1,false) // not morphonly, width, height, no invert
flutterAtrialTimer=0;
}
flutterAtrialTimer +=2;
}
// ------------------ WENKEBACH ------------------
// Four key elements of Wenkebach rhythm
// (if I want to be perfectly accurate, I should incororate this stuff)
// [ ] incorporate the principles below to make more realistic (pretty insignificant though)
//
// 1. Progressive lengthening of each successive PR interval.
// 2. The pause produced by the non-conducted P wave is equal to the increment between the last PR interval (preceding the pause) and the first PR interval following the pause (shortest) subtracted from twice the PP interval.
// = 2(PP) - (PR4-PR1)
// 3. The RR interval between the first and second conducted beats is the largest and between the last conducted beats, the shortest.
// 4. There is progressive shortening of the RR intervals.
if (currentRhythm!='2ndtypeI' || currentRhythm!='2ndtypeII')
{
document.getElementById("wenckStuff").hidden=true
//document.getElementById("CHBbox").disabled=false
}
if (currentRhythm=='2ndtypeI') // Wenckebach/Wenkebach
{
// show wenck options
document.getElementById("wenckStuff").hidden=false
wenkDegree = parseInt(document.getElementById("wenckeDegreeBox").value - 1) // if wenkDegree is 2, then there are 2 P waves per 1 QRS. if wenkDegree is 5, then there are 5 Ps per QRS
// update AV block label
document.getElementById("AVblockLabel").innerText = (wenkDegree+1).toString() + ":" + (wenkDegree).toString()
// turn off CHB options
//document.getElementById("CHBbox").disabled=true
//document.getElementById("CHBbox").checked=false
//let timeSinceV = timeSinceLastV();
let timeSinceP = timeSinceLastP();
let timeSinceV = timeSinceLastV();
if (wenkCount == 0)
{
currentWenkPR = PRInterval
}
if (timeSinceP >= goalMS && timeSinceV >= goalMS - currentWenkPR && timeSinceLastV() > 200 )
{
if (wenkCount < wenkDegree)
{
currentWenkPR+=wenkPRincreaseAmount
}
drawPWave();
timeSinceP=timeSinceLastP();
if (wenkCount < wenkDegree)
{
drawQRS = true; // flag that QRS should come follow sinus P
wenkCount++
}
else
{
//drawQRS=false;
currentWenkPR = PRInterval
wenkCount=0
}
}
testClock = dataClock;
timeSinceP=timeSinceLastP()
timeSinceV=timeSinceLastV()
if (timeSinceP==0 || timeSinceP == 2)
{
PRtimer=0; // start P-R timer (QRS should follow a P wave, whether P is intrinsic or paced)
drawQRS=true;
}
if (PRtimer>=0)
{
PRtimer+=2;
}
// if (drawQRS && timeSinceLastV()>=goalMS && timeSinceLastP()>=HRadjustedPR && !CHB) // QRS should respond to any P's after a PR interval (unless CHB)
if (drawQRS && PRtimer >= currentWenkPR && timeSinceLastV() > 150 && wenkCount < wenkDegree) // !!! THIS PART CAUSING DOUBLE V-PACING -- built in minimum V-refractory 150 ms
{
drawQRST();
drawQRS=false;
PRtimer=-1; // stop PRtimer
}
else if (drawQRS && PRtimer >= currentWenkPR) // if above never runs, then clear QRS and PR timer
{
drawQRS=false;
PRtimer=-1;
}
}
if (currentRhythm=='highDegreeBlock') // high degree AV block (fixed ratios )
// fixed ratio blocks (anything 3:1 or higher is "high degree")
// 2:1 could possibly be a Wenkbach
// rated as P:QRS degree block (e.g. 2:1, 3:1, 4:1, etc.)
// initially presents as just occasional dropped QRS (intermittent 2:1)
{
// show wenck options
document.getElementById("wenckStuff").hidden=false
wenkDegree = parseInt(document.getElementById("wenckeDegreeBox").value)
// update AV block label
document.getElementById("AVblockLabel").innerText = (wenkDegree).toString() + ":" + 1
// turn off CHB options
//document.getElementById("CHBbox").disabled=true
//document.getElementById("CHBbox").checked=false
//let timeSinceV = timeSinceLastV();
let timeSinceP = timeSinceLastP();
let timeSinceV = timeSinceLastV();
wenkDegree = parseInt(document.getElementById("wenckeDegreeBox").value)
// intermittent block
if (wenkCount == 0)
{
currentWenkPR = PRInterval
}
if (timeSinceP >= goalMS && timeSinceV >= goalMS - currentWenkPR && timeSinceLastV() > 200 )
{
drawPWave();
timeSinceP=timeSinceLastP();
if (wenkCount < wenkDegree)
{
drawQRS = true; // flag that QRS should come follow sinus P
wenkCount++
}
else
{
//drawQRS=false;
currentWenkPR = PRInterval
wenkCount=0
}
}
testClock = dataClock;
timeSinceP=timeSinceLastP()
timeSinceV=timeSinceLastV()
if (timeSinceP==0 || timeSinceP == 2)
{
PRtimer=0; // start P-R timer (QRS should follow a P wave, whether P is intrinsic or paced)
drawQRS=true;
}
if (PRtimer>=0)
{
PRtimer+=2;
}
// if (drawQRS && timeSinceLastV()>=goalMS && timeSinceLastP()>=HRadjustedPR && !CHB) // QRS should respond to any P's after a PR interval (unless CHB)
if (drawQRS && PRtimer >= currentWenkPR && timeSinceLastV() > 150 && wenkCount == wenkDegree) // !!! THIS PART CAUSING DOUBLE V-PACING -- built in minimum V-refractory 150 ms
{
drawQRST();
drawQRS=false;
PRtimer=-1; // stop PRtimer
wenkCount=0
}
else if (drawQRS && PRtimer >= currentWenkPR) // if above never runs, then clear QRS and PR timer
{
drawQRS=false;
PRtimer=-1;
}
}
if (currentRhythm!='intermAVBlock')
{
document.getElementById("intermAVBlockStuff").hidden=true
}
if (currentRhythm=='intermAVBlock') // Mobitz II
// 1 or more sequential P waves do not conduct
// rated as P:QRS degree block (e.g. 2:1, 3:1, 4:1, etc.)
// initially presents as just occasional dropped QRS (intermittent 2:1)
{
// show intermittent block options
document.getElementById("intermAVBlockStuff").hidden=false
ratioBlockedPs = parseFloat(document.getElementById("blockFreqBox").value)
document.getElementById("blockedRatioLabel").innerText = (ratioBlockedPs*100).toString() + "% blocked P's"
// turn off CHB options
//document.getElementById("CHBbox").disabled=true
//document.getElementById("CHBbox").checked=false
//let timeSinceV = timeSinceLastV();
let timeSinceP = timeSinceLastP();
let timeSinceV = timeSinceLastV();
//if (timeSinceP >= goalMS && timeSinceV >= goalMS - HRadjustedPR) // this working 9/27
if (timeSinceP >= goalMS && timeSinceV >= goalMS - HRadjustedPR && timeSinceLastV() > 200 )
{
drawPWave();
timeSinceP=timeSinceLastP();
drawQRS = true; // flag that QRS should come follow sinus P
if (lastBlocked) // if last P was blocked
{
AVBlockRandom = 1 // force next P to conduct
lastBlocked = false
}
else // if it wasn't blocked, keep generating new
{
AVBlockRandom = Math.random();
}
}
testClock = dataClock;
timeSinceP=timeSinceLastP()
timeSinceV=timeSinceLastV()
if (timeSinceP==0 || timeSinceP == 2)
{
PRtimer=0; // start P-R timer (QRS should follow a P wave, whether P is intrinsic or paced)
drawQRS=true;
}
if (PRtimer>=0)
{
PRtimer+=2;
}
// if (drawQRS && timeSinceLastV()>=goalMS && timeSinceLastP()>=HRadjustedPR && !CHB) // QRS should respond to any P's after a PR interval (unless CHB)
if (drawQRS && PRtimer >= HRadjustedPR && !CHB && timeSinceLastV() > 150) // !!! THIS PART CAUSING DOUBLE V-PACING -- built in minimum V-refractory 150 ms
{
if (AVBlockRandom > ratioBlockedPs) // 20% of time, drop a QRS
{
drawQRST();
drawQRS=false;
lastBlocked=false
}
else
{
lastBlocked=true
}
PRtimer=-1; // stop PRtimer
}