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
esp32.svd
.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 <WiFiUdp.h>
#include <NTPClient.h>
#include "times.h"
#include <Time.h>
#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<WS2812, NEOPIXEL_PIN, GRB>(grid, GRIDLEN);
@ -44,6 +46,16 @@ void setup() {
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);