Merge branch 'master' of https://github.com/Red-The-Hunter/BangleApps into Red-The-Hunter-master

pull/309/head^2
Gordon Williams 2020-04-16 10:11:44 +01:00
commit 083e4f752f
3 changed files with 49 additions and 21 deletions

View File

@ -411,7 +411,7 @@
{ "id": "swatch", { "id": "swatch",
"name": "Stopwatch", "name": "Stopwatch",
"icon": "stopwatch.png", "icon": "stopwatch.png",
"version":"0.05", "version":"0.06",
"interface": "interface.html", "interface": "interface.html",
"description": "Simple stopwatch with Lap Time logging to a JSON file", "description": "Simple stopwatch with Lap Time logging to a JSON file",
"tags": "health", "tags": "health",

View File

@ -5,3 +5,4 @@
Fixed bug from 0.01 where BN1 (reset) could clear the lap log when timer is running Fixed bug from 0.01 where BN1 (reset) could clear the lap log when timer is running
0.04: Changed save file filename, add interface.html to allow laps to be loaded 0.04: Changed save file filename, add interface.html to allow laps to be loaded
0.05: Added widgets 0.05: Added widgets
0.06: Added total running time, moved lap time to smaller display, total run time now appends as first entry in array, saving now saves last lap as well

View File

@ -1,8 +1,11 @@
var tTotal = Date.now();
var tStart = Date.now(); var tStart = Date.now();
var tCurrent = Date.now(); var tCurrent = Date.now();
var started = false; var started = false;
var timeY = 60; var timeY = 45;
var hsXPos = 0; var hsXPos = 0;
var TtimeY = 75;
var ThsXPos = 0;
var lapTimes = []; var lapTimes = [];
var displayInterval; var displayInterval;
@ -12,6 +15,7 @@ function timeToText(t) {
var hs = Math.floor(t/10)%100; var hs = Math.floor(t/10)%100;
return mins+":"+("0"+secs).substr(-2)+"."+("0"+hs).substr(-2); return mins+":"+("0"+secs).substr(-2)+"."+("0"+hs).substr(-2);
} }
function updateLabels() { function updateLabels() {
g.reset(1); g.reset(1);
g.clearRect(0,23,g.getWidth()-1,g.getHeight()-24); g.clearRect(0,23,g.getWidth()-1,g.getHeight()-24);
@ -24,35 +28,53 @@ function updateLabels() {
g.setFontAlign(-1,-1); g.setFontAlign(-1,-1);
for (var i in lapTimes) { for (var i in lapTimes) {
if (i<16) if (i<16)
{g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),35,timeY + 30 + i*8);} {g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),35,timeY + 40 + i*8);}
else if (i<32) else if (i<32)
{g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),125,timeY + 30 + (i-16)*8);} {g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),125,timeY + 40 + (i-16)*8);}
} }
drawsecs(); drawsecs();
} }
function drawsecs() { function drawsecs() {
var t = tCurrent-tStart; var t = tCurrent-tStart;
g.reset(1); var Tt = tCurrent-tTotal;
g.setFont("Vector",48);
g.setFontAlign(0,0);
var secs = Math.floor(t/1000)%60; var secs = Math.floor(t/1000)%60;
var mins = Math.floor(t/60000); var mins = Math.floor(t/60000);
var txt = mins+":"+("0"+secs).substr(-2); var txt = mins+":"+("0"+secs).substr(-2);
var Tsecs = Math.floor(Tt/1000)%60;
var Tmins = Math.floor(Tt/60000);
var Ttxt = Tmins+":"+("0"+Tsecs).substr(-2);
var x = 100; var x = 100;
g.clearRect(0,timeY-26,200,timeY+26); var Tx = 125;
g.drawString(txt,x,timeY); g.reset(1);
g.setFont("Vector",38);
g.setFontAlign(0,0);
g.clearRect(0,timeY-21,200,timeY+21);
g.drawString(Ttxt,x,timeY);
hsXPos = 5+x+g.stringWidth(txt)/2; hsXPos = 5+x+g.stringWidth(txt)/2;
g.setFont("6x8",2);
g.clearRect(0,TtimeY-7,200,TtimeY+7);
g.drawString(txt,Tx,TtimeY);
ThsXPos = 5+Tx+g.stringWidth(Ttxt)/2;
drawms(); drawms();
} }
function drawms() { function drawms() {
var t = tCurrent-tStart; var t = tCurrent-tStart;
var hs = Math.floor(t/10)%100; var hs = Math.floor(t/10)%100;
var Tt = tCurrent-tTotal;
var Ths = Math.floor(Tt/10)%100;
g.setFontAlign(-1,0); g.setFontAlign(-1,0);
g.setFont("6x8",2); g.setFont("6x8",2);
g.clearRect(hsXPos,timeY,220,timeY+20); g.clearRect(hsXPos,timeY,220,timeY+20);
g.drawString("."+("0"+hs).substr(-2),hsXPos,timeY+10); g.drawString("."+("0"+Ths).substr(-2),hsXPos-5,timeY+14);
g.setFont("6x8",1);
g.clearRect(ThsXPos,TtimeY,220,TtimeY+5);
g.drawString("."+("0"+hs).substr(-2),ThsXPos-5,TtimeY+3);
} }
function getLapTimesArray() { function getLapTimesArray() {
lapTimes.push(tCurrent-tTotal);
return lapTimes.map(timeToText).reverse(); return lapTimes.map(timeToText).reverse();
} }
@ -61,7 +83,8 @@ setWatch(function() { // Start/stop
Bangle.beep(); Bangle.beep();
if (started) if (started)
tStart = Date.now()+tStart-tCurrent; tStart = Date.now()+tStart-tCurrent;
tCurrent = Date.now(); tTotal = Date.now()+tTotal-tCurrent;
tCurrent = Date.now();
if (displayInterval) { if (displayInterval) {
clearInterval(displayInterval); clearInterval(displayInterval);
displayInterval = undefined; displayInterval = undefined;
@ -69,37 +92,41 @@ setWatch(function() { // Start/stop
updateLabels(); updateLabels();
if (started) if (started)
displayInterval = setInterval(function() { displayInterval = setInterval(function() {
var last = tCurrent; var last = tCurrent;
if (started) tCurrent = Date.now(); if (started) tCurrent = Date.now();
if (Math.floor(last/1000)!=Math.floor(tCurrent/1000)) if (Math.floor(last/1000)!=Math.floor(tCurrent/1000))
drawsecs(); drawsecs();
else else
drawms(); drawms();
}, 20); }, 20);
}, BTN2, {repeat:true}); }, BTN2, {repeat:true});
setWatch(function() { // Lap setWatch(function() { // Lap
Bangle.beep(); Bangle.beep();
if (started) { if (started) {
tCurrent = Date.now(); tCurrent = Date.now();
lapTimes.unshift(tCurrent-tStart); lapTimes.unshift(tCurrent-tStart);
} }
tStart = tCurrent;
if (!started) { // save if (!started) { // save
var timenow= Date(); var timenow= Date();
var filename = "swatch-"+(new Date()).toISOString().substr(0,16).replace("T","_")+".json"; var filename = "swatch-"+(new Date()).toISOString().substr(0,16).replace("T","_")+".json";
// this maxes out the 28 char maximum // this maxes out the 28 char maximum
lapTimes.unshift(tCurrent-tStart);
require("Storage").writeJSON(filename, getLapTimesArray()); require("Storage").writeJSON(filename, getLapTimesArray());
tStart = tCurrent = tTotal = Date.now();
lapTimes = [];
E.showMessage("Laps Saved","Stopwatch"); E.showMessage("Laps Saved","Stopwatch");
setTimeout(updateLabels, 1000); setTimeout(updateLabels, 1000);
} else { } else {
tStart = tCurrent;
updateLabels(); updateLabels();
} }
}, BTN1, {repeat:true}); }, BTN1, {repeat:true});
setWatch(function() { // Reset setWatch(function() { // Reset
if (!started) { if (!started) {
Bangle.beep(); Bangle.beep();
tStart = tCurrent = Date.now(); tStart = tCurrent = tTotal = Date.now();
lapTimes = []; lapTimes = [];
} }
updateLabels(); updateLabels();
}, BTN3, {repeat:true}); }, BTN3, {repeat:true});