bthrm - Change from timeouts to timestamps for resetting values

master
Martin Boonk 2022-12-02 23:50:14 +01:00
parent 85c2f92e0a
commit ddbbef2bf8
1 changed files with 11 additions and 44 deletions

View File

@ -1,7 +1,3 @@
var intervalInt;
var intervalBt;
var intervalAgg;
const BPM_FONT_SIZE="19%"; const BPM_FONT_SIZE="19%";
const VALUE_TIMEOUT=3000; const VALUE_TIMEOUT=3000;
@ -19,7 +15,6 @@ var Layout = require("Layout");
function border(l,c) { function border(l,c) {
g.setColor(c).drawLine(l.x+l.w*0.05, l.y-4, l.x+l.w*0.95, l.y-4); g.setColor(c).drawLine(l.x+l.w*0.05, l.y-4, l.x+l.w*0.95, l.y-4);
c++;
} }
function getRow(id, text, additionalInfo){ function getRow(id, text, additionalInfo){
@ -68,15 +63,7 @@ var layout = new Layout( {
var int,agg,bt; var int,agg,bt;
var firstEvent = true; var firstEvent = true;
var drawTimeout;
function draw(){ function draw(){
if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = setTimeout(function() {
drawTimeout = undefined;
draw();
}, 1000);
if (!(int || agg || bt)) return; if (!(int || agg || bt)) return;
if (firstEvent) { if (firstEvent) {
@ -84,7 +71,9 @@ function draw(){
firstEvent = false; firstEvent = false;
} }
if (int){ let now = Date.now();
if (int && int.time > (now - VALUE_TIMEOUT)){
layout.int.label = int.bpm; layout.int.label = int.bpm;
if (!isNaN(int.confidence)) layout.intConfidence.label = int.confidence; if (!isNaN(int.confidence)) layout.intConfidence.label = int.confidence;
} else { } else {
@ -92,7 +81,7 @@ function draw(){
layout.intConfidence.label = "--"; layout.intConfidence.label = "--";
} }
if (agg){ if (agg && agg.time > (now - VALUE_TIMEOUT)){
layout.agg.label = agg.bpm; layout.agg.label = agg.bpm;
if (!isNaN(agg.confidence)) layout.aggConfidence.label = agg.confidence; if (!isNaN(agg.confidence)) layout.aggConfidence.label = agg.confidence;
if (agg.src) layout.aggSource.label = agg.src; if (agg.src) layout.aggSource.label = agg.src;
@ -102,7 +91,7 @@ function draw(){
layout.aggSource.label = "--"; layout.aggSource.label = "--";
} }
if (bt) { if (bt && bt.time > (now - VALUE_TIMEOUT)) {
layout.bt.label = bt.bpm; layout.bt.label = bt.bpm;
if (bt.battery) layout.btBattery.label = bt.battery; if (bt.battery) layout.btBattery.label = bt.battery;
if (bt.rr) layout.btRR.label = bt.rr.join(","); if (bt.rr) layout.btRR.label = bt.rr.join(",");
@ -131,9 +120,6 @@ function draw(){
} }
} }
var firstEventBt = true;
var firstEventInt = true;
// This can get called for the boot code to show what's happening // This can get called for the boot code to show what's happening
function showStatusInfo(txt) { function showStatusInfo(txt) {
@ -145,37 +131,18 @@ function showStatusInfo(txt) {
function onBtHrm(e) { function onBtHrm(e) {
bt = e; bt = e;
if (e.bpm === 0){ bt.time = Date.now();
Bangle.buzz(100,0.2);
}
if (intervalBt){
clearInterval(intervalBt);
}
intervalBt = setInterval(()=>{
bt = undefined;
}, VALUE_TIMEOUT);
} }
function onInt(e) { function onInt(e) {
int = e; int = e;
if (intervalInt){ int.time = Date.now();
clearInterval(intervalInt);
}
intervalInt = setInterval(()=>{
int = undefined;
}, VALUE_TIMEOUT);
} }
function onAgg(e) { function onAgg(e) {
agg = e; agg = e;
if (intervalAgg){ agg.time = Date.now();
clearInterval(intervalAgg);
} }
intervalAgg = setInterval(()=>{
agg = undefined;
}, VALUE_TIMEOUT);
}
var settings = require('Storage').readJSON("bthrm.json", true) || {}; var settings = require('Storage').readJSON("bthrm.json", true) || {};
@ -195,7 +162,7 @@ Bangle.drawWidgets();
if (Bangle.setBTHRMPower){ if (Bangle.setBTHRMPower){
g.reset().setFont("6x8",2).setFontAlign(0,0); g.reset().setFont("6x8",2).setFontAlign(0,0);
g.drawString("Please wait...",g.getWidth()/2,g.getHeight()/2); g.drawString("Please wait...",g.getWidth()/2,g.getHeight()/2);
draw(); setInterval(draw, 1000);
} else { } else {
g.reset().setFont("6x8",2).setFontAlign(0,0); g.reset().setFont("6x8",2).setFontAlign(0,0);
g.drawString("BTHRM disabled",g.getWidth()/2,g.getHeight()/2); g.drawString("BTHRM disabled",g.getWidth()/2,g.getHeight()/2);