From 0379530672cedf08803cf02812edd8a2417ac901 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sun, 9 Apr 2023 12:42:02 -0600 Subject: [PATCH] Daylight Saving Time support --- .gitignore | 1 + times.h => durations.h | 0 timezones.h | 17 +++++++++++++++++ wallart.ino | 41 ++++++++++++++++++++++++++--------------- 4 files changed, 44 insertions(+), 15 deletions(-) rename times.h => durations.h (100%) create mode 100644 timezones.h diff --git a/.gitignore b/.gitignore index 96109b4..796871f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ debug.cfg debug_custom.json esp32.svd .vscode/ +build/ diff --git a/times.h b/durations.h similarity index 100% rename from times.h rename to durations.h diff --git a/timezones.h b/timezones.h new file mode 100644 index 0000000..e7a64b2 --- /dev/null +++ b/timezones.h @@ -0,0 +1,17 @@ +#include + +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); diff --git a/wallart.ino b/wallart.ino index 85ef703..1af35b9 100644 --- a/wallart.ino +++ b/wallart.ino @@ -3,21 +3,23 @@ #include #include #include -#include "times.h" +#include +#include "durations.h" +#include "timezones.h" #include "picker.h" #include "network.h" #define NEOPIXEL_PIN 32 #define GRIDLEN 64 #define WFM_PASSWORD "artsy fartsy" -#define TIME_OFFSET (-7 * HOUR / SECOND) +#define TIMEZONE TZ_US_Mountain /* * 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_END 21 +#define DAY_BEGIN 6 +#define DAY_END 20 #define DAY_BRIGHTNESS 0x80 #define NIGHT_BRIGHTNESS 0x10 @@ -35,7 +37,7 @@ CRGB grid[GRIDLEN]; WiFiUDP ntpUDP; -NTPClient timeClient(ntpUDP, TIME_OFFSET); +NTPClient timeClient(ntpUDP); void setup() { FastLED.addLeds(grid, GRIDLEN); @@ -43,7 +45,17 @@ void setup() { FastLED.setCorrection(0xc0ffff); 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) { int reps = (cycles*GRIDLEN) + random(GRIDLEN); int hue = random(256); @@ -243,18 +255,18 @@ void spinner(int count=32) { } void displayTime(unsigned long duration = 20 * SECOND) { - if (!connected()) return; + if (timeStatus() != timeSet) return; unsigned long end = millis() + duration; FastLED.clear(); while (millis() < end) { - timeClient.update(); - int hh = timeClient.getHours(); - int mmss = timeClient.getMinutes()*60 + timeClient.getSeconds(); + updateTime(); + int hh = hour(); + int mmss = now() % 3600; uint8_t hue = HUE_YELLOW; // Top: Hours - if (hh >= 12) { + if (isPM()) { hue = HUE_ORANGE; hh -= 12; } @@ -295,9 +307,8 @@ void loop() { bool conn = connected(); bool day = true; - timeClient.update(); - if (timeClient.isTimeSet()) { - int hh = timeClient.getHours(); + if (updateTime()) { + int hh = hour(); day = ((hh >= DAY_BEGIN) && (hh < DAY_END)); } FastLED.setBrightness(day?DAY_BRIGHTNESS:NIGHT_BRIGHTNESS);