BangleApps/apps/circlesclock/app.js

332 lines
8.5 KiB
JavaScript
Raw Normal View History

2021-12-16 07:06:53 +00:00
const locale = require("locale");
const heatshrink = require("heatshrink");
const shoesIcon = heatshrink.decompress(atob("h0OwYJGgmAAgUBkgECgVJB4cSoAUDyEBkARDpADBhMAyQRBgVAkgmDhIUDAAuQAgY1DAAYA="));
const shoesIconGreen = heatshrink.decompress(atob("h0OwYJGhIEDgVIAgUEyQKDkmACgcggVACIeQAYMSgIRCgmApIbDiQUDAAkBkAFDGoYAD"));
const heartIcon = heatshrink.decompress(atob("h0OwYOLkmQhMkgACByVJgESpIFBpEEBAIFBCgIFCCgsABwcAgQOCAAMSpAwDyBNM"));
const powerIcon = heatshrink.decompress(atob("h0OwYQNsAED7AEDmwEDtu2AgUbtuABwXbBIUN23AAoYOCgEDFIgODABI"));
const powerIconGreen = heatshrink.decompress(atob("h0OwYQNkAEDpAEDiQEDkmSAgUJkmABwVJBIUEyVAAoYOCgEBFIgODABI"));
const powerIconRed = heatshrink.decompress(atob("h0OwYQNoAEDyAEDkgEDpIFDiVJBweSAgUJkmAAoYZDgQpEBwYAJA"));
2021-12-16 07:06:53 +00:00
let settings;
function loadSettings() {
settings = require("Storage").readJSON("circlesclock.json", 1) || {
'minHR': 40,
2021-12-16 07:06:53 +00:00
'maxHR': 200,
'stepGoal': 10000,
'stepDistanceGoal': 8000,
'stepLength': 0.8,
'batteryWarn': 30,
'showWidgets': false,
'circle1': 'hr',
'circle2': 'steps',
'circle3': 'battery'
2021-12-16 07:06:53 +00:00
};
// Load step goal from pedometer widget as fallback
if (settings.stepGoal == undefined) {
const d = require('Storage').readJSON("wpedom.json", 1) || {};
settings.stepGoal = d != undefined && d.settings != undefined ? d.settings.goal : 10000;
}
2021-12-16 07:06:53 +00:00
}
loadSettings();
const showWidgets = settings.showWidgets || false;
let hrtValue;
2021-12-16 07:06:53 +00:00
// layout values:
2021-12-17 21:36:34 +00:00
const colorFg = g.theme.dark ? '#fff' : '#000';
const colorBg = g.theme.dark ? '#000' : '#fff';
2021-12-16 07:06:53 +00:00
const colorGrey = '#808080';
const colorRed = '#ff0000';
const colorGreen = '#008000';
const colorBlue = '#0000ff';
const colorYellow = '#ffff00';
2022-01-06 10:08:46 +00:00
const widgetOffset = showWidgets ? 24 : 0;
const h = g.getHeight() - widgetOffset;
2021-12-16 07:06:53 +00:00
const w = g.getWidth();
const hOffset = 30 - widgetOffset;
2021-12-16 07:06:53 +00:00
const h1 = Math.round(1 * h / 5 - hOffset);
const h2 = Math.round(3 * h / 5 - hOffset);
const h3 = Math.round(8 * h / 8 - hOffset - 3);
2021-12-16 07:06:53 +00:00
const w1 = Math.round(w / 6);
const w2 = Math.round(3 * w / 6);
const w3 = Math.round(5 * w / 6);
const radiusOuter = 25;
const radiusInner = 18;
const circleFont = "Vector:15";
2021-12-16 07:06:53 +00:00
function draw() {
2022-01-05 18:38:18 +00:00
g.clear(true);
2022-01-05 18:50:07 +00:00
if (!showWidgets) {
/*
* we are not drawing the widgets as we are taking over the whole screen
* so we will blank out the draw() functions of each widget and change the
* area to the top bar doesn't get cleared.
*/
if (WIDGETS && typeof WIDGETS === "object") {
for (let wd of WIDGETS) {
wd.draw = () => {};
wd.area = "";
}
}
} else {
Bangle.drawWidgets();
}
2021-12-16 07:06:53 +00:00
g.setColor(colorBg);
2022-01-05 18:38:18 +00:00
g.fillRect(0, widgetOffset, w, h);
2021-12-16 07:06:53 +00:00
// time
g.setFont("Vector:50");
g.setFontAlign(0, -1);
2021-12-16 07:06:53 +00:00
g.setColor(colorFg);
g.drawString(locale.time(new Date(), 1), w / 2, h1 + 8);
2021-12-16 07:06:53 +00:00
// date & dow
g.setFont("Vector:21");
2021-12-16 07:06:53 +00:00
g.setFontAlign(-1, 0);
g.drawString(locale.date(new Date()), w > 180 ? 2 * w / 10 : w / 10, h2);
g.drawString(locale.dow(new Date()), w > 180 ? 2 * w / 10 : w / 10, h2 + 22);
2021-12-16 07:06:53 +00:00
drawCircle(1, "steps");
drawCircle(2, "hr");
drawCircle(3, "battery");
}
2021-12-16 07:06:53 +00:00
function drawCircle(index, defaultType) {
const type = settings['circle' + index] || defaultType;
const w = index == 1 ? w1: index == 2 ? w2 : w3;
switch (type) {
case "steps":
drawSteps(w);
break;
case "stepsDist":
drawStepsDistance(w);
break;
case "hr":
drawHeartRate(w);
break;
case "battery":
drawBattery(w);
break;
}
}
function getCirclePosition(type, defaultPos) {
for (let i = 1; i <= 3; i++) {
const setting = settings['circle' + i];
if (setting == type) return i == 1 ? w1: i == 2 ? w2 : w3;
}
return defaultPos;
2021-12-16 07:06:53 +00:00
}
function isCircleEnabled(type) {
return getCirclePosition(type) != undefined;
}
2021-12-16 07:06:53 +00:00
function drawSteps(w) {
if (!w) w = getCirclePosition("steps", w1);
2021-12-16 07:06:53 +00:00
const steps = getSteps();
g.setColor(colorGrey);
g.fillCircle(w, h3, radiusOuter);
2021-12-16 07:06:53 +00:00
2021-12-17 21:36:34 +00:00
const stepGoal = settings.stepGoal || 10000;
2021-12-16 07:06:53 +00:00
if (stepGoal > 0) {
let percent = steps / stepGoal;
if (stepGoal < steps) percent = 1;
drawGauge(w, h3, percent, colorBlue);
2021-12-16 07:06:53 +00:00
}
g.setColor(colorBg);
g.fillCircle(w, h3, radiusInner);
2021-12-16 07:06:53 +00:00
g.fillPoly([w, h3, w - 15, h3 + radiusOuter + 5, w + 15, h3 + radiusOuter + 5]);
2021-12-16 07:06:53 +00:00
g.setFont(circleFont);
2021-12-16 07:06:53 +00:00
g.setFontAlign(0, 0);
g.setColor(colorFg);
g.drawString(shortValue(steps), w + 2, h3);
2021-12-16 07:06:53 +00:00
g.drawImage(shoesIcon, w - 6, h3 + radiusOuter - 6);
2021-12-16 07:06:53 +00:00
}
function drawStepsDistance(w) {
if (!w) w = getCirclePosition("steps", w1);
const steps = getSteps();
const stepDistance = settings.stepLength || 0.8;
2022-01-06 12:01:36 +00:00
const stepsDistance = Math.round(steps * stepDistance);
2021-12-16 07:06:53 +00:00
g.setColor(colorGrey);
g.fillCircle(w, h3, radiusOuter);
const stepDistanceGoal = settings.stepDistanceGoal || 8000;
if (stepDistanceGoal > 0) {
let percent = stepsDistance / stepDistanceGoal;
if (stepDistanceGoal < stepsDistance) percent = 1;
drawGauge(w, h3, percent, colorGreen);
}
g.setColor(colorBg);
g.fillCircle(w, h3, radiusInner);
g.fillPoly([w, h3, w - 15, h3 + radiusOuter + 5, w + 15, h3 + radiusOuter + 5]);
g.setFont(circleFont);
g.setFontAlign(0, 0);
g.setColor(colorFg);
2022-01-06 12:01:36 +00:00
g.drawString(shortValue(stepsDistance), w + 2, h3);
g.drawImage(shoesIconGreen, w - 6, h3 + radiusOuter - 6);
}
function drawHeartRate(w) {
if (!w) w = getCirclePosition("hr", w2);
g.setColor(colorGrey);
g.fillCircle(w, h3, radiusOuter);
2021-12-16 07:06:53 +00:00
2021-12-17 21:36:34 +00:00
if (hrtValue != undefined && hrtValue > 0) {
const minHR = settings.minHR || 40;
const percent = (hrtValue - minHR) / (settings.maxHR - minHR);
drawGauge(w, h3, percent, colorRed);
2021-12-16 07:06:53 +00:00
}
g.setColor(colorBg);
g.fillCircle(w, h3, radiusInner);
2021-12-16 07:06:53 +00:00
g.fillPoly([w, h3, w - 15, h3 + radiusOuter + 5, w + 15, h3 + radiusOuter + 5]);
2021-12-16 07:06:53 +00:00
g.setFont(circleFont);
2021-12-16 07:06:53 +00:00
g.setFontAlign(0, 0);
g.setColor(colorFg);
g.drawString(hrtValue != undefined ? hrtValue : "-", w, h3);
2021-12-16 07:06:53 +00:00
g.drawImage(heartIcon, w - 6, h3 + radiusOuter - 6);
2021-12-16 07:06:53 +00:00
}
function drawBattery(w) {
if (!w) w = getCirclePosition("battery", w3);
2021-12-16 07:06:53 +00:00
const battery = E.getBattery();
g.setColor(colorGrey);
g.fillCircle(w, h3, radiusOuter);
2021-12-16 07:06:53 +00:00
if (battery > 0) {
const percent = battery / 100;
drawGauge(w, h3, percent, colorYellow);
2021-12-16 07:06:53 +00:00
}
g.setColor(colorBg);
g.fillCircle(w, h3, radiusInner);
2021-12-16 07:06:53 +00:00
g.fillPoly([w, h3, w - 15, h3 + radiusOuter + 5, w + 15, h3 + radiusOuter + 5]);
2021-12-16 07:06:53 +00:00
g.setFont(circleFont);
2021-12-16 07:06:53 +00:00
g.setFontAlign(0, 0);
let icon = powerIcon;
let color = colorFg;
if (Bangle.isCharging()) {
color = colorGreen;
icon = powerIconGreen;
}
else {
if (settings.batteryWarn != undefined && battery <= settings.batteryWarn) {
color = colorRed;
icon = powerIconRed;
}
}
g.setColor(color);
g.drawString(battery + '%', w, h3);
2021-12-16 07:06:53 +00:00
g.drawImage(icon, w - 6, h3 + radiusOuter - 6);
2021-12-16 07:06:53 +00:00
}
function radians(a) {
return a * Math.PI / 180;
}
function drawGauge(cx, cy, percent, color) {
let offset = 30;
let end = 300;
var i = 0;
var r = radiusInner + 3;
if (percent <= 0) return;
2021-12-16 07:06:53 +00:00
if (percent > 1) percent = 1;
var startrot = -offset;
2021-12-17 21:36:34 +00:00
var endrot = startrot - ((end - offset) * percent) - 15;
2021-12-16 07:06:53 +00:00
g.setColor(color);
const size = radiusOuter - radiusInner - 2;
2021-12-16 07:06:53 +00:00
// draw gauge
2021-12-17 21:36:34 +00:00
for (i = startrot; i > endrot - size; i -= size) {
2021-12-16 07:06:53 +00:00
x = cx + r * Math.sin(radians(i));
y = cy + r * Math.cos(radians(i));
2021-12-17 21:36:34 +00:00
g.fillCircle(x, y, size);
2021-12-16 07:06:53 +00:00
}
}
function shortValue(v) {
if (isNaN(v)) return '-';
if (v <= 999) return v;
if (v >= 1000 && v < 10000) {
v = Math.floor(v / 100) * 100;
return (v / 1000).toFixed(1).replace(/\.0$/, '') + 'k';
}
if (v >= 10000) {
v = Math.floor(v / 1000) * 1000;
return (v / 1000).toFixed(1).replace(/\.0$/, '') + 'k';
}
}
function getSteps() {
2022-01-05 18:38:18 +00:00
if (WIDGETS && WIDGETS.wpedom !== undefined) {
2021-12-16 07:06:53 +00:00
return WIDGETS.wpedom.getSteps();
}
return 0;
}
Bangle.on('lock', function(isLocked) {
if (!isLocked) {
if (isCircleEnabled("hr")) {
Bangle.setHRMPower(1, "watch");
if (hrtValue == undefined) {
hrtValue = '...';
drawHeartRate();
}
2021-12-17 21:36:34 +00:00
}
2022-01-05 18:50:07 +00:00
draw();
2021-12-16 07:06:53 +00:00
} else {
if (isCircleEnabled("hr")) {
Bangle.setHRMPower(0, "watch");
}
2021-12-16 07:06:53 +00:00
}
});
if (isCircleEnabled("hr")) {
Bangle.on('HRM', function(hrm) {
//if(hrm.confidence > 90){
hrtValue = hrm.bpm;
if (Bangle.isLCDOn())
drawHeartRate();
//} else {
// hrtValue = undefined;
//}
});
}
2021-12-16 07:06:53 +00:00
if (isCircleEnabled("battery")) {
Bangle.on('charging', function(charging) {
drawBattery();
});
}
2022-01-01 12:53:28 +00:00
Bangle.setUI("clock");
2021-12-16 07:06:53 +00:00
Bangle.loadWidgets();
2021-12-16 07:06:53 +00:00
draw();
setInterval(draw, 60000);