2021-04-10 17:06:40 +00:00
|
|
|
E.setFlags({pretokenise:1});
|
2021-03-27 09:45:03 +00:00
|
|
|
const GraphXZero = 40;
|
|
|
|
const GraphYZero = 200;
|
|
|
|
const GraphY100 = 80;
|
|
|
|
|
|
|
|
const GraphMarkerOffset = 5;
|
|
|
|
const MaxValueCount = 164;
|
|
|
|
const GraphXMax = GraphXZero + MaxValueCount;
|
|
|
|
|
2020-03-09 12:24:38 +00:00
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
|
|
|
|
|
|
|
var settings = require("Storage").readJSON("heart.json",1)||{};
|
|
|
|
|
2021-03-27 09:45:03 +00:00
|
|
|
var globalSettings = require('Storage').readJSON('setting.json', true) || {timezone: 0};
|
|
|
|
require('DateExt').locale({
|
|
|
|
str: "0D.0M. 0h:0m",
|
|
|
|
offset: [
|
|
|
|
globalSettings.timezone * 60,
|
|
|
|
globalSettings.timezone * 60
|
|
|
|
]
|
|
|
|
});
|
2021-04-10 17:06:40 +00:00
|
|
|
globalSettings = undefined;
|
2021-03-27 09:45:03 +00:00
|
|
|
|
2020-03-09 12:24:38 +00:00
|
|
|
function getFileNbr(n) {
|
|
|
|
return ".heart"+n.toString(36);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSettings() {
|
|
|
|
require("Storage").write("heart.json", settings);
|
|
|
|
if (WIDGETS["heart"])
|
|
|
|
WIDGETS["heart"].reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
function showMainMenu() {
|
|
|
|
const mainMenu = {
|
|
|
|
'': { 'title': 'Heart Recorder' },
|
|
|
|
'RECORD': {
|
|
|
|
value: !!settings.isRecording,
|
|
|
|
format: v=>v?"On":"Off",
|
|
|
|
onchange: v => {
|
|
|
|
settings.isRecording = v;
|
|
|
|
updateSettings();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'File Number': {
|
|
|
|
value: settings.fileNbr|0,
|
|
|
|
min: 0,
|
|
|
|
max: 35,
|
|
|
|
step: 1,
|
|
|
|
onchange: v => {
|
|
|
|
settings.isRecording = false;
|
|
|
|
settings.fileNbr = v;
|
|
|
|
updateSettings();
|
|
|
|
}
|
|
|
|
},
|
2021-04-10 17:06:40 +00:00
|
|
|
'View Records': createRecordMenu.bind(null, viewRecord.bind()),
|
|
|
|
'Graph Records': createRecordMenu.bind(null, graphRecord.bind()),
|
2020-03-09 12:24:38 +00:00
|
|
|
'< Back': ()=>{load();}
|
|
|
|
};
|
|
|
|
return E.showMenu(mainMenu);
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:06:40 +00:00
|
|
|
function createRecordMenu(func) {
|
2020-03-09 12:24:38 +00:00
|
|
|
const menu = {
|
|
|
|
'': { 'title': 'Heart Records' }
|
|
|
|
};
|
|
|
|
var found = false;
|
|
|
|
for (var n=0;n<36;n++) {
|
2021-04-10 17:06:40 +00:00
|
|
|
var line = require("Storage").open(getFileNbr(n),"r").readLine();
|
|
|
|
if (line!==undefined) {
|
|
|
|
menu["#"+n+" "+Date(line.split(",")[0]*1000).as().str] = func.bind(null,n);
|
2020-03-09 12:24:38 +00:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
menu["No Records Found"] = function(){};
|
2021-04-14 13:02:07 +00:00
|
|
|
menu['< Back'] = ()=>{showMainMenu()};
|
2020-03-09 12:24:38 +00:00
|
|
|
return E.showMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
function viewRecord(n) {
|
|
|
|
const menu = {
|
|
|
|
'': { 'title': 'Heart Record '+n }
|
|
|
|
};
|
|
|
|
var heartTime;
|
|
|
|
var f = require("Storage").open(getFileNbr(n),"r");
|
|
|
|
var l = f.readLine();
|
2021-04-10 17:06:40 +00:00
|
|
|
var limits = {"min": 2000, "max": 0, "avg": 0, "cnt": 0};
|
|
|
|
var value = 0;
|
|
|
|
if (l!==undefined)
|
|
|
|
heartTime = new Date(l.split(",")[0]*1000);
|
|
|
|
console.log("heart: parsing records");
|
2020-03-09 12:24:38 +00:00
|
|
|
while (l!==undefined) {
|
2021-04-10 17:06:40 +00:00
|
|
|
if (parseInt(l.split(',')[2]) > 70) {
|
|
|
|
limits.cnt++;
|
|
|
|
value = parseInt(l.split(',')[1]);
|
|
|
|
if (value < limits.min) {
|
|
|
|
limits.min = value;
|
|
|
|
} else if (value > limits.max) {
|
|
|
|
limits.max = value;
|
|
|
|
}
|
|
|
|
limits.avg += value;
|
|
|
|
}
|
2020-03-09 12:24:38 +00:00
|
|
|
l = f.readLine();
|
|
|
|
}
|
2021-04-10 17:06:40 +00:00
|
|
|
l = undefined;
|
|
|
|
value = undefined;
|
|
|
|
console.log("heart: finished parsing");
|
|
|
|
limits.avg = limits.avg / limits.cnt;
|
2020-03-09 12:24:38 +00:00
|
|
|
if (heartTime)
|
|
|
|
menu[" "+heartTime.toString().substr(4,17)] = function(){};
|
2021-04-10 17:06:40 +00:00
|
|
|
menu[limits.cnt+" records"] = function(){};
|
|
|
|
menu["Min: " + limits.min] = function(){};
|
|
|
|
menu["Max: " + limits.max] = function(){};
|
|
|
|
menu["Avg: " + Math.round(limits.avg)] = function(){};
|
|
|
|
menu["Erase"] = function() {
|
2020-03-09 12:24:38 +00:00
|
|
|
E.showPrompt("Delete Record?").then(function(v) {
|
|
|
|
if (v) {
|
|
|
|
settings.isRecording = false;
|
|
|
|
updateSettings();
|
2021-04-10 17:06:40 +00:00
|
|
|
require("Storage").open(getFileNbr(n),"r").erase();
|
|
|
|
E.showMenu();
|
|
|
|
load();
|
2020-03-09 12:24:38 +00:00
|
|
|
} else
|
|
|
|
viewRecord(n);
|
|
|
|
});
|
|
|
|
};
|
2021-04-10 17:06:40 +00:00
|
|
|
menu['< Back'] = function() {
|
|
|
|
limits = undefined;
|
|
|
|
E.showMenu();
|
|
|
|
createRecordMenu(viewRecord);
|
2021-03-27 09:45:03 +00:00
|
|
|
};
|
2021-04-10 17:06:40 +00:00
|
|
|
limits = undefined;
|
2021-03-27 09:45:03 +00:00
|
|
|
return E.showMenu(menu);
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:06:40 +00:00
|
|
|
function renderChart() {
|
|
|
|
// Home for Btn2
|
2021-03-27 09:45:03 +00:00
|
|
|
g.setColor(1, 1, 1);
|
|
|
|
g.drawLine(220, 118, 227, 110);
|
|
|
|
g.drawLine(227, 110, 234, 118);
|
|
|
|
|
|
|
|
g.drawPoly([222,117,222,125,232,125,232,117], false);
|
|
|
|
g.drawRect(226,120,229,125);
|
|
|
|
|
2021-04-10 17:06:40 +00:00
|
|
|
// Chart
|
2021-03-27 09:45:03 +00:00
|
|
|
g.setColor(1, 1, 0);
|
|
|
|
g.drawLine(GraphXZero, GraphYZero + GraphMarkerOffset, GraphXZero, GraphY100);
|
|
|
|
|
|
|
|
g.setFontAlign(1, -1, 0);
|
|
|
|
g.drawString("150", 35, GraphY100 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, GraphY100, GraphXZero, GraphY100);
|
|
|
|
|
|
|
|
g.drawString("125", 35, GraphYZero - 110 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 150, GraphXZero, 150);
|
|
|
|
|
|
|
|
g.drawString("100", 35, GraphYZero - 100 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 150, GraphXZero, 150);
|
|
|
|
|
|
|
|
g.drawString("90", 35, GraphYZero - 90 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 150, GraphXZero, 150);
|
|
|
|
|
|
|
|
g.drawString("80", 35, GraphYZero - 70 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 150, GraphXZero, 150);
|
|
|
|
|
|
|
|
g.drawString("70", 35, GraphYZero - 50 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 150, GraphXZero, 150);
|
|
|
|
|
|
|
|
g.drawString("60", 35, GraphYZero - 30 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 150, GraphXZero, 150);
|
|
|
|
|
|
|
|
g.drawString("50", 35, GraphYZero - 20 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 150, GraphXZero, 150);
|
|
|
|
|
|
|
|
g.drawString("40", 35, GraphYZero - 10 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 150, GraphXZero, 150);
|
|
|
|
|
|
|
|
g.drawString("30", 35, GraphYZero - GraphMarkerOffset);
|
|
|
|
|
|
|
|
g.setColor(1, 1, 1);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, GraphYZero, GraphXMax + GraphMarkerOffset, GraphYZero);
|
|
|
|
|
2021-04-10 17:06:40 +00:00
|
|
|
console.log("heart: Finished drawing chart");
|
2021-03-27 09:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// as drawing starts at 30 HRM decreasing measrure by 30
|
|
|
|
// recalculate for range 110-150 as only 20 pixels are available
|
|
|
|
function getY(measure) {
|
2021-04-10 17:06:40 +00:00
|
|
|
var positionY = GraphYZero - measure + 30;
|
2021-03-27 09:45:03 +00:00
|
|
|
if (100 < measure < 150) {
|
|
|
|
positionY = GraphYZero - ( 100 + Math.round((measure - 100)/2) ) + 30;
|
|
|
|
} else if (60 < measrure < 100) {
|
|
|
|
positionY = GraphYZero - ( 30 + Math.round((measure - 30)/2) ) + 30;
|
|
|
|
}
|
|
|
|
if (positionY > GraphYZero) {
|
|
|
|
positionY = GraphYZero;
|
|
|
|
}
|
|
|
|
if (positionY < GraphY100) {
|
|
|
|
positionY = GraphY100;
|
|
|
|
}
|
|
|
|
return positionY;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop() {
|
|
|
|
E.showMenu();
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
function graphRecord(n) {
|
|
|
|
E.showMenu({'': 'Heart Record '+n});
|
|
|
|
E.showMessage(
|
|
|
|
"Loading Data ...\n\nMay take a while,\nwill vibrate\nwhen done.",
|
|
|
|
'Heart Record '+n
|
|
|
|
);
|
|
|
|
g.setFont("Vector", 10);
|
|
|
|
|
|
|
|
var lineCount = 0;
|
|
|
|
var startLine = 1;
|
|
|
|
var f = require("Storage").open(getFileNbr(n),"r");
|
|
|
|
var line = f.readLine();
|
2021-04-10 17:06:40 +00:00
|
|
|
console.log("heart: Counting lines");
|
|
|
|
|
2021-03-27 09:45:03 +00:00
|
|
|
while (line !== undefined) {
|
|
|
|
lineCount++;
|
|
|
|
line = f.readLine();
|
|
|
|
}
|
2021-04-10 17:06:40 +00:00
|
|
|
|
|
|
|
console.log(`heart: Line count: ${lineCount}`);
|
|
|
|
if (lineCount > MaxValueCount)
|
2021-03-27 09:45:03 +00:00
|
|
|
startLine = lineCount - MaxValueCount;
|
2021-04-10 17:06:40 +00:00
|
|
|
f = undefined;
|
|
|
|
line = undefined;
|
|
|
|
lineCount = undefined;
|
|
|
|
console.log(`heart: start: ${startLine}`);
|
2021-03-27 09:45:03 +00:00
|
|
|
|
|
|
|
f = require("Storage").open(getFileNbr(n),"r");
|
|
|
|
line = f.readLine();
|
2021-04-10 17:06:40 +00:00
|
|
|
|
|
|
|
var times = Uint32Array(2);
|
|
|
|
var tempCount = 0;
|
|
|
|
var positionX = GraphXZero;
|
|
|
|
var positionY = GraphYZero;
|
|
|
|
|
2021-03-27 09:45:03 +00:00
|
|
|
while (line !== undefined) {
|
|
|
|
currentLine = line;
|
|
|
|
line = f.readLine();
|
|
|
|
tempCount++;
|
|
|
|
if (tempCount == startLine) {
|
|
|
|
g.clear();
|
|
|
|
renderChart();
|
|
|
|
} else if (tempCount > startLine) {
|
|
|
|
positionX++;
|
|
|
|
if (parseInt(currentLine.split(",")[2]) >= 70) {
|
|
|
|
g.setColor(1, 1, 1);
|
|
|
|
oldPositionY = positionY;
|
|
|
|
positionY = getY(parseInt(currentLine.split(",")[1]));
|
|
|
|
if (times[0] === undefined) {
|
|
|
|
times[0] = parseInt(currentLine.split(",")[0]);
|
|
|
|
}
|
|
|
|
if (tempCount == startLine + 1) {
|
|
|
|
g.setPixel(positionX, positionY);
|
|
|
|
} else {
|
|
|
|
g.drawLine(positionX - 1, oldPositionY, positionX, positionY);
|
|
|
|
times[1] = parseInt(currentLine.split(",")[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 17:06:40 +00:00
|
|
|
g.flip();
|
2021-03-27 09:45:03 +00:00
|
|
|
|
2021-04-10 17:06:40 +00:00
|
|
|
g.setColor(1, 1, 0).setFont("Vector", 10);
|
|
|
|
console.log('heart: start: ' + times[0]);
|
|
|
|
console.log('heart: end: ' + times[1]);
|
2021-03-27 09:45:03 +00:00
|
|
|
if (times[0] !== undefined) {
|
|
|
|
g.setFontAlign(-1, -1, 0);
|
2021-04-10 17:06:40 +00:00
|
|
|
g.drawString(Date(times[0]*1000).local().as("0h:0m").str, 15, GraphYZero + 12);
|
2021-03-27 09:45:03 +00:00
|
|
|
}
|
2021-04-10 17:06:40 +00:00
|
|
|
|
2021-03-27 09:45:03 +00:00
|
|
|
if (times[1] !== undefined) {
|
|
|
|
g.setFontAlign(1, -1, 0);
|
2021-04-10 17:06:40 +00:00
|
|
|
g.drawString(Date(times[1]*1000).local().as().str, GraphXMax, GraphYZero + 12);
|
2021-03-27 09:45:03 +00:00
|
|
|
}
|
2021-04-10 17:06:40 +00:00
|
|
|
|
|
|
|
console.log("heart: Finished rendering data");
|
2021-03-27 09:45:03 +00:00
|
|
|
Bangle.buzz(200, 0.3);
|
|
|
|
setWatch(stop, BTN2, {edge:"falling", debounce:50, repeat:false});
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:24:38 +00:00
|
|
|
showMainMenu();
|
2021-03-27 09:45:03 +00:00
|
|
|
|
|
|
|
// vim: et ts=2 sw=2
|