var correlator = function(cspan) {
var CMIN = 7; var CMAX = 37; var NSLOT = 128;
var buffer = new Array(NSLOT); var next=0;
cspan = cspan || 1;
return {
put: (v)=>{ buffer[next] = v; next = (next+1)%NSLOT; },
get bpm(){
var minCorr=0x7FFFFFFF;
var span=0;
for (var c=CMIN; c<CMAX; c++) {
var s = 0; var a = (next-c*cspan) % NSLOT; var b = next;
for (var i=0; i<(NSLOT-CMAX);i++){
var d = buffer[b]-buffer[a];
b = (b+1)%NSLOT; a = (a+1)%NSLOT;
s += d*d;
}
if (s<minCorr) {minCorr = s; span = c;}
}
return span == 0 ? 0:(60000/(span*40));
}
};
};
function hrm(){
var corr = correlator();
var iv;
var hrm = {
read:()=>{
var v = wOS.hrs.read_hrs();
corr.put(v.hrs);
},
start: (record)=>{
if (iv) return;
wOS.hrs.enable();
iv = setInterval(hrm.read, 40);
},
stop: ()=>{
if (!iv) return;
iv = clearInterval(iv);
wOS.hrs.disable();
},
get bpm() { return corr.bpm(); },
};
return hrm;
}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Here's my "driver" at this point, if anyone wants to give a shot at understanding what's missing.
And here's the module I use it with