[Nifty-A Clock] Use ClockFace library

pull/1935/head
Alessandro Cocco 2022-06-07 21:20:02 +02:00
parent 48a14da4e1
commit 7222465e50
1 changed files with 36 additions and 82 deletions

View File

@ -1,22 +1,3 @@
const locale = require("locale");
const is12Hour = Object.assign({ "12hour": false }, require("Storage").readJSON("setting.json", true))["12hour"];
const showWeekNum = Object.assign({ showWeekNum: true }, require('Storage').readJSON("ffcniftya.json", true))["showWeekNum"];
/* Clock *********************************************/
const scale = g.getWidth() / 176;
const widget = 24;
const viewport = {
width: g.getWidth(),
height: g.getHeight(),
}
const center = {
x: viewport.width / 2,
y: Math.round(((viewport.height - widget) / 2) + widget),
}
// copied from: https://gist.github.com/IamSilviu/5899269#gistcomment-3035480 // copied from: https://gist.github.com/IamSilviu/5899269#gistcomment-3035480
function ISO8601_week_no(date) { function ISO8601_week_no(date) {
var tdt = new Date(date.valueOf()); var tdt = new Date(date.valueOf());
@ -30,77 +11,50 @@ function ISO8601_week_no(date) {
return 1 + Math.ceil((firstThursday - tdt) / 604800000); return 1 + Math.ceil((firstThursday - tdt) / 604800000);
} }
function d02(value) { function format(value) {
return ('0' + value).substr(-2); return ("0" + value).substr(-2);
} }
function draw() { const ClockFace = require("ClockFace");
g.reset(); const clock = new ClockFace({
g.clearRect(0, widget, viewport.width, viewport.height); init: function () {
const now = new Date(); const appRect = Bangle.appRect;
const hour = d02(now.getHours() - (is12Hour && now.getHours() > 12 ? 12 : 0)); this.viewport = {
const minutes = d02(now.getMinutes()); width: appRect.w,
const day = d02(now.getDate()); height: appRect.h
const month = d02(now.getMonth() + 1); };
const year = now.getFullYear(now);
const weekNum = d02(ISO8601_week_no(now));
const monthName = locale.month(now, 3);
const dayName = locale.dow(now, 3);
const centerTimeScaleX = center.x + 32 * scale; this.center = {
g.setFontAlign(1, 0).setFont("Vector", 90 * scale); x: this.viewport.width / 2,
g.drawString(hour, centerTimeScaleX, center.y - 31 * scale); y: Math.round((this.viewport.height / 2) + appRect.y)
g.drawString(minutes, centerTimeScaleX, center.y + 46 * scale); };
g.fillRect(center.x + 30 * scale, center.y - 72 * scale, center.x + 32 * scale, center.y + 74 * scale); this.scale = g.getWidth() / this.viewport.width;
this.centerTimeScaleX = this.center.x + 32 * this.scale;
this.centerDatesScaleX = this.center.x + 40 * this.scale;
const centerDatesScaleX = center.x + 40 * scale; this.showWeekNum = Object.assign({ showWeekNum: true }, require("Storage").readJSON("ffcniftya.json", true))["showWeekNum"];
g.setFontAlign(-1, 0).setFont("Vector", 16 * scale); },
g.drawString(year, centerDatesScaleX, center.y - 62 * scale); draw: function (date) {
g.drawString(month, centerDatesScaleX, center.y - 44 * scale); const hour = date.getHours() - (this.is12Hour && date.getHours() > 12 ? 12 : 0);
g.drawString(day, centerDatesScaleX, center.y - 26 * scale); const month = date.getMonth() + 1;
if (showWeekNum) g.drawString(weekNum, centerDatesScaleX, center.y + 15 * scale); const monthName = require("date_utils").month(month, 1);
g.drawString(monthName, centerDatesScaleX, center.y + 48 * scale); const dayName = require("date_utils").dow(date.getDay(), 1);
g.drawString(dayName, centerDatesScaleX, center.y + 66 * scale);
}
g.setFontAlign(1, 0).setFont("Vector", 90 * this.scale);
g.drawString(format(hour), this.centerTimeScaleX, this.center.y - 31 * this.scale);
g.drawString(format(date.getMinutes()), this.centerTimeScaleX, this.center.y + 46 * this.scale);
/* Minute Ticker *************************************/ g.fillRect(this.center.x + 30 * this.scale, this.center.y - 72 * this.scale, this.center.x + 32 * this.scale, this.center.y + 74 * this.scale);
let tickTimer; g.setFontAlign(-1, 0).setFont("Vector", 16 * this.scale);
g.drawString(date.getFullYear(date), this.centerDatesScaleX, this.center.y - 62 * this.scale);
function clearTickTimer() { g.drawString(format(month), this.centerDatesScaleX, this.center.y - 44 * this.scale);
if (tickTimer) { g.drawString(format(date.getDate()), this.centerDatesScaleX, this.center.y - 26 * this.scale);
clearTimeout(tickTimer); if (this.showWeekNum) g.drawString(format(ISO8601_week_no(date)), this.centerDatesScaleX, this.center.y + 15 * this.scale);
tickTimer = undefined; g.drawString(monthName, this.centerDatesScaleX, this.center.y + 48 * this.scale);
} g.drawString(dayName, this.centerDatesScaleX, this.center.y + 66 * this.scale);
}
function queueNextTick() {
clearTickTimer();
tickTimer = setTimeout(tick, 60000 - (Date.now() % 60000));
}
function tick() {
draw();
queueNextTick();
}
/* Init **********************************************/
// Clear the screen once, at startup
g.clear();
tick();
Bangle.on('lcdPower', (on) => {
if (on) {
tick();
} else {
clearTickTimer();
} }
}); });
clock.start();
Bangle.setUI("clock");
Bangle.loadWidgets();
Bangle.drawWidgets();