Skip to content

Commit d05ad91

Browse files
committed
Fix data types and includes
Also added a few more comments
1 parent af84e10 commit d05ad91

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/Astronomy.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,36 @@ uint8_t Astronomy::calculateMoonPhase(time_t timestamp) {
4545
*
4646
* Source: https://www.voidware.com/moon_phase.htm
4747
*/
48-
uint8_t Astronomy::calculateMoonPhase(uint16_t year, uint16_t month, uint16_t day) {
49-
uint16_t yearAsDays, monthAsDays;
48+
uint8_t Astronomy::calculateMoonPhase(uint16_t year, uint8_t month, uint8_t day) {
49+
50+
// This floating point moon phase algorithm works by simply dividing the lunar month of 29.53 days into the number
51+
// of days elapsed since a known new moon. So, it’s highly simplified, but should be good enough to get quarter
52+
// phases.
53+
54+
uint32_t yearAsDays;
55+
uint16_t monthAsDays;
5056
double daysSinceReferenceNewMoon;
5157
double moonCycles;
5258
uint16_t completedMoonCycles;
53-
  double moonAge;
59+
double moonAge;
5460
uint8_t phase;
5561

62+
// This adjustment ultimately comes from the calculation to turn y/m/d into a whole number of days offset. It’s a
63+
// modified version of the Julian Day number, but here it’s been simplified to work only between years 2000/1/1 and
64+
// 2099/12/31.
5665
if (month < 3) {
5766
year--;
5867
month += 12;
5968
}
60-
++month;          /* why incrementing the month? */
69+
month++;
6170

62-
yearAsDays = 365.25 * year;                                             /* 365.25 -> mean length of a calendar year */
63-
monthAsDays = 30.6 * month;                                             /* 30.6 -> mean length of a month */
64-
daysSinceReferenceNewMoon = yearAsDays + monthAsDays + day - 694039.09; /* number of days since known new moon on 1900-01-01, 694039.09 -> days elapsed since zero */
65-
moonCycles = daysSinceReferenceNewMoon / 29.53;                         /* 29.53 -> long-term average moon cycle duration in days */
66-
completedMoonCycles = moonCycles;                                       /* "casting" to int to get *completed* moon cycles i.e. only integer part */
67-
moonAge = moonCycles - completedMoonCycles;                             /* subtract integer part to leave fractional part which represents the current moon age */
68-
phase = moonAge * 8 + 0.5;                                              /* scale fraction from 0-8 and round by adding 0.5 */
69-
phase = phase & 7; /* 0 and 8 are the same so turn 8 into 0 */
71+
yearAsDays = 365.25 * year; // 365.25 -> mean length of a calendar year
72+
monthAsDays = 30.6 * month; // 30.6 -> mean length of a month
73+
daysSinceReferenceNewMoon = yearAsDays + monthAsDays + day - 694039.09; // number of days since known new moon on 1900-01-01, 694039.09 -> days elapsed since zero
74+
moonCycles = daysSinceReferenceNewMoon / 29.53; // 29.53 -> long-term average moon cycle duration in days
75+
completedMoonCycles = moonCycles; // "casting" to int to get *completed* moon cycles i.e. only integer part
76+
moonAge = moonCycles - completedMoonCycles; // subtract integer part to leave fractional part which represents the current moon age
77+
phase = moonAge * 8 + 0.5; // scale fraction from 0-8 and round by adding 0.5
78+
phase = phase & 7; // 0 and 8 are the same so turn 8 into 0
7079
return phase;
7180
}

src/Astronomy.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ See more at https://thingpulse.com
2424
*/
2525

2626
#pragma once
27-
#include <JsonListener.h>
28-
#include <JsonStreamingParser.h>
27+
#include "Arduino.h"
2928

3029
typedef struct AstronomyData {
3130

@@ -38,6 +37,6 @@ class Astronomy {
3837
public:
3938
Astronomy();
4039
uint8_t calculateMoonPhase(time_t timestamp);
41-
uint8_t calculateMoonPhase(uint16_t year, uint16_t month, uint16_t day);
40+
uint8_t calculateMoonPhase(uint16_t year, uint8_t month, uint8_t day);
4241

4342
};

0 commit comments

Comments
 (0)