Begin writing stop watch

This commit is contained in:
2026-03-09 13:19:40 -06:00
parent 9091d7a783
commit dda3130c56

View File

@@ -11,11 +11,16 @@
let watchState = STATE_IDLE; 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 myMessage = "";
let temperature = ""; let temperature = "";
let drawTimer = null; let drawTimer = null;
let swipeLockout = false;
let menu = require("Storage").readJSON("menu.json", true); let menu = require("Storage").readJSON("menu.json", true);
let subMenu = null; let subMenu = null;
@@ -47,10 +52,18 @@
// Show date and day of week // Show date and day of week
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var dateStr = date.getDate() + " " + days[date.getDay()] + " " + temperature; var dateStr = date.getDate() + " " + days[date.getDay()] + " " + temperature;
// 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); g.setFontAlign(0, 0).setFont("Vector", 26).drawString(dateStr, x, y+76);
}
//var wrapped = g.wrapString(myMessage, g.getWidth()-10).join("\n"); //var wrapped = g.wrapString(myMessage, g.getWidth()-10).join("\n");
// don't draw message if stopwatch 1 is running
if (!stopWatch.start1) {
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(myMessage, x, y-45); g.setFontAlign(0, 0).setFont("Vector", 26).drawString(myMessage, x, y-45);
}
}; };
// Actually draw the watch face // Actually draw the watch face
@@ -77,11 +90,54 @@
}, 60000 - (Date.now() % 60000)); }, 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) { let handleSwipe = function(dir1, dir2) {
var direction; var direction;
if (swipeLockout) return;
if (dir1 == 1) { if (dir1 == 1) {
direction = "right"; direction = "right";
} else if (dir1 == -1) { } 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) { let handleTouch = function(button, xy) {
console.log(button, xy); console.log(button, xy);
@@ -160,10 +237,22 @@
const keys = Object.keys(subMenu); const keys = Object.keys(subMenu);
const key = keys[quad]; const key = keys[quad];
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]; subMenu = subMenu[key];
}
menuCommand += key + ","; menuCommand += key + ",";
//console.log("submenu:", subMenu); console.log("submenu:", subMenu);
if (subMenu) { if (subMenu) {
drawMenu(subMenu); drawMenu(subMenu);
@@ -177,8 +266,19 @@
console.log("Pulling new menu..."); console.log("Pulling new menu...");
menu = null; menu = null;
require("Storage").writeJSON("menu.json", menu); require("Storage").writeJSON("menu.json", menu);
} } 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){ if (Bangle.http){
const headers = {"Authorization": "Bearer " + apiKey}; const headers = {"Authorization": "Bearer " + apiKey};
const options = {timeout:3000, method: "post", body: menuCommand, headers: headers}; const options = {timeout:3000, method: "post", body: menuCommand, headers: headers};
@@ -190,6 +290,7 @@
if (paintFace) paintFace(); if (paintFace) paintFace();
}); });
} }
}
myMessage = key; myMessage = key;
if (paintFace) paintFace(); if (paintFace) paintFace();