2022-05-02 19:37:31 +00:00
|
|
|
(() => {
|
2023-01-26 10:04:50 +00:00
|
|
|
require("Font5x9Numeric7Seg").add(Graphics);
|
|
|
|
const config = Object.assign({
|
|
|
|
maxhours: 24,
|
|
|
|
drawBell: false,
|
|
|
|
showSeconds: 0, // 0=never, 1=only when display is unlocked, 2=for less than a minute
|
|
|
|
}, require("Storage").readJSON("widalarmeta.json",1) || {});
|
2022-05-02 19:37:31 +00:00
|
|
|
|
2023-02-26 08:28:45 +00:00
|
|
|
function getNextAlarm(date) {
|
|
|
|
const alarms = (require("Storage").readJSON("sched.json",1) || []).filter(alarm => alarm.on && alarm.hidden !== true);
|
|
|
|
WIDGETS["widalarmeta"].numActiveAlarms = alarms.length;
|
|
|
|
const times = alarms.map(alarm => require("sched").getTimeToAlarm(alarm, date) || Number.POSITIVE_INFINITY);
|
|
|
|
const eta = times.length > 0 ? Math.min.apply(null, times) : 0;
|
|
|
|
if (eta !== Number.POSITIVE_INFINITY) {
|
|
|
|
const idx = times.indexOf(eta);
|
|
|
|
const alarm = alarms[idx];
|
|
|
|
delete alarm.msg; delete alarm.id; delete alarm.data; // free some memory
|
|
|
|
return alarm;
|
|
|
|
}
|
|
|
|
} // getNextAlarm
|
|
|
|
|
2023-02-26 19:00:20 +00:00
|
|
|
function draw(fromInterval) {
|
2023-02-26 08:08:28 +00:00
|
|
|
if (this.nextAlarm === undefined) {
|
|
|
|
let alarm = getNextAlarm();
|
|
|
|
if (alarm === undefined) {
|
|
|
|
// try again with next hour
|
|
|
|
const nextHour = new Date();
|
|
|
|
nextHour.setHours(nextHour.getHours()+1);
|
|
|
|
alarm = getNextAlarm(nextHour);
|
|
|
|
}
|
|
|
|
if (alarm !== undefined) {
|
|
|
|
this.nextAlarm = alarm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const next = this.nextAlarm !== undefined ? require("sched").getTimeToAlarm(this.nextAlarm) : 0;
|
|
|
|
|
2023-01-26 10:04:50 +00:00
|
|
|
let calcWidth = 0;
|
|
|
|
let drawSeconds = false;
|
|
|
|
|
2023-02-26 08:08:28 +00:00
|
|
|
if (next > 0 && next <= config.maxhours*60*60*1000) {
|
2023-01-26 10:04:50 +00:00
|
|
|
const hours = Math.floor((next-1) / 3600000).toString();
|
|
|
|
const minutes = Math.floor(((next-1) % 3600000) / 60000).toString();
|
|
|
|
const seconds = Math.floor(((next-1) % 60000) / 1000).toString();
|
|
|
|
drawSeconds = (config.showSeconds & 0b01 && !Bangle.isLocked()) || (config.showSeconds & 0b10 && next <= 1000*60);
|
2022-05-02 19:37:31 +00:00
|
|
|
|
|
|
|
g.reset(); // reset the graphics context to defaults (color/font/etc)
|
|
|
|
g.setFontAlign(0,0); // center fonts
|
|
|
|
g.clearRect(this.x, this.y, this.x+this.width-1, this.y+23);
|
|
|
|
|
|
|
|
var text = hours.padStart(2, '0') + ":" + minutes.padStart(2, '0');
|
2023-01-26 10:04:50 +00:00
|
|
|
if (drawSeconds) {
|
|
|
|
text += ":" + seconds.padStart(2, '0');
|
|
|
|
}
|
|
|
|
g.setFont("5x9Numeric7Seg:1x2");
|
2022-05-02 19:37:31 +00:00
|
|
|
g.drawString(text, this.x+this.width/2, this.y+12);
|
2023-01-26 10:04:50 +00:00
|
|
|
|
|
|
|
calcWidth = 5*5+2;
|
|
|
|
if (drawSeconds) {
|
|
|
|
calcWidth += 3*5;
|
2022-05-02 19:37:31 +00:00
|
|
|
}
|
2023-02-26 19:00:20 +00:00
|
|
|
this.bellVisible = false;
|
2023-02-26 08:08:28 +00:00
|
|
|
} else if (config.drawBell && this.numActiveAlarms > 0) {
|
2023-01-26 10:04:50 +00:00
|
|
|
calcWidth = 24;
|
2023-02-26 19:00:20 +00:00
|
|
|
// next alarm too far in future, draw only widalarm bell
|
|
|
|
if (this.bellVisible !== true || fromInterval !== true) {
|
|
|
|
g.reset().drawImage(atob("GBgBAAAAAAAAABgADhhwDDwwGP8YGf+YMf+MM//MM//MA//AA//AA//AA//AA//AA//AB//gD//wD//wAAAAADwAABgAAAAAAAAA"),this.x,this.y);
|
|
|
|
this.bellVisible = true;
|
|
|
|
}
|
2022-05-02 19:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 10:04:50 +00:00
|
|
|
if (this.width !== calcWidth) {
|
|
|
|
// width changed, re-layout
|
|
|
|
this.width = calcWidth;
|
|
|
|
Bangle.drawWidgets();
|
|
|
|
}
|
|
|
|
|
2023-02-26 08:08:28 +00:00
|
|
|
// redraw next hour when no alarm else full minute or second
|
|
|
|
const period = next === 0 ? 3600000 : (drawSeconds ? 1000 : 60000);
|
2023-01-26 10:04:50 +00:00
|
|
|
let timeout = next > 0 ? next % period : period - (Date.now() % period);
|
|
|
|
if (timeout === 0) {
|
|
|
|
timeout += period;
|
|
|
|
}
|
2023-01-27 16:57:45 +00:00
|
|
|
|
|
|
|
if (this.timeoutId !== undefined) {
|
|
|
|
clearTimeout(this.timeoutId);
|
|
|
|
}
|
|
|
|
this.timeoutId = setTimeout(()=>{
|
2023-02-26 19:00:20 +00:00
|
|
|
WIDGETS["widalarmeta"].timeoutId = undefined;
|
|
|
|
WIDGETS["widalarmeta"].draw(true);
|
2023-01-26 10:04:50 +00:00
|
|
|
}, timeout);
|
|
|
|
} /* draw */
|
2022-05-02 19:37:31 +00:00
|
|
|
|
2023-01-26 10:04:50 +00:00
|
|
|
if (config.maxhours > 0) {
|
|
|
|
// add your widget
|
|
|
|
WIDGETS["widalarmeta"]={
|
|
|
|
area:"tl",
|
|
|
|
width: 0, // hide by default = assume no timer
|
|
|
|
draw:draw
|
|
|
|
};
|
|
|
|
}
|
2022-05-02 19:37:31 +00:00
|
|
|
})();
|