commit
282ff71577
|
|
@ -15,8 +15,8 @@
|
|||
{ "id": "moonphase",
|
||||
"name": "Moonphase",
|
||||
"icon": "app.png",
|
||||
"version":"0.01",
|
||||
"description": "Shows current moon phase. Currently only with fixed coordinates (northern hemisphere).",
|
||||
"version":"0.02",
|
||||
"description": "Shows current moon phase. Now with GPS function.",
|
||||
"tags": "",
|
||||
"allow_emulator":true,
|
||||
"storage": [
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
0.01: New App!
|
||||
0.02: Added GPS to obtain coordinates, added buttons
|
||||
|
|
@ -1,6 +1,26 @@
|
|||
//Icons from https://icons8.com
|
||||
//Sun and Moon calculations from https://github.com/mourner/suncalc and https://gist.github.com/endel/dfe6bb2fbe679781948c
|
||||
|
||||
//varibales
|
||||
const storage = require('Storage');
|
||||
let coords;
|
||||
var timer;
|
||||
var fix;
|
||||
|
||||
var PI = Math.PI,
|
||||
sin = Math.sin,
|
||||
cos = Math.cos,
|
||||
tan = Math.tan,
|
||||
asin = Math.asin,
|
||||
atan = Math.atan2,
|
||||
acos = Math.acos,
|
||||
rad = PI / 180,
|
||||
dayMs = 1000 * 60 * 60 * 24,
|
||||
J1970 = 2440588,
|
||||
J2000 = 2451545;
|
||||
|
||||
var SunCalc = {};
|
||||
|
||||
//pictures
|
||||
function getImg(i) {
|
||||
var data = {
|
||||
|
|
@ -18,83 +38,63 @@ function getImg(i) {
|
|||
"LastQuarterSouth" : "AD8AAH/4AHx/gDwf8BwH/g4B/8MAf/HAH/5gB/+YAf/sAH//AB//wAf/8AH//AB//wAf/2AH/5gB/+cAf/jAH/w4B/8HAf+A8H/AHx/gAf/gAA/AAA==",
|
||||
"WaningCrescentNorth" : "AD8AAH/4AH8PgD+A8B/ADg/gAcP4ADH+AA5/AAGfwABv8AAP/AAD/wAA/8AAP/AAD/wAA38AAZ/AAGf4ADj+AAw/gAcH8AOA/gPAH8PgAf/gAA/AAA==",
|
||||
"WaxingCrescentSouth" : "AD8AAH/4AH8PgD+A8B/ADg/gAcP4ADH+AA5/AAGfwABv8AAP/AAD/wAA/8AAP/AAD/wAA38AAZ/AAGf4ADj+AAw/gAcH8AOA/gPAH8PgAf/gAA/AAA=="
|
||||
};
|
||||
};
|
||||
return {
|
||||
width : 26, height : 26, bpp : 1,
|
||||
transparent : 0,
|
||||
buffer : E.toArrayBuffer(atob(data[i]))
|
||||
};
|
||||
}
|
||||
// sun calculations are based on http://aa.quae.nl/en/reken/zonpositie.html formulas
|
||||
// date/time constants and conversions
|
||||
function toJulian(date) { return date.valueOf() / dayMs - 0.5 + J1970; }
|
||||
function fromJulian(j) { return new Date((j + 0.5 - J1970) * dayMs); }
|
||||
function toDays(date) { return toJulian(date) - J2000; }
|
||||
|
||||
//coordinates (will get from GPS later on real device)
|
||||
var lat = 52.96236,
|
||||
lon = 7.62571;
|
||||
|
||||
var PI = Math.PI,
|
||||
sin = Math.sin,
|
||||
cos = Math.cos,
|
||||
tan = Math.tan,
|
||||
asin = Math.asin,
|
||||
atan = Math.atan2,
|
||||
acos = Math.acos,
|
||||
rad = PI / 180;
|
||||
|
||||
// sun calculations are based on http://aa.quae.nl/en/reken/zonpositie.html formulas
|
||||
// date/time constants and conversions
|
||||
var dayMs = 1000 * 60 * 60 * 24,
|
||||
J1970 = 2440588,
|
||||
J2000 = 2451545;
|
||||
|
||||
function toJulian(date) { return date.valueOf() / dayMs - 0.5 + J1970; }
|
||||
function fromJulian(j) { return new Date((j + 0.5 - J1970) * dayMs); }
|
||||
function toDays(date) { return toJulian(date) - J2000; }
|
||||
|
||||
// general calculations for position
|
||||
var e = rad * 23.4397; // obliquity of the Earth
|
||||
function rightAscension(l, b) { return atan(sin(l) * cos(e) - tan(b) * sin(e), cos(l)); }
|
||||
function declination(l, b) { return asin(sin(b) * cos(e) + cos(b) * sin(e) * sin(l)); }
|
||||
function azimuth(H, phi, dec) { return atan(sin(H), cos(H) * sin(phi) - tan(dec) * cos(phi)); }
|
||||
function altitude(H, phi, dec) { return asin(sin(phi) * sin(dec) + cos(phi) * cos(dec) * cos(H)); }
|
||||
function siderealTime(d, lw) { return rad * (280.16 + 360.9856235 * d) - lw; }
|
||||
function astroRefraction(h) {
|
||||
// general calculations for position
|
||||
var e = rad * 23.4397; // obliquity of the Earth
|
||||
function rightAscension(l, b) { return atan(sin(l) * cos(e) - tan(b) * sin(e), cos(l)); }
|
||||
function declination(l, b) { return asin(sin(b) * cos(e) + cos(b) * sin(e) * sin(l)); }
|
||||
function azimuth(H, phi, dec) { return atan(sin(H), cos(H) * sin(phi) - tan(dec) * cos(phi)); }
|
||||
function altitude(H, phi, dec) { return asin(sin(phi) * sin(dec) + cos(phi) * cos(dec) * cos(H)); }
|
||||
function siderealTime(d, lw) { return rad * (280.16 + 360.9856235 * d) - lw; }
|
||||
function astroRefraction(h) {
|
||||
if (h < 0) // the following formula works for positive altitudes only.
|
||||
h = 0; // if h = -0.08901179 a div/0 would occur.
|
||||
|
||||
// formula 16.4 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
|
||||
// 1.02 / tan(h + 10.26 / (h + 5.10)) h in degrees, result in arc minutes -> converted to rad:
|
||||
return 0.0002967 / Math.tan(h + 0.00312536 / (h + 0.08901179));
|
||||
}
|
||||
}
|
||||
|
||||
// general sun calculations
|
||||
function solarMeanAnomaly(d) { return rad * (357.5291 + 0.98560028 * d); }
|
||||
function eclipticLongitude(M) {
|
||||
// general sun calculations
|
||||
function solarMeanAnomaly(d) { return rad * (357.5291 + 0.98560028 * d); }
|
||||
function eclipticLongitude(M) {
|
||||
|
||||
var C = rad * (1.9148 * sin(M) + 0.02 * sin(2 * M) + 0.0003 * sin(3 * M)), // equation of center
|
||||
P = rad * 102.9372; // perihelion of the Earth
|
||||
|
||||
return M + C + P + PI;
|
||||
}
|
||||
|
||||
function sunCoords(d) {
|
||||
}
|
||||
|
||||
function sunCoords(d) {
|
||||
var M = solarMeanAnomaly(d),
|
||||
L = eclipticLongitude(M);
|
||||
|
||||
return {
|
||||
dec: declination(L, 0),
|
||||
ra: rightAscension(L, 0)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
var SunCalc = {};
|
||||
|
||||
// adds a custom time to the times config
|
||||
SunCalc.addTime = function (angle, riseName, setName) {
|
||||
|
||||
// adds a custom time to the times config
|
||||
SunCalc.addTime = function (angle, riseName, setName) {
|
||||
times.push([angle, riseName, setName]);
|
||||
};
|
||||
};
|
||||
|
||||
// moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas
|
||||
function moonCoords(d) { // geocentric ecliptic coordinates of the moon
|
||||
// moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas
|
||||
function moonCoords(d) { // geocentric ecliptic coordinates of the moon
|
||||
var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude
|
||||
M = rad * (134.963 + 13.064993 * d), // mean anomaly
|
||||
F = rad * (93.272 + 13.229350 * d), // mean distance
|
||||
|
|
@ -107,9 +107,9 @@ function getImg(i) {
|
|||
dec: declination(l, b),
|
||||
dist: dt
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
SunCalc.getMoonPosition = function (date, lat, lng) {
|
||||
SunCalc.getMoonPosition = function (date, lat, lng) {
|
||||
|
||||
var lw = rad * -lng,
|
||||
phi = rad * lat,
|
||||
|
|
@ -126,13 +126,12 @@ function getImg(i) {
|
|||
distance: c.dist,
|
||||
parallacticAngle: pa
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
// calculations for illumination parameters of the moon,
|
||||
// based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and
|
||||
// Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
|
||||
|
||||
SunCalc.getMoonIllumination = function (date) {
|
||||
// calculations for illumination parameters of the moon,
|
||||
// based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and
|
||||
// Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
|
||||
SunCalc.getMoonIllumination = function (date) {
|
||||
var year = date.getFullYear();
|
||||
var month = date.getMonth();
|
||||
var day = date.getDate();
|
||||
|
|
@ -156,20 +155,18 @@ function getImg(i) {
|
|||
jd -= b; // subtract integer part to leave fractional part of original jd
|
||||
b = Math.round(jd * 8); // scale fraction from 0-8 and round
|
||||
if (b >= 8) b = 0; // 0 and 8 are the same so turn 8 into 0
|
||||
//print ({phase: b, name: Moon.phases[b]});
|
||||
return {phase: b, name: Moon.phases[b]};
|
||||
}
|
||||
};
|
||||
return (Moon.phase(year, month, day));
|
||||
};
|
||||
};
|
||||
|
||||
function hoursLater(date, h) {
|
||||
function hoursLater(date, h) {
|
||||
return new Date(date.valueOf() + h * dayMs / 24);
|
||||
}
|
||||
}
|
||||
|
||||
// calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article
|
||||
|
||||
SunCalc.getMoonTimes = function (date, lat, lng, inUTC) {
|
||||
// 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);
|
||||
if (inUTC) t.setUTCHours(0, 0, 0, 0);
|
||||
else t.setHours(0, 0, 0, 0);
|
||||
|
|
@ -210,9 +207,9 @@ function getImg(i) {
|
|||
if (set) result.set = hoursLater(t, set);
|
||||
if (!rise && !set) result[ye > 0 ? 'alwaysUp' : 'alwaysDown'] = true;
|
||||
return result;
|
||||
};
|
||||
};
|
||||
|
||||
function getMPhaseComp (offset) {
|
||||
function getMPhaseComp (offset) {
|
||||
var date = new Date();
|
||||
date.setDate(date.getDate() + offset);
|
||||
var dd = String(date.getDate());
|
||||
|
|
@ -222,9 +219,9 @@ function getImg(i) {
|
|||
var yyyy = date.getFullYear();
|
||||
var phase = SunCalc.getMoonIllumination(date);
|
||||
return dd + "." + mm + "." + yyyy + ": "+ phase.name;
|
||||
}
|
||||
}
|
||||
|
||||
function getMPhaseSim (offset) {
|
||||
function getMPhaseSim (offset) {
|
||||
var date = new Date();
|
||||
date.setDate(date.getDate() + offset);
|
||||
var dd = String(date.getDate());
|
||||
|
|
@ -234,10 +231,10 @@ function getImg(i) {
|
|||
var yyyy = date.getFullYear();
|
||||
var phase = SunCalc.getMoonIllumination(date);
|
||||
return phase.name;
|
||||
}
|
||||
}
|
||||
|
||||
function drawMoonPhase(offset, x, y){
|
||||
if (lat >= 0 && lat <= 90){ //Northern hemisphere
|
||||
function drawMoonPhase(offset, x, y){
|
||||
if (coords.lat >= 0 && coords.lat <= 90){ //Northern hemisphere
|
||||
if (getMPhaseSim(offset) == "new") {g.drawImage(getImg("NewMoon"), x, y);}
|
||||
if (getMPhaseSim(offset) == "waxing-crescent") {g.drawImage(getImg("WaxingCrescentNorth"), x, y);}
|
||||
if (getMPhaseSim(offset) == "first-quarter") {g.drawImage(getImg("FirstQuarterNorth"), x, y);}
|
||||
|
|
@ -246,7 +243,7 @@ function getImg(i) {
|
|||
if (getMPhaseSim(offset) == "waning-gibbous") {g.drawImage(getImg("WaningGibbousNorth"), x, y);}
|
||||
if (getMPhaseSim(offset) == "last-quarter") {g.drawImage(getImg("LastQuarterNorth"), x, y);}
|
||||
if (getMPhaseSim(offset) == "waning-crescent") {g.drawImage(getImg("WaningCrescentNorth"), x, y);}
|
||||
}
|
||||
}
|
||||
else { //Southern hemisphere
|
||||
if (getMPhaseSim(offset) == "new") {g.drawImage(getImg("NewMoon"), x, y);}
|
||||
if (getMPhaseSim(offset) == "waxing-crescent") {g.drawImage(getImg("WaxingCrescentSouth"), x, y);}
|
||||
|
|
@ -257,40 +254,100 @@ function getImg(i) {
|
|||
if (getMPhaseSim(offset) == "last-quarter") {g.drawImage(getImg("LastQuarterSouth"), x, y);}
|
||||
if (getMPhaseSim(offset) == "waning-crescent") {g.drawImage(getImg("WaningCrescentSouth"), x, y);}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawMoon(offset, x, y) {
|
||||
function drawMoon(offset, x, y) {
|
||||
g.setFont("6x8");
|
||||
g.clear();
|
||||
g.drawString("Key1: increase day, Key3:decrease day",10,10);
|
||||
g.drawString(getMPhaseComp(offset),x,y-10);
|
||||
drawMoonPhase(offset, x, y);
|
||||
g.drawString("Key1: day+, Key2:today, Key3:day-",x,y-30);
|
||||
g.drawString("Last known coordinates: " + coords.lat.toFixed(4) + " " + coords.lon.toFixed(4), x, y-20);
|
||||
g.drawString("Press BTN4 to update",x, y-10);
|
||||
|
||||
g.drawString(getMPhaseComp(offset+2),x,y+40);
|
||||
drawMoonPhase(offset+2, x, y+50);
|
||||
g.drawString(getMPhaseComp(offset),x,y+30);
|
||||
drawMoonPhase(offset, x+35, y+40);
|
||||
|
||||
g.drawString(getMPhaseComp(offset+4),x,y+90);
|
||||
drawMoonPhase(offset+4, x, y+100);
|
||||
g.drawString(getMPhaseComp(offset+2),x,y+70);
|
||||
drawMoonPhase(offset+2, x+35, y+80);
|
||||
|
||||
g.drawString(getMPhaseComp(offset+6),x,y+140);
|
||||
drawMoonPhase(offset+6, x, y+150);
|
||||
g.drawString(getMPhaseComp(offset+4),x,y+110);
|
||||
drawMoonPhase(offset+4, x+35, y+120);
|
||||
|
||||
g.drawString(getMPhaseComp(offset+6),x,y+150);
|
||||
drawMoonPhase(offset+6, x+35, y+160);
|
||||
}
|
||||
|
||||
//Write coordinates to file
|
||||
function updateCoords() {
|
||||
storage.write('coords.json', coords);
|
||||
}
|
||||
|
||||
//set coordinates to default (city where I live)
|
||||
function resetCoords() {
|
||||
coords = {
|
||||
lat : 52.96236,
|
||||
lon : 7.62571,
|
||||
};
|
||||
updateCoords();
|
||||
}
|
||||
|
||||
function getGpsFix() {
|
||||
Bangle.on('GPS', function(fix) {
|
||||
g.clear();
|
||||
|
||||
if (fix.fix == 1) {
|
||||
var gpsString = "lat: " + fix.lat.toFixed(4) + " lon: " + fix.lon.toFixed(4);
|
||||
coords.lat = fix.lat;
|
||||
coords.lon = fix.lon;
|
||||
updateCoords();
|
||||
g.drawString("Got GPS fix and wrote coords to file",10,20);
|
||||
g.drawString(gpsString,10,30);
|
||||
g.drawString("Press BTN5 to return to app",10,40);
|
||||
clearInterval(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
else {
|
||||
g.drawString("Searching satellites...",10,20);
|
||||
g.drawString("Press BTN5 to stop GPS",10, 30);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function start() {
|
||||
function start() {
|
||||
var x = 10;
|
||||
var y = 40;
|
||||
var y = 50;
|
||||
var offsetMoon = 0;
|
||||
coords = storage.readJSON('coords.json',1); //read coordinates from file
|
||||
if (!coords) resetCoords(); //if coordinates could not be read, reset them
|
||||
drawMoon(offsetMoon, x, y); //offset, x, y
|
||||
|
||||
//define button functions
|
||||
setWatch(function() {
|
||||
setWatch(function() { //BTN1
|
||||
offsetMoon++; //jump to next day
|
||||
drawMoon(offsetMoon, x, y); //offset, x, y
|
||||
}, BTN1, {edge:"rising", debounce:50, repeat:true});
|
||||
setWatch(function() {
|
||||
|
||||
setWatch(function() { //BTN2
|
||||
offsetMoon = 0; //jump to today
|
||||
drawMoon(offsetMoon, x, y); //offset, x, y
|
||||
}, BTN2, {edge:"rising", debounce:50, repeat:true});
|
||||
|
||||
setWatch(function() { //BTN3
|
||||
offsetMoon--; //jump to next day
|
||||
drawMoon(offsetMoon, x, y); //offset, x, y
|
||||
}, BTN3, {edge:"rising", debounce:50, repeat:true});
|
||||
}
|
||||
|
||||
start();
|
||||
setWatch(function() { //BTN4
|
||||
g.drawString("--- Getting GPS signal ---",x, y);
|
||||
Bangle.setGPSPower(1);
|
||||
timer = setInterval(getGpsFix, 10000);
|
||||
}, BTN4, {edge:"rising", debounce:50, repeat:true});
|
||||
|
||||
setWatch(function() { //BTN5
|
||||
if (timer) clearInterval(timer);
|
||||
timer = undefined;
|
||||
Bangle.setGPSPower(0);
|
||||
drawMoon(offsetMoon, x, y); //offset, x, y
|
||||
}, BTN5, {edge:"rising", debounce:50, repeat:true});
|
||||
}
|
||||
|
||||
start();
|
||||
Loading…
Reference in New Issue