|
8 | 8 | #include <Ticker.h> |
9 | 9 | #include <time.h> |
10 | 10 |
|
11 | | - |
12 | 11 | namespace Fade |
13 | 12 | { |
14 | 13 |
|
15 | | -FadeMode mode = FadeMode::NONE; |
16 | | -uint16_t targetBrightness = 0; |
17 | | -Ticker fadeTicker; |
18 | | -Ticker debounce; |
19 | | - |
20 | | -void initialize() |
21 | | -{ |
22 | | - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); |
23 | | - getSunsetTime(); |
24 | | -} |
| 14 | + FadeMode mode = FadeMode::NONE; |
| 15 | + uint16_t targetBrightness = 0; |
| 16 | + Ticker fadeTicker; |
| 17 | + Ticker debounce; |
25 | 18 |
|
26 | | -void handle() |
27 | | -{ |
28 | | - if (mode != FadeMode::NONE || debounce.active()) |
29 | | - return; |
30 | | - |
31 | | - int8_t hour, minute; |
32 | | - if (!getCurrentTime(&hour, &minute)) |
33 | | - return; |
34 | | - |
35 | | - if (Config.alarmEnabled && hour == Config.alarmHour && minute == Config.alarmMinute) |
36 | | - { |
37 | | - Fade::begin(FadeMode::ALARM); |
38 | | - } |
39 | | - else if (Config.sunsetEnabled && hour == Config.sunsetHour && minute == Config.sunsetMinute && FastLEDHub.isDim()) |
| 19 | + void initialize() |
40 | 20 | { |
41 | | - Fade::begin(FadeMode::SUNSET); |
| 21 | + configTime(0, 0, "pool.ntp.org", "time.nist.gov"); |
| 22 | + getSunsetTime(); |
42 | 23 | } |
43 | | -} |
44 | | - |
45 | | -void begin(FadeMode fadeMode) |
46 | | -{ |
47 | | - mode = fadeMode; |
48 | | - targetBrightness = FastLEDHub.brightness10; |
49 | | - FastLEDHub.brightness10 = 0; |
50 | | - FastLEDHub.show(); |
51 | | - |
52 | | - debounce.once(61, [&](){ debounce.detach(); }); |
53 | 24 |
|
54 | | - if (fadeMode == FadeMode::ALARM) |
| 25 | + void handle() |
55 | 26 | { |
56 | | - FastLEDHub.begin(FastLEDHub.getAnimation(Config.alarmAnimation)); |
57 | | - fadeTicker.attach_ms(Config.alarmDuration * 60 * 1000 / 1024, tick); |
58 | | - PRINTLN("[FastLEDHub] Start fade 'Alarm'"); |
| 27 | + if (mode != FadeMode::NONE || debounce.active()) |
| 28 | + return; |
| 29 | + |
| 30 | + int8_t hour, minute; |
| 31 | + if (!getCurrentTime(&hour, &minute)) |
| 32 | + return; |
| 33 | + |
| 34 | + if (Config.alarmEnabled && hour == Config.alarmHour && minute == Config.alarmMinute) |
| 35 | + { |
| 36 | + Fade::begin(FadeMode::ALARM); |
| 37 | + } |
| 38 | + else if (Config.sunsetEnabled && hour == Config.sunsetHour && minute == Config.sunsetMinute && FastLEDHub.isDim()) |
| 39 | + { |
| 40 | + Fade::begin(FadeMode::SUNSET); |
| 41 | + } |
59 | 42 | } |
60 | | - else if (fadeMode == FadeMode::SUNSET) |
61 | | - { |
62 | | - FastLEDHub.begin(FastLEDHub.getAnimation(Config.sunsetAnimation)); |
63 | | - fadeTicker.attach_ms(Config.sunsetDuration * 60 * 1000 / targetBrightness, tick); |
64 | | - PRINTLN("[FastLEDHub] Start fade 'Sunset'"); |
65 | | - } |
66 | | -} |
67 | 43 |
|
68 | | -void stop() |
69 | | -{ |
70 | | - fadeTicker.detach(); |
71 | | - mode = FadeMode::NONE; |
72 | | -} |
73 | | - |
74 | | -void tick() |
75 | | -{ |
76 | | - if (FastLEDHub.status == PAUSED) |
77 | | - return; |
78 | | - |
79 | | - if (mode == FadeMode::ALARM && FastLEDHub.brightness10 == 1023) |
| 44 | + void begin(FadeMode fadeMode) |
80 | 45 | { |
81 | | - if (Config.postAlarmAnimation != Config.alarmAnimation) |
82 | | - FastLEDHub.begin(FastLEDHub.getAnimation(Config.postAlarmAnimation)); |
83 | | - |
84 | | - stop(); |
85 | | - PRINTLN("[FastLEDHub] End fade 'Alarm'"); |
| 46 | + mode = fadeMode; |
| 47 | + targetBrightness = FastLEDHub.brightness10; |
| 48 | + FastLEDHub.brightness10 = 0; |
| 49 | + FastLEDHub.show(); |
| 50 | + |
| 51 | + debounce.once(61, [&]() |
| 52 | + { debounce.detach(); }); |
| 53 | + |
| 54 | + if (fadeMode == FadeMode::ALARM) |
| 55 | + { |
| 56 | + FastLEDHub.begin(FastLEDHub.getAnimation(Config.alarmAnimation)); |
| 57 | + fadeTicker.attach_ms(Config.alarmDuration * 60 * 1000 / 1024, tick); |
| 58 | + PRINTLN("[FastLEDHub] Start fade 'Alarm'"); |
| 59 | + } |
| 60 | + else if (fadeMode == FadeMode::SUNSET) |
| 61 | + { |
| 62 | + FastLEDHub.begin(FastLEDHub.getAnimation(Config.sunsetAnimation)); |
| 63 | + fadeTicker.attach_ms(Config.sunsetDuration * 60 * 1000 / targetBrightness, tick); |
| 64 | + PRINTLN("[FastLEDHub] Start fade 'Sunset'"); |
| 65 | + } |
86 | 66 | } |
87 | | - else if (mode == FadeMode::SUNSET && FastLEDHub.brightness10 == targetBrightness) |
88 | | - { |
89 | | - stop(); |
90 | | - PRINTLN("[FastLEDHub] End fade 'Sunset'"); |
91 | | - } |
92 | | - else |
| 67 | + |
| 68 | + void stop() |
93 | 69 | { |
94 | | - FastLEDHub.brightness10++; |
95 | | - PRINTLN("[FastLEDHub] Fade brightness: " + String(FastLEDHub.brightness10)); |
| 70 | + fadeTicker.detach(); |
| 71 | + mode = FadeMode::NONE; |
96 | 72 | } |
97 | 73 |
|
98 | | - FastLEDHub.brightness10 = FastLEDHub.brightness10; |
99 | | -} |
100 | | - |
101 | | -void getSunsetTime() |
102 | | -{ |
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")) |
| 74 | + void tick() |
117 | 75 | { |
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)); |
| 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; |
127 | 99 | } |
128 | | - else |
| 100 | + |
| 101 | + void getSunsetTime() |
129 | 102 | { |
130 | | - PRINTLN("failed. Using last known time instead."); |
| 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 | + } |
131 | 132 | } |
132 | | -} |
133 | 133 |
|
134 | | -bool getCurrentTime(int8_t *hour, int8_t *minute) |
135 | | -{ |
136 | | - time_t n = time(nullptr); |
| 134 | + bool getCurrentTime(int8_t *hour, int8_t *minute) |
| 135 | + { |
| 136 | + time_t n = time(nullptr); |
137 | 137 |
|
138 | | - if (!n) |
139 | | - return false; |
| 138 | + if (!n) |
| 139 | + return false; |
140 | 140 |
|
141 | | - tm *now = gmtime(&n); |
142 | | - *hour = (now->tm_hour + Config.timeZone + Config.summerTime) % 24; |
143 | | - *minute = now->tm_min; |
| 141 | + tm *now = gmtime(&n); |
| 142 | + *hour = (now->tm_hour + Config.timeZone + Config.summerTime) % 24; |
| 143 | + *minute = now->tm_min; |
144 | 144 |
|
145 | | - return true; |
146 | | -} |
| 145 | + return true; |
| 146 | + } |
147 | 147 |
|
148 | 148 | } // namespace Fade |
0 commit comments