// http://forum.espruino.com/conversations/345155/#comment15172813 const locale = require('locale'); const p = Math.PI / 2; const pRad = Math.PI / 180; const faceWidth = g.getWidth()/2 - 20; // watch face radius (240/2 - 24px for widget area) const widgetHeight=24+1; let timer = null; let currentDate = new Date(); const centerX = g.getWidth() / 2; const centerY = (g.getWidth() / 2) + widgetHeight/2; g.theme.dark=false; let colSecA = g.theme.dark ? "#00A" : "#58F"; // before the second let colSecB = g.theme.dark ? "#58F" : "#00A"; // after the second let colSec1 = g.theme.dark ? "#F83" : "#000"; // ON the second const seconds = (angle) => { const a = angle * pRad; const x = centerX + Math.sin(a) * faceWidth; const y = centerY - Math.cos(a) * faceWidth; // 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; const r3 = 3; g.fillPoly([ 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) ]); }; const drawAll = () => { g.clear(); currentDate = new Date(); // draw hands first onMinute(); // draw seconds const currentSec = currentDate.getSeconds(); // draw all secs for (let i = 0; i < 60; i++) { g.setColor((i > currentSec) ? colSecA : colSecB); seconds((360 * i) / 60); } onSecond(); }; const resetSeconds = () => { g.setColor(colSecA); for (let i = 0; i < 60; i++) { seconds((360 * i) / 60); } }; const onSecond = () => { g.setColor(colSecB); seconds((360 * currentDate.getSeconds()) / 60); if (currentDate.getSeconds() === 59) { resetSeconds(); onMinute(); } g.setColor(colSec1); currentDate = new Date(); seconds((360 * currentDate.getSeconds()) / 60); g.setColor(g.theme.fg); }; const drawDate = () => { g.reset(); g.setColor("#f00"); g.setFont('6x8', 2); const dayString = locale.dow(currentDate, true); // pad left date const dateString = ("0"+currentDate.getDate().toString()).substr(-2); const dateDisplay = `${dayString}-${dateString}`; // console.log(`${dayString}|${dateString}`); // center date const l = (g.getWidth() - g.stringWidth(dateDisplay)) / 2; const t = centerY + faceWidth*0.37; g.drawString(dateDisplay, l, t, true); // console.log(l, t); }; const onMinute = () => { if (currentDate.getHours() === 0 && currentDate.getMinutes() === 0) { g.clear(); resetSeconds(); } // clear existing hands g.setColor(g.theme.bg); // 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(g.theme.fg); // Hour hand((360 * (currentDate.getHours() + currentDate.getMinutes() / 60)) / 12, -8, faceWidth - 35); g.setColor(g.theme.fg); // Minute hand((360 * currentDate.getMinutes()) / 60, -8, faceWidth - 10); drawDate(); }; const startTimers = () => { timer = setInterval(onSecond, 1000); }; Bangle.on('lcdPower', (on) => { if (on) { // g.clear(); drawAll(); startTimers(); Bangle.drawWidgets(); } else { if (timer) { clearInterval(timer); } } }); g.clear(); resetSeconds(); startTimers(); drawAll(); // Show launcher when button pressed Bangle.setUI("clock"); Bangle.loadWidgets(); Bangle.drawWidgets();