1
0
Fork 0

Merge pull request #3039 from g-rden/sunrise

Sunrise 0.05
master
Gordon Williams 2023-10-09 10:53:48 +01:00 committed by GitHub
commit 1ec0b94362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 100 deletions

View File

@ -2,3 +2,4 @@
0.02: Faster sinus line and fix button to open menu 0.02: Faster sinus line and fix button to open menu
0.03: Show day/month, add animations, fix !mylocation and text glitch 0.03: Show day/month, add animations, fix !mylocation and text glitch
0.04: Always show the widgets, swifter animations and lighter sea line 0.04: Always show the widgets, swifter animations and lighter sea line
0.05: Fixed hours increasing too early, added missing 23 o'clock, simplified code

View File

@ -14,9 +14,6 @@ function loadLocation () {
return { lat: 41.38, lon: 2.168 }; return { lat: 41.38, lon: 2.168 };
} }
} }
let frames = 0; // amount of pending frames to render (0 if none)
let curPos = 0; // x position of the sun
let realPos = 0; // x position of the sun depending on currentime
const latlon = loadLocation() || {}; const latlon = loadLocation() || {};
const lat = latlon.lat || 41.38; const lat = latlon.lat || 41.38;
const lon = latlon.lon || 2.168; const lon = latlon.lon || 2.168;
@ -162,13 +159,8 @@ Math.mod = function (a, b) {
return result; return result;
}; };
const delta = 2;
const sunrise = new Date().sunrise(lat, lon); const sunrise = new Date().sunrise(lat, lon);
const sr = sunrise.getHours() + ':' + sunrise.getMinutes();
console.log('sunrise', sunrise);
const sunset = new Date().sunset(lat, lon); const sunset = new Date().sunset(lat, lon);
const ss = sunset.getHours() + ':' + sunset.getMinutes();
console.log('sunset', sunset);
const w = g.getWidth(); const w = g.getWidth();
const h = g.getHeight(); const h = g.getHeight();
@ -176,37 +168,42 @@ const oy = h / 1.7;
let sunRiseX = 0; let sunRiseX = 0;
let sunSetX = 0; let sunSetX = 0;
const sinStep = 12; const sinStep = 13;
let pos = 0;
let realTime = true;
const r = 10;
let frames = 0; // amount of pending frames to render (0 if none)
// set to 1 because pos 0 is displayed as 0-1:59
let curPos = 1; // x position of the sun
let realPos = 0; // x position of the sun depending on currentime
function formatAsTime (hour, minute) {
return '' + ((hour < 10) ? '0' : '') + (0 | hour) +
':' + ((minute < 10) ? '0' : '') + (0 | minute);
}
function drawSinuses () { function drawSinuses () {
let x = 0; let x = 0;
g.setColor(0, 0, 0);
// g.fillRect(0,oy,w, h);
g.setColor(1, 1, 1); g.setColor(1, 1, 1);
let y = oy; let y = ypos(x);
for (i = 0; i < w; i++) { while (x < w) {
x = i; y2 = ypos(x + sinStep);
x2 = x + sinStep + 1; g.drawLine(x, y, x + sinStep, y2);
y2 = ypos(i);
if (x == 0) {
y = y2;
}
g.drawLine(x, y, x2, y2);
y = y2; y = y2;
i += sinStep; // no need to draw all steps x += sinStep; // no need to draw all steps
} }
// sea level line // sea level line
const hh0 = sunrise.getHours(); const sl0 = seaLevel(sunrise.getHours());
const hh1 = sunset.getHours(); const sl1 = seaLevel(sunset.getHours());
const sl0 = seaLevel(hh0); sunRiseX = xfromTime(sunrise.getHours() + sunrise.getMinutes() / 60);
const sl1 = seaLevel(hh1); sunSetX = xfromTime(sunset.getHours() + sunset.getMinutes() / 60);
sunRiseX = xfromTime(hh0) + (r / 2);
sunSetX = xfromTime(hh1) + (r / 2);
g.setColor(0, 0.5, 1); g.setColor(0, 0.5, 1);
g.drawLine(0, sl0, w, sl1); g.drawLine(0, sl0, w, sl1);
g.setColor(0, 0.5, 1);
g.drawLine(0, sl0 + 1, w, sl1 + 1); g.drawLine(0, sl0 + 1, w, sl1 + 1);
/* /*
g.setColor(0, 0, 1); g.setColor(0, 0, 1);
@ -219,30 +216,23 @@ function drawSinuses () {
function drawTimes () { function drawTimes () {
g.setColor(1, 1, 1); g.setColor(1, 1, 1);
g.setFont('6x8', 2); g.setFont('6x8', 2);
g.drawString(sr, 10, h - 20); g.drawString(formatAsTime(sunrise.getHours(), sunrise.getMinutes()), 10, h - 20);
g.drawString(ss, w - 60, h - 20); g.drawString(formatAsTime(sunset.getHours(), sunset.getMinutes()), w - 60, h - 20);
} }
let pos = 0;
let realTime = true;
const r = 10;
function drawGlow () { function drawGlow () {
const now = new Date(); const now = new Date();
if (frames < 1 && realTime) { if (frames < 1 && realTime) {
pos = xfromTime(now.getHours()); pos = xfromTime(now.getHours() + now.getMinutes() / 60);
} }
const rh = r / 2;
const x = pos; const x = pos;
const y = ypos(x - r); const y = ypos(x);
const r2 = 0;
g.setColor(0.2, 0.2, 0);
// wide glow
if (x > sunRiseX && x < sunSetX) { if (x > sunRiseX && x < sunSetX) {
g.setColor(0.2, 0.2, 0);
g.fillCircle(x, y, r + 20); g.fillCircle(x, y, r + 20);
g.setColor(0.5, 0.5, 0); g.setColor(0.5, 0.5, 0);
// wide glow
} else {
g.setColor(0.2, 0.2, 0);
} }
// smol glow // smol glow
g.fillCircle(x, y, r + 8); g.fillCircle(x, y, r + 8);
@ -255,8 +245,8 @@ function seaLevel (hour) {
} }
function ypos (x) { function ypos (x) {
const pc = (x * 100 / w); // offset, resulting in zenith being at the correct time
return oy + (32 * Math.sin(1.7 + (pc / 16))); return oy + (32 * Math.sin(((x + sunRiseX - 12) / w) * 6.28 ));
} }
function xfromTime (t) { function xfromTime (t) {
@ -264,20 +254,19 @@ function xfromTime (t) {
} }
function drawBall () { function drawBall () {
let x = pos;
const now = new Date(); const now = new Date();
if (frames < 1 && realTime) { if (frames < 1 && realTime) {
x = xfromTime(now.getHours()); pos = xfromTime(now.getHours() + now.getMinutes() / 60);
} }
const y = ypos(x - r); const x = pos;
const y = ypos(x);
// glow // glow
if (x < sunRiseX || x > sunSetX) { if (x > sunRiseX && x < sunSetX) {
g.setColor(0.5, 0.5, 0);
} else {
g.setColor(1, 1, 1); g.setColor(1, 1, 1);
} else {
g.setColor(0.5, 0.5, 0);
} }
const rh = r / 2;
g.fillCircle(x, y, r); g.fillCircle(x, y, r);
g.setColor(1, 1, 0); g.setColor(1, 1, 0);
g.drawCircle(x, y, r); g.drawCircle(x, y, r);
@ -285,52 +274,39 @@ function drawBall () {
function drawClock () { function drawClock () {
const now = new Date(); const now = new Date();
let curTime = ''; let hours = 0.0;
let fhours = 0.0; let mins = 0.0;
let fmins = 0.0;
let ypos = 32;
if (realTime) { if (realTime) {
fhours = now.getHours(); hours = now.getHours();
fmins = now.getMinutes(); mins = now.getMinutes();
} else { } else {
ypos = 32; hours = 24 * (pos / w);
fhours = 24 * (pos / w);
if (fhours > 23) {
fhours = 0;
}
const nexth = 24 * 60 * (pos / w); const nexth = 24 * 60 * (pos / w);
fmins = 59 - ((24 * 60) - nexth) % 60; mins = 59 - ((24 * 60) - nexth) % 60;
if (fmins < 0) {
fmins = 0; // this prevents the displayed time to jump from 11:50 to 12:59 to 12:07
if (mins == 59) {
hours--;
} }
} }
if (fmins > 59) {
fmins = 59;
}
const hours = ((fhours < 10) ? '0' : '') + (0 | fhours);
const mins = ((fmins < 10) ? '0' : '') + (0 | fmins);
curTime = hours + ':' + mins;
g.setFont('Vector', 30); g.setFont('Vector', 30);
if (realTime) { g.setColor(realTime, 1, 1);
g.setColor(1, 1, 1); g.drawString(formatAsTime(hours, mins), w / 1.9, 32);
} else {
g.setColor(0, 1, 1);
}
g.drawString(curTime, w / 1.9, ypos);
// day-month // day-month
if (realTime) { if (realTime) {
const mo = now.getMonth() + 1; const mo = now.getMonth() + 1;
const da = now.getDate(); const da = now.getDate();
const daymonth = '' + da + '/' + mo;
g.setFont('6x8', 2); g.setFont('6x8', 2);
g.drawString(daymonth, 5, 30); g.drawString('' + da + '/' + mo, 5, 30);
} }
} }
function renderScreen () { function renderScreen () {
const now = new Date();
g.setColor(0, 0, 0); g.setColor(0, 0, 0);
g.fillRect(0, 30, w, h); g.fillRect(0, 30, w, h);
realPos = xfromTime((new Date()).getHours()); realPos = xfromTime(now.getHours() + now.getMinutes() / 60);
g.setFontAlign(-1, -1, 0); g.setFontAlign(-1, -1, 0);
Bangle.drawWidgets(); Bangle.drawWidgets();
@ -347,7 +323,7 @@ Bangle.on('drag', function (tap, top) {
curPos = pos; curPos = pos;
initialAnimation(); initialAnimation();
} else { } else {
pos = tap.x - 5; pos = tap.x;
realTime = false; realTime = false;
} }
renderScreen(); renderScreen();
@ -359,23 +335,13 @@ Bangle.on('lock', () => {
renderScreen(); renderScreen();
}); });
renderScreen();
realPos = xfromTime((new Date()).getHours());
function initialAnimationFrame () { function initialAnimationFrame () {
let distance = (realPos - curPos) / 4; if (frames > 0) {
if (distance > 20) { let distance = (realPos - curPos) / frames;
distance = 20; pos = curPos;
} curPos += distance;
curPos += distance; renderScreen();
pos = curPos; frames--;
renderScreen();
if (curPos >= realPos) {
frame = 0;
}
frames--;
if (frames-- > 0) {
setTimeout(initialAnimationFrame, 50); setTimeout(initialAnimationFrame, 50);
} else { } else {
realTime = true; realTime = true;
@ -384,17 +350,21 @@ function initialAnimationFrame () {
} }
function initialAnimation () { function initialAnimation () {
const now = new Date();
realPos = xfromTime(now.getHours() + now.getMinutes() / 60);
const distance = Math.abs(realPos - pos); const distance = Math.abs(realPos - pos);
frames = distance / 4; frames = distance / 16;
realTime = false; realTime = false;
initialAnimationFrame(); initialAnimationFrame();
} }
function main () { function main () {
sunRiseX = xfromTime(sunrise.getHours() + sunrise.getMinutes() / 60);
sunSetX = xfromTime(sunset.getHours() + sunset.getMinutes() / 60);
g.setBgColor(0, 0, 0); g.setBgColor(0, 0, 0);
g.clear(); g.clear();
setInterval(renderScreen, 60 * 1000); setInterval(renderScreen, 60 * 1000);
pos = 0;
initialAnimation(); initialAnimation();
} }

View File

@ -2,7 +2,7 @@
"id": "sunrise", "id": "sunrise",
"name": "Sunrise", "name": "Sunrise",
"shortName": "Sunrise", "shortName": "Sunrise",
"version": "0.04", "version": "0.05",
"type": "clock", "type": "clock",
"description": "Show sunrise and sunset times", "description": "Show sunrise and sunset times",
"icon": "app.png", "icon": "app.png",