aclock refactor and updates
To get my feet wet with Espruino and my new Bangle.js, I dove into the analog clock code and enhanced and refactored a few things to simplify the interface, fix a few bugs, and improve readability: added date added distinct hour markers refactor timers down to a single timer add elapsed seconds display add datemaster
parent
7ff958ad2c
commit
83b5b9b8aa
|
|
@ -1,94 +1,146 @@
|
||||||
|
let g;
|
||||||
|
let Bangle;
|
||||||
|
|
||||||
const p = Math.PI / 2;
|
const p = Math.PI / 2;
|
||||||
const PRad = Math.PI/180;
|
const pRad = Math.PI / 180;
|
||||||
|
const faceWidth = 100; // watch face radius
|
||||||
|
let timer = null;
|
||||||
|
let currentDate = new Date();
|
||||||
|
const centerPx = g.getWidth() / 2;
|
||||||
|
|
||||||
let intervalRefMin = null;
|
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'];
|
||||||
let intervalRefSec = null;
|
|
||||||
|
|
||||||
let minuteDate = new Date();
|
const seconds = (angle) => {
|
||||||
let secondDate = new Date();
|
const a = angle * pRad;
|
||||||
|
const x = centerPx + Math.sin(a) * faceWidth;
|
||||||
|
const y = centerPx - Math.cos(a) * faceWidth;
|
||||||
|
|
||||||
function seconds(angle, r) {
|
// if 15 degrees, make hour marker larger
|
||||||
const a = angle*PRad;
|
const radius = (angle % 15) ? 2 : 4;
|
||||||
const x = 120+Math.sin(a)*r;
|
g.fillCircle(x, y, radius);
|
||||||
const y = 120-Math.cos(a)*r;
|
};
|
||||||
g.fillRect(x-1,y-1,x+1,y+1);
|
|
||||||
}
|
const hand = (angle, r1, r2) => {
|
||||||
function hand(angle, r1,r2) {
|
const a = angle * pRad;
|
||||||
const a = angle*PRad;
|
|
||||||
const r3 = 3;
|
const r3 = 3;
|
||||||
g.fillPoly([
|
|
||||||
120+Math.sin(a)*r1,
|
|
||||||
120-Math.cos(a)*r1,
|
|
||||||
120+Math.sin(a+p)*r3,
|
|
||||||
120-Math.cos(a+p)*r3,
|
|
||||||
120+Math.sin(a)*r2,
|
|
||||||
120-Math.cos(a)*r2,
|
|
||||||
120+Math.sin(a-p)*r3,
|
|
||||||
120-Math.cos(a-p)*r3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function drawAll() {
|
g.fillPoly([
|
||||||
|
Math.round(centerPx + Math.sin(a) * r1),
|
||||||
|
Math.round(centerPx - Math.cos(a) * r1),
|
||||||
|
Math.round(centerPx + Math.sin(a + p) * r3),
|
||||||
|
Math.round(centerPx - Math.cos(a + p) * r3),
|
||||||
|
Math.round(centerPx + Math.sin(a) * r2),
|
||||||
|
Math.round(centerPx - Math.cos(a) * r2),
|
||||||
|
Math.round(centerPx + Math.sin(a - p) * r3),
|
||||||
|
Math.round(centerPx - Math.cos(a - p) * r3)
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const drawAll = () => {
|
||||||
g.clear();
|
g.clear();
|
||||||
secondDate = minuteDate = new Date();
|
currentDate = new Date();
|
||||||
// draw hands first
|
// draw hands first
|
||||||
onMinute();
|
onMinute();
|
||||||
// draw seconds
|
// draw seconds
|
||||||
|
const currentSec = currentDate.getSeconds();
|
||||||
|
// draw all secs
|
||||||
|
|
||||||
|
for (let i = 0; i < 60; i++) {
|
||||||
|
if (i > currentSec) {
|
||||||
g.setColor(0, 0, 0.6);
|
g.setColor(0, 0, 0.6);
|
||||||
for (let i=0;i<60;i++)
|
} else {
|
||||||
seconds(360*i/60, 90);
|
g.setColor(0.3, 0.3, 1);
|
||||||
|
}
|
||||||
|
seconds((360 * i) / 60);
|
||||||
|
}
|
||||||
onSecond();
|
onSecond();
|
||||||
}
|
};
|
||||||
|
|
||||||
function onSecond() {
|
const resetSeconds = () => {
|
||||||
g.setColor(0, 0, 0.6);
|
g.setColor(0, 0, 0.6);
|
||||||
seconds(360*secondDate.getSeconds()/60, 90);
|
for (let i = 0; i < 60; i++) {
|
||||||
g.setColor(1,0,0);
|
seconds((360 * i) / 60);
|
||||||
secondDate = new Date();
|
|
||||||
seconds(360*secondDate.getSeconds()/60, 90);
|
|
||||||
g.setColor(1,1,1);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function onMinute() {
|
const onSecond = () => {
|
||||||
g.setColor(0,0,0);
|
g.setColor(0.3, 0.3, 1);
|
||||||
hand(360*(minuteDate.getHours() + (minuteDate.getMinutes()/60))/12, -10, 50);
|
seconds((360 * currentDate.getSeconds()) / 60);
|
||||||
hand(360*minuteDate.getMinutes()/60, -10, 82);
|
if (currentDate.getSeconds() === 59) {
|
||||||
minuteDate = new Date();
|
resetSeconds();
|
||||||
|
onMinute();
|
||||||
|
}
|
||||||
|
g.setColor(1, 0.7, 0.2);
|
||||||
|
currentDate = new Date();
|
||||||
|
seconds((360 * currentDate.getSeconds()) / 60);
|
||||||
g.setColor(1, 1, 1);
|
g.setColor(1, 1, 1);
|
||||||
hand(360*(minuteDate.getHours() + (minuteDate.getMinutes()/60))/12, -10, 50);
|
};
|
||||||
hand(360*minuteDate.getMinutes()/60, -10, 82);
|
|
||||||
if(minuteDate.getHours() >= 0 && minuteDate.getMinutes() === 0) {
|
const drawDate = () => {
|
||||||
|
g.reset();
|
||||||
|
g.setColor(1, 0, 0);
|
||||||
|
g.setFont('6x8', 2);
|
||||||
|
|
||||||
|
const dayString = days[currentDate.getDay()];
|
||||||
|
// pad left date
|
||||||
|
const dateString = (currentDate.getDate() < 10) ? '0' : '' + currentDate.getDate().toString();
|
||||||
|
const dateDisplay = `${dayString}-${dateString}`;
|
||||||
|
// console.log(`${dayString}|${dateString}`);
|
||||||
|
// center date
|
||||||
|
const l = (g.getWidth() - g.stringWidth(dateDisplay)) / 2;
|
||||||
|
const t = centerPx + 37;
|
||||||
|
g.drawString(dateDisplay, l, t);
|
||||||
|
// console.log(l, t);
|
||||||
|
};
|
||||||
|
const onMinute = () => {
|
||||||
|
if (currentDate.getHours() === 0 && currentDate.getMinutes() === 0) {
|
||||||
|
g.clear();
|
||||||
|
resetSeconds();
|
||||||
|
}
|
||||||
|
// clear existing hands
|
||||||
|
g.setColor(0, 0, 0);
|
||||||
|
// Hour
|
||||||
|
hand((360 * (currentDate.getHours() + currentDate.getMinutes() / 60)) / 12, -8, faceWidth - 35);
|
||||||
|
// Minute
|
||||||
|
hand((360 * currentDate.getMinutes()) / 60, -8, faceWidth - 10);
|
||||||
|
|
||||||
|
// get new date, then draw new hands
|
||||||
|
currentDate = new Date();
|
||||||
|
g.setColor(1, 0.9, 0.9);
|
||||||
|
// Hour
|
||||||
|
hand((360 * (currentDate.getHours() + currentDate.getMinutes() / 60)) / 12, -8, faceWidth - 35);
|
||||||
|
g.setColor(1, 1, 0.9);
|
||||||
|
// Minute
|
||||||
|
hand((360 * currentDate.getMinutes()) / 60, -8, faceWidth - 10);
|
||||||
|
if (currentDate.getHours() >= 0 && currentDate.getMinutes() === 0) {
|
||||||
Bangle.buzz();
|
Bangle.buzz();
|
||||||
}
|
}
|
||||||
}
|
drawDate();
|
||||||
|
};
|
||||||
|
|
||||||
function clearTimers() {
|
const startTimers = () => {
|
||||||
if(intervalRefMin) {clearInterval(intervalRefMin);}
|
timer = setInterval(onSecond, 1000);
|
||||||
if(intervalRefSec) {clearInterval(intervalRefSec);}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
function startTimers() {
|
Bangle.on('lcdPower', (on) => {
|
||||||
minuteDate = new Date();
|
|
||||||
secondDate = new Date();
|
|
||||||
intervalRefSec = setInterval(onSecond,1000);
|
|
||||||
intervalRefMin = setInterval(onMinute,60*1000);
|
|
||||||
drawAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
Bangle.on('lcdPower',function(on) {
|
|
||||||
if (on) {
|
if (on) {
|
||||||
g.clear();
|
// g.clear();
|
||||||
Bangle.drawWidgets();
|
drawAll();
|
||||||
startTimers();
|
startTimers();
|
||||||
|
Bangle.drawWidgets();
|
||||||
} else {
|
} else {
|
||||||
clearTimers();
|
if (timer) {
|
||||||
|
clearInterval(timer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
g.clear();
|
g.clear();
|
||||||
|
resetSeconds();
|
||||||
|
startTimers();
|
||||||
|
drawAll();
|
||||||
Bangle.loadWidgets();
|
Bangle.loadWidgets();
|
||||||
Bangle.drawWidgets();
|
Bangle.drawWidgets();
|
||||||
drawAll();
|
|
||||||
startTimers();
|
|
||||||
// Show launcher when middle button pressed
|
// Show launcher when middle button pressed
|
||||||
setWatch(Bangle.showLauncher, BTN2, { repeat: false, edge: "falling" });
|
setWatch(Bangle.showLauncher, BTN2, { repeat: false, edge: "falling" });
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue