2020-03-30 17:09:46 +00:00
|
|
|
// http://forum.espruino.com/conversations/345155/#comment15172813
|
|
|
|
const locale = require('locale');
|
2020-03-30 15:37:59 +00:00
|
|
|
const p = Math.PI / 2;
|
|
|
|
const pRad = Math.PI / 180;
|
2020-04-04 17:54:57 +00:00
|
|
|
const faceWidth = 100; // watch face radius (240/2 - 24px for widget area)
|
|
|
|
const widgetHeight=24+1;
|
2020-03-30 15:37:59 +00:00
|
|
|
let timer = null;
|
|
|
|
let currentDate = new Date();
|
2020-04-04 17:54:57 +00:00
|
|
|
const centerX = g.getWidth() / 2;
|
|
|
|
const centerY = (g.getWidth() / 2) + widgetHeight/2;
|
|
|
|
|
2020-03-30 15:37:59 +00:00
|
|
|
|
|
|
|
const seconds = (angle) => {
|
|
|
|
const a = angle * pRad;
|
2020-04-04 17:54:57 +00:00
|
|
|
const x = centerX + Math.sin(a) * faceWidth;
|
|
|
|
const y = centerY - Math.cos(a) * faceWidth;
|
2020-03-30 15:37:59 +00:00
|
|
|
|
|
|
|
// if 15 degrees, make hour marker larger
|
|
|
|
const radius = (angle % 15) ? 2 : 4;
|
|
|
|
g.fillCircle(x, y, radius);
|
|
|
|
};
|
|
|
|
|
|
|
|
const hand = (angle, r1, r2) => {
|
|
|
|
const a = angle * pRad;
|
2020-01-17 11:43:26 +00:00
|
|
|
const r3 = 3;
|
2020-03-30 15:37:59 +00:00
|
|
|
|
2020-01-17 11:43:26 +00:00
|
|
|
g.fillPoly([
|
2020-04-04 17:54:57 +00:00
|
|
|
Math.round(centerX + Math.sin(a) * r1),
|
|
|
|
Math.round(centerY - Math.cos(a) * r1),
|
|
|
|
Math.round(centerX + Math.sin(a + p) * r3),
|
|
|
|
Math.round(centerY - Math.cos(a + p) * r3),
|
|
|
|
Math.round(centerX + Math.sin(a) * r2),
|
|
|
|
Math.round(centerY - Math.cos(a) * r2),
|
|
|
|
Math.round(centerX + Math.sin(a - p) * r3),
|
|
|
|
Math.round(centerY - Math.cos(a - p) * r3)
|
2020-03-30 15:37:59 +00:00
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const drawAll = () => {
|
2019-11-11 13:01:47 +00:00
|
|
|
g.clear();
|
2020-03-30 15:37:59 +00:00
|
|
|
currentDate = new Date();
|
2020-01-17 11:43:26 +00:00
|
|
|
// draw hands first
|
|
|
|
onMinute();
|
|
|
|
// draw seconds
|
2020-03-30 15:37:59 +00:00
|
|
|
const currentSec = currentDate.getSeconds();
|
|
|
|
// draw all secs
|
|
|
|
|
|
|
|
for (let i = 0; i < 60; i++) {
|
|
|
|
if (i > currentSec) {
|
|
|
|
g.setColor(0, 0, 0.6);
|
|
|
|
} else {
|
|
|
|
g.setColor(0.3, 0.3, 1);
|
|
|
|
}
|
|
|
|
seconds((360 * i) / 60);
|
|
|
|
}
|
2020-01-17 11:43:26 +00:00
|
|
|
onSecond();
|
2020-04-04 17:54:57 +00:00
|
|
|
|
2020-03-30 15:37:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const resetSeconds = () => {
|
|
|
|
g.setColor(0, 0, 0.6);
|
|
|
|
for (let i = 0; i < 60; i++) {
|
|
|
|
seconds((360 * i) / 60);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSecond = () => {
|
|
|
|
g.setColor(0.3, 0.3, 1);
|
|
|
|
seconds((360 * currentDate.getSeconds()) / 60);
|
|
|
|
if (currentDate.getSeconds() === 59) {
|
|
|
|
resetSeconds();
|
|
|
|
onMinute();
|
|
|
|
}
|
|
|
|
g.setColor(1, 0.7, 0.2);
|
|
|
|
currentDate = new Date();
|
|
|
|
seconds((360 * currentDate.getSeconds()) / 60);
|
|
|
|
g.setColor(1, 1, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
const drawDate = () => {
|
|
|
|
g.reset();
|
|
|
|
g.setColor(1, 0, 0);
|
|
|
|
g.setFont('6x8', 2);
|
|
|
|
|
2020-03-30 17:09:46 +00:00
|
|
|
const dayString = locale.dow(currentDate, true);
|
2020-03-30 15:37:59 +00:00
|
|
|
// 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;
|
2020-04-04 17:54:57 +00:00
|
|
|
const t = centerY + 37;
|
|
|
|
g.drawString(dateDisplay, l, t, true);
|
2020-03-30 15:37:59 +00:00
|
|
|
// 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) {
|
2020-01-17 11:43:26 +00:00
|
|
|
Bangle.buzz();
|
2019-11-11 13:01:47 +00:00
|
|
|
}
|
2020-03-30 15:37:59 +00:00
|
|
|
drawDate();
|
|
|
|
};
|
|
|
|
|
|
|
|
const startTimers = () => {
|
|
|
|
timer = setInterval(onSecond, 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
Bangle.on('lcdPower', (on) => {
|
2020-01-17 11:43:26 +00:00
|
|
|
if (on) {
|
2020-03-30 15:37:59 +00:00
|
|
|
// g.clear();
|
|
|
|
drawAll();
|
2020-01-17 11:43:26 +00:00
|
|
|
startTimers();
|
2020-03-30 15:37:59 +00:00
|
|
|
Bangle.drawWidgets();
|
|
|
|
} else {
|
|
|
|
if (timer) {
|
|
|
|
clearInterval(timer);
|
|
|
|
}
|
2020-01-17 11:43:26 +00:00
|
|
|
}
|
|
|
|
});
|
2019-12-17 13:56:46 +00:00
|
|
|
|
2020-01-17 11:43:26 +00:00
|
|
|
g.clear();
|
2020-03-30 15:37:59 +00:00
|
|
|
resetSeconds();
|
|
|
|
startTimers();
|
|
|
|
drawAll();
|
2020-01-17 11:43:26 +00:00
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
2020-03-30 15:37:59 +00:00
|
|
|
|
2020-01-17 11:43:26 +00:00
|
|
|
// Show launcher when middle button pressed
|
2020-03-30 15:37:59 +00:00
|
|
|
setWatch(Bangle.showLauncher, BTN2, { repeat: false, edge: "falling" });
|