hrmaccevents - Show status info on display

pull/1983/head
Martin Boonk 2022-06-19 15:36:05 +02:00
parent 7b6cbdfdb6
commit 32967fa548
1 changed files with 58 additions and 18 deletions

View File

@ -14,25 +14,26 @@
<p id="result"></p>
<script>
function createCode(){
//modes: 0 print, 1 BT, 2 File
//modes: 1 BT, 2 File
return "var method=" + (document.getElementById("chkLocal").checked ? 2 : 1) + ";\n" + String.raw`
var accData=[];
var maxSize=0;
var filename="log.csv";
var running = true;
var gotHRMraw = false;
var gotBTHRM = false;
var gotHRM = false;
var gotAcc = false;
var events = -1;
var hrmRaw,hrmPulse,bthrmPulse
function gotAll(){
return running && gotBTHRM && gotHRM && gotHRMraw && gotAcc;
return gotBTHRM && gotHRM && gotHRMraw && gotAcc;
}
Bangle.setHRMPower(1);
if (Bangle.setBTHRMPower){
print("Use BTHRM");
Bangle.setBTHRMPower(1);
} else {
gotBTHRM = true;
@ -44,11 +45,9 @@ function createCode(){
var f = require('Storage').open(filename,"w");
f.erase();
f = require('Storage').open(filename,"a");
write = function(str){f.write(str);};
write = function(str){f.write(str);events++;};
} else if (method == 1){
write = function(str){Bluetooth.print("DATA: " + str);};
} else {
write=print;
write = function(str){Bluetooth.print("DATA: " + str);events++;};
}
write("Time,Acc_x,Acc_y,Acc_z,HRM_b,HRM_c,HRM_r,HRM_f,PPG_r,PPG_o,BTHRM\n");
@ -56,6 +55,7 @@ function createCode(){
function writeAcc(e){
gotAcc = true;
acc = e;
e.date=Date.now();
accData.push(e);
accData.splice(0, accData.length - maxSize);
@ -63,18 +63,21 @@ function createCode(){
function writeAccDirect(e){
gotAcc = true;
acc = e;
if (!gotAll()) return;
write(Date.now()+","+e.x+","+e.y+","+e.z+",,,,,,,,\n");
}
function writeBTHRM(e){
gotBTHRM = true;
bthrmPulse = e.bpm;
if (!gotAll()) return;
write(Date.now()+",,,,,,,,,,"+e.bpm+"\n");
}
function writeHRM(e){
gotHRM = true;
hrmPulse = e.bpm;
if (!gotAll()) return;
while(accData.length > 0){
var c = accData.shift();
@ -85,6 +88,7 @@ function createCode(){
function writeHRMraw(e){
gotHRMraw = true;
hrmRaw = e.raw;
if (!gotAll()) return;
write(Date.now()+",,,,,,"+e.raw+","+e.filt+","+e.vcPPG+","+e.vcPPGoffs+",\n");
}
@ -99,18 +103,54 @@ function createCode(){
Bangle.on("BTHRM", writeBTHRM);
g.clear();
g.setColor(1,0,0);
g.fillRect(0,0,g.getWidth(),g.getHeight());
function drawStatusText(name, y){
g.setFont12x20();
g.setColor(g.theme.fg);
g.drawString(name, 24, y * 22 + 2);
}
function drawStatus(isOk, y, value){
g.setFont12x20();
if (isOk) g.setColor(0,1,0); else g.setColor(1,0,0);
g.fillRect(0,y * 22, 20, y * 22 + 20);
g.setColor(g.theme.bg);
let x = 120
g.fillRect(x,y*22,g.getWidth(),y*22+20);
g.setColor(g.theme.fg);
if (value) g.drawString(value, x, y * 22 + 2);
}
function updateStatus(){
let h = 1;
drawStatus(gotAcc, h++);
drawStatus(gotBTHRM, h++, bthrmPulse); bthrmPulse = null;
drawStatus(gotHRM, h++, hrmPulse); hrmPulse = null;
drawStatus(gotHRMraw, h++, hrmRaw); hrmRaw = null;
drawStatus(events>0, h++, Math.max(events,0));
if (method == 2){
let free = require('Storage').getFree();
drawStatus(free>0.25*process.env.STORAGE, h++, Math.floor(free/1024) + "K");
}
}
var intervalId = -1;
g.setFont12x20();
g.setColor(g.theme.fg);
g.drawString("Target " + (method==2?"log.csv":"Bluetooth"), 0, 2);
let h = 1;
drawStatusText("Acc", h++);
drawStatusText("BTHRM", h++);
drawStatusText("HRM", h++);
drawStatusText("HRM_r", h++);
drawStatusText("Events", h++);
if (method == 2) drawStatusText("Storage", h++);
updateStatus();
intervalId = setInterval(()=>{
print("Checking... Acc:" + gotAcc + " BTHRM:" + gotBTHRM + " HRM:" + gotHRM + " HRM raw:" + gotHRMraw);
if (gotAll()){
g.setColor(0,1,0);
g.fillRect(0,0,g.getWidth(),g.getHeight());
clearInterval(intervalId);
}
updateStatus();
}, 1000);
if (Bangle.setBTHRMPower){
@ -161,10 +201,10 @@ document.getElementById("btnStop").addEventListener("click", function() {
});
document.getElementById("btnConnect").addEventListener("click", function() {
localStorage.setItem("data", "");
lineCount=-1;
if (connection) {
stop();
document.getElementById("result").innerText="0";
lineCount=-1;
}
Puck.connect(function(c) {
if (!c) {