diff --git a/apps/kitchen/compass.kit.js b/apps/kitchen/compass.kit.js index f8b2c9393..25bf5433f 100644 --- a/apps/kitchen/compass.kit.js +++ b/apps/kitchen/compass.kit.js @@ -21,7 +21,7 @@ //console.log(o); } - function init(gps) { + function init(gps,sw) { showMem("compass init() START"); gpsObject = gps; pal_by = new Uint16Array([0x0000,0xFFC0],0,1); // black, yellow diff --git a/apps/kitchen/digi.kit.js b/apps/kitchen/digi.kit.js index 1a64882a7..9689d5fd9 100644 --- a/apps/kitchen/digi.kit.js +++ b/apps/kitchen/digi.kit.js @@ -15,7 +15,7 @@ const Y_ACTIVITY = 116; const Y_MODELINE = 200; - function init(gps) { + function init(gps,sw) { showMem("digi init 1"); days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday"]; prevInfo = ""; diff --git a/apps/kitchen/gps.kit.js b/apps/kitchen/gps.kit.js index ad2dd0ee4..adebc1838 100644 --- a/apps/kitchen/gps.kit.js +++ b/apps/kitchen/gps.kit.js @@ -20,7 +20,7 @@ //console.log(o); } - function init(gps) { + function init(gps, sw) { log_debug("gps init"); //log_debug(gps); gpsObject = gps; diff --git a/apps/kitchen/kitchen.app.js b/apps/kitchen/kitchen.app.js index 8e2a5c560..c13b0eb10 100644 --- a/apps/kitchen/kitchen.app.js +++ b/apps/kitchen/kitchen.app.js @@ -26,7 +26,7 @@ function nextFace(){ g.clear(); g.reset(); - face.init(gpsObj); + face.init(gpsObj, swObj); startdraw(); } @@ -470,13 +470,197 @@ function to_map_ref(digits, easting, northing) { /***************************************************************************** -End of GPS object +Stopwatch Class Code ******************************************************************************/ +function STOPWATCH() { + this.tTotal = Date.now(); + this.tStart = Date.now(); + this.tCurrent = Date.now(); + this.running = false; + this.timeY = 45; + this.TtimeY = 75; + this.lapTimes = []; + this.displayInterval; + this.redrawButtons = true; + this.redrawLaps = true; +} + +STOPWATCH.prototype.log_debug = function(o) { + console.log(o); +} + +STOPWATCH.prototype.timeToText = function(t) { + let hrs = Math.floor(t/3600000); + let mins = Math.floor(t/60000)%60; + let secs = Math.floor(t/1000)%60; + + let text = ("00"+hrs).substr(-3) + ":" + ("0"+mins).substr(-2) + ":" + ("0"+secs).substr(-2); + this.log_debug(text); + return text; +} + +STOPWATCH.prototype.getLapTimesArray = function() { + this.lapTimes.push(tCurrent-tTotal); + return this.lapTimes.map(timeToText).reverse(); +} + +STOPWATCH.prototype.stopStart = function() { + this.log_debug("stopStart()"); + + // if (this.running) + // this.stopTimer(); + + this.running = !this.running; + + if (this.running) + this.tStart = Date.now() + this.tStart - this.tCurrent; + + this.tTotal = Date.now() + this.tTotal - this.tCurrent; + this.tCurrent = Date.now(); + this.redrawButtons = true; + this.redrawLaps = true; + + /* + if (this.running) { + this.startTimer(); + } else { + this.draw(); + } + */ + + this.draw(); +} + +STOPWATCH.prototype.lap = function() { + this.log_debug("lap()"); + if (this.running) { + this.tCurrent = Date.now(); + this.lapTimes.unshift(this.tCurrent - this.tStart); + console.log(this.tCurrent - this.tStart); + } + + this.tStart = this.tCurrent; + this.redrawButtons = true; + this.redrawLaps = true; + this.draw(); +} + +STOPWATCH.prototype.reset = function() { + this.log_debug("reset()"); + if (this.running === false) { + this.tStart = this.tCurrent = this.tTotal = Date.now(); + this.lapTimes = []; + } + g.clear(); + this.draw(); +} + +// lap or reset +STOPWATCH.prototype.lapOrReset = function() { + this.redrawButtons = true; + this.redrawLaptimes = true; + + this.log_debug("lapReset()"); + if (this.running) + this.lap() + else + this.reset(); +} + +STOPWATCH.prototype.draw = function() { + if (this.running) this.tCurrent = Date.now(); + this.log_debug("draw()" + getTime()); + + g.setColor(1,1,1); + if (this.redrawButtons) this.drawButtons(); + this.drawTime(); + if (this.redrawLaps) this.drawLaptimes(); +} + +STOPWATCH.prototype.drawButtons = function() { + this.log_debug("drawButtons()"); + + g.clearRect(0,23,g.getWidth()-1,g.getHeight()-24); + g.setColor(1,1,1); + g.setFont("Vector", 20); + g.setFontAlign(0,0,3); + g.drawString(this.running? "STOP" : "GO", 230, 50); // BTN1 + g.drawString(this.running? "LAP" : "RESET", 230, 120); // BTN2 + this.redrawButtons = false; +} + +STOPWATCH.prototype.drawLaptimes = function() { + g.setFont("Vector",24); + g.setFontAlign(-1,-1); + g.clearRect(4, 205, 239, 229); // clear the last line of the lap times + + let laps = 0; + for (let i in this.lapTimes) { + g.drawString(this.lapTimes.length-i + ": " + this.timeToText(this.lapTimes[i]), 4, this.timeY + 40 + i*24); + if (++laps > 5) break; + } + this.redrawLaps = false; +} + +STOPWATCH.prototype.drawTime = function() { + this.log_debug("drawTime()"); + let t = this.tCurrent - this.tStart; + let Tt = this.tCurrent - this.tTotal; + + let txt = this.timeToText(t); + let Ttxt = this.timeToText(Tt); + + let x = 100; + let Tx = 125; + + // total time + g.setFont("Vector",38); + g.setFontAlign(0,0); + g.clearRect(0,this.timeY-21,200,this.timeY+21); + g.setColor(0xFFC0); + g.drawString(Ttxt,x,this.timeY); + + // current lap time + g.setFont("Vector", 20); + g.clearRect(0, this.TtimeY-7,200, this.TtimeY+7); + g.setColor(1,1,1); + g.drawString(txt,Tx, this.TtimeY); +} + +STOPWATCH.prototype.startTimer = function() { + this.log_debug("startTimer()"); + this.redrawButtons = true; + this.redrawLaps = true; + this.draw(); + this.displayInterval = setInterval(stopwatchDraw, 1000); +} + +STOPWATCH.prototype.stopTimer = function() { + this.log_debug("stopTimer()"); + if (this.displayInterval) { + clearInterval(this.displayInterval); + this.displayInterval = undefined; + } +} + +let swObj = new STOPWATCH(); + +function stopwatchDraw() { + swObj.draw(); +} + + +/***************************************************************************** + +Start App + +******************************************************************************/ + g.clear(); Bangle.loadWidgets(); -face.init(gpsObj); +face.init(gpsObj,swObj); startdraw(); setButtons(); diff --git a/apps/kitchen/stepo.kit.js b/apps/kitchen/stepo.kit.js index 9d7601e2a..068309072 100644 --- a/apps/kitchen/stepo.kit.js +++ b/apps/kitchen/stepo.kit.js @@ -5,7 +5,7 @@ var buf; var intervalRefSec; - function init(g) { + function init(g,sw) { showMem("stepo init 1"); pal4color = new Uint16Array([0x0000,0xFFFF,0x7BEF,0xAFE5],0,2); // b,w,grey,greenyellow pal4red = new Uint16Array([0x0000,0xFFFF,0xF800,0xAFE5],0,2); // b,w,red,greenyellow diff --git a/apps/kitchen/swatch.kit.js b/apps/kitchen/swatch.kit.js new file mode 100644 index 000000000..0e910949d --- /dev/null +++ b/apps/kitchen/swatch.kit.js @@ -0,0 +1,55 @@ +(() => { + function getFace(){ + let swObject = undefined; + + function init(gps, sw) { + showMem("swatch init 1"); + swObject = sw; + g.clear(); + showMem("swatch init 2"); + } + + function freeResources() { + showMem("swatch free 1"); + swObject = undefined; + showMem("swatch free 2"); + } + + function showMem(msg) { + var val = process.memory(); + var str = msg + " " + Math.round(val.usage*100/val.total) + "%"; + console.log(str); + } + + function startTimer() { + console.log("swObject.startTimer()"); + swObject.startTimer(); + } + + function stopTimer() { + console.log("swObject.stopTimer()"); + swObject.stopTimer(); + } + + function onButtonShort(btn) { + switch (btn) { + case 1: + swObject.stopStart(); + break; + case 2: + swObject.lapOrReset(); + break; + case 3: + default: + return; + } + } + + function onButtonLong(btn) {} + + return {init:init, freeResources:freeResources, startTimer:startTimer, stopTimer:stopTimer, + onButtonShort:onButtonShort, onButtonLong:onButtonLong}; + } + + return getFace; +})();