diff --git a/docs/assets/js/UI/ui.js b/docs/assets/js/UI/ui.js index 5a8175a..de9eb84 100644 --- a/docs/assets/js/UI/ui.js +++ b/docs/assets/js/UI/ui.js @@ -25,32 +25,42 @@ function drawEndUI() { function rectUI() {}; //Heart rate monitor history -var heartBeatHistory = [] +let heartBeatHistory = [] heartBeatHistory.length = constants.ui.heartRate.history_length; heartBeatHistory.fill(0); //Shift accumulation -var shiftAccum = 0; +let shiftAccum = 0; //Beat progression -var beatTimeElapsed = 0; +let beatTimeElapsed = 0; // Draw heartbeat UI function heartBeatUI(x, y, width, height){ + //Shift monitor over once a full scrolling unit is accumulated shiftAccum += constants.ui.heartRate.scroll_speed; if(shiftAccum>=1){ shiftAccum%=1; beatTimeElapsed += 0.04; + + //Remove oldest value heartBeatHistory.shift(); + + //Append new value pushNextBeatValue(); } - if(timeSinceLastBeat===0){ + //If heart is beaten, reset beat timer. + if(heartBeat){ beatTimeElapsed = 0; + heartBeat = false; } + //Backdrop rect(x+width/2,y+height/2,width,height,"black"); + + //Graph for (let index = 0; index < heartBeatHistory.length; index++) { const qrsValueAtPosition = heartBeatHistory[index]; const qrsValueAtNextPosition = heartBeatHistory[index+1]; @@ -58,28 +68,34 @@ function heartBeatUI(x, y, width, height){ } } +//Determine next value to be added to the graph function pushNextBeatValue(){ - var nextBeatValue = 0; + let nextBeatValue = 0; + //Timespan of one "square" on the EKG const squareSize = constants.ui.heartRate.square_size; + //Length of full complex const complexTime = constants.ui.heartRate.complex_width*squareSize; + //Length of PR segment of complex const prTime = constants.ui.heartRate.pr_width*squareSize; + //Length of QRS component of complex const qrsTime = constants.ui.heartRate.qrs_width*squareSize; + //Length of QT component of complex const qtTime = constants.ui.heartRate.qt_width*squareSize; - if(beatTimeElapsed<=complexTime) { + //PR Segment if (beatTimeElapsed <= prTime) { nextBeatValue = 0.5*(Math.pow((beatTimeElapsed/squareSize - (prTime/2/squareSize)), 2)) - 2; - } else if (beatTimeElapsed > prTime + squareSize && beatTimeElapsed <= prTime + squareSize + (qrsTime / 4)) { + } else if (beatTimeElapsed > prTime + squareSize && beatTimeElapsed <= prTime + squareSize + (qrsTime / 4)) { //QRS Segment pt. 1 nextBeatValue = -4 + beatTimeElapsed/squareSize; - } else if (beatTimeElapsed > prTime + squareSize + qrsTime / 4 && beatTimeElapsed <= prTime + squareSize + qrsTime / 2) { + } else if (beatTimeElapsed > prTime + squareSize + qrsTime / 4 && beatTimeElapsed <= prTime + squareSize + qrsTime / 2) { //QRS Segment pt. 2 nextBeatValue = -14 * (beatTimeElapsed/squareSize - 4.5) - 0.5; - } else if (beatTimeElapsed > prTime + squareSize + qrsTime / 2 && beatTimeElapsed <= prTime + squareSize + (3*qrsTime / 4)) { + } else if (beatTimeElapsed > prTime + squareSize + qrsTime / 2 && beatTimeElapsed <= prTime + squareSize + (3*qrsTime / 4)) { //QRS Segment pt. 3 nextBeatValue = 7 * (beatTimeElapsed/squareSize - 5) - 6.5; - } else if (beatTimeElapsed > prTime + squareSize + (3*qrsTime / 4) && beatTimeElapsed <= prTime + squareSize + qrsTime) { + } else if (beatTimeElapsed > prTime + squareSize + (3*qrsTime / 4) && beatTimeElapsed <= prTime + squareSize + qrsTime) { //QRS Segment pt. 4 nextBeatValue = 2 * (beatTimeElapsed/squareSize - 6); - } else if (beatTimeElapsed > prTime + squareSize*2 + qrsTime && beatTimeElapsed <= prTime + squareSize*2 + qrsTime + qtTime) { + } else if (beatTimeElapsed > prTime + squareSize*2 + qrsTime && beatTimeElapsed <= prTime + squareSize*2 + qrsTime + qtTime) { //PT Segment nextBeatValue = 0.5 * Math.pow((beatTimeElapsed/squareSize - (prTime + squareSize*2 + qrsTime + qtTime/2)/squareSize),2) - 3; } } diff --git a/docs/assets/js/player/lifeFunctions.js b/docs/assets/js/player/lifeFunctions.js index c0edc9b..9fdf42f 100644 --- a/docs/assets/js/player/lifeFunctions.js +++ b/docs/assets/js/player/lifeFunctions.js @@ -1,9 +1,9 @@ -var breath = 180; -var fullBreathTimer = 0; -var heartRate = 60; +let breath = 180; +let fullBreathTimer = 0; +let heartRate = 60; -var timeSinceLastBeat = 0; +let heartBeat = false; function updateLife() { @@ -16,8 +16,6 @@ function updateLife() { if(keyPress[k.x]) { heartbeat(); - } else { - timeSinceLastBeat++; } }; @@ -38,6 +36,6 @@ function breathe() { }; function heartbeat() { - timeSinceLastBeat = 0; + heartBeat = true; }; \ No newline at end of file