From 90b68051c57fafe6f245f133d5ff2956bf0a6ad4 Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Sun, 11 Dec 2022 22:09:16 +0100 Subject: [PATCH] astrocalc - Compatibility with Bangle.js 2 - Get location from My Location --- .gitignore | 1 + apps/astrocalc/ChangeLog | 1 + apps/astrocalc/astrocalc-app.js | 107 ++++++-------------------------- apps/astrocalc/metadata.json | 7 ++- modules/suncalc.js | 2 +- 5 files changed, 27 insertions(+), 91 deletions(-) diff --git a/.gitignore b/.gitignore index f4588ac6f..7687a770a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ _site .owncloudsync.log Desktop.ini .sync_*.db* +*.swp diff --git a/apps/astrocalc/ChangeLog b/apps/astrocalc/ChangeLog index 746ab2162..11b2d7177 100644 --- a/apps/astrocalc/ChangeLog +++ b/apps/astrocalc/ChangeLog @@ -1,3 +1,4 @@ 0.01: Create astrocalc app 0.02: Store last GPS lock, can be used instead of waiting for new GPS on start 0.03: Use 'modules/suncalc.js' to avoid it being copied 8 times for different apps +0.04: Compatibility with Bangle.js 2, get location from My Location diff --git a/apps/astrocalc/astrocalc-app.js b/apps/astrocalc/astrocalc-app.js index 46fb855ec..3c879779d 100644 --- a/apps/astrocalc/astrocalc-app.js +++ b/apps/astrocalc/astrocalc-app.js @@ -11,8 +11,7 @@ const SunCalc = require("suncalc"); // from modules folder const storage = require("Storage"); -const LAST_GPS_FILE = "astrocalc.gps.json"; -let lastGPS = (storage.readJSON(LAST_GPS_FILE, 1) || null); +const BANGLEJS2 = process.env.HWVERSION == 2; // check for bangle 2 function drawMoon(phase, x, y) { const moonImgFiles = [ @@ -73,7 +72,7 @@ function drawTitle(key) { */ function drawPoint(angle, radius, color) { const pRad = Math.PI / 180; - const faceWidth = 80; // watch face radius + const faceWidth = g.getWidth()/3; // watch face radius const centerPx = g.getWidth() / 2; const a = angle * pRad; @@ -141,6 +140,7 @@ function drawData(title, obj, startX, startY) { function drawMoonPositionPage(gps, title) { const pos = SunCalc.getMoonPosition(new Date(), gps.lat, gps.lon); + const moonColor = g.theme.dark ? {r: 1, g: 1, b: 1} : {r: 0, g: 0, b: 0}; const pageData = { Azimuth: pos.azimuth.toFixed(2), @@ -150,13 +150,13 @@ function drawMoonPositionPage(gps, title) { }; const azimuthDegrees = parseInt(pos.azimuth * 180 / Math.PI); - drawData(title, pageData, null, 80); + drawData(title, pageData, null, g.getHeight()/2 - Object.keys(pageData).length/2*20); drawPoints(); - drawPoint(azimuthDegrees, 8, {r: 1, g: 1, b: 1}); + drawPoint(azimuthDegrees, 8, moonColor); let m = setWatch(() => { let m = moonIndexPageMenu(gps); - }, BTN3, {repeat: false, edge: "falling"}); + }, BANGLEJS2 ? BTN : BTN3, {repeat: false, edge: "falling"}); } function drawMoonIlluminationPage(gps, title) { @@ -166,43 +166,45 @@ function drawMoonIlluminationPage(gps, title) { ]; const phase = SunCalc.getMoonIllumination(new Date()); + const phaseIdx = Math.round(phase.phase*8), const pageData = { - Phase: phaseNames[phase.phase], + Phase: phaseNames[phaseIdx], }; drawData(title, pageData, null, 35); - drawMoon(phase.phase, g.getWidth() / 2, g.getHeight() / 2); + drawMoon(phaseIdx, g.getWidth() / 2, g.getHeight() / 2); let m = setWatch(() => { let m = moonIndexPageMenu(gps); - }, BTN3, {repease: false, edge: "falling"}); + }, BANGLEJS2 ? BTN : BTN3, {repease: false, edge: "falling"}); } function drawMoonTimesPage(gps, title) { const times = SunCalc.getMoonTimes(new Date(), gps.lat, gps.lon); + const moonColor = g.theme.dark ? {r: 1, g: 1, b: 1} : {r: 0, g: 0, b: 0}; const pageData = { Rise: dateToTimeString(times.rise), Set: dateToTimeString(times.set), }; - drawData(title, pageData, null, 105); + drawData(title, pageData, null, g.getHeight()/2 - Object.keys(pageData).length/2*20 + 5); drawPoints(); // Draw the moon rise position const risePos = SunCalc.getMoonPosition(times.rise, gps.lat, gps.lon); const riseAzimuthDegrees = parseInt(risePos.azimuth * 180 / Math.PI); - drawPoint(riseAzimuthDegrees, 8, {r: 1, g: 1, b: 1}); + drawPoint(riseAzimuthDegrees, 8, moonColor); // Draw the moon set position const setPos = SunCalc.getMoonPosition(times.set, gps.lat, gps.lon); const setAzimuthDegrees = parseInt(setPos.azimuth * 180 / Math.PI); - drawPoint(setAzimuthDegrees, 8, {r: 1, g: 1, b: 1}); + drawPoint(setAzimuthDegrees, 8, moonColor); let m = setWatch(() => { let m = moonIndexPageMenu(gps); - }, BTN3, {repease: false, edge: "falling"}); + }, BANGLEJS2 ? BTN : BTN3, {repease: false, edge: "falling"}); } function drawSunShowPage(gps, key, date) { @@ -224,7 +226,7 @@ function drawSunShowPage(gps, key, date) { Degrees: azimuthDegrees }; - drawData(key, pageData, null, 85); + drawData(key, pageData, null, g.getHeight()/2 - Object.keys(pageData).length/2*20 + 5); drawPoints(); @@ -233,7 +235,7 @@ function drawSunShowPage(gps, key, date) { m = setWatch(() => { m = sunIndexPageMenu(gps); - }, BTN3, {repeat: false, edge: "falling"}); + }, BANGLEJS2 ? BTN : BTN3, {repeat: false, edge: "falling"}); return null; } @@ -300,7 +302,7 @@ function indexPageMenu(gps) { "Moon": () => { m = moonIndexPageMenu(gps); }, - "< Exit": () => { load(); } + "< Back": () => { load(); } }; return E.showMenu(menu); @@ -310,78 +312,9 @@ function getCenterStringX(str) { return (g.getWidth() - g.stringWidth(str)) / 2; } -/** - * GPS wait page, shows GPS locating animation until it gets a lock, then moves to the Sun page - */ -function drawGPSWaitPage() { - const img = require("heatshrink").decompress(atob("mEwxH+AH4A/AH4AW43GF1wwsFwYwqFwowoFw4wmFxIwdE5YAPF/4vM5nN6YAE5vMF8YtHGIgvhFpQxKF7AuOGA4vXFyAwGF63MFyIABF6xeWMC4UDLwvNGpAJG5gwSdhIIDRBLyWCIgcJHAgJJDoouQF4vMQoICBBJoeGFx6GGACIfHL6YvaX6gvZeCIdFc4gAFXogvGFxgwFDwovQCAguOGAnMMBxeG5guTGAggGGAwNKFySREcA3N5vM5gDBdpQvXEY4AKXqovGGCKbFF7AwPZQwvZGJgtGF7vGdQItG5gSIF7gASF/44WEzgwRF0wwHF1AwFF1QwDF1gvwAH4A/AFAA==")); - const str1 = "Astrocalc v0.02"; - const str2 = "Locating GPS"; - const str3 = "Please wait..."; - - g.clear(); - g.drawImage(img, 100, 50); - g.setFont("6x8", 1); - g.drawString(str1, getCenterStringX(str1), 105); - g.drawString(str2, getCenterStringX(str2), 140); - g.drawString(str3, getCenterStringX(str3), 155); - - if (lastGPS) { - lastGPS = JSON.parse(lastGPS); - lastGPS.time = new Date(); - - const str4 = "Press Button 3 to use last GPS"; - g.setColor("#d32e29"); - g.fillRect(0, 190, g.getWidth(), 215); - g.setColor("#ffffff"); - g.drawString(str4, getCenterStringX(str4), 200); - - setWatch(() => { - clearWatch(); - Bangle.setGPSPower(0); - m = indexPageMenu(lastGPS); - }, BTN3, {repeat: false}); - } - - g.flip(); - - const DEBUG = false; - if (DEBUG) { - clearWatch(); - - const gps = { - "lat": 56.45783133333, - "lon": -3.02188583333, - "alt": 75.3, - "speed": 0.070376, - "course": NaN, - "time":new Date(), - "satellites": 4, - "fix": 1 - }; - - m = indexPageMenu(gps); - - return; - } - - Bangle.on('GPS', (gps) => { - if (gps.fix === 0) return; - clearWatch(); - - if (isNaN(gps.course)) gps.course = 0; - require("Storage").writeJSON(LAST_GPS_FILE, JSON.stringify(gps)); - Bangle.setGPSPower(0); - Bangle.buzz(); - Bangle.setLCDPower(true); - - m = indexPageMenu(gps); - }); -} - function init() { - Bangle.setGPSPower(1); - drawGPSWaitPage(); + let location = require("Storage").readJSON("mylocation.json",1)||{"lat":51.5072,"lon":0.1276,"location":"London"}; + indexPageMenu(location); } let m; diff --git a/apps/astrocalc/metadata.json b/apps/astrocalc/metadata.json index d77474700..1f238be12 100644 --- a/apps/astrocalc/metadata.json +++ b/apps/astrocalc/metadata.json @@ -1,12 +1,13 @@ { "id": "astrocalc", "name": "Astrocalc", - "version": "0.03", + "version": "0.04", "description": "Calculates interesting information on the sun and moon cycles for the current day based on your location.", "icon": "astrocalc.png", - "tags": "app,sun,moon,cycles,tool,outdoors", - "supports": ["BANGLEJS"], + "tags": "app,sun,moon,cycles,tool", + "supports": ["BANGLEJS", "BANGLEJS2"], "allow_emulator": true, + "dependencies": {"mylocation":"app"}, "storage": [ {"name":"astrocalc.app.js","url":"astrocalc-app.js"}, {"name":"astrocalc.img","url":"astrocalc-icon.js","evaluate":true}, diff --git a/modules/suncalc.js b/modules/suncalc.js index 0c22c6cb2..fe17148e1 100644 --- a/modules/suncalc.js +++ b/modules/suncalc.js @@ -279,7 +279,7 @@ function hoursLater(date, h) { // calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article SunCalc.getMoonTimes = function (date, lat, lng, inUTC) { - var t = new Date(date); + var t = typeof(date) === "object" ? date : new Date(date); if (inUTC) t.setUTCHours(0, 0, 0, 0); else t.setHours(0, 0, 0, 0);