2022-03-15 08:19:14 +00:00
|
|
|
/* sclock.app.js for Bangle2
|
|
|
|
Peter Bernschneider 30.12.2021
|
|
|
|
Update current latitude and longitude in My Location app
|
|
|
|
Update current Timezone in Settings app, menu item "System"
|
|
|
|
Update for summer time by incrementing Timezone += 1 */
|
2024-03-13 10:51:40 +00:00
|
|
|
const setting = require("Storage").readJSON("setting.json",1);
|
2022-03-15 08:19:14 +00:00
|
|
|
E.setTimeZone(setting.timezone); // timezone = 1 for MEZ, = 2 for MESZ
|
2024-03-13 10:51:40 +00:00
|
|
|
const SunCalc = require("suncalc.js");
|
|
|
|
const loc = require('locale');
|
2022-03-15 08:19:14 +00:00
|
|
|
const LOCATION_FILE = "mylocation.json";
|
|
|
|
const xyCenter = g.getWidth() / 2 + 3;
|
2022-12-09 09:49:33 +00:00
|
|
|
const yposTime = 60;
|
|
|
|
const yposDate = 100;
|
2022-03-15 08:19:14 +00:00
|
|
|
const yposRS = 135;
|
|
|
|
const yposPos = 160;
|
|
|
|
var rise = "07:00";
|
|
|
|
var set = "20:00";
|
|
|
|
var pos = {altitude: 20, azimuth: 135};
|
|
|
|
var noonpos = {altitude: 37, azimuth: 180};
|
|
|
|
let idTimeout = null;
|
|
|
|
|
2022-12-09 09:49:33 +00:00
|
|
|
|
|
|
|
|
2022-03-15 08:19:14 +00:00
|
|
|
function updatePos() {
|
2022-12-09 09:49:33 +00:00
|
|
|
function radToDeg(pos) {
|
|
|
|
return { // instead of mofidying suncalc
|
|
|
|
azimuth: Math.round((pos.azimuth / rad + 180) % 360),
|
|
|
|
altitude: Math.round( pos.altitude / rad)
|
|
|
|
};
|
|
|
|
}
|
2024-03-13 10:51:40 +00:00
|
|
|
const coord = require("Storage").readJSON(LOCATION_FILE,1)|| {"lat":53.3,"lon":10.1,"location":"Pattensen"};
|
2022-12-09 09:49:33 +00:00
|
|
|
pos = radToDeg(SunCalc.getPosition(Date.now(), coord.lat, coord.lon));
|
2024-03-13 10:51:40 +00:00
|
|
|
const times = SunCalc.getTimes(Date.now(), coord.lat, coord.lon);
|
2022-03-15 08:19:14 +00:00
|
|
|
rise = times.sunrise.toString().split(" ")[4].substr(0,5);
|
|
|
|
set = times.sunset.toString().split(" ")[4].substr(0,5);
|
2022-12-09 09:49:33 +00:00
|
|
|
noonpos = radToDeg(SunCalc.getPosition(times.solarNoon, coord.lat, coord.lon));
|
2022-03-15 08:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function drawSimpleClock() {
|
|
|
|
var d = new Date(); // get date
|
|
|
|
var da = d.toString().split(" ");
|
|
|
|
g.clear();
|
|
|
|
Bangle.drawWidgets();
|
|
|
|
g.reset(); // default draw styles
|
|
|
|
g.setFontAlign(0, 0); // drawSting centered
|
|
|
|
|
|
|
|
var time = da[4].substr(0, 5); // draw time
|
|
|
|
|
2022-12-09 09:49:33 +00:00
|
|
|
g.setFont("Vector",60);
|
2022-03-15 08:19:14 +00:00
|
|
|
g.drawString(time, xyCenter, yposTime, true);
|
|
|
|
|
2022-03-15 09:43:10 +00:00
|
|
|
var date = [loc.dow(new Date(),1), loc.date(d,1)].join(" "); // draw day of week, date
|
2022-03-15 08:19:14 +00:00
|
|
|
g.setFont("Vector",24);
|
|
|
|
g.drawString(date, xyCenter, yposDate, true);
|
|
|
|
|
|
|
|
g.setFont("Vector",25);
|
|
|
|
g.drawString(`${rise} ${set}`, xyCenter, yposRS, true); // draw riseset
|
|
|
|
g.drawImage(require("Storage").read("sunrise.img"), xyCenter-16, yposRS-16);
|
|
|
|
|
|
|
|
g.setFont("Vector",21);
|
|
|
|
g.drawString(`H${pos.altitude}/${noonpos.altitude} Az${pos.azimuth}`, xyCenter, yposPos, true); // draw sun pos
|
2022-12-09 09:49:33 +00:00
|
|
|
|
2022-03-15 08:19:14 +00:00
|
|
|
let t = d.getSeconds()*1000 + d.getMilliseconds();
|
|
|
|
idTimeout = setTimeout(drawSimpleClock, 60000 - t); // time till next minute
|
|
|
|
}
|
|
|
|
|
|
|
|
// special function to handle display switch on
|
|
|
|
Bangle.on('lcdPower', function(on){
|
|
|
|
if (on) {
|
|
|
|
drawSimpleClock();
|
|
|
|
} else {
|
|
|
|
if(idTimeout) {
|
|
|
|
clearTimeout(idTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
g.clear(); // clean app screen
|
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
|
|
|
|
2022-03-15 09:43:10 +00:00
|
|
|
setInterval(updatePos, 60*5E3); // refesh every 5 mins
|
2022-03-15 08:19:14 +00:00
|
|
|
|
|
|
|
updatePos();
|
|
|
|
drawSimpleClock(); // draw now
|
|
|
|
|
2022-03-15 09:43:10 +00:00
|
|
|
setWatch(Bangle.showLauncher, BTN1, { repeat: false, edge: "falling" }); // Show launcher when button pressed
|