barclock: coding style (add semicolons)

pull/833/head
Richard de Boer 2021-10-02 14:55:41 +02:00
parent a786726622
commit 346eb8f77b
1 changed files with 95 additions and 95 deletions

View File

@ -3,159 +3,159 @@
* A simple digital clock showing seconds as a bar * A simple digital clock showing seconds as a bar
**/ **/
// Check settings for what type our clock should be // Check settings for what type our clock should be
const is12Hour = (require('Storage').readJSON('setting.json', 1) || {})['12hour'] const is12Hour = (require("Storage").readJSON("setting.json", 1) || {})["12hour"];
let locale = require('locale') let locale = require("locale");
{ // add some more info to locale { // add some more info to locale
let date = new Date() let date = new Date();
date.setFullYear(1111) date.setFullYear(1111);
date.setMonth(1, 3) // februari: months are zero-indexed date.setMonth(1, 3); // februari: months are zero-indexed
const localized = locale.date(date, true) const localized = locale.date(date, true);
locale.dayFirst = /3.*2/.test(localized) locale.dayFirst = /3.*2/.test(localized);
locale.hasMeridian = false locale.hasMeridian = false;
if(typeof locale.meridian === 'function') { // function does not exists if languages app is not installed if (typeof locale.meridian==="function") { // function does not exists if languages app is not installed
locale.hasMeridian = (locale.meridian(date) !== '') locale.hasMeridian = (locale.meridian(date)!=="");
} }
} }
const screen = { const screen = {
width: g.getWidth(), width: g.getWidth(),
height: g.getWidth(), height: g.getWidth(),
middle: g.getWidth() / 2, middle: g.getWidth()/2,
center: g.getHeight() / 2, center: g.getHeight()/2,
} };
// hardcoded "settings" // hardcoded "settings"
const settings = { const settings = {
time: { time: {
font: '6x8', font: "6x8",
size: (is12Hour && locale.hasMeridian) ? 6 : 8, size: (is12Hour && locale.hasMeridian) ? 6 : 8,
middle: screen.middle, middle: screen.middle,
center: screen.center, center: screen.center,
ampm: { ampm: {
color: -1, color: -1,
font: '6x8', font: "6x8",
size: 2, size: 2,
}, },
}, },
date: { date: {
font: 'Vector', font: "Vector",
size: 20, size: 20,
middle: screen.height - 20, // at bottom of screen middle: screen.height-20, // at bottom of screen
center: screen.center, center: screen.center,
}, },
bar: { bar: {
top: 155, // just below time top: 155, // just below time
thickness: 6, // matches 24h time "pixel" size thickness: 6, // matches 24h time "pixel" size
}, },
} };
const SECONDS_PER_MINUTE = 60 const SECONDS_PER_MINUTE = 60;
const timeText = function (date) { const timeText = function(date) {
if (!is12Hour) { if (!is12Hour) {
return locale.time(date, true) return locale.time(date, true);
} }
const date12 = new Date(date.getTime()) const date12 = new Date(date.getTime());
const hours = date12.getHours() const hours = date12.getHours();
if (hours === 0) { if (hours===0) {
date12.setHours(12) date12.setHours(12);
} else if (hours > 12) { } else if (hours>12) {
date12.setHours(hours - 12) date12.setHours(hours-12);
} }
return locale.time(date12, true) return locale.time(date12, true);
} };
const ampmText = function (date) { const ampmText = function(date) {
return is12Hour ? locale.meridian(date) : '' return is12Hour ? locale.meridian(date) : "";
} };
const dateText = function (date) { const dateText = function(date) {
const dayName = locale.dow(date, true), const dayName = locale.dow(date, true),
month = locale.month(date, true), month = locale.month(date, true),
day = date.getDate() day = date.getDate();
const dayMonth = locale.dayFirst ? `${day} ${month}` : `${month} ${day}` const dayMonth = locale.dayFirst ? `${day} ${month}` : `${month} ${day}`;
return `${dayName} ${dayMonth}` return `${dayName} ${dayMonth}`;
} };
const drawDateTime = function (date) { const drawDateTime = function(date) {
const t = settings.time const t = settings.time;
g.setFont(t.font, t.size) g.setFont(t.font, t.size);
g.setFontAlign(0, 0) // centered g.setFontAlign(0, 0); // centered
g.drawString(timeText(date), t.center, t.middle, true) g.drawString(timeText(date), t.center, t.middle, true);
if (is12Hour && locale.hasMeridian) { if (is12Hour && locale.hasMeridian) {
const a = settings.time.ampm const a = settings.time.ampm;
g.setFont(a.font, a.size) g.setFont(a.font, a.size);
g.setFontAlign(1, -1) // right top g.setFontAlign(1, -1); // right top
// at right edge of screen, aligned with time bottom // at right edge of screen, aligned with time bottom
const left = screen.width - a.size * 2, const left = screen.width-a.size*2,
top = t.middle + t.size - a.size top = t.middle+t.size-a.size;
g.drawString(ampmText(date), left, top, true) g.drawString(ampmText(date), left, top, true);
} }
const d = settings.date const d = settings.date;
g.setFont(d.font, d.size) g.setFont(d.font, d.size);
g.setFontAlign(0, 0) // centered g.setFontAlign(0, 0); // centered
g.drawString(dateText(date), d.center, d.middle, true) g.drawString(dateText(date), d.center, d.middle, true);
} };
const drawBar = function (date) { const drawBar = function(date) {
const b = settings.bar const b = settings.bar;
const seconds = date.getSeconds() const seconds = date.getSeconds();
if (seconds === 0) { if (seconds===0) {
// zero-size rect stills draws one line of pixels, we don't want that // zero-size rect stills draws one line of pixels, we don't want that
return return;
} }
const fraction = seconds / SECONDS_PER_MINUTE, const fraction = seconds/SECONDS_PER_MINUTE,
width = fraction * screen.width width = fraction*screen.width;
g.setColor(g.theme.fg2).fillRect(0, b.top, width, b.top + b.thickness) g.setColor(g.theme.fg2).fillRect(0, b.top, width, b.top+b.thickness);
} };
const clearScreen = function () { const clearScreen = function() {
const timeTop = settings.time.middle - (settings.time.size * 4) const timeTop = settings.time.middle-(settings.time.size*4);
g.clearRect(0, timeTop, screen.width, screen.height) g.clearRect(0, timeTop, screen.width, screen.height);
} };
let lastSeconds, tTick let lastSeconds, tTick;
const tick = function () { const tick = function() {
g.reset() g.reset();
const date = new Date() const date = new Date();
const seconds = date.getSeconds() const seconds = date.getSeconds();
if (lastSeconds > seconds) { if (lastSeconds>seconds) {
// new minute // new minute
clearScreen() clearScreen();
drawDateTime(date) drawDateTime(date);
} }
// the bar only gets larger, so drawing on top of the previous one is fine // the bar only gets larger, so drawing on top of the previous one is fine
drawBar(date) drawBar(date);
lastSeconds = seconds lastSeconds = seconds;
// schedule next update // schedule next update
const millis = date.getMilliseconds() const millis = date.getMilliseconds();
tTick = setTimeout(tick, 1000-millis) tTick = setTimeout(tick, 1000-millis);
} };
const start = function () { const start = function() {
lastSeconds = 99 // force redraw lastSeconds = 99; // force redraw
tick() tick();
} };
const stop = function () { const stop = function() {
if (tTick) { if (tTick) {
clearTimeout(tTick) clearTimeout(tTick);
tTick = undefined tTick = undefined;
} }
} };
// clean app screen // clean app screen
g.clear() g.clear();
Bangle.loadWidgets() Bangle.loadWidgets();
Bangle.drawWidgets() Bangle.drawWidgets();
// Show launcher when button pressed // Show launcher when button pressed
Bangle.setUI("clock"); Bangle.setUI("clock");
Bangle.on('lcdPower', function (on) { Bangle.on("lcdPower", function(on) {
if (on) { if (on) {
start() start();
} else { } else {
stop() stop();
} }
}) });
start() start();