Begin writing stop watch
This commit is contained in:
@@ -11,11 +11,16 @@
|
||||
|
||||
let watchState = STATE_IDLE;
|
||||
|
||||
let stopWatch = require("Storage").readJSON("stopwatch.json", {start1: null, elapsed1: null, start2: null, elapsed2: null});
|
||||
let saveStopWatch = function() {
|
||||
require("Storage").writeJSON("stopwatch.json", stopWatch);
|
||||
}
|
||||
let stopWatch1Timer = null;
|
||||
|
||||
let myMessage = "";
|
||||
let temperature = "";
|
||||
|
||||
let drawTimer = null;
|
||||
let swipeLockout = false;
|
||||
|
||||
let menu = require("Storage").readJSON("menu.json", true);
|
||||
let subMenu = null;
|
||||
@@ -47,10 +52,18 @@
|
||||
// Show date and day of week
|
||||
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
var dateStr = date.getDate() + " " + days[date.getDay()] + " " + temperature;
|
||||
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(dateStr, x, y+76);
|
||||
|
||||
// don't draw date string if stopwatch 2 is running
|
||||
if (!stopWatch.start2) {
|
||||
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(dateStr, x, y+76);
|
||||
}
|
||||
|
||||
//var wrapped = g.wrapString(myMessage, g.getWidth()-10).join("\n");
|
||||
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(myMessage, x, y-45);
|
||||
|
||||
// don't draw message if stopwatch 1 is running
|
||||
if (!stopWatch.start1) {
|
||||
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(myMessage, x, y-45);
|
||||
}
|
||||
};
|
||||
|
||||
// Actually draw the watch face
|
||||
@@ -77,11 +90,54 @@
|
||||
}, 60000 - (Date.now() % 60000));
|
||||
};
|
||||
|
||||
let timeToText = function(t) {
|
||||
let hrs = Math.floor(t/3600000);
|
||||
let mins = Math.floor(t/60000)%60;
|
||||
let secs = Math.floor(t/1000)%60;
|
||||
let tnth = Math.floor(t/100)%10;
|
||||
let text;
|
||||
|
||||
if (hrs === 0) {
|
||||
text = ("0"+mins).substr(-2) + ":" + ("0"+secs).substr(-2) + "." + tnth;
|
||||
} else {
|
||||
text = ("0"+hrs) + ":" + ("0"+mins).substr(-2) + ":" + ("0"+secs).substr(-2);
|
||||
}
|
||||
|
||||
return text;
|
||||
};
|
||||
|
||||
let drawStopWatches = function () {
|
||||
if (watchState != STATE_IDLE) return;
|
||||
|
||||
var w = g.getWidth();
|
||||
var x = w / 2;
|
||||
var y = g.getHeight() / 2;
|
||||
|
||||
if (stopWatch.start1) {
|
||||
let Tt1 = Date.now() - stopWatch.start1;
|
||||
let Ttxt1 = timeToText(Tt1);
|
||||
|
||||
//g.drawLine(0, y-60, w, y-60);
|
||||
//g.drawLine(0, y-34, w, y-34);
|
||||
g.clearRect(0, y-60, w, y-34);
|
||||
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(Ttxt1, x, y-45);
|
||||
}
|
||||
|
||||
if (stopWatch.start2) {
|
||||
let Tt2 = Date.now() - stopWatch.start2;
|
||||
let Ttxt2 = timeToText(Tt2);
|
||||
|
||||
//g.drawLine(0, y+88, w, y+88);
|
||||
//g.drawLine(0, y+61, w, y+61);
|
||||
g.clearRect(0, y+61, w, y+88);
|
||||
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(Ttxt2, x, y+76);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let handleSwipe = function(dir1, dir2) {
|
||||
var direction;
|
||||
|
||||
if (swipeLockout) return;
|
||||
|
||||
if (dir1 == 1) {
|
||||
direction = "right";
|
||||
} else if (dir1 == -1) {
|
||||
@@ -126,6 +182,27 @@
|
||||
|
||||
};
|
||||
|
||||
let startSW1 = function() {
|
||||
console.log("Starting stopwatch 1...");
|
||||
stopWatch.start1 = Date.now();
|
||||
|
||||
if (!stopWatch1Timer) {
|
||||
drawStopWatches();
|
||||
stopWatch1Timer = setInterval(drawStopWatches, 100);
|
||||
}
|
||||
}
|
||||
|
||||
let startSW2 = function() {
|
||||
console.log("Starting stopwatch 2...");
|
||||
stopWatch.start2 = Date.now();
|
||||
|
||||
if (!stopWatch1Timer) {
|
||||
drawStopWatches();
|
||||
stopWatch1Timer = setInterval(drawStopWatches, 100);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let handleTouch = function(button, xy) {
|
||||
console.log(button, xy);
|
||||
|
||||
@@ -160,10 +237,22 @@
|
||||
|
||||
const keys = Object.keys(subMenu);
|
||||
const key = keys[quad];
|
||||
subMenu = subMenu[key];
|
||||
|
||||
if (key == "stop watch") {
|
||||
subMenu = {};
|
||||
const sw2key = stopWatch.start2 ? 'pause2' : 'start2';
|
||||
subMenu[sw2key] = null;
|
||||
subMenu["stop2"] = null;
|
||||
const sw1key = stopWatch.start1 ? 'pause1' : 'start1';
|
||||
subMenu[sw1key] = null;
|
||||
subMenu["stop1"] = null;
|
||||
} else {
|
||||
subMenu = subMenu[key];
|
||||
}
|
||||
|
||||
menuCommand += key + ",";
|
||||
|
||||
//console.log("submenu:", subMenu);
|
||||
console.log("submenu:", subMenu);
|
||||
|
||||
if (subMenu) {
|
||||
drawMenu(subMenu);
|
||||
@@ -177,18 +266,30 @@
|
||||
console.log("Pulling new menu...");
|
||||
menu = null;
|
||||
require("Storage").writeJSON("menu.json", menu);
|
||||
}
|
||||
|
||||
if (Bangle.http){
|
||||
const headers = {"Authorization": "Bearer " + apiKey};
|
||||
const options = {timeout:3000, method: "post", body: menuCommand, headers: headers};
|
||||
Bangle.http("https://api.home.dns.t0.vc/menu", options).then(event => {
|
||||
myMessage = "ok";
|
||||
if (paintFace) paintFace();
|
||||
}).catch((e)=>{
|
||||
myMessage = "POST error";
|
||||
if (paintFace) paintFace();
|
||||
});
|
||||
} else if (menuCommand == "states,stop watch,start1,") {
|
||||
startSW1();
|
||||
} else if (menuCommand == "states,stop watch,start2,") {
|
||||
startSW2();
|
||||
} else if (menuCommand == "states,stop watch,pause1,") {
|
||||
pauseSW1();
|
||||
} else if (menuCommand == "states,stop watch,pause2,") {
|
||||
pauseSW2();
|
||||
} else if (menuCommand == "states,stop watch,stop1,") {
|
||||
stopSW1();
|
||||
} else if (menuCommand == "states,stop watch,stop2,") {
|
||||
stopSW2();
|
||||
} else {
|
||||
if (Bangle.http){
|
||||
const headers = {"Authorization": "Bearer " + apiKey};
|
||||
const options = {timeout:3000, method: "post", body: menuCommand, headers: headers};
|
||||
Bangle.http("https://api.home.dns.t0.vc/menu", options).then(event => {
|
||||
myMessage = "ok";
|
||||
if (paintFace) paintFace();
|
||||
}).catch((e)=>{
|
||||
myMessage = "POST error";
|
||||
if (paintFace) paintFace();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
myMessage = key;
|
||||
|
||||
Reference in New Issue
Block a user