Merge pull request #1430 from HilmarSt/master

Moon Phase Widget: Replace the 8 phases by a more exact drawing
master
Gordon Williams 2022-02-14 09:42:31 +00:00 committed by GitHub
commit 14334c32d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 34 deletions

View File

@ -9,32 +9,32 @@ let s = {
'lat': 51.5072,
'lon': 0.1276,
'location': "London"
}
};
function loadSettings() {
settings = require('Storage').readJSON(SETTINGS_FILE, 1) || s;
}
function save() {
settings = s
require('Storage').write(SETTINGS_FILE, settings)
settings = s;
require('Storage').write(SETTINGS_FILE, settings);
}
const locations = ["London", "Newcastle", "Edinburgh", "Paris", "New York", "Tokyo","???"];
const lats = [51.5072 ,54.9783 ,55.9533 ,48.8566 ,40.7128 ,35.6762, 0.0];
const lons = [-0.1276 ,-1.6178 ,-3.1883 ,2.3522 , -74.0060 ,139.6503, 0.0];
const locations = ["London" ,"Newcastle","Edinburgh", "Paris" , "New York" , "Tokyo" , "Frankfurt", "Auckland", "???"];
const lats = [ 51.5072 , 54.9783 , 55.9533 , 48.8566 , 40.7128 , 35.6762 , 50.1236 , -36.9 , 0.0 ];
const lons = [ -0.1276 , -1.6178 , -3.1883 , 2.3522 , -74.0060 , 139.6503 , 8.6553 , 174.7832 , 0.0 ];
function setFromGPS() {
Bangle.on('GPS', (gps) => {
//console.log(".");
if (gps.fix === 0) return;
//console.log("fix from GPS");
s = {'lat': gps.lat, 'lon': gps.lon, 'location': '???' }
s = {'lat': gps.lat, 'lon': gps.lon, 'location': '???' };
Bangle.buzz(1500); // buzz on first position
Bangle.setGPSPower(0);
save();
Bangle.setUI("updown", ()=>{ load() });
Bangle.setUI("updown", ()=>{ load(); });
E.showPrompt("Location has been saved from the GPS fix",{
title:"Location Saved",
buttons : {"OK":1}
@ -49,13 +49,13 @@ function setFromGPS() {
}
function showMainMenu() {
console.log("showMainMenu");
// console.log("showMainMenu");
const mainmenu = {
'': { 'title': 'My Location' },
'<Back': ()=>{ load(); },
'City': {
value: 0 | locations.indexOf(s.location),
min: 0, max: 6,
min: 0, max: locations.length - 1,
format: v => locations[v],
onchange: v => {
if (v != 6) {
@ -67,7 +67,7 @@ function showMainMenu() {
}
},
'Set From GPS': ()=>{ setFromGPS(); }
}
};
return E.showMenu(mainmenu);
}

View File

@ -1,3 +1,4 @@
0.01: New App!
0.02: Fix position and overdraw bugs
Better memory usage, theme support
0.03: Better memory usage, theme support
0.04: Replace the 8 phases by a more exact drawing, see forum.espruino.com/conversations/371985

View File

@ -1,8 +1,8 @@
{
"id": "widmp",
"name": "Moon Phase Widget",
"version": "0.02",
"description": "Display the current moon phase in blueish for the northern hemisphere in eight phases",
"version": "0.04",
"description": "Display the current moon phase in blueish for both hemispheres. In the southern hemisphere the 'My Location' app is needed.",
"icon": "widget.png",
"type": "widget",
"tags": "widget,tools",

View File

@ -1,26 +1,63 @@
WIDGETS["widmoon"] = { area: "tr", width: 24, draw: function() {
const MC = 29.5305882, NM = 694039.09;
var r = 11, mx = this.x + 12; my = this.y + 12;
const CenterX = this.x + 12, CenterY = this.y + 12, Radius = 11;
var southernHemisphere = false; // when in southern hemisphere, use the "My Location" App
function moonPhase(d) {
var tmp, month = d.getMonth(), year = d.getFullYear(), day = d.getDate();
const simulate = false; // simulate one month in one minute
const updateR = 1000; // update every x ms in simulation
function moonPhase() {
const d = Date();
var month = d.getMonth(), year = d.getFullYear(), day = d.getDate();
if (simulate) day = d.getSeconds() / 2 +1;
if (month < 3) {year--; month += 12;}
tmp = ((365.25 * year + 30.6 * ++month + day - NM) / MC);
return Math.round(((tmp - (tmp | 0)) * 7)+1);
mproz = ((365.25 * year + 30.6 * ++month + day - 694039.09) / 29.5305882);
mproz = mproz - (mproz | 0); // strip integral digits, result is between 0 and <1
if (simulate) console.log(mproz + " " + day);
return (mproz);
}
const BLACK = g.theme.bg, MOON = 0x41f;
var moon = {
0: () => { g.reset().setColor(BLACK).fillRect(mx - r, my - r, mx + r, my + r);},
1: () => { moon[0](); g.setColor(MOON).drawCircle(mx, my, r);},
2: () => { moon[3](); g.setColor(BLACK).fillEllipse(mx - r / 2, my - r, mx + r / 2, my + r);},
3: () => { moon[0](); g.setColor(MOON).fillCircle(mx, my, r).setColor(BLACK).fillRect(mx - r, my - r, mx, my + r);},
4: () => { moon[3](); g.setColor(MOON).fillEllipse(mx - r / 2, my - r, mx + r / 2, my + r);},
5: () => { moon[0](); g.setColor(MOON).fillCircle(mx, my, r);},
6: () => { moon[7](); g.setColor(MOON).fillEllipse(mx - r / 2, my - r, mx + r / 2, my + r);},
7: () => { moon[0](); g.setColor(MOON).fillCircle(mx, my, r).setColor(BLACK).fillRect(mx, my - r, mx + r, my + r);},
8: () => { moon[7](); g.setColor(BLACK).fillEllipse(mx - r / 2, my - r, mx + r / 2, my + r);}
};
moon[moonPhase(Date())]();
} };
function loadLocation() {
// "mylocation.json" is created by the "My Location" app
location = require("Storage").readJSON("mylocation.json",1)||{"lat":50.1236,"lon":8.6553,"location":"Frankfurt"};
if (location.lat < 0) southernHemisphere = true;
}
// code source: github.com/rozek/banglejs-2-activities/blob/main/README.md#drawmoonphase
function drawMoonPhase (CenterX,CenterY, Radius, leftFactor,rightFactor) {
let x = Radius, y = 0, Error = Radius;
g.drawLine(CenterX-leftFactor*x,CenterY, CenterX+rightFactor*x,CenterY);
let dx,dy;
while (y <= x) {
dy = 1 + 2*y; y++; Error -= dy;
if (Error < 0) {
dx = 1 - 2*x; x--; Error -= dx;
}
g.drawLine(CenterX-leftFactor*x,CenterY-y, CenterX+rightFactor*x,CenterY-y);
g.drawLine(CenterX-leftFactor*x,CenterY+y, CenterX+rightFactor*x,CenterY+y);
g.drawLine(CenterX-leftFactor*y,CenterY-x, CenterX+rightFactor*y,CenterY-x);
g.drawLine(CenterX-leftFactor*y,CenterY+x, CenterX+rightFactor*y,CenterY+x);
}
}
function updateWidget() {
g.reset().setColor(g.theme.bg);
g.fillRect(CenterX - Radius, CenterY - Radius, CenterX + Radius, CenterY + Radius);
g.setColor(0x41f);
mproz = moonPhase(); // mproz = 0..<1
leftFactor = mproz * 4 - 1;
rightFactor = (1 - mproz) * 4 - 1;
if (mproz >= 0.5) leftFactor = 1; else rightFactor = 1;
if (true == southernHemisphere) {
var tmp=leftFactor; leftFactor=rightFactor; rightFactor=tmp;
}
drawMoonPhase(CenterX,CenterY, Radius, leftFactor,rightFactor);
if (simulate) setTimeout(updateWidget, updateR);
}
loadLocation();
updateWidget();
} };