reduced memory usage of heart-app

Signed-off-by: Andrwe Lord Weber <github@andrwe.org>
master
Andrwe Lord Weber 2021-04-10 19:06:40 +02:00
parent 4d0aa393bc
commit a338d5e20e
1 changed files with 69 additions and 74 deletions

View File

@ -1,3 +1,4 @@
E.setFlags({pretokenise:1});
const GraphXZero = 40; const GraphXZero = 40;
const GraphYZero = 200; const GraphYZero = 200;
const GraphY100 = 80; const GraphY100 = 80;
@ -19,6 +20,7 @@ require('DateExt').locale({
globalSettings.timezone * 60 globalSettings.timezone * 60
] ]
}); });
globalSettings = undefined;
function getFileNbr(n) { function getFileNbr(n) {
return ".heart"+n.toString(36); return ".heart"+n.toString(36);
@ -52,22 +54,22 @@ function showMainMenu() {
updateSettings(); updateSettings();
} }
}, },
'View Records': ()=>{viewRecords()}, 'View Records': createRecordMenu.bind(null, viewRecord.bind()),
'Graph Records': ()=>{graphRecords()}, 'Graph Records': createRecordMenu.bind(null, graphRecord.bind()),
'< Back': ()=>{load();} '< Back': ()=>{load();}
}; };
return E.showMenu(mainMenu); return E.showMenu(mainMenu);
} }
function viewRecords() { function createRecordMenu(func) {
const menu = { const menu = {
'': { 'title': 'Heart Records' } '': { 'title': 'Heart Records' }
}; };
var found = false; var found = false;
for (var n=0;n<36;n++) { for (var n=0;n<36;n++) {
var f = require("Storage").open(getFileNbr(n),"r"); var line = require("Storage").open(getFileNbr(n),"r").readLine();
if (f.readLine()!==undefined) { if (line!==undefined) {
menu["Record "+n] = viewRecord.bind(null,n); menu["#"+n+" "+Date(line.split(",")[0]*1000).as().str] = func.bind(null,n);
found = true; found = true;
} }
} }
@ -81,72 +83,68 @@ function viewRecord(n) {
const menu = { const menu = {
'': { 'title': 'Heart Record '+n } '': { 'title': 'Heart Record '+n }
}; };
var heartCount = 0;
var heartTime; var heartTime;
var f = require("Storage").open(getFileNbr(n),"r"); var f = require("Storage").open(getFileNbr(n),"r");
var l = f.readLine(); var l = f.readLine();
if (l!==undefined) { var limits = {"min": 2000, "max": 0, "avg": 0, "cnt": 0};
var c = l.split(","); var value = 0;
heartTime = new Date(c[0]*1000); if (l!==undefined)
} heartTime = new Date(l.split(",")[0]*1000);
console.log("heart: parsing records");
while (l!==undefined) { while (l!==undefined) {
heartCount++; if (parseInt(l.split(',')[2]) > 70) {
// TODO: min/max/average of heart rate? 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;
}
l = f.readLine(); l = f.readLine();
} }
l = undefined;
value = undefined;
console.log("heart: finished parsing");
limits.avg = limits.avg / limits.cnt;
if (heartTime) if (heartTime)
menu[" "+heartTime.toString().substr(4,17)] = function(){}; menu[" "+heartTime.toString().substr(4,17)] = function(){};
menu[heartCount+" records"] = function(){}; menu[limits.cnt+" records"] = function(){};
// TODO: option to draw it? Just scan through, project using min/max menu["Min: " + limits.min] = function(){};
menu['Erase'] = function() { menu["Max: " + limits.max] = function(){};
menu["Avg: " + Math.round(limits.avg)] = function(){};
menu["Erase"] = function() {
E.showPrompt("Delete Record?").then(function(v) { E.showPrompt("Delete Record?").then(function(v) {
if (v) { if (v) {
settings.isRecording = false; settings.isRecording = false;
updateSettings(); updateSettings();
var f = require("Storage").open(getFileNbr(n),"r"); require("Storage").open(getFileNbr(n),"r").erase();
f.erase(); E.showMenu();
viewRecords(); load();
} else } else
viewRecord(n); viewRecord(n);
}); });
}; };
menu['< Back'] = ()=>{viewRecords()}; menu['< Back'] = function() {
print(menu); limits = undefined;
return E.showMenu(menu); E.showMenu();
} createRecordMenu(viewRecord);
function graphRecords() {
const menu = {
'': { 'title': 'Heart Records' }
}; };
var found = false; limits = undefined;
for (var n=0;n<36;n++) {
var f = require("Storage").open(getFileNbr(n),"r");
var line = f.readLine();
if (line!==undefined) {
menu["#"+n+" "+Date(line.split(",")[0]*1000).as().str] = graphRecord.bind(null,n);
found = true;
}
}
if (!found)
menu["No Records Found"] = function(){};
menu['< Back'] = ()=>{showMainMenu()};
return E.showMenu(menu); return E.showMenu(menu);
} }
// based on batchart function renderChart() {
function renderHomeIcon() { // Home for Btn2
//Home for Btn2
g.setColor(1, 1, 1); g.setColor(1, 1, 1);
g.drawLine(220, 118, 227, 110); g.drawLine(220, 118, 227, 110);
g.drawLine(227, 110, 234, 118); g.drawLine(227, 110, 234, 118);
g.drawPoly([222,117,222,125,232,125,232,117], false); g.drawPoly([222,117,222,125,232,125,232,117], false);
g.drawRect(226,120,229,125); g.drawRect(226,120,229,125);
}
function renderChart() { // Chart
// Left Y axis (Battery)
g.setColor(1, 1, 0); g.setColor(1, 1, 0);
g.drawLine(GraphXZero, GraphYZero + GraphMarkerOffset, GraphXZero, GraphY100); g.drawLine(GraphXZero, GraphYZero + GraphMarkerOffset, GraphXZero, GraphY100);
@ -183,27 +181,23 @@ function renderChart() {
g.setColor(1, 1, 1); g.setColor(1, 1, 1);
g.drawLine(GraphXZero - GraphMarkerOffset, GraphYZero, GraphXMax + GraphMarkerOffset, GraphYZero); g.drawLine(GraphXZero - GraphMarkerOffset, GraphYZero, GraphXMax + GraphMarkerOffset, GraphYZero);
console.log("Finished drawing chart"); console.log("heart: Finished drawing chart");
} }
// as drawing starts at 30 HRM decreasing measrure by 30 // as drawing starts at 30 HRM decreasing measrure by 30
// recalculate for range 110-150 as only 20 pixels are available // recalculate for range 110-150 as only 20 pixels are available
function getY(measure) { function getY(measure) {
positionY = GraphYZero - measure + 30; var positionY = GraphYZero - measure + 30;
if (100 < measure < 150) { if (100 < measure < 150) {
positionY = GraphYZero - ( 100 + Math.round((measure - 100)/2) ) + 30; positionY = GraphYZero - ( 100 + Math.round((measure - 100)/2) ) + 30;
g.setColor(1, 0, 0);
} else if (60 < measrure < 100) { } else if (60 < measrure < 100) {
positionY = GraphYZero - ( 30 + Math.round((measure - 30)/2) ) + 30; positionY = GraphYZero - ( 30 + Math.round((measure - 30)/2) ) + 30;
g.setColor(0, 1, 0);
} }
if (positionY > GraphYZero) { if (positionY > GraphYZero) {
positionY = GraphYZero; positionY = GraphYZero;
g.setColor(1, 0, 0);
} }
if (positionY < GraphY100) { if (positionY < GraphY100) {
positionY = GraphY100; positionY = GraphY100;
g.setColor(1, 0, 0);
} }
return positionY; return positionY;
} }
@ -221,37 +215,39 @@ function graphRecord(n) {
); );
g.setFont("Vector", 10); g.setFont("Vector", 10);
var lastPixel;
var lineCount = 0; var lineCount = 0;
var positionX = GraphXZero;
var positionY = GraphYZero;
var startLine = 1; var startLine = 1;
var tempCount = 0;
var f = require("Storage").open(getFileNbr(n),"r"); var f = require("Storage").open(getFileNbr(n),"r");
var line = f.readLine(); var line = f.readLine();
var times = Array(2); console.log("heart: Counting lines");
console.log("Counting lines");
while (line !== undefined) { while (line !== undefined) {
lineCount++; lineCount++;
line = f.readLine(); line = f.readLine();
} }
console.log(`Line count: ${lineCount}`);
if (lineCount > MaxValueCount) { console.log(`heart: Line count: ${lineCount}`);
if (lineCount > MaxValueCount)
startLine = lineCount - MaxValueCount; startLine = lineCount - MaxValueCount;
} f = undefined;
console.log(`start: ${startLine}`); line = undefined;
lineCount = undefined;
console.log(`heart: start: ${startLine}`);
f = require("Storage").open(getFileNbr(n),"r"); f = require("Storage").open(getFileNbr(n),"r");
line = f.readLine(); line = f.readLine();
var times = Uint32Array(2);
var tempCount = 0;
var positionX = GraphXZero;
var positionY = GraphYZero;
while (line !== undefined) { while (line !== undefined) {
currentLine = line; currentLine = line;
line = f.readLine(); line = f.readLine();
tempCount++; tempCount++;
if (tempCount == startLine) { if (tempCount == startLine) {
g.clear(); g.clear();
Bangle.loadWidgets();
Bangle.drawWidgets();
renderHomeIcon();
renderChart(); renderChart();
} else if (tempCount > startLine) { } else if (tempCount > startLine) {
positionX++; positionX++;
@ -270,24 +266,23 @@ function graphRecord(n) {
} }
} }
} }
g.flip();
} }
g.flip();
g.setColor(1, 1, 0); g.setColor(1, 1, 0).setFont("Vector", 10);
g.setFont("Vector", 10); console.log('heart: start: ' + times[0]);
console.log('start: ' + times[0]); console.log('heart: end: ' + times[1]);
console.log('end: ' + times[1]);
if (times[0] !== undefined) { if (times[0] !== undefined) {
g.setFontAlign(-1, -1, 0); g.setFontAlign(-1, -1, 0);
var startdate = new Date(times[0]*1000); g.drawString(Date(times[0]*1000).local().as("0h:0m").str, 15, GraphYZero + 12);
g.drawString(startdate.local().as("0h:0m").str, 15, GraphYZero + 12);
} }
if (times[1] !== undefined) { if (times[1] !== undefined) {
g.setFontAlign(1, -1, 0); g.setFontAlign(1, -1, 0);
var enddate = new Date(times[1]*1000); g.drawString(Date(times[1]*1000).local().as().str, GraphXMax, GraphYZero + 12);
g.drawString(enddate.local().as().str, GraphXMax, GraphYZero + 12);
} }
console.log("Finished rendering data");
console.log("heart: Finished rendering data");
Bangle.buzz(200, 0.3); Bangle.buzz(200, 0.3);
setWatch(stop, BTN2, {edge:"falling", debounce:50, repeat:false}); setWatch(stop, BTN2, {edge:"falling", debounce:50, repeat:false});
} }