1
0
Fork 0
BangleApps/apps/smclock/app.js

164 lines
4.0 KiB
JavaScript
Raw Normal View History

2022-02-25 20:54:38 +00:00
const SETTINGSFILE = "smclock.json";
2022-02-22 14:37:37 +00:00
const background = {
2022-03-03 12:36:56 +00:00
width: 176,
height: 176,
bpp: 3,
transparent: 1,
buffer: require("heatshrink").decompress(
atob(
"/4A/AH4ACUb8H9MkyVJAThB/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/INP/AH4A/AAX8Yz4Afn5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/IP5B/INI="
)
),
2022-02-22 14:37:37 +00:00
};
2022-03-03 12:36:56 +00:00
const monthName = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
2022-02-22 14:37:37 +00:00
2022-02-25 20:54:38 +00:00
// dynamic variables
var batLevel = -1;
2022-03-03 12:36:56 +00:00
var batColor = [0, 0, 0];
// settings variables
var dateFormat;
var drawInterval;
var pollInterval;
var showAnalogFace;
var showWeekInfo;
var useVectorFont;
// load settings
function loadSettings() {
// Helper function default setting
function def(value, def) {
return value !== undefined ? value : def;
}
var settings = require("Storage").readJSON(SETTINGSFILE, true) || {};
dateFormat = def(settings.dateFormat, "Short");
drawInterval = def(settings.drawInterval, 10);
pollInterval = def(settings.pollInterval, 60);
showAnalogFace = def(settings.showAnalogFace, false);
showWeekInfo = def(settings.showWeekInfo, false);
useVectorFont = def(settings.useVectorFont, false);
}
2022-02-25 20:54:38 +00:00
// copied from: https://gist.github.com/IamSilviu/5899269#gistcomment-3035480
function ISO8601_week_no(date) {
2022-03-03 12:36:56 +00:00
var tdt = new Date(date.valueOf());
var dayn = (date.getDay() + 6) % 7;
tdt.setDate(tdt.getDate() - dayn + 3);
var firstThursday = tdt.valueOf();
tdt.setMonth(0, 1);
if (tdt.getDay() !== 4) {
tdt.setMonth(0, 1 + ((4 - tdt.getDay() + 7) % 7));
}
return 1 + Math.ceil((firstThursday - tdt) / 604800000);
2022-02-22 14:37:37 +00:00
}
function d02(value) {
2022-03-03 12:36:56 +00:00
return ("0" + value).substr(-2);
2022-02-22 14:37:37 +00:00
}
function pollBattery() {
2022-02-25 20:54:38 +00:00
batLevel = E.getBattery();
2022-02-22 14:37:37 +00:00
}
function getBatteryColor(level) {
var color;
if (level < 0) {
2022-02-27 22:22:32 +00:00
pollBattery();
level = batLevel;
2022-02-22 14:37:37 +00:00
}
2022-03-03 12:36:56 +00:00
if (level > 80) {
color = [0, 0, 1];
} else if (level > 60) {
color = [0, 1, 1];
} else if (level > 40) {
color = [0, 1, 0];
} else if (level > 20) {
color = [1, 1, 0];
2022-02-22 14:37:37 +00:00
} else {
2022-03-03 12:36:56 +00:00
color = [1, 0, 0];
2022-02-22 14:37:37 +00:00
}
return color;
}
function draw() {
g.drawImage(background);
2022-02-27 22:22:32 +00:00
const color = getBatteryColor(batLevel);
2022-02-22 14:37:37 +00:00
const bat = d02(E.getBattery()) + "%";
const d = new Date();
const day = d.getDate();
2022-03-03 12:36:56 +00:00
const month = d.getMonth() + 1;
2022-02-22 14:37:37 +00:00
const week = d02(ISO8601_week_no(d));
const date1 = d02(day) + "/" + d02(month);
const date2 = weekday[d.getDay()] + " " + d02(week);
const h = d.getHours();
const m = d.getMinutes();
const time = d02(h) + ":" + d02(m);
g.reset();
g.setColor(0, 0, 0);
2022-03-03 12:36:56 +00:00
console.log(useVectorFont, dateFormat);
if (useVectorFont == true && dateFormat == "Short") {
g.setFont("Vector", 20);
} else {
g.setFont("6x8", 2);
}
2022-02-22 14:37:37 +00:00
g.drawString(date1, 105, 20, false);
g.setFont("Vector", 16);
g.drawString(date2, 105, 55, false);
g.setColor(1, 1, 1);
g.setFont("Vector", 60);
g.drawString(time, 10, 108, false);
g.setColor(1, 1, 1);
g.setFont("Vector", 16);
g.drawString("Bat:", 12, 22, false);
g.setColor(color[0], color[1], color[2]);
g.drawString(bat, 52, 22, false);
}
2022-03-03 12:36:56 +00:00
loadSettings();
2022-02-22 14:37:37 +00:00
g.clear();
pollBattery();
draw();
var batInterval = setInterval(pollBattery, 60000);
var drawInterval = setInterval(draw, 10000);
// Stop updates when LCD is off, restart when on
2022-03-03 12:36:56 +00:00
Bangle.on("lcdPower", (on) => {
2022-02-22 14:37:37 +00:00
if (batInterval) clearInterval(batInterval);
batInterval = undefined;
if (drawInterval) clearInterval(drawInterval);
drawInterval = undefined;
if (on) {
batInterval = setInterval(pollBattery, 60000);
drawInterval = setInterval(draw, 10000);
2022-03-03 12:36:56 +00:00
2022-02-22 14:37:37 +00:00
pollBattery();
2022-02-25 20:54:38 +00:00
draw();
2022-02-22 14:37:37 +00:00
}
});
// Show launcher when middle button pressed
Bangle.setUI("clock");