Skip to content

Commit

Permalink
Add SunAndMoon feature
Browse files Browse the repository at this point in the history
  • Loading branch information
emericg committed Apr 16, 2024
1 parent 9af0705 commit cc32363
Show file tree
Hide file tree
Showing 26 changed files with 1,495 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE MobileUI)
add_subdirectory(src/thirdparty/MobileSharing)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE MobileSharing)

# SunAndMoon
add_subdirectory(src/thirdparty/SunAndMoon)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE SunAndMoon)

# Qt
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
Qt6::Core
Expand Down
3 changes: 3 additions & 0 deletions WatchFlower.pro
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ include(src/thirdparty/MobileSharing/MobileSharing.pri)
include(src/thirdparty/SingleApplication/SingleApplication.pri)
DEFINES += QAPPLICATION_CLASS=QApplication

# Sun and moon utils
include(src/thirdparty/SunAndMoon/SunAndMoon.pri)

# Project files ################################################################

SOURCES += src/main.cpp \
Expand Down
3 changes: 3 additions & 0 deletions qml/DesktopApplication.qml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ ApplicationWindow {
case Qt.ApplicationActive:
//console.log("Qt.ApplicationActive")

// Update sun position
sunAndMoon.update()

// Check if we need an 'automatic' theme change
Theme.loadTheme(settingsManager.appTheme)

Expand Down
12 changes: 12 additions & 0 deletions qml/DeviceListSplit.qml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ Item {
anchors.left: parent.left
anchors.right: parent.right

WeatherWidget {
width: devicesView.cellWidth
height: devicesView.cellWidth * (devicesView.listWidget ? 0.4 : 0.5)
listMode: devicesView.listWidget
}

SunAndMoonWidget {
width: devicesView.cellWidth
height: devicesView.cellWidth * (devicesView.listWidget ? 0.6 : 0.5)
listMode: devicesView.listWidget
}

DeviceBrowserWidget {
width: devicesView.cellWidth
height: devicesView.cellWidth * 0.33
Expand Down
3 changes: 3 additions & 0 deletions qml/MobileApplication.qml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ ApplicationWindow {
case Qt.ApplicationActive:
//console.log("Qt.ApplicationActive")

// Update sun position
sunAndMoon.update()

// Check if we need an 'automatic' theme change
Theme.loadTheme(settingsManager.appTheme)

Expand Down
2 changes: 2 additions & 0 deletions qml/qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

<file alias="DeviceWidget.qml">widgets/DeviceWidget.qml</file>
<file alias="DeviceNearbyWidget.qml">widgets/DeviceNearbyWidget.qml</file>
<file alias="SunAndMoonWidget.qml">widgets/SunAndMoonWidget.qml</file>
<file alias="WeatherWidget.qml">widgets/WeatherWidget.qml</file>
<file alias="DeviceBrowserWidget.qml">widgets/DeviceBrowserWidget.qml</file>
<file alias="PlantBrowserWidget.qml">widgets/PlantBrowserWidget.qml</file>

Expand Down
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <MobileUI>
#include <MobileSharing>
#include <SingleApplication>
#include <SunAndMoon>

#include <QtGlobal>
#include <QLibraryInfo>
Expand Down Expand Up @@ -162,6 +163,10 @@ int main(int argc, char *argv[])
MenubarManager *mb = MenubarManager::getInstance();
#endif

// Sun and Moon
SunAndMoon sam;
sam.set(45.5, 6, QDateTime::currentDateTime());

// Plant database
PlantDatabase *pdb = PlantDatabase::getInstance();

Expand Down Expand Up @@ -203,6 +208,9 @@ int main(int argc, char *argv[])
engine_context->setContextProperty("utilsScreen", utilsScreen);
engine_context->setContextProperty("utilsLanguage", utilsLanguage);
//engine_context->setContextProperty("utilsPlant", utilsPlant);
engine_context->setContextProperty("sunAndMoon", &sam);
//engine_context->setContextProperty("weather", &ww);

engine_context->setContextProperty("startMinimized", (start_minimized || sm->getMinimized()));
engine_context->setContextProperty("qtConnectivityPatched", qtConnectivityPatched);

Expand Down
39 changes: 39 additions & 0 deletions src/thirdparty/SunAndMoon/CMakeLists.txt
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})
23 changes: 23 additions & 0 deletions src/thirdparty/SunAndMoon/LICENSE
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.
7 changes: 7 additions & 0 deletions src/thirdparty/SunAndMoon/MoonPhase/LICENSE
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.
98 changes: 98 additions & 0 deletions src/thirdparty/SunAndMoon/MoonPhase/MoonPhase.cpp
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);
}
34 changes: 34 additions & 0 deletions src/thirdparty/SunAndMoon/MoonPhase/MoonPhase.h
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
79 changes: 79 additions & 0 deletions src/thirdparty/SunAndMoon/MoonPhase/README.md
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.
7 changes: 7 additions & 0 deletions src/thirdparty/SunAndMoon/MoonRise/LICENSE
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.
Loading

0 comments on commit cc32363

Please sign in to comment.