Skip to content

Commit

Permalink
Refactored for less branching.
Browse files Browse the repository at this point in the history
  • Loading branch information
CelliesProjects committed Mar 26, 2021
1 parent d08f390 commit b9974c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
27 changes: 11 additions & 16 deletions moonPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ inline T map(T2 val, T2 in_min, T2 in_max, T out_min, T out_max) {
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

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

static double _Julian( int32_t year, int32_t month, const double &day )
static double _Julian(int32_t year, int32_t month, const double &day)
{
int32_t b, c, e;
b = 0;
Expand All @@ -30,15 +30,15 @@ static double _Julian( int32_t year, int32_t month, const double &day )
return b + c + e + day + 1720994.5;
}

static double _sun_position( const double &j )
static double _sun_position(const double &j)
{
double n, x, e, l, dl, v;
int32_t i;
n = 360 / 365.2422 * j;
i = n / 360;
n = n - i * 360.0;
x = n - 3.762863;
if (x < 0) x += 360;
x += (x < 0) ? 360 : 0;
x *= DEG_TO_RAD;
e = x;
do {
Expand All @@ -52,16 +52,16 @@ static double _sun_position( const double &j )
return l;
}

static double _moon_position( const double &j, const double &ls )
static double _moon_position(const double &j, const double &ls)
{
double ms, l, mm, ev, sms, ae, ec;
int32_t i;
ms = 0.985647332099 * j - 3.762863;
if (ms < 0) ms += 360.0;
ms += (ms < 0) ? 360.0 : 0;
l = 13.176396 * j + 64.975464;
i = l / 360;
l = l - i * 360.0;
if (l < 0) l += 360.0;
l += (l < 0) ? 360 : 0;
mm = l - 0.1114041 * j - 349.383063;
i = mm / 360;
mm -= i * 360.0;
Expand All @@ -75,7 +75,7 @@ static double _moon_position( const double &j, const double &ls )
return l;
}

static moonData_t _getPhase( const int32_t &year, const int32_t &month, const int32_t &day, const double &hour )
moonData_t moonPhase::_getPhase(const int32_t year, const int32_t month, const int32_t day, const double &hour)
{
/*
Calculates the phase of the moon at the given epoch.
Expand All @@ -85,19 +85,14 @@ static moonData_t _getPhase( const int32_t &year, const int32_t &month, const in
double ls = _sun_position(j);
double lm = _moon_position(j, ls);
double t = lm - ls;
if (t < 0) t += 360;
t += (t < 0) ? 360 : 0;
moonData_t returnValue;
returnValue.angle = t;
returnValue.percentLit = (1.0 - cos((lm - ls) * DEG_TO_RAD)) / 2;
return returnValue;
}

moonData_t moonPhase::getPhase( const time_t t )
{
struct tm timeinfo;
gmtime_r( &t, &timeinfo );
return _getPhase( 1900 + timeinfo.tm_year, 1 + timeinfo.tm_mon, timeinfo.tm_mday, _fhour( timeinfo ) );
}




18 changes: 15 additions & 3 deletions moonPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef MoonPhase_h
#define MoonPhase_h

#include "Arduino.h"
#include <Arduino.h>

struct moonData_t
{
Expand All @@ -20,7 +20,19 @@ struct moonData_t
class moonPhase
{
public:
moonData_t getPhase(){ return getPhase( time(NULL) ); };
moonData_t getPhase( const time_t t );
moonData_t getPhase(const time_t t)
{
struct tm timeinfo;
gmtime_r(&t, &timeinfo);
return _getPhase(1900 + timeinfo.tm_year, 1 + timeinfo.tm_mon, timeinfo.tm_mday, _fhour(timeinfo));
}

moonData_t getPhase()
{
return getPhase(time(NULL));
}
private:
double _fhour(const struct tm &timeinfo);
moonData_t _getPhase(const int32_t year, const int32_t month, const int32_t day, const double &hour);
};
#endif

0 comments on commit b9974c9

Please sign in to comment.