Update app.js

master
Hilmar Strauch 2022-01-27 21:58:24 +01:00 committed by GitHub
parent 6718b4317f
commit 38ea17592a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 64 additions and 9 deletions

View File

@ -4,7 +4,7 @@ var settings = require('Storage').readJSON("vectorclock.json", true) || {};
var dowcol = settings.dowcol || g.theme.fg; var dowcol = settings.dowcol || g.theme.fg;
var timecol = settings.timecol || g.theme.fg; var timecol = settings.timecol || g.theme.fg;
var datecol = settings.datecol || g.theme.fg; var datecol = settings.datecol || g.theme.fg;
var chimetype = settings.chimetype;
function padNum(n, l) { function padNum(n, l) {
return ("0".repeat(l)+n).substr(-l); return ("0".repeat(l)+n).substr(-l);
@ -44,7 +44,7 @@ function drawVectorText(text, size, x, y, alignX, alignY, color) {
function draw() { function draw() {
g.reset(); g.reset();
let d = new Date(); let d = new Date();
let hours = is12Hour ? ((d.getHours() + 11) % 12) + 1 : d.getHours(); let hours = is12Hour ? ((d.getHours() + 11) % 12) + 1 : d.getHours();
let timeText = `${hours}:${padNum(d.getMinutes(), 2)}`; let timeText = `${hours}:${padNum(d.getMinutes(), 2)}`;
@ -58,27 +58,27 @@ function draw() {
let timeFontSize = width / ((g.stringWidth(timeText) / 256) + (Math.max(g.stringWidth(meridian), g.stringWidth(secondsText)) / 512 * 9 / 10)); let timeFontSize = width / ((g.stringWidth(timeText) / 256) + (Math.max(g.stringWidth(meridian), g.stringWidth(secondsText)) / 512 * 9 / 10));
let dowFontSize = width / (g.stringWidth(dowText) / 256); let dowFontSize = width / (g.stringWidth(dowText) / 256);
let dateFontSize = width / (g.stringWidth(dateText) / 256); let dateFontSize = width / (g.stringWidth(dateText) / 256);
let timeHeight = g.setFont("Vector", timeFontSize).getFontHeight() * 9 / 10; let timeHeight = g.setFont("Vector", timeFontSize).getFontHeight() * 9 / 10;
let dowHeight = g.setFont("Vector", dowFontSize).getFontHeight(); let dowHeight = g.setFont("Vector", dowFontSize).getFontHeight();
let dateHeight = g.setFont("Vector", dateFontSize).getFontHeight(); let dateHeight = g.setFont("Vector", dateFontSize).getFontHeight();
let remainingHeight = g.getHeight() - 24 - timeHeight - dowHeight - dateHeight; let remainingHeight = g.getHeight() - 24 - timeHeight - dowHeight - dateHeight;
let spacer = remainingHeight / 4; let spacer = remainingHeight / 4;
let x = 2; let x = 2;
let y = 24 + spacer; let y = 24 + spacer;
pushCommand(drawVectorText, timeText, timeFontSize, x, y, -1, -1, timecol); pushCommand(drawVectorText, timeText, timeFontSize, x, y, -1, -1, timecol);
pushCommand(drawVectorText, meridian, timeFontSize*9/20, x + width, y, 1, -1, timecol); pushCommand(drawVectorText, meridian, timeFontSize*9/20, x + width, y, 1, -1, timecol);
if (showSeconds) pushCommand(drawVectorText, secondsText, timeFontSize*9/20, x + width, y + timeHeight, 1, 1, timecol); if (showSeconds) pushCommand(drawVectorText, secondsText, timeFontSize*9/20, x + width, y + timeHeight, 1, 1, timecol);
y += timeHeight + spacer; y += timeHeight + spacer;
pushCommand(drawVectorText, dowText, dowFontSize, x + width/2, y, 0, -1, dowcol); pushCommand(drawVectorText, dowText, dowFontSize, x + width/2, y, 0, -1, dowcol);
y += dowHeight + spacer; y += dowHeight + spacer;
pushCommand(drawVectorText, dateText, dateFontSize, x + width/2, y, 0, -1, datecol); pushCommand(drawVectorText, dateText, dateFontSize, x + width/2, y, 0, -1, datecol);
executeCommands(); executeCommands();
} }
@ -90,6 +90,57 @@ function tick() {
timeout = setTimeout(tick, period - getTime() * 1000 % period); timeout = setTimeout(tick, period - getTime() * 1000 % period);
} }
// ====================================== Vibration (taken from "Vibrate Clock")
// vibrate 0..9
function vibrateDigitBuzz(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);
});
});
}
function vibrateDigitBeep(num) {
if (num==0) return Bangle.beep(500);
return new Promise(function f(resolve){
if (num--<=0) return resolve();
Bangle.beep(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);
if ("buzz"==chimetype)
vibrateDigitBuzz(digit).then(()=>{
setTimeout(()=>f(resolve),500);});
if ("beep"==chimetype)
vibrateDigitBeep(digit).then(()=>{
setTimeout(()=>f(resolve),500);});
});
}
var vibrateBusy;
function vibrateTime() {
if (vibrateBusy) return;
vibrateBusy = true;
var d = new Date();
var hours = d.getHours(), minutes = d.getMinutes();
if (is12Hour) {
if (hours == 0) hours = 12;
else if (hours>12) hours -= 12;
}
vibrateNumber(hours.toString()).
then(() => new Promise(resolve=>setTimeout(resolve,500))).
then(() => vibrateNumber(minutes.toString())).
then(() => vibrateBusy=false);
}
Bangle.on('lcdPower', function(on) { Bangle.on('lcdPower', function(on) {
if (timeout) clearTimeout(timeout); if (timeout) clearTimeout(timeout);
timeout = null; timeout = null;
@ -103,6 +154,10 @@ Bangle.on('lock', function(locked) {
tick(); tick();
}); });
if ("buzz"==chimetype || "beep"==chimetype)
Bangle.on("swipe",function(direction){
if (0==direction) vibrateTime();}); // 0 = swipe up/down
g.clear(); g.clear();
tick(); tick();
Bangle.loadWidgets(); Bangle.loadWidgets();