Skip to content

Commit b9974c9

Browse files
Refactored for less branching.
1 parent d08f390 commit b9974c9

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

moonPhase.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ inline T map(T2 val, T2 in_min, T2 in_max, T out_min, T out_max) {
77
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
88
}
99

10-
static inline double _fhour( const struct tm &timeinfo ) {
11-
return timeinfo.tm_hour + map( ( timeinfo.tm_min * 60 ) + timeinfo.tm_sec, 0, 3600, 0.0, 1.0 );
10+
double moonPhase::_fhour(const struct tm &timeinfo) {
11+
return timeinfo.tm_hour + map((timeinfo.tm_min * 60) + timeinfo.tm_sec, 0, 3600, 0.0, 1.0);
1212
}
1313

14-
static double _Julian( int32_t year, int32_t month, const double &day )
14+
static double _Julian(int32_t year, int32_t month, const double &day)
1515
{
1616
int32_t b, c, e;
1717
b = 0;
@@ -30,15 +30,15 @@ static double _Julian( int32_t year, int32_t month, const double &day )
3030
return b + c + e + day + 1720994.5;
3131
}
3232

33-
static double _sun_position( const double &j )
33+
static double _sun_position(const double &j)
3434
{
3535
double n, x, e, l, dl, v;
3636
int32_t i;
3737
n = 360 / 365.2422 * j;
3838
i = n / 360;
3939
n = n - i * 360.0;
4040
x = n - 3.762863;
41-
if (x < 0) x += 360;
41+
x += (x < 0) ? 360 : 0;
4242
x *= DEG_TO_RAD;
4343
e = x;
4444
do {
@@ -52,16 +52,16 @@ static double _sun_position( const double &j )
5252
return l;
5353
}
5454

55-
static double _moon_position( const double &j, const double &ls )
55+
static double _moon_position(const double &j, const double &ls)
5656
{
5757
double ms, l, mm, ev, sms, ae, ec;
5858
int32_t i;
5959
ms = 0.985647332099 * j - 3.762863;
60-
if (ms < 0) ms += 360.0;
60+
ms += (ms < 0) ? 360.0 : 0;
6161
l = 13.176396 * j + 64.975464;
6262
i = l / 360;
6363
l = l - i * 360.0;
64-
if (l < 0) l += 360.0;
64+
l += (l < 0) ? 360 : 0;
6565
mm = l - 0.1114041 * j - 349.383063;
6666
i = mm / 360;
6767
mm -= i * 360.0;
@@ -75,7 +75,7 @@ static double _moon_position( const double &j, const double &ls )
7575
return l;
7676
}
7777

78-
static moonData_t _getPhase( const int32_t &year, const int32_t &month, const int32_t &day, const double &hour )
78+
moonData_t moonPhase::_getPhase(const int32_t year, const int32_t month, const int32_t day, const double &hour)
7979
{
8080
/*
8181
Calculates the phase of the moon at the given epoch.
@@ -85,19 +85,14 @@ static moonData_t _getPhase( const int32_t &year, const int32_t &month, const in
8585
double ls = _sun_position(j);
8686
double lm = _moon_position(j, ls);
8787
double t = lm - ls;
88-
if (t < 0) t += 360;
88+
t += (t < 0) ? 360 : 0;
8989
moonData_t returnValue;
9090
returnValue.angle = t;
9191
returnValue.percentLit = (1.0 - cos((lm - ls) * DEG_TO_RAD)) / 2;
9292
return returnValue;
9393
}
9494

95-
moonData_t moonPhase::getPhase( const time_t t )
96-
{
97-
struct tm timeinfo;
98-
gmtime_r( &t, &timeinfo );
99-
return _getPhase( 1900 + timeinfo.tm_year, 1 + timeinfo.tm_mon, timeinfo.tm_mday, _fhour( timeinfo ) );
100-
}
95+
10196

10297

10398

moonPhase.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef MoonPhase_h
1010
#define MoonPhase_h
1111

12-
#include "Arduino.h"
12+
#include <Arduino.h>
1313

1414
struct moonData_t
1515
{
@@ -20,7 +20,19 @@ struct moonData_t
2020
class moonPhase
2121
{
2222
public:
23-
moonData_t getPhase(){ return getPhase( time(NULL) ); };
24-
moonData_t getPhase( const time_t t );
23+
moonData_t getPhase(const time_t t)
24+
{
25+
struct tm timeinfo;
26+
gmtime_r(&t, &timeinfo);
27+
return _getPhase(1900 + timeinfo.tm_year, 1 + timeinfo.tm_mon, timeinfo.tm_mday, _fhour(timeinfo));
28+
}
29+
30+
moonData_t getPhase()
31+
{
32+
return getPhase(time(NULL));
33+
}
34+
private:
35+
double _fhour(const struct tm &timeinfo);
36+
moonData_t _getPhase(const int32_t year, const int32_t month, const int32_t day, const double &hour);
2537
};
2638
#endif

0 commit comments

Comments
 (0)