1111namespace Fade
1212{
1313
14- FadeMode mode = FadeMode::NONE;
15- uint16_t targetBrightness = 0 ;
16- Ticker fadeTicker;
17- Ticker debounce;
14+ namespace
15+ {
16+
17+ FadeMode mode = FadeMode::NONE;
18+ uint16_t targetBrightness = 0 ;
19+ Ticker fadeTicker;
20+ Ticker debounce;
21+
22+ void tick ()
23+ {
24+ if (FastLEDHub.status == PAUSED)
25+ return ;
26+
27+ if (mode == FadeMode::ALARM && FastLEDHub.brightness10 == 1023 )
28+ {
29+ if (Config.postAlarmAnimation != Config.alarmAnimation )
30+ FastLEDHub.begin (FastLEDHub.getAnimation (Config.postAlarmAnimation ));
31+
32+ stop ();
33+ PRINTLN (" [FastLEDHub] End fade 'Alarm'" );
34+ }
35+ else if (mode == FadeMode::SUNSET && FastLEDHub.brightness10 == targetBrightness)
36+ {
37+ stop ();
38+ PRINTLN (" [FastLEDHub] End fade 'Sunset'" );
39+ }
40+ else
41+ {
42+ FastLEDHub.brightness10 ++;
43+ PRINTLN (" [FastLEDHub] Fade brightness: " + String (FastLEDHub.brightness10 ));
44+ }
45+
46+ FastLEDHub.brightness10 = FastLEDHub.brightness10 ;
47+ }
48+
49+ void getSunsetTime ()
50+ {
51+ PRINT (" [FastLEDHub] Getting sunset time..." );
52+
53+ WiFiClient client;
54+ HTTPClient http;
55+ String url = " http://api.sunrise-sunset.org/json?lat=" + String (Config.latitude ) + " &lng=" + String (Config.longitude ) + " &date=today&formatted=0" ;
56+ http.begin (client, url);
57+ String payload = " " ;
58+ if (http.GET () > 0 )
59+ payload = http.getString ();
60+ http.end ();
61+
62+ StaticJsonDocument<1024 > doc;
63+ deserializeJson (doc, payload);
64+ if (doc.containsKey (" results" ) && doc[" results" ].containsKey (" sunset" ))
65+ {
66+ String sunset = doc[" results" ][" sunset" ].as <String>();
67+ int16_t sunsetHour = (sunset.substring (11 , 13 ).toInt () + Config.timeZone + Config.summerTime + 24 ) % 24 ;
68+ int16_t sunsetMinute = sunset.substring (14 , 16 ).toInt ();
69+ int16_t minutesSinceMidnight = sunsetHour * 60 + sunsetMinute;
70+ minutesSinceMidnight = (minutesSinceMidnight + Config.sunsetOffset + 1440 ) % 1440 ;
71+ Config.sunsetHour = minutesSinceMidnight / 60 ;
72+ Config.sunsetMinute = minutesSinceMidnight % 60 ;
73+ Config.save ();
74+ PRINTLN (" " + String (Config.sunsetHour ) + " :" + String (Config.sunsetMinute ));
75+ }
76+ else
77+ {
78+ PRINTLN (" failed. Using last known time instead." );
79+ }
80+ }
81+
82+ bool getCurrentTime (int8_t *hour, int8_t *minute)
83+ {
84+ time_t n = time (nullptr );
85+
86+ if (!n)
87+ return false ;
88+
89+ tm *now = gmtime (&n);
90+ *hour = (now->tm_hour + Config.timeZone + Config.summerTime ) % 24 ;
91+ *minute = now->tm_min ;
92+
93+ return true ;
94+ }
95+
96+ } // namespace
1897
1998 void initialize ()
2099 {
@@ -54,7 +133,7 @@ namespace Fade
54133 if (fadeMode == FadeMode::ALARM)
55134 {
56135 FastLEDHub.begin (FastLEDHub.getAnimation (Config.alarmAnimation ));
57- fadeTicker.attach_ms (Config.alarmDuration * 60 * 1000 / 1024 , tick);
136+ fadeTicker.attach_ms (Config.alarmDuration * 60 * 1000 / 1023 , tick);
58137 PRINTLN (" [FastLEDHub] Start fade 'Alarm'" );
59138 }
60139 else if (fadeMode == FadeMode::SUNSET)
@@ -71,78 +150,9 @@ namespace Fade
71150 mode = FadeMode::NONE;
72151 }
73152
74- void tick ()
75- {
76- if (FastLEDHub.status == PAUSED)
77- return ;
78-
79- if (mode == FadeMode::ALARM && FastLEDHub.brightness10 == 1023 )
80- {
81- if (Config.postAlarmAnimation != Config.alarmAnimation )
82- FastLEDHub.begin (FastLEDHub.getAnimation (Config.postAlarmAnimation ));
83-
84- stop ();
85- PRINTLN (" [FastLEDHub] End fade 'Alarm'" );
86- }
87- else if (mode == FadeMode::SUNSET && FastLEDHub.brightness10 == targetBrightness)
88- {
89- stop ();
90- PRINTLN (" [FastLEDHub] End fade 'Sunset'" );
91- }
92- else
93- {
94- FastLEDHub.brightness10 ++;
95- PRINTLN (" [FastLEDHub] Fade brightness: " + String (FastLEDHub.brightness10 ));
96- }
97-
98- FastLEDHub.brightness10 = FastLEDHub.brightness10 ;
99- }
100-
101- void getSunsetTime ()
153+ FadeMode getMode ()
102154 {
103- PRINT (" [FastLEDHub] Getting sunset time..." );
104-
105- WiFiClient client;
106- HTTPClient http;
107- String url = " http://api.sunrise-sunset.org/json?lat=" + String (Config.latitude ) + " &lng=" + String (Config.longitude ) + " &date=today&formatted=0" ;
108- http.begin (client, url);
109- String payload = " " ;
110- if (http.GET () > 0 )
111- payload = http.getString ();
112- http.end ();
113-
114- DynamicJsonDocument doc (2048 );
115- deserializeJson (doc, payload);
116- if (doc.containsKey (" results" ) && doc[" results" ].containsKey (" sunset" ))
117- {
118- String sunset = doc[" results" ][" sunset" ].as <String>();
119- int16_t sunsetHour = (sunset.substring (11 , 13 ).toInt () + Config.timeZone + Config.summerTime + 24 ) % 24 ;
120- int16_t sunsetMinute = sunset.substring (14 , 16 ).toInt ();
121- int16_t minutesSinceMidnight = sunsetHour * 60 + sunsetMinute;
122- minutesSinceMidnight = (minutesSinceMidnight + Config.sunsetOffset + 1440 ) % 1440 ;
123- Config.sunsetHour = minutesSinceMidnight / 60 ;
124- Config.sunsetMinute = minutesSinceMidnight % 60 ;
125- Config.save ();
126- PRINTLN (" " + String (Config.sunsetHour ) + " :" + String (Config.sunsetMinute ));
127- }
128- else
129- {
130- PRINTLN (" failed. Using last known time instead." );
131- }
132- }
133-
134- bool getCurrentTime (int8_t *hour, int8_t *minute)
135- {
136- time_t n = time (nullptr );
137-
138- if (!n)
139- return false ;
140-
141- tm *now = gmtime (&n);
142- *hour = (now->tm_hour + Config.timeZone + Config.summerTime ) % 24 ;
143- *minute = now->tm_min ;
144-
145- return true ;
155+ return mode;
146156 }
147157
148158} // namespace Fade
0 commit comments