Skip to content

Instantly share code, notes, and snippets.

@deltam
Last active November 27, 2020 10:42
Show Gist options
  • Save deltam/aafa35a0597fe1204fab442a81ce37d6 to your computer and use it in GitHub Desktop.
Save deltam/aafa35a0597fe1204fab442a81ce37d6 to your computer and use it in GitHub Desktop.
// record heart beat while sleeping
var MEASURE_PERIOD = 15*60*1000; // 15 min
var SAMPLE_TIME = 3*60*1000; // 3 min
var DISPOSE_TIME = 30*1000; // 30 sec
var sumBPM;
var sumCount;
var csvFile;
function init() {
d = new Date();
loc = require('locale');
locTime = `${loc.date(d)}_${loc.time(d)}`.trim().split(/[\/:]/).join('');
csvName = `sleeprec.bpm.${locTime}.csv`;
csvFile = require("Storage").open(csvName, 'w');
csvFile.write(`time, bpm, ${locTime}\n`);
Bangle.on('HRM', function(hrm) {
console.log(`hrm ${hrm.bpm} , ${hrm.confidence}`);
if (hrm.confidence < 100)
return;
sumBPM += hrm.bpm;
sumCount++;
});
E.showMessage('Waiting...', 'sleeprec');
}
function record() {
avg = Math.floor(sumBPM/sumCount + 0.5);
// console.log(`sum ${sumBPM}, cnt ${sumCount}, avg ${avg}`);
t = require('locale').time(new Date()).trim();
csvFile.write(`${t}, ${avg}\n`);
}
init();
setInterval(() => {
Bangle.setHRMPower(1);
setTimeout(()=> { // dispose first N sec bpm
E.showMessage('Recoding...', 'sleeprec');
sumBPM = 0;
sumCount = 0;
setTimeout(()=>{
record();
Bangle.setHRMPower(0);
E.showMessage('Waiting...', 'sleeprec');
}, SAMPLE_TIME);
}, DISPOSE_TIME);
}, MEASURE_PERIOD);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment