Merge pull request #1360 from HilmarSt/master

Add "Chime the Time" to Vector Clock
master
Gordon Williams 2022-01-31 10:48:03 +00:00 committed by GitHub
commit 7f21a5d46d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 12 deletions

View File

@ -2,3 +2,4 @@
0.02: Use Bangle.setUI for button/launcher handling
0.03: Bangle.js 2 support
0.04: Adds costumizable colours and the respective settings menu
0.05: "Chime the time" (buzz or beep) with up/down swipe added

View File

@ -4,7 +4,7 @@ var settings = require('Storage').readJSON("vectorclock.json", true) || {};
var dowcol = settings.dowcol || g.theme.fg;
var timecol = settings.timecol || g.theme.fg;
var datecol = settings.datecol || g.theme.fg;
var chimetype = settings.chimetype;
function padNum(n, l) {
return ("0".repeat(l)+n).substr(-l);
@ -44,7 +44,7 @@ function drawVectorText(text, size, x, y, alignX, alignY, color) {
function draw() {
g.reset();
let d = new Date();
let hours = is12Hour ? ((d.getHours() + 11) % 12) + 1 : d.getHours();
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 dowFontSize = width / (g.stringWidth(dowText) / 256);
let dateFontSize = width / (g.stringWidth(dateText) / 256);
let timeHeight = g.setFont("Vector", timeFontSize).getFontHeight() * 9 / 10;
let dowHeight = g.setFont("Vector", dowFontSize).getFontHeight();
let dateHeight = g.setFont("Vector", dateFontSize).getFontHeight();
let remainingHeight = g.getHeight() - 24 - timeHeight - dowHeight - dateHeight;
let spacer = remainingHeight / 4;
let x = 2;
let y = 24 + spacer;
pushCommand(drawVectorText, timeText, timeFontSize, x, 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);
y += timeHeight + spacer;
pushCommand(drawVectorText, dowText, dowFontSize, x + width/2, y, 0, -1, dowcol);
y += dowHeight + spacer;
pushCommand(drawVectorText, dateText, dateFontSize, x + width/2, y, 0, -1, datecol);
executeCommands();
}
@ -90,6 +90,57 @@ function tick() {
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) {
if (timeout) clearTimeout(timeout);
timeout = null;
@ -103,6 +154,10 @@ Bangle.on('lock', function(locked) {
tick();
});
if ("buzz"==chimetype || "beep"==chimetype)
Bangle.on("swipe",function(direction){
if (0==direction) vibrateTime();}); // 0 = swipe up/down
g.clear();
tick();
Bangle.loadWidgets();

View File

@ -1,7 +1,7 @@
{
"id": "vectorclock",
"name": "Vector Clock",
"version": "0.04",
"version": "0.05",
"description": "A digital clock that uses the built-in vector font.",
"icon": "app.png",
"type": "clock",

View File

@ -10,10 +10,10 @@
var colnames = ["white", "yellow", "green", "cyan", "red", "orange", "magenta", "black"];
var colvalues = [0xFFFF, 0xFFE0, 0x07E0, 0x07FF, 0xF800, 0xFD20, 0xF81F, 0x0000];
var chimenames = ["off", "buzz", "beep"];
// Show the menu
E.showMenu({
"" : { "title" : "VectorClock colours" },
"< Back" : () => back(),
"" : { "title" : "VectorClock settings" },
'Time': {
value: Math.max(0 | colvalues.indexOf(settings.timecol),0),
min: 0, max: colvalues.length-1,
@ -41,5 +41,15 @@
writeSettings();
}
},
'Chimetype': {
value: Math.max(0 | chimenames.indexOf(settings.chimetype),0),
min: 0, max: chimenames.length-1,
format: v => chimenames[v],
onchange: v => {
settings.chimetype = chimenames[v];
writeSettings();
}
},
"< Back" : () => back(),
});
})