Skip to content

Commit

Permalink
Merge pull request #3588 from mjearlb/master
Browse files Browse the repository at this point in the history
Update 0.02
  • Loading branch information
thyttan authored Oct 14, 2024
2 parents 9d75427 + 51684c9 commit 13d9828
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 48 deletions.
5 changes: 4 additions & 1 deletion apps/phystrax/ChangeLog
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
0.01: New App.
0.01: New App.
0.02:
- Removed HRV functionality
- Added milliseconds to the timestamps
66 changes: 20 additions & 46 deletions apps/phystrax/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ let isMeasuring = false;
let currentHR = null;
let lcdTimeout;
let logData = [];
let bpmValues = [];

function startMeasure() {
logData = [];
Expand All @@ -14,11 +13,11 @@ function startMeasure() {

setTimeout(() => {
Bangle.setHRMPower(1); // starts HRM
Bangle.on('HRM', handleHeartRate);
Bangle.on('HRM', handleHeartRate); // Use function handleHeartRate
Bangle.buzz(200, 10); // Buzz to indicate measurement start
drawScreen();
}, 500);
}
} // startMeasure

function stopMeasure() {
isMeasuring = false;
Expand All @@ -29,50 +28,26 @@ function stopMeasure() {
saveDataToCSV(); // Save data to CSV when measurement stops
Bangle.buzz(200, 10); // Buzz to indicate measurement stop
drawScreen();
}
} //stopMeasure

function handleHeartRate(hrm) {
if (isMeasuring && hrm.confidence > 90) {
// Build the timestamp
let date = new Date();
let dateStr = require("locale").date(date);
let timeStr = require("locale").time(date, 1);
let seconds = date.getSeconds();
let timestamp = `${dateStr} ${timeStr}:${seconds}`; // Concatenate date, time, and seconds
let milliseconds = date.getMilliseconds();

// Concatenate date, time, seconds, & milliseconds
let timestamp = `${dateStr} ${timeStr}:${seconds}.${milliseconds}`;
currentHR = hrm.bpm;

logData.push({ timestamp: timestamp, heartRate: currentHR });
bpmValues.push(currentHR); // Store heart rate for HRV calculation
if (bpmValues.length > 30) bpmValues.shift(); // Keep last 30 heart rate values
// Calculate and add SDNN (standard deviation of NN intervals) to the last log entry
logData[logData.length - 1].hrv = calcSDNN();
drawScreen();

}
}

function calcSDNN() {
if (bpmValues.length < 5) return 0; // No calculation if insufficient data

// Calculate differences between adjacent heart rate values
const differences = [];
for (let i = 1; i < bpmValues.length; i++) {
differences.push(Math.abs(bpmValues[i] - bpmValues[i - 1]));
}

// Calculate mean difference
const meanDifference = differences.reduce((acc, val) => acc + val, 0) / differences.length;

// Calculate squared differences from mean difference
const squaredDifferences = differences.map(diff => Math.pow(diff - meanDifference, 2));

// Calculate mean squared difference
const meanSquaredDifference = squaredDifferences.reduce((acc, val) => acc + val, 0) / squaredDifferences.length;

// Calculate SDNN (standard deviation of NN intervals)
const sdnn = Math.sqrt(meanSquaredDifference);

return sdnn;
}
drawScreen();
} // if
} // handleHeartRate

function drawScreen(message) {
g.clear(); // Clear the display
Expand Down Expand Up @@ -121,27 +96,26 @@ function drawScreen(message) {
g.drawString('No data', g.getWidth() / 2, g.getHeight() / 2 + 5);
g.setFont('6x8', 1);
g.drawString(message || 'Press button to start', g.getWidth() / 2, g.getHeight() / 2 + 30);
}
}
} // if
} // if

// Update the display
g.flip();
}

} // drawScreen
function saveDataToCSV() {
let fileName = "phystrax_hrm.csv";
let file = require("Storage").open(fileName, "a"); // Open the file for appending

// Check if the file is empty (i.e., newly created)
if (file.getLength() === 0) {
// Write the header if the file is empty
file.write("Timestamp,Heart Rate(bpm),HRV(ms)\n");
}
file.write("Timestamp,Heart Rate(bpm)\n");
} // if

// Append the data
logData.forEach(entry => {
let scaledHRV = entry.hrv * 13.61;
file.write(`${entry.timestamp},${entry.heartRate},${scaledHRV}\n`);
file.write(`${entry.timestamp},${entry.heartRate}\n`);
});

}
Expand All @@ -151,7 +125,7 @@ setWatch(function() {
startMeasure();
} else {
stopMeasure();
}
}, BTN1, { repeat: true, edge: 'rising' });
} // if
}, BTN1, { repeat: true, edge: 'rising' }); // setWatch

drawScreen();
2 changes: 1 addition & 1 deletion apps/phystrax/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "PhysTrax",
"shortName":"PhysTrax",
"icon": "app.png",
"version":"0.01",
"version":"0.02",
"description": "Tracking physiological measurements to support active learning in classrooms",
"tags": "health",
"interface": "interface.html",
Expand Down

0 comments on commit 13d9828

Please sign in to comment.