-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,495 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
cmake_minimum_required(VERSION 3.20) | ||
|
||
project(SunAndMoon LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
set(CMAKE_AUTOMOC ON) | ||
|
||
################################################################################ | ||
|
||
if(NOT QT_DEFAULT_MAJOR_VERSION) | ||
set(QT_DEFAULT_MAJOR_VERSION 6 CACHE STRING "Qt version to use (5 or 6), defaults to 6") | ||
endif() | ||
|
||
# Generic dependencies | ||
set(CORE_COMPONENTS Core Gui Qml) | ||
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} REQUIRED COMPONENTS ${CORE_COMPONENTS}) | ||
|
||
set(CORE_LIBRARIES Qt::Core Qt::Gui Qt::Qml) | ||
|
||
# Generic sources | ||
set(CORE_SOURCES | ||
SunAndMoon.cpp | ||
SunRise/SunRise.cpp | ||
MoonRise/MoonRise.cpp | ||
MoonPhase/MoonPhase.cpp | ||
SunAndMoon.h | ||
SunRise/SunRise.h | ||
MoonRise/MoonRise.h | ||
MoonPhase/MoonPhase.h | ||
) | ||
|
||
################################################################################ | ||
|
||
add_library(SunAndMoon OBJECT ${CORE_SOURCES}) | ||
add_library(SunAndMoon::SunAndMoon ALIAS SunAndMoon) | ||
|
||
target_link_libraries(SunAndMoon PRIVATE ${CORE_LIBRARIES}) | ||
target_include_directories(SunAndMoon PUBLIC ${CMAKE_CURRENT_LIST_DIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
MIT License | ||
|
||
Copyright (c) 2007 Stephen R. Schmitt | ||
Copyright (c) 2020 Cyrus Rahman | ||
Copyright (c) 2023 Emeric Grange | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2007 Stephen R. Schmitt | ||
Subsequent work Copyright 2020 Cyrus Rahman | ||
You may use or modify this source code in any way you find useful, provided | ||
that you agree that the author(s) have no warranty, obligations or liability. | ||
You must determine the suitability of this source code for your use. | ||
|
||
Redistributions of this source code must retain this copyright notice. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Calculate the phase and position of the moon for a given date. | ||
// The algorithm is simple and adequate for many purposes. | ||
// | ||
// This software was originally adapted to javascript by Stephen R. Schmitt | ||
// from a BASIC program from the 'Astronomical Computing' column of Sky & Telescope, | ||
// April 1994, page 86, written by Bradley E. Schaefer. | ||
// | ||
// Subsequently adapted from Stephen R. Schmitt's javascript to c++ for the Arduino | ||
// by Cyrus Rahman, this work is subject to Stephen Schmitt's copyright: | ||
// | ||
// Copyright 2004 Stephen R. Schmitt | ||
// Subsequent work Copyright 2020 Cyrus Rahman | ||
// You may use or modify this source code in any way you find useful, provided | ||
// that you agree that the author(s) have no warranty, obligations or liability. | ||
// You must determine the suitability of this source code for your use. | ||
|
||
#include "MoonPhase.h" | ||
|
||
#include <math.h> | ||
|
||
// Names of lunar phases | ||
static const char *phaseNames[] = {"New", "Evening Crescent", "First Quarter", | ||
"Waxing Gibbous", "Full", "Waning Gibbous", | ||
"Last Quarter", "Morning Crescent"}; | ||
// Names of Zodiac constellations | ||
static const char *zodiacNames[] = {"Pisces", "Aries", "Taurus", "Gemini", "Cancer", | ||
"Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", | ||
"Capricorn", "Aquarius"}; | ||
// Ecliptic angles of Zodiac constellations | ||
static const float zodiacAngles[] = {33.18, 51.16, 93.44, 119.48, 135.30, 173.34, | ||
224.17, 242.57, 271.26, 302.49, 311.72, 348.58}; | ||
|
||
// Constructor initialization. | ||
MoonPhase::MoonPhase() { | ||
jDate = 0; | ||
phase = 0; | ||
age = 0; | ||
fraction = 0; | ||
distance = 0; | ||
latitude = 0; | ||
longitude = 0; | ||
phaseName = zodiacName = ""; | ||
} | ||
|
||
// Determine the Moon Phase and orbital positions for the specified time. | ||
void | ||
MoonPhase::calculate(time_t t) { | ||
jDate = julianDate(t); | ||
|
||
// Calculate illumination (synodic) phase. | ||
// From number of days since new moon on Julian date MOON_SYNODIC_OFFSET | ||
// (1815UTC January 6, 2000), determine remainder of incomplete cycle. | ||
phase = (jDate - MOON_SYNODIC_OFFSET) / MOON_SYNODIC_PERIOD; | ||
phase -= floor(phase); | ||
|
||
// Calculate age and illumination fraction. | ||
age = phase * MOON_SYNODIC_PERIOD; | ||
fraction = (1.0 - cos(2 * M_PI * phase)) * 0.5; | ||
phaseName = phaseNames[(int)(phase * 8 + 0.5) % 8]; | ||
|
||
// Calculate distance from anomalistic phase. | ||
double distancePhase = (jDate - MOON_DISTANCE_OFFSET) / MOON_DISTANCE_PERIOD; | ||
distancePhase -= floor(distancePhase); | ||
distance = 60.4 - 3.3 * cos(2 * M_PI * distancePhase) | ||
- 0.6 * cos(2 * 2 * M_PI * phase - 2 * M_PI * distancePhase) | ||
- 0.5 * cos(2 * 2 * M_PI * phase); | ||
|
||
// Calculate ecliptic latitude from nodal (draconic) phase. | ||
double latPhase = (jDate - MOON_LATITUDE_OFFSET) / MOON_LATITUDE_PERIOD; | ||
latPhase -= floor(latPhase); | ||
latitude = 5.1 * sin(2 * M_PI * latPhase); | ||
|
||
// Calculate ecliptic longitude from sidereal motion. | ||
double longPhase = (jDate - MOON_LONGITUDE_OFFSET) / MOON_LONGITUDE_PERIOD; | ||
longPhase -= floor(longPhase); | ||
longitude = 360 * longPhase | ||
+ 6.3 * sin(2 * M_PI * distancePhase) | ||
+ 1.3 * sin(2 * 2 * M_PI * phase - 2 * M_PI * distancePhase) | ||
+ 0.7 * sin(2 * 2 * M_PI * phase); | ||
if (longitude > 360) | ||
longitude -= 360; | ||
|
||
// Select the Zodiac name. | ||
zodiacName = zodiacNames[0]; | ||
for (unsigned i = 0; i < sizeof(zodiacAngles) / sizeof(float); i++) { | ||
if (longitude < zodiacAngles[i]) { | ||
zodiacName = zodiacNames[i]; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Determine Julian date from Unix time. | ||
// Provides marginally accurate results with older Arduino 4-byte double. | ||
double | ||
MoonPhase::julianDate(time_t t) { | ||
return (t / 86400.0L + 2440587.5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef MoonPhase_h | ||
#define MoonPhase_h | ||
|
||
#include <time.h> | ||
|
||
#define MOON_SYNODIC_PERIOD 29.530588853 // Period of moon cycle in days. | ||
#define MOON_SYNODIC_OFFSET 2451550.26 // Reference cycle offset in days. | ||
#define MOON_DISTANCE_PERIOD 27.55454988 // Period of distance oscillation | ||
#define MOON_DISTANCE_OFFSET 2451562.2 | ||
#define MOON_LATITUDE_PERIOD 27.212220817 // Latitude oscillation | ||
#define MOON_LATITUDE_OFFSET 2451565.2 | ||
#define MOON_LONGITUDE_PERIOD 27.321582241 // Longitude oscillation | ||
#define MOON_LONGITUDE_OFFSET 2451555.8 | ||
|
||
class MoonPhase { | ||
public: | ||
double jDate; | ||
double phase; // 0 - 1, 0.5 = full | ||
double age; // Age in days of current cycle | ||
double fraction; // Fraction of illuminated disk | ||
double distance; // Moon distance in earth radii | ||
double latitude; // Moon ecliptic latitude | ||
double longitude; // Moon ecliptic longitude | ||
const char *phaseName; // New, Full, etc. | ||
const char *zodiacName; // Constellation | ||
|
||
MoonPhase(); | ||
void calculate(time_t); | ||
|
||
private: | ||
double julianDate(time_t); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# MoonPhase | ||
Library / C++ class for calculating the phase and position of the moon for Unix, Linux, Arduino | ||
|
||
## Overview | ||
Calculate the phase and position of the moon for a given date. | ||
|
||
## Synopsis | ||
Determine the phase of the moon and its ecliptic coordinates (ecliptic | ||
latitude, ecliptic longitude, and distance) for the specified time in seconds | ||
since the Unix epoch (January 1, 1970). The name of the moon phase (New, Full, etc.) | ||
and the name of the Zodiac constellation the moon is in are also provided. | ||
|
||
## Discussion and Applicability | ||
The algorithm is simple and adequate for many purposes. It requires very | ||
little computational effort and is suitable for small embedded systems. For | ||
systems with small 4-byte double precision floating point types (such as early | ||
Arduino processors) there is a version of the software included in | ||
this distribution (MoonPhaseFXP, for Floating X Precision) that avoids exceeding the | ||
available precision. | ||
|
||
To paraphrase the original author Bradley E Schaefer: | ||
|
||
I can only think of one application where lunar phase is needed to a | ||
hundredth of a day, whereas nearly all applications are happy to have | ||
one-day accuracy (which gives the phase to 3% accuracy). If you need | ||
high accuracy, then you should be using some other program... | ||
|
||
A limiting trouble is that the times of any particular lunar phase are | ||
not exactly periodic, but they wander around a bit, for example due to | ||
the usual effects of solar gravity. | ||
|
||
## Historical background | ||
|
||
This software was originally adapted to javascript by Stephen R. Schmitt | ||
from a BASIC program from the 'Astronomical Computing' column of Sky & Telescope, | ||
April 1994, page 84, written by Bradley E. Schaefer. | ||
|
||
## Usage | ||
|
||
To use the MoonPhase library, include MoonPhase.h | ||
|
||
#include <MoonPhase.h> | ||
#include <MoonPhaseFXP.h> // (for 4-byte double precision) | ||
|
||
### Detailed synopsis | ||
MoonPhase mp; | ||
mp.calculate(time_t time); | ||
|
||
MoonPhaseFXP mp; | ||
mp.calculate(time_t time); // (for 4-byte double precision) | ||
|
||
### Arguments | ||
time: // The time of the desired the moon phase and position, in UTC | ||
// seconds from the Unix epoch (January 1, 1970). | ||
|
||
// As the moon phase changes slowly and precision within a few | ||
// hours is not likely necessary for most applications, using the | ||
// time provided by the Arduino now(), which will be the time in | ||
// seconds from January 1, 1970 in your local time zone, should | ||
// be fine. | ||
|
||
### Returned values | ||
double mp.jDate; // The fractional Julian date for *time*. | ||
|
||
double mp.phase; // The phase of the moon, from 0 (new) to 0.5 (full) to 1.0 (new). | ||
|
||
double mp.age; // Age in days of the current cycle. | ||
|
||
double mp.fraction; // The illumination fraction, from 0% - 100%. | ||
|
||
double mp.distance; // Moon distance in earth radii. | ||
|
||
double mp.latitude; // Moon ecliptic latitude in degrees. | ||
|
||
double mp.longitude; // Moon ecliptic longitude in degrees. | ||
|
||
const char * mp.phaseName; // The name of the moon phase: New, Full, etc. | ||
|
||
const char * mp.zodiacName; // The name of the Zodiac constellation the moon is in. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2007 Stephen R. Schmitt | ||
Subsequent work Copyright 2020 Cyrus Rahman | ||
You may use or modify this source code in any way you find useful, provided | ||
that you agree that the author(s) have no warranty, obligations or liability. | ||
You must determine the suitability of this source code for your use. | ||
|
||
Redistributions of this source code must retain this copyright notice. |
Oops, something went wrong.