Daylight Saving Time support

This commit is contained in:
Neale Pickett 2023-04-09 12:42:02 -06:00
parent 1681139205
commit 0379530672
4 changed files with 44 additions and 15 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ debug.cfg
debug_custom.json debug_custom.json
esp32.svd esp32.svd
.vscode/ .vscode/
build/

17
timezones.h Normal file
View File

@ -0,0 +1,17 @@
#include <Timezone.h>
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240};
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -300};
Timezone TZ_US_Eastern(usEDT, usEST);
TimeChangeRule usCDT = {"CDT", Second, Sun, Mar, 2, -300};
TimeChangeRule usCST = {"CST", First, Sun, Nov, 2, -360};
Timezone TZ_US_Central(usCDT, usCST);
TimeChangeRule usMDT = {"MDT", Second, Sun, Mar, 2, -360};
TimeChangeRule usMST = {"MST", First, Sun, Nov, 2, -420};
Timezone TZ_US_Mountain(usMDT, usMST);
TimeChangeRule usPDT = {"EDT", Second, Sun, Mar, 2, -420};
TimeChangeRule usPST = {"EST", First, Sun, Nov, 2, -480};
Timezone TZ_US_Pacific(usPDT, usPST);

View File

@ -3,21 +3,23 @@
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <NTPClient.h> #include <NTPClient.h>
#include "times.h" #include <Time.h>
#include "durations.h"
#include "timezones.h"
#include "picker.h" #include "picker.h"
#include "network.h" #include "network.h"
#define NEOPIXEL_PIN 32 #define NEOPIXEL_PIN 32
#define GRIDLEN 64 #define GRIDLEN 64
#define WFM_PASSWORD "artsy fartsy" #define WFM_PASSWORD "artsy fartsy"
#define TIME_OFFSET (-7 * HOUR / SECOND) #define TIMEZONE TZ_US_Mountain
/* /*
* The hours when the day begins and ends. * The hours when the day begins and ends.
* During the day, everything is brighter. * At night, all you get is a dim clock.
*/ */
#define DAY_BEGIN 7 #define DAY_BEGIN 6
#define DAY_END 21 #define DAY_END 20
#define DAY_BRIGHTNESS 0x80 #define DAY_BRIGHTNESS 0x80
#define NIGHT_BRIGHTNESS 0x10 #define NIGHT_BRIGHTNESS 0x10
@ -35,7 +37,7 @@
CRGB grid[GRIDLEN]; CRGB grid[GRIDLEN];
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, TIME_OFFSET); NTPClient timeClient(ntpUDP);
void setup() { void setup() {
FastLED.addLeds<WS2812, NEOPIXEL_PIN, GRB>(grid, GRIDLEN); FastLED.addLeds<WS2812, NEOPIXEL_PIN, GRB>(grid, GRIDLEN);
@ -43,7 +45,17 @@ void setup() {
FastLED.setCorrection(0xc0ffff); FastLED.setCorrection(0xc0ffff);
network_setup(WFM_PASSWORD); network_setup(WFM_PASSWORD);
} }
bool updateTime() {
if (timeClient.update()) {
time_t now = timeClient.getEpochTime();
time_t local = TIMEZONE.toLocal(now);
setTime(local);
return true;
}
return false;
}
void fade(int cycles = 2) { void fade(int cycles = 2) {
int reps = (cycles*GRIDLEN) + random(GRIDLEN); int reps = (cycles*GRIDLEN) + random(GRIDLEN);
int hue = random(256); int hue = random(256);
@ -243,18 +255,18 @@ void spinner(int count=32) {
} }
void displayTime(unsigned long duration = 20 * SECOND) { void displayTime(unsigned long duration = 20 * SECOND) {
if (!connected()) return; if (timeStatus() != timeSet) return;
unsigned long end = millis() + duration; unsigned long end = millis() + duration;
FastLED.clear(); FastLED.clear();
while (millis() < end) { while (millis() < end) {
timeClient.update(); updateTime();
int hh = timeClient.getHours(); int hh = hour();
int mmss = timeClient.getMinutes()*60 + timeClient.getSeconds(); int mmss = now() % 3600;
uint8_t hue = HUE_YELLOW; uint8_t hue = HUE_YELLOW;
// Top: Hours // Top: Hours
if (hh >= 12) { if (isPM()) {
hue = HUE_ORANGE; hue = HUE_ORANGE;
hh -= 12; hh -= 12;
} }
@ -295,9 +307,8 @@ void loop() {
bool conn = connected(); bool conn = connected();
bool day = true; bool day = true;
timeClient.update(); if (updateTime()) {
if (timeClient.isTimeSet()) { int hh = hour();
int hh = timeClient.getHours();
day = ((hh >= DAY_BEGIN) && (hh < DAY_END)); day = ((hh >= DAY_BEGIN) && (hh < DAY_END));
} }
FastLED.setBrightness(day?DAY_BRIGHTNESS:NIGHT_BRIGHTNESS); FastLED.setBrightness(day?DAY_BRIGHTNESS:NIGHT_BRIGHTNESS);