2020-09-23 07:51:24 +00:00
|
|
|
// Simple clock from https://www.espruino.com/Bangle.js+Clock
|
|
|
|
// Load fonts
|
|
|
|
require("Font7x11Numeric7Seg").add(Graphics);
|
2020-09-23 07:44:58 +00:00
|
|
|
// Check settings for what type our clock should be
|
|
|
|
var is12Hour = (require("Storage").readJSON("setting.json",1)||{})["12hour"];
|
2020-09-23 07:51:24 +00:00
|
|
|
// position on screen
|
2021-06-24 13:00:40 +00:00
|
|
|
const big = g.getWidth()>200;
|
|
|
|
const X = big?160:135, Y = big?140:100;
|
|
|
|
|
2020-09-23 07:51:24 +00:00
|
|
|
function draw() {
|
|
|
|
// work out how to display the current time
|
2020-09-23 07:44:58 +00:00
|
|
|
var d = new Date();
|
2020-09-23 07:51:24 +00:00
|
|
|
var h = d.getHours(), m = d.getMinutes();
|
2020-09-23 07:44:58 +00:00
|
|
|
if (is12Hour) {
|
2020-09-23 07:51:24 +00:00
|
|
|
if (h == 0) h = 12;
|
|
|
|
else if (h>12) h -= 12;
|
2020-09-23 07:44:58 +00:00
|
|
|
}
|
2020-09-23 07:51:24 +00:00
|
|
|
var time = (" "+h).substr(-2) + ":" + ("0"+m).substr(-2);
|
|
|
|
// Reset the state of the graphics library
|
|
|
|
g.reset();
|
|
|
|
// draw the current time (4x size 7 segment)
|
|
|
|
g.setFont("7x11Numeric7Seg",4);
|
|
|
|
g.setFontAlign(1,1); // align right bottom
|
|
|
|
g.drawString(time, X, Y, true /*clear background*/);
|
|
|
|
// draw the seconds (2x size 7 segment)
|
|
|
|
g.setFont("7x11Numeric7Seg",2);
|
2021-03-22 19:19:31 +00:00
|
|
|
g.drawString(("0"+d.getSeconds()).substr(-2), X+35, Y, true /*clear background*/);
|
2020-09-23 07:51:24 +00:00
|
|
|
// draw the date, in a normal font
|
2021-06-24 13:00:40 +00:00
|
|
|
g.setFont("6x8", big?3:2);
|
2020-09-23 07:51:24 +00:00
|
|
|
g.setFontAlign(0,1); // align center bottom
|
|
|
|
// pad the date - this clears the background if the date were to change length
|
|
|
|
var dateStr = " "+require("locale").date(d)+" ";
|
2021-03-22 19:19:31 +00:00
|
|
|
g.drawString(dateStr, g.getWidth()/2, Y+35, true /*clear background*/);
|
2020-09-23 07:44:58 +00:00
|
|
|
}
|
2021-06-24 13:00:40 +00:00
|
|
|
|
2020-09-23 07:51:24 +00:00
|
|
|
// Clear the screen once, at startup
|
2020-09-23 07:44:58 +00:00
|
|
|
g.clear();
|
2020-09-23 07:51:24 +00:00
|
|
|
// draw immediately at first
|
|
|
|
draw();
|
|
|
|
var secondInterval = setInterval(draw, 1000);
|
|
|
|
// Stop updates when LCD is off, restart when on
|
|
|
|
Bangle.on('lcdPower',on=>{
|
|
|
|
if (secondInterval) clearInterval(secondInterval);
|
|
|
|
secondInterval = undefined;
|
|
|
|
if (on) {
|
|
|
|
secondInterval = setInterval(draw, 1000);
|
|
|
|
draw(); // draw immediately
|
|
|
|
}
|
|
|
|
});
|
2021-06-24 13:00:40 +00:00
|
|
|
|
|
|
|
// Show launcher when button pressed
|
|
|
|
Bangle.setUI("clockupdown", btn=>{
|
|
|
|
if (btn==0) vibrateTime();
|
|
|
|
});
|
2020-09-23 07:51:24 +00:00
|
|
|
// Load widgets
|
2020-09-23 07:44:58 +00:00
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
2021-06-24 13:00:40 +00:00
|
|
|
|
2020-09-23 07:51:24 +00:00
|
|
|
// ====================================== Vibration
|
2020-09-23 07:44:58 +00:00
|
|
|
// vibrate 0..9
|
|
|
|
function vibrateDigit(num) {
|
|
|
|
if (num==0) return Bangle.buzz(500);
|
|
|
|
return new Promise(function f(resolve){
|
|
|
|
if (num--<=0) return resolve();
|
|
|
|
Bangle.buzz(100).then(()=>{
|
|
|
|
setTimeout(()=>f(resolve), 200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// vibrate multiple digits (num must be a string)
|
|
|
|
function vibrateNumber(num) {
|
|
|
|
return new Promise(function f(resolve){
|
|
|
|
if (!num.length) return resolve();
|
|
|
|
var digit = num[0];
|
|
|
|
num = num.substr(1);
|
|
|
|
vibrateDigit(digit).then(()=>{
|
|
|
|
setTimeout(()=>f(resolve),500);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-24 13:00:40 +00:00
|
|
|
|
2020-09-23 07:44:58 +00:00
|
|
|
var vibrateBusy;
|
|
|
|
function vibrateTime() {
|
|
|
|
if (vibrateBusy) return;
|
|
|
|
vibrateBusy = true;
|
2021-06-24 13:00:40 +00:00
|
|
|
|
2020-09-23 07:44:58 +00:00
|
|
|
var d = new Date();
|
|
|
|
var hours = d.getHours(), minutes = d.getMinutes();
|
|
|
|
if (is12Hour) {
|
|
|
|
if (hours == 0) hours = 12;
|
|
|
|
else if (hours>12) hours -= 12;
|
|
|
|
}
|
2021-06-24 13:00:40 +00:00
|
|
|
|
2020-09-23 07:44:58 +00:00
|
|
|
vibrateNumber(hours.toString()).
|
|
|
|
then(() => new Promise(resolve=>setTimeout(resolve,500))).
|
|
|
|
then(() => vibrateNumber(minutes.toString())).
|
|
|
|
then(() => vibrateBusy=false);
|
|
|
|
}
|