Minor tweaks & optimisations.

This commit is contained in:
Blaz Kristan 2021-03-07 11:14:16 +01:00
parent b5abc6c724
commit 6fa136da0c
4 changed files with 36 additions and 28 deletions

View File

@ -118,7 +118,7 @@
</select><br> </select><br>
UTC offset: <input name="UO" type="number" min="-65500" max="65500" required> seconds (max. 18 hours)<br> UTC offset: <input name="UO" type="number" min="-65500" max="65500" required> seconds (max. 18 hours)<br>
Current local time is <span class="times">unknown</span>.<br> Current local time is <span class="times">unknown</span>.<br>
Longitude: <input name="LN" type="number" min="-180" max="180" step="0.01"> Latitude: <input name="LT" type="number" min="-90" max="90" step="0.01"> Longitude: <input name="LN" type="number" min="-180" max="180" step="0.01"> Latitude: <input name="LT" type="number" min="-66.6" max="66.6" step="0.01">
<h3>Clock</h3> <h3>Clock</h3>
Clock Overlay: Clock Overlay:
<select name="OL" onchange="Cs()"> <select name="OL" onchange="Cs()">

View File

@ -318,9 +318,9 @@ US-MST/MDT</option><option value="7">US-AZ</option><option value="8">US-PST/PDT
max="65500" required> seconds (max. 18 hours)<br>Current local time is <span max="65500" required> seconds (max. 18 hours)<br>Current local time is <span
class="times">unknown</span>.<br>Longitude: <input name="LN" type="number" class="times">unknown</span>.<br>Longitude: <input name="LN" type="number"
min="-180" max="180" step="0.01"> Latitude: <input name="LT" type="number" min="-180" max="180" step="0.01"> Latitude: <input name="LT" type="number"
min="-90" max="90" step="0.01"><h3>Clock</h3>Clock Overlay: <select name="OL" min="-66.6" max="66.6" step="0.01"><h3>Clock</h3>Clock Overlay: <select
onchange="Cs()"><option value="0" id="cn" selected="selected">None</option> name="OL" onchange="Cs()"><option value="0" id="cn" selected="selected">None
<option value="1" id="ca">Analog Clock</option><option value="2"> </option><option value="1" id="ca">Analog Clock</option><option value="2">
Single Digit Clock</option><option value="3" id="cc">Cronixie Clock</option> Single Digit Clock</option><option value="3" id="cc">Cronixie Clock</option>
</select><br><div id="coc">First LED: <input name="O1" type="number" min="0" </select><br><div id="coc">First LED: <input name="O1" type="number" min="0"
max="255" required> Last LED: <input name="O2" type="number" min="0" max="255" max="255" required> Last LED: <input name="O2" type="number" min="0" max="255"

View File

@ -219,7 +219,7 @@ void getTimeString(char* out)
if (hr > 11) hr -= 12; if (hr > 11) hr -= 12;
if (hr == 0) hr = 12; if (hr == 0) hr = 12;
} }
sprintf(out,"%i-%i-%i, %02d:%02d:%02d",year(localTime), month(localTime), day(localTime), hr, minute(localTime), second(localTime)); sprintf_P(out,PSTR("%i-%i-%i, %02d:%02d:%02d"),year(localTime), month(localTime), day(localTime), hr, minute(localTime), second(localTime));
if (useAMPM) if (useAMPM)
{ {
strcat(out,(hour(localTime) > 11)? " PM":" AM"); strcat(out,(hour(localTime) > 11)? " PM":" AM");
@ -266,7 +266,7 @@ void checkTimers()
lastTimerMinute = minute(localTime); lastTimerMinute = minute(localTime);
// calculate sunrise and sunset at midnight (if longitude and latitude are set) // calculate sunrise and sunset at midnight (if longitude and latitude are set)
if (((int)longitude || (int)latitude) && ((!hour(localTime) && !minute(localTime)) || !sunrise)) { if (((int)longitude || (int)latitude) && ((!hour(localTime) && !minute(localTime)) || (!sunrise && !sunset))) {
struct tm tim_0; struct tm tim_0;
tim_0.tm_year = year(localTime)-1900; tim_0.tm_year = year(localTime)-1900;
tim_0.tm_mon = month(localTime)-1; tim_0.tm_mon = month(localTime)-1;
@ -275,16 +275,26 @@ void checkTimers()
tim_0.tm_isdst = 0; tim_0.tm_isdst = 0;
int minUTC = getSunriseUTC(year(localTime), month(localTime), day(localTime), latitude, longitude); int minUTC = getSunriseUTC(year(localTime), month(localTime), day(localTime), latitude, longitude);
if (minUTC) {
// there is a sunrise
tim_0.tm_hour = minUTC / 60; tim_0.tm_hour = minUTC / 60;
tim_0.tm_min = minUTC % 60; tim_0.tm_min = minUTC % 60;
sunrise = tz->toLocal(mktime(&tim_0) - utcOffsetSecs); sunrise = tz->toLocal(mktime(&tim_0) - utcOffsetSecs);
DEBUG_PRINTF("Sunrise: %02d:%02d\n", hour(sunrise), minute(sunrise)); DEBUG_PRINTF("Sunrise: %02d:%02d\n", hour(sunrise), minute(sunrise));
} else {
sunrise = 0;
}
minUTC = getSunriseUTC(year(localTime), month(localTime), day(localTime), latitude, longitude, true); minUTC = getSunriseUTC(year(localTime), month(localTime), day(localTime), latitude, longitude, true);
if (minUTC) {
// there is a sunset
tim_0.tm_hour = minUTC / 60; tim_0.tm_hour = minUTC / 60;
tim_0.tm_min = minUTC % 60; tim_0.tm_min = minUTC % 60;
sunset = tz->toLocal(mktime(&tim_0) - utcOffsetSecs); sunset = tz->toLocal(mktime(&tim_0) - utcOffsetSecs);
DEBUG_PRINTF("Sunset: %02d:%02d\n", hour(sunset), minute(sunset)); DEBUG_PRINTF("Sunset: %02d:%02d\n", hour(sunset), minute(sunset));
} else {
sunset = 0;
}
} }
if (sunrise && sunset) daytime = difftime(localTime, sunrise) > 0 && difftime(localTime, sunset) < 0; if (sunrise && sunset) daytime = difftime(localTime, sunrise) > 0 && difftime(localTime, sunset) < 0;
@ -320,7 +330,7 @@ void checkTimers()
} }
} }
#define ZENITH -.83 #define ZENITH -0.83
int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunset) { int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunset) {
//1. first calculate the day of the year //1. first calculate the day of the year
float N1 = floor(275 * month / 9); float N1 = floor(275 * month / 9);
@ -336,10 +346,10 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
float M = (0.9856 * t) - 3.289; float M = (0.9856 * t) - 3.289;
//4. calculate the Sun's true longitude //4. calculate the Sun's true longitude
float L = fmod(M + (1.916 * sin((PI/180)*M)) + (0.020 * sin(2 *(PI/180) * M)) + 282.634,360.0); float L = fmod(M + (1.916 * sin(DEG_TO_RAD*M)) + (0.020 * sin(2*DEG_TO_RAD*M)) + 282.634, 360.0);
//5a. calculate the Sun's right ascension //5a. calculate the Sun's right ascension
float RA = fmod(180/PI*atan(0.91764 * tan((PI/180)*L)),360.0); float RA = fmod(RAD_TO_DEG*atan(0.91764 * tan(DEG_TO_RAD*L)), 360.0);
//5b. right ascension value needs to be in the same quadrant as L //5b. right ascension value needs to be in the same quadrant as L
float Lquadrant = floor( L/90) * 90; float Lquadrant = floor( L/90) * 90;
@ -347,22 +357,20 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
RA = RA + (Lquadrant - RAquadrant); RA = RA + (Lquadrant - RAquadrant);
//5c. right ascension value needs to be converted into hours //5c. right ascension value needs to be converted into hours
RA = RA / 15; RA /= 15.;
//6. calculate the Sun's declination //6. calculate the Sun's declination
float sinDec = 0.39782 * sin((PI/180)*L); float sinDec = 0.39782 * sin(DEG_TO_RAD*L);
float cosDec = cos(asin(sinDec)); float cosDec = cos(asin(sinDec));
//7a. calculate the Sun's local hour angle //7a. calculate the Sun's local hour angle
float cosH = (sin((PI/180)*ZENITH) - (sinDec * sin((PI/180)*lat))) / (cosDec * cos((PI/180)*lat)); float cosH = (sin(DEG_TO_RAD*ZENITH) - (sinDec * sin(DEG_TO_RAD*lat))) / (cosDec * cos(DEG_TO_RAD*lat));
/* if (cosH > 1 && !sunset) return 0; // the sun never rises on this location (on the specified date)
if (cosH > 1) the sun never rises on this location (on the specified date) if (cosH < -1 && sunset) return 0; // the sun never sets on this location (on the specified date)
if (cosH < -1) the sun never sets on this location (on the specified date)
*/
//7b. finish calculating H and convert into hours //7b. finish calculating H and convert into hours
float H = sunset ? (180/PI)*acos(cosH) : 360 - (180/PI)*acos(cosH); float H = sunset ? RAD_TO_DEG*acos(cosH) : 360 - RAD_TO_DEG*acos(cosH);
H = H / 15; H /= 15.;
//8. calculate local mean time of rising/setting //8. calculate local mean time of rising/setting
float T = H + RA - (0.06571 * t) - 6.622; float T = H + RA - (0.06571 * t) - 6.622;

View File

@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2103070 #define VERSION 2103071
//uncomment this if you have a "my_config.h" file you'd like to use //uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG //#define WLED_USE_MY_CONFIG