From b12cb9ff8bf5431e9d12b023112ad486683a1265 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Tue, 24 Sep 2024 23:40:36 +0200 Subject: [PATCH] sixths: add sunset/sunrise support. --- apps/sixths/sixths.app.js | 58 +++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/apps/sixths/sixths.app.js b/apps/sixths/sixths.app.js index c8d2e668e..00352a44c 100644 --- a/apps/sixths/sixths.app.js +++ b/apps/sixths/sixths.app.js @@ -182,37 +182,65 @@ let gps = { }, }; -/* sun version 0.0.1 */ +/* sun version 0.0.2 */ let sun = { SunCalc: null, lat: 50, lon: 14, + rise: 0, /* Unix time of sunrise/sunset */ + set: 0, init: function() { try { this.SunCalc = require("suncalc"); // from modules folder } catch (e) { print("Require error", e); } - print("Have suncalc: ", this.SunCalc); }, - get_sun_pos: function() { + sunPos: function() { let d = new Date(); let sun = this.SunCalc.getPosition(d, this.lat, this.lon); print(sun.azimuth, sun.altitude); return sun; }, - get_sun_time: function() { + sunTime: function() { let d = new Date(); let sun = this.SunCalc.getTimes(d, this.lat, this.lon); - print(sun.sunrise, sun.sunset); return sun; }, + adj: function (x) { + if (x < 0) + return x + 24*60*60; + return x; + }, + toSunrise: function () { + return this.adj(this.rise - getTime()); + }, + toSunset: function () { + return this.adj(this.set - getTime()); + }, + update: function () { + if (this.SunCalc) { + let t = this.sunTime(); + this.rise = t.sunrise.getTime() / 1000; + this.set = t.sunset.getTime() / 1000; + } + }, + // < 0 : next is sunrise, in abs(ret) seconds + // > 0 + getNext: function () { + let rise = this.toSunrise(); + let set = this.toSunset(); + if (rise < set) { + return -rise; + } + return set; + // set = set / 60; + // return s + (set / 60).toFixed(0) + ":" + (set % 60).toFixed(0); + }, }; sun.init(); -sun.get_sun_pos(); -sun.get_sun_time(); fmt.init(); gps.init(); @@ -226,7 +254,7 @@ var buzz = "", /* Set this to transmit morse via vibrations */ inm = "", l = "", /* For incoming morse handling */ in_str = "", note = "", - debug = "v0.5.11", debug2 = "(otherdb)", debug3 = "(short)"; + debug = "v0.10.2", debug2 = "(otherdb)", debug3 = "(short)"; var note_limit = 0; var mode = 0, mode_time = 0; // 0 .. normal, 1 .. note, 2.. mark name var disp_mode = 0; // 0 .. normal, 1 .. small time @@ -637,6 +665,7 @@ function hourly() { } if (is_active) doBuzz(toMorse(s)); + sun.update(); //logstamp(""); } function showMsg(msg, timeout) { @@ -827,6 +856,19 @@ function draw() { if (state.cur_mark) { msg += markHandle() + "\n"; } + + { + let set = sun.getNext(); + if (true || (set > -8*60*60 && set < 2*60*60)) { + if (set < 0) { + msg += "^"; + set = -set; + } else msg += "v"; + set = Math.floor(set / 60); + msg += (set / 60).toFixed(0) + ":" + fmt.add0(set % 60); + msg += "\n"; + } + } if (note != "") { if (getTime() > note_limit)