[Nifty-B Clock] Use ClockFace library
parent
146244aecf
commit
d9a0884c5a
|
|
@ -1,20 +1,10 @@
|
||||||
const is12Hour = Object.assign({ "12hour": false }, require("Storage").readJSON("setting.json", true))["12hour"];
|
var scale;
|
||||||
const color = Object.assign({ color: 63488 }, require("Storage").readJSON("ffcniftyb.json", true)).color; // Default to RED
|
var screen;
|
||||||
|
var center;
|
||||||
|
var buf;
|
||||||
|
var img;
|
||||||
|
|
||||||
/* Clock *********************************************/
|
function format(value) {
|
||||||
const scale = g.getWidth() / 176;
|
|
||||||
|
|
||||||
const screen = {
|
|
||||||
width: g.getWidth(),
|
|
||||||
height: g.getHeight() - 24,
|
|
||||||
};
|
|
||||||
|
|
||||||
const center = {
|
|
||||||
x: screen.width / 2,
|
|
||||||
y: screen.height / 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
function d02(value) {
|
|
||||||
return ("0" + value).substr(-2);
|
return ("0" + value).substr(-2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,37 +12,45 @@ function renderEllipse(g) {
|
||||||
g.fillEllipse(center.x - 5 * scale, center.y - 70 * scale, center.x + 160 * scale, center.y + 90 * scale);
|
g.fillEllipse(center.x - 5 * scale, center.y - 70 * scale, center.x + 160 * scale, center.y + 90 * scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderText(g) {
|
function renderText(g, date) {
|
||||||
const now = new Date();
|
const hour = date.getHours() - (this.is12Hour && date.getHours() > 12 ? 12 : 0);
|
||||||
|
const month = date.getMonth() + 1;
|
||||||
|
|
||||||
const hour = d02(now.getHours() - (is12Hour && now.getHours() > 12 ? 12 : 0));
|
const monthName = require("date_utils").month(month, 1);
|
||||||
const minutes = d02(now.getMinutes());
|
const dayName = require("date_utils").dow(date.getDay(), 1);
|
||||||
const day = d02(now.getDate());
|
|
||||||
const month = d02(now.getMonth() + 1);
|
|
||||||
const year = now.getFullYear();
|
|
||||||
|
|
||||||
const month2 = require("locale").month(now, 3);
|
|
||||||
const day2 = require("locale").dow(now, 3);
|
|
||||||
|
|
||||||
g.setFontAlign(1, 0).setFont("Vector", 90 * scale);
|
g.setFontAlign(1, 0).setFont("Vector", 90 * scale);
|
||||||
g.drawString(hour, center.x + 32 * scale, center.y - 31 * scale);
|
g.drawString(format(hour), center.x + 32 * scale, center.y - 31 * scale);
|
||||||
g.drawString(minutes, center.x + 32 * scale, center.y + 46 * scale);
|
g.drawString(format(date.getMinutes()), center.x + 32 * scale, center.y + 46 * scale);
|
||||||
|
|
||||||
g.setFontAlign(1, 0).setFont("Vector", 16 * scale);
|
g.setFontAlign(1, 0).setFont("Vector", 16 * scale);
|
||||||
g.drawString(year, center.x + 80 * scale, center.y - 42 * scale);
|
g.drawString(date.getFullYear(), center.x + 80 * scale, center.y - 42 * scale);
|
||||||
g.drawString(month, center.x + 80 * scale, center.y - 26 * scale);
|
g.drawString(format(month), center.x + 80 * scale, center.y - 26 * scale);
|
||||||
g.drawString(day, center.x + 80 * scale, center.y - 10 * scale);
|
g.drawString(format(date.getDate()), center.x + 80 * scale, center.y - 10 * scale);
|
||||||
g.drawString(month2, center.x + 80 * scale, center.y + 44 * scale);
|
g.drawString(monthName, center.x + 80 * scale, center.y + 44 * scale);
|
||||||
g.drawString(day2, center.x + 80 * scale, center.y + 60 * scale);
|
g.drawString(dayName, center.x + 80 * scale, center.y + 60 * scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
const buf = Graphics.createArrayBuffer(screen.width, screen.height, 1, {
|
const ClockFace = require("ClockFace");
|
||||||
msb: true
|
const clock = new ClockFace({
|
||||||
});
|
init: function () {
|
||||||
|
const appRect = Bangle.appRect;
|
||||||
|
|
||||||
function draw() {
|
screen = {
|
||||||
|
width: appRect.w,
|
||||||
|
height: appRect.h
|
||||||
|
};
|
||||||
|
|
||||||
const img = {
|
center = {
|
||||||
|
x: screen.width / 2,
|
||||||
|
y: screen.height / 2
|
||||||
|
};
|
||||||
|
|
||||||
|
buf = Graphics.createArrayBuffer(screen.width, screen.height, 1, { msb: true });
|
||||||
|
|
||||||
|
scale = g.getWidth() / screen.width;
|
||||||
|
|
||||||
|
img = {
|
||||||
width: screen.width,
|
width: screen.width,
|
||||||
height: screen.height,
|
height: screen.height,
|
||||||
transparent: 0,
|
transparent: 0,
|
||||||
|
|
@ -60,53 +58,23 @@ function draw() {
|
||||||
buffer: buf.buffer
|
buffer: buf.buffer
|
||||||
};
|
};
|
||||||
|
|
||||||
// cleat screen area
|
// default to RED (see settings.js)
|
||||||
g.clearRect(0, 24, g.getWidth(), g.getHeight());
|
// don't use || to default because 0 is a valid color
|
||||||
|
this.color = this.color === undefined ? 63488 : this.color;
|
||||||
|
},
|
||||||
|
draw: function (date) {
|
||||||
// render outside text with ellipse
|
// render outside text with ellipse
|
||||||
buf.clear();
|
buf.clear();
|
||||||
renderText(buf.setColor(1));
|
renderText(buf.setColor(1), date);
|
||||||
renderEllipse(buf.setColor(0));
|
renderEllipse(buf.setColor(0));
|
||||||
g.setColor(color).drawImage(img, 0, 24);
|
g.setColor(this.color).drawImage(img, 0, 24);
|
||||||
|
|
||||||
// render ellipse with inside text
|
// render ellipse with inside text
|
||||||
buf.clear();
|
buf.clear();
|
||||||
renderEllipse(buf.setColor(1));
|
renderEllipse(buf.setColor(1));
|
||||||
renderText(buf.setColor(0));
|
renderText(buf.setColor(0), date);
|
||||||
g.setColor(color).drawImage(img, 0, 24);
|
g.setColor(this.color).drawImage(img, 0, 24);
|
||||||
}
|
},
|
||||||
|
settingsFile: "ffcniftyb.json"
|
||||||
|
|
||||||
/* Minute Ticker *************************************/
|
|
||||||
|
|
||||||
let ticker;
|
|
||||||
|
|
||||||
function stopTick() {
|
|
||||||
if (ticker) {
|
|
||||||
clearTimeout(ticker);
|
|
||||||
ticker = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function startTick(run) {
|
|
||||||
stopTick();
|
|
||||||
run();
|
|
||||||
ticker = setTimeout(() => startTick(run), 60000 - (Date.now() % 60000));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Init **********************************************/
|
|
||||||
|
|
||||||
g.clear();
|
|
||||||
startTick(draw);
|
|
||||||
|
|
||||||
Bangle.on("lcdPower", (on) => {
|
|
||||||
if (on) {
|
|
||||||
startTick(draw);
|
|
||||||
} else {
|
|
||||||
stopTick();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
clock.start();
|
||||||
Bangle.setUI("clock");
|
|
||||||
Bangle.loadWidgets();
|
|
||||||
Bangle.drawWidgets();
|
|
||||||
Loading…
Reference in New Issue