Heartrate monitor optimizations & comments

This commit is contained in:
Silas Bartha 2020-04-18 05:15:44 -04:00
parent 44a42ba4c8
commit 138f27a691
2 changed files with 32 additions and 18 deletions

View File

@ -25,32 +25,42 @@ function drawEndUI() {
function rectUI() {}; function rectUI() {};
//Heart rate monitor history //Heart rate monitor history
var heartBeatHistory = [] let heartBeatHistory = []
heartBeatHistory.length = constants.ui.heartRate.history_length; heartBeatHistory.length = constants.ui.heartRate.history_length;
heartBeatHistory.fill(0); heartBeatHistory.fill(0);
//Shift accumulation //Shift accumulation
var shiftAccum = 0; let shiftAccum = 0;
//Beat progression //Beat progression
var beatTimeElapsed = 0; let beatTimeElapsed = 0;
// Draw heartbeat UI // Draw heartbeat UI
function heartBeatUI(x, y, width, height){ function heartBeatUI(x, y, width, height){
//Shift monitor over once a full scrolling unit is accumulated
shiftAccum += constants.ui.heartRate.scroll_speed; shiftAccum += constants.ui.heartRate.scroll_speed;
if(shiftAccum>=1){ if(shiftAccum>=1){
shiftAccum%=1; shiftAccum%=1;
beatTimeElapsed += 0.04; beatTimeElapsed += 0.04;
//Remove oldest value
heartBeatHistory.shift(); heartBeatHistory.shift();
//Append new value
pushNextBeatValue(); pushNextBeatValue();
} }
if(timeSinceLastBeat===0){ //If heart is beaten, reset beat timer.
if(heartBeat){
beatTimeElapsed = 0; beatTimeElapsed = 0;
heartBeat = false;
} }
//Backdrop
rect(x+width/2,y+height/2,width,height,"black"); rect(x+width/2,y+height/2,width,height,"black");
//Graph
for (let index = 0; index < heartBeatHistory.length; index++) { for (let index = 0; index < heartBeatHistory.length; index++) {
const qrsValueAtPosition = heartBeatHistory[index]; const qrsValueAtPosition = heartBeatHistory[index];
const qrsValueAtNextPosition = heartBeatHistory[index+1]; 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(){ function pushNextBeatValue(){
var nextBeatValue = 0; let nextBeatValue = 0;
//Timespan of one "square" on the EKG
const squareSize = constants.ui.heartRate.square_size; const squareSize = constants.ui.heartRate.square_size;
//Length of full complex
const complexTime = constants.ui.heartRate.complex_width*squareSize; const complexTime = constants.ui.heartRate.complex_width*squareSize;
//Length of PR segment of complex
const prTime = constants.ui.heartRate.pr_width*squareSize; const prTime = constants.ui.heartRate.pr_width*squareSize;
//Length of QRS component of complex
const qrsTime = constants.ui.heartRate.qrs_width*squareSize; const qrsTime = constants.ui.heartRate.qrs_width*squareSize;
//Length of QT component of complex
const qtTime = constants.ui.heartRate.qt_width*squareSize; const qtTime = constants.ui.heartRate.qt_width*squareSize;
if(beatTimeElapsed<=complexTime) { if(beatTimeElapsed<=complexTime) {
//PR Segment
if (beatTimeElapsed <= prTime) { if (beatTimeElapsed <= prTime) {
nextBeatValue = 0.5*(Math.pow((beatTimeElapsed/squareSize - (prTime/2/squareSize)), 2)) - 2; 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; 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; 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; 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); 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; nextBeatValue = 0.5 * Math.pow((beatTimeElapsed/squareSize - (prTime + squareSize*2 + qrsTime + qtTime/2)/squareSize),2) - 3;
} }
} }

View File

@ -1,9 +1,9 @@
var breath = 180; let breath = 180;
var fullBreathTimer = 0; let fullBreathTimer = 0;
var heartRate = 60; let heartRate = 60;
var timeSinceLastBeat = 0; let heartBeat = false;
function updateLife() { function updateLife() {
@ -16,8 +16,6 @@ function updateLife() {
if(keyPress[k.x]) { if(keyPress[k.x]) {
heartbeat(); heartbeat();
} else {
timeSinceLastBeat++;
} }
}; };
@ -38,6 +36,6 @@ function breathe() {
}; };
function heartbeat() { function heartbeat() {
timeSinceLastBeat = 0; heartBeat = true;
}; };