FIX: hanging due to NTP + added days to NEW_YEAR countdown

This commit is contained in:
Patrick Haßel 2023-01-02 10:15:54 +01:00
parent 684bbe8c5a
commit 897972059b
7 changed files with 155 additions and 79 deletions

View File

@ -14,9 +14,9 @@ board = esp32dev
framework = arduino framework = arduino
lib_deps = https://github.com/adafruit/Adafruit_NeoPixel lib_deps = https://github.com/adafruit/Adafruit_NeoPixel
build_flags = build_flags =
;upload_port = 10.0.0.153 upload_port = 10.0.0.153
;upload_protocol = espota upload_protocol = espota
upload_port = /dev/ttyUSB0 ;upload_port = /dev/ttyUSB0
upload_speed = 921600 ;upload_speed = 921600
monitor_port = /dev/ttyUSB0 monitor_port = /dev/ttyUSB0
monitor_speed = 115200 monitor_speed = 115200

View File

@ -1,95 +1,102 @@
#include "Display.h" #include "Display.h"
bool SYMBOLS[SYMBOL_COUNT][DISPLAY_CHAR_WIDTH * DISPLAY_CHAR_HEIGHT] = { bool SYMBOLS[SYMBOL_COUNT][DISPLAY_CHAR_WIDTH * DISPLAY_CHAR_HEIGHT] = {
{ {
X, X, X, X, X, X,
X, _, X, X, _, X,
X, _, X, X, _, X,
X, _, X, X, _, X,
X, X, X, X, X, X,
}, },
{ {
_, _, X, _, _, X,
_, X, X, _, X, X,
_, _, X, X, _, X,
_, _, X, _, _, X,
_, _, X, _, _, X,
}, },
{ {
X, X, X, X, X, X,
_, _, X, _, _, X,
X, X, X, X, X, X,
X, _, _, X, _, _,
X, X, X, X, X, X,
}, },
{ {
X, X, X, X, X, X,
_, _, X, _, _, X,
_, X, X, _, X, X,
_, _, X, _, _, X,
X, X, X, X, X, X,
}, },
{ {
X, _, X, X, _, X,
X, _, X, X, _, X,
X, X, X, X, X, X,
_, _, X, _, _, X,
_, _, X, _, _, X,
}, },
{ {
X, X, X, X, X, X,
X, _, _, X, _, _,
X, X, X, X, X, X,
_, _, X, _, _, X,
X, X, X, X, X, X,
}, },
{ {
X, X, X, X, X, X,
X, _, _, X, _, _,
X, X, X, X, X, X,
X, _, X, X, _, X,
X, X, X, X, X, X,
}, },
{ {
X, X, X, X, X, X,
_, _, X, _, _, X,
_, X, _, _, X, _,
X, _, _, X, _, _,
X, _, _, X, _, _,
}, },
{ {
X, X, X, X, X, X,
X, _, X, X, _, X,
X, X, X, X, X, X,
X, _, X, X, _, X,
X, X, X, X, X, X,
}, },
{ {
X, X, X, X, X, X,
X, _, X, X, _, X,
X, X, X, X, X, X,
_, _, X, _, _, X,
X, X, X, X, X, X,
}, },
{ {
_, _, _, _, _, _,
_, X, _, _, X, _,
_, _, _, _, _, _,
_, X, _, _, X, _,
_, _, _, _, _, _,
},
{
_, _, X,
_, _, X,
_, _, X,
X, _, X,
_, X, _,
},
{
X, _, X,
X, X, X,
_, X, _,
X, X, X,
X, _, X,
}, },
{
_, _, X,
_, _, X,
_, _, X,
X, _, X,
_, X, _,
},
{
X, _, X,
X, X, X,
_, X, _,
X, X, X,
X, _, X,
},
{
_, _, _,
_, _, _,
_, _, _,
_, _, _,
_, _, _,
},
}; };

View File

@ -5,7 +5,7 @@
#include "Adafruit_NeoPixel.h" #include "Adafruit_NeoPixel.h"
#include "Vector.h" #include "Vector.h"
#define SYMBOL_COUNT 13 #define SYMBOL_COUNT 14
#define DISPLAY_CHAR_WIDTH 3 #define DISPLAY_CHAR_WIDTH 3
#define DISPLAY_CHAR_HEIGHT 5 #define DISPLAY_CHAR_HEIGHT 5
@ -48,7 +48,7 @@ public:
void setup() { void setup() {
leds.begin(); leds.begin();
leds.setBrightness(32); leds.setBrightness(8);
clear(); clear();
} }

View File

@ -11,7 +11,6 @@
#include "mode/Test/Border.h" #include "mode/Test/Border.h"
#include "mode/Clock/Clock.h" #include "mode/Clock/Clock.h"
#include "mode/SpaceInvaders/SpaceInvaders.h" #include "mode/SpaceInvaders/SpaceInvaders.h"
#include "mode/NewYear/NewYear.h" #include "mode/NewYear/NewYear.h"
enum ModeId { enum ModeId {
@ -31,7 +30,7 @@ WebServer server(80);
Display display(32, 8); Display display(32, 8);
ModeId newModeId = SPACE_INVADERS; ModeId newModeId = NEW_YEAR;
ModeId currentModeId = NONE; ModeId currentModeId = NONE;
@ -163,9 +162,9 @@ void wifi_loop() {
if (!connected) { if (!connected) {
if (hasIp) { if (hasIp) {
connected = true; connected = true;
configTime(3600, 3600, WiFi.gatewayIP().toString().c_str());
Serial.printf("WiFi connected: %s\n", WiFi.localIP().toString().c_str()); Serial.printf("WiFi connected: %s\n", WiFi.localIP().toString().c_str());
// configTime(3600, 3600, WiFi.gatewayIP().toString().c_str()); yield();
// yield();
} }
} else { } else {
if (!hasIp) { if (!hasIp) {

View File

@ -8,7 +8,7 @@ class Clock : public Mode<Clock> {
public: public:
explicit Clock(Display *display) : explicit Clock(Display *display) :
Mode(display) { Mode(display) {
// nothing // nothing
} }
@ -19,21 +19,25 @@ public:
} }
void doStep(microseconds_t dt) override { void doStep(microseconds_t dt) override {
tm time{}; tm info{};
getLocalTime(&time); time_t now;
time(&now);
localtime_r(&now, &info);
bool ok = info.tm_year >= (2023 - 1900);
display->clear(); display->clear();
uint8_t x = 0; uint8_t x = 2;
display->print(&x, 1, time.tm_hour / 10); display->print(&x, 1, ok ? info.tm_hour / 10 : 13);
x++; x++;
display->print(&x, 1, time.tm_hour % 10); display->print(&x, 1, ok ? info.tm_hour % 10 : 13);
display->print(&x, 1, 10); display->print(&x, 1, 10);
display->print(&x, 1, time.tm_min / 10); display->print(&x, 1, ok ? info.tm_min / 10 : 13);
x++; x++;
display->print(&x, 1, time.tm_min % 10); display->print(&x, 1, ok ? info.tm_min % 10 : 13);
display->print(&x, 1, 10); display->print(&x, 1, 10);
display->print(&x, 1, time.tm_sec / 10); display->print(&x, 1, ok ? info.tm_sec / 10 : 13);
x++; x++;
display->print(&x, 1, time.tm_sec % 10); display->print(&x, 1, ok ? info.tm_sec % 10 : 13);
} }
}; };

View File

@ -22,7 +22,7 @@ protected:
public: public:
explicit ModeBase(Display *display) : explicit ModeBase(Display *display) :
display(display) { display(display) {
for (Timer **timer = timers; timer < timers + TIMER_COUNT; timer++) { for (Timer **timer = timers; timer < timers + TIMER_COUNT; timer++) {
*timer = nullptr; *timer = nullptr;
} }

View File

@ -21,37 +21,103 @@ class NewYear : public Mode<NewYear> {
} }
void step(Timer *timer, uint32_t counter, uint32_t currentCount) { void step(Timer *timer, uint32_t counter, uint32_t currentCount) {
tm t{}; tm info{};
getLocalTime(&t); time_t now;
time(&now);
localtime_r(&now, &info);
info.tm_year += 1900;
info.tm_mon += 1;
bool ok = info.tm_year >= 2023;
display->clear(); display->clear();
if (t.tm_mon + 1 == 12 && t.tm_mday == 31) { if (!ok) {
drawCountdown(t); drawNoTime();
} else { } else if (info.tm_mon == 1 && info.tm_mday == 1 && info.tm_hour == 0) {
drawYear(counter, t.tm_year + 1900); drawYear(counter, info.tm_year + 1900);
drawFirework(timer->interval); drawFirework(timer->interval);
} else {
drawCountdown(info);
} }
} }
void drawNoTime() {
uint8_t x = 2;
display->print(&x, 1, 13);
x++;
display->print(&x, 1, 13);
display->print(&x, 1, 10);
display->print(&x, 1, 13);
x++;
display->print(&x, 1, 13);
display->print(&x, 1, 10);
display->print(&x, 1, 13);
x++;
display->print(&x, 1, 13);
}
void drawCountdown(const tm &time) { void drawCountdown(const tm &time) {
size_t h = (24 - time.tm_hour - (time.tm_min > 0 || time.tm_sec > 0 ? 1 : 0)); size_t days = getDayCountForYear(time.tm_year) - time.tm_yday - 1;
size_t m = (60 - time.tm_min - (time.tm_sec > 0 ? 1 : 0)) % 60; size_t hours = (24 - time.tm_hour - (time.tm_min > 0 || time.tm_sec > 0 ? 1 : 0));
size_t s = (60 - time.tm_sec) % 60; size_t minutes = (60 - time.tm_min - (time.tm_sec > 0 ? 1 : 0)) % 60;
size_t seconds = (60 - time.tm_sec) % 60;
uint8_t x = 0; uint8_t x = 0;
if (h >= 10) {
display->print(&x, 1, h / 10, COLOR_WHITE); if (days > 0) {
drawDay(days, &x);
display->print(&x, 1, 10, COLOR_WHITE);
} else { } else {
x += 3; x += 2;
} }
x++; drawHour(days, hours, &x);
display->print(&x, 1, h % 10, COLOR_WHITE);
display->print(&x, 1, 10, COLOR_WHITE); display->print(&x, 1, 10, COLOR_WHITE);
display->print(&x, 1, m / 10, COLOR_WHITE); draw2Digit(minutes, &x);
x++; if (days <= 0) {
display->print(&x, 1, m % 10, COLOR_WHITE); display->print(&x, 1, 10, COLOR_WHITE);
display->print(&x, 1, 10, COLOR_WHITE); draw2Digit(seconds, &x);
display->print(&x, 1, s / 10, COLOR_WHITE); }
x++; }
display->print(&x, 1, s % 10, COLOR_WHITE);
void drawDay(size_t days, uint8_t *x) {
if (days > 100) {
display->print(x, 1, days / 100, COLOR_WHITE);
} else {
*x += 3;
}
(*x)++;
if (days > 10) {
display->print(x, 1, days / 10 % 10, COLOR_WHITE);
} else {
*x += 3;
}
(*x)++;
display->print(x, 1, days % 10, COLOR_WHITE);
}
void drawHour(size_t days, size_t hours, uint8_t *x) {
if (days > 0 || hours >= 10) {
display->print(x, 1, hours / 10, COLOR_WHITE);
} else {
*x += 3;
}
(*x)++;
display->print(x, 1, hours % 10, COLOR_WHITE);
}
void draw2Digit(size_t value, uint8_t *x) {
display->print(x, 1, value / 10, COLOR_WHITE);
(*x)++;
display->print(x, 1, value % 10, COLOR_WHITE);
}
static int getDayCountForYear(int year) {
bool leapYear = year % 4 == 0 && (year % 400 == 0 || year % 100 != 0);
if (leapYear) {
return 366;
}
return 365;
} }
void drawYear(uint32_t counter, int year) { void drawYear(uint32_t counter, int year) {
@ -77,7 +143,7 @@ class NewYear : public Mode<NewYear> {
public: public:
explicit NewYear(Display *display) : explicit NewYear(Display *display) :
Mode(display) { Mode(display) {
createTimer(500, [](Timer *timer, uint32_t counter, uint32_t currentCount) { instance->launch(timer, counter, currentCount); }); createTimer(500, [](Timer *timer, uint32_t counter, uint32_t currentCount) { instance->launch(timer, counter, currentCount); });
createTimer(50, [](Timer *timer, uint32_t counter, uint32_t currentCount) { instance->step(timer, counter, currentCount); }); createTimer(50, [](Timer *timer, uint32_t counter, uint32_t currentCount) { instance->step(timer, counter, currentCount); });
for (Firework *firework = fireworksBegin; firework < fireworksEnd; firework++) { for (Firework *firework = fireworksBegin; firework < fireworksEnd; firework++) {