1
0
Fork 0
BangleApps/apps/swatch/stopwatch.js

94 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-11-06 22:12:54 +00:00
var tStart = Date.now();
var tCurrent = Date.now();
var started = false;
var timeY = 60;
var hsXPos = 0;
var lapTimes = [];
var displayInterval;
function timeToText(t) {
var secs = Math.floor(t/1000)%60;
var mins = Math.floor(t/60000);
var hs = Math.floor(t/10)%100;
return mins+":"+("0"+secs).substr(-2)+"."+("0"+hs).substr(-2);
}
function updateLabels() {
g.clear();
g.setFont("6x8",2);
g.setFontAlign(0,0,3);
g.drawString(started?"STOP":"GO",230,120);
if (!started) g.drawString("RESET",230,50);
g.drawString("LAP",230,190);
g.setFont("6x8",1);
g.setFontAlign(-1,-1);
for (var i in lapTimes) {
2020-03-27 17:00:49 +00:00
if (i<18)
{g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),8,timeY + 30 + i*8);}
else if (i<36)
{g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),80,timeY + 30 + (i-18)*8);}
else
{g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),152,timeY + 30 + (i-36)*8);}
2019-11-06 22:12:54 +00:00
}
drawsecs();
}
function drawsecs() {
var t = tCurrent-tStart;
g.setFont("Vector",48);
g.setFontAlign(0,0);
var secs = Math.floor(t/1000)%60;
var mins = Math.floor(t/60000);
var txt = mins+":"+("0"+secs).substr(-2);
var x = 100;
g.clearRect(0,timeY-26,200,timeY+26);
g.drawString(txt,x,timeY);
hsXPos = 5+x+g.stringWidth(txt)/2;
drawms();
}
function drawms() {
var t = tCurrent-tStart;
var hs = Math.floor(t/10)%100;
g.setFontAlign(-1,0);
g.setFont("6x8",2);
g.clearRect(hsXPos,timeY,220,timeY+20);
g.drawString("."+("0"+hs).substr(-2),hsXPos,timeY+10);
}
setWatch(function() { // Start/stop
started = !started;
Bangle.beep();
if (started)
2019-11-06 22:12:54 +00:00
tStart = Date.now()+tStart-tCurrent;
tCurrent = Date.now();
2019-11-06 22:12:54 +00:00
if (displayInterval) {
clearInterval(displayInterval);
displayInterval = undefined;
}
updateLabels();
if (started)
2019-11-06 22:12:54 +00:00
displayInterval = setInterval(function() {
var last = tCurrent;
if (started) tCurrent = Date.now();
2019-11-06 22:12:54 +00:00
if (Math.floor(last/1000)!=Math.floor(tCurrent/1000))
drawsecs();
else
drawms();
}, 20);
}, BTN2, {repeat:true});
setWatch(function() { // Reset
Bangle.beep();
2019-11-06 22:12:54 +00:00
if (!started) {
tStart = tCurrent = Date.now();
}
lapTimes = [];
2019-11-06 22:12:54 +00:00
updateLabels();
}, BTN1, {repeat:true});
setWatch(function() { // Lap
Bangle.beep();
2019-11-06 22:12:54 +00:00
if (started) tCurrent = Date.now();
lapTimes.unshift(tCurrent-tStart);
tStart = tCurrent;
updateLabels();
}, BTN3, {repeat:true});
updateLabels();