Skip to content

Commit 87ed9b7

Browse files
committed
Change algorithm to determine moon icon
Includes suggestions & fixes by @Th0mg, thanks. Fixes #143.
1 parent 9c708c2 commit 87ed9b7

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

esp8266-weather-station-color.ino

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ TouchControllerWS touchController(&ts);
8787
void calibrationCallback(int16_t x, int16_t y);
8888
CalibrationCallback calibration = &calibrationCallback;
8989

90-
9190
OpenWeatherMapCurrentData currentWeather;
9291
OpenWeatherMapForecastData forecasts[MAX_FORECASTS];
92+
9393
SunMoonCalc::Moon moonData;
9494

9595
void updateData();
@@ -100,6 +100,7 @@ void drawCurrentWeather();
100100
void drawForecast();
101101
void drawForecastDetail(uint16_t x, uint16_t y, uint8_t dayIndex);
102102
void drawAstronomy();
103+
char determineMoonIcon();
103104
void drawCurrentWeatherDetail();
104105
void drawLabelValue(uint8_t line, String label, String value);
105106
void drawForecastTable(uint8_t start);
@@ -386,8 +387,8 @@ void updateData() {
386387
delete forecastClient;
387388
forecastClient = nullptr;
388389

389-
drawProgress(80, "Updating astronomy data...");
390-
// 'now' has to be UTC, lat/lng in degrees not radians
390+
drawProgress(80, "Updating astronomy...");
391+
// 'now' has to be epoch instant, lat/lng in degrees not radians
391392
SunMoonCalc *smCalc = new SunMoonCalc(now, currentWeather.lat, currentWeather.lon);
392393
moonData = smCalc->calculateSunAndMoonData().moon;
393394
delete smCalc;
@@ -519,13 +520,14 @@ void drawForecastDetail(uint16_t x, uint16_t y, uint8_t dayIndex) {
519520
gfx.drawString(x + 25, y + 60, String(forecasts[dayIndex].rain, 1) + (IS_METRIC ? "mm" : "in"));
520521
}
521522

522-
// draw moonphase and sunrise/set and moonrise/set
523+
// draw moonphase, sunrise/set and moonrise/set
523524
void drawAstronomy() {
524525

525526
gfx.setFont(MoonPhases_Regular_36);
526527
gfx.setColor(MINI_WHITE);
527528
gfx.setTextAlignment(TEXT_ALIGN_CENTER);
528-
gfx.drawString(120, 275, String((char) (97 + (moonData.illumination * 26))));
529+
// gfx.drawString(120, 275, String((char) (97 + (moonData.illumination * 26))));
530+
gfx.drawString(120, 275, String(determineMoonIcon()));
529531

530532
gfx.setColor(MINI_WHITE);
531533
gfx.setFont(ArialRoundedMTBold_14);
@@ -548,12 +550,35 @@ void drawAstronomy() {
548550
gfx.setColor(MINI_YELLOW);
549551
gfx.drawString(235, 250, SUN_MOON_TEXT[3]);
550552
gfx.setColor(MINI_WHITE);
553+
554+
float lunarMonth = 29.53;
555+
// approximate moon age
551556
gfx.drawString(190, 276, SUN_MOON_TEXT[4] + ":");
552557
gfx.drawString(235, 276, String(moonData.age, 1) + "d");
553558
gfx.drawString(190, 291, SUN_MOON_TEXT[5] + ":");
554559
gfx.drawString(235, 291, String(moonData.illumination * 100, 0) + "%");
555560
}
556561

562+
// The Moon Phases font has 26 icons for gradiations, 1 full icon, and 1 empty icon: https://www.dafont.com/moon-phases.font
563+
// All of them are an approximation.
564+
// Depending on date and location they would have to be rotated left or right by a varying degree.
565+
// GOTCHA I: as we use white to display the moon icon, what is black on that font page (link above) will effectively be rendered white!
566+
// GOTCHA II: illumination in the range {0,1} will with near certainty never be exactly 0 or 1; rounding is, therefore, essential to ever get full/new moon!
567+
char determineMoonIcon() {
568+
char moonIcon;
569+
// index in range of 0..14
570+
char index = round(moonData.illumination * 14);
571+
// Serial.printf("Moon illumination: %f -> moon icon index: %d\n", moonData.illumination, index);
572+
if (moonData.phase.index > 4) {
573+
// waning (4 = full moon)
574+
moonIcon = currentWeather.lat > 0 ? MOON_ICONS_NORTH_WANING[index] : MOON_ICONS_SOUTH_WANING[index];
575+
} else {
576+
// waxing
577+
moonIcon = currentWeather.lat > 0 ? MOON_ICONS_NORTH_WAXING[index] : MOON_ICONS_SOUTH_WAXING[index];
578+
}
579+
return moonIcon;
580+
}
581+
557582
void drawCurrentWeatherDetail() {
558583
gfx.setFont(ArialRoundedMTBold_14);
559584
gfx.setTextAlignment(TEXT_ALIGN_CENTER);
@@ -791,6 +816,8 @@ void loadPropertiesFromSpiffs() {
791816
Serial.printf(msg, "is12hStyle");
792817
}
793818
}
819+
} else {
820+
Serial.println("Does not exist.");
794821
}
795822
f.close();
796823
Serial.println("Effective properties now as follows:");

settings.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const boolean HARD_SLEEP = false; // true go into deepSleep false = turn
3737
// OpenWeatherMap Settings
3838
// Sign up here to get an API key: https://docs.thingpulse.com/how-tos/openweathermap-key/
3939
String OPEN_WEATHER_MAP_API_KEY = "";
40+
4041
/*
4142
Go to https://openweathermap.org/find?q= and search for a location. Go through the
4243
result set and select the entry closest to the actual location you want to display
@@ -45,6 +46,13 @@ at the end is what you assign to the constant below.
4546
*/
4647
String OPEN_WEATHER_MAP_LOCATION_ID = "2657896";
4748
String DISPLAYED_LOCATION_NAME = "Zurich";
49+
//String OPEN_WEATHER_MAP_LOCATION_ID = "3833367";
50+
//String DISPLAYED_LOCATION_NAME = "Ushuaia";
51+
//String OPEN_WEATHER_MAP_LOCATION_ID = "2147714";
52+
//String DISPLAYED_LOCATION_NAME = "Sydney";
53+
//String OPEN_WEATHER_MAP_LOCATION_ID = "5879400";
54+
//String DISPLAYED_LOCATION_NAME = "Anchorage";
55+
4856
/*
4957
Arabic -> ar, Bulgarian -> bg, Catalan -> ca, Czech -> cz, German -> de, Greek -> el,
5058
English -> en, Persian (Farsi) -> fa, Finnish -> fi, French -> fr, Galician -> gl,
@@ -79,6 +87,15 @@ bool IS_STYLE_HHMM = false; // true => HH:MM
7987
#define NTP_SERVERS "pool.ntp.org"
8088
// #define NTP_SERVERS "us.pool.ntp.org", "time.nist.gov", "pool.ntp.org"
8189

90+
// Locations on the northern hemisphere (latitude > 0) and those on the southern hemisphere need
91+
// an inverted set of moon phase icons/characters.
92+
// fully illuminated -> full moon -> char 48
93+
// zero illumination -> new moon -> char 64
94+
const char MOON_ICONS_NORTH_WANING[] = {64, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 48};
95+
const char MOON_ICONS_NORTH_WAXING[] = {64, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 48};
96+
const char MOON_ICONS_SOUTH_WANING[] = {64, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 48};
97+
const char MOON_ICONS_SOUTH_WAXING[] = {64, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 48};
98+
8299
// August 1st, 2018
83100
#define NTP_MIN_VALID_EPOCH 1533081600
84101

0 commit comments

Comments
 (0)