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
lib_deps = https://github.com/adafruit/Adafruit_NeoPixel
build_flags =
;upload_port = 10.0.0.153
;upload_protocol = espota
upload_port = /dev/ttyUSB0
upload_speed = 921600
upload_port = 10.0.0.153
upload_protocol = espota
;upload_port = /dev/ttyUSB0
;upload_speed = 921600
monitor_port = /dev/ttyUSB0
monitor_speed = 115200

View File

@ -11,7 +11,7 @@ bool SYMBOLS[SYMBOL_COUNT][DISPLAY_CHAR_WIDTH * DISPLAY_CHAR_HEIGHT] = {
{
_, _, X,
_, X, X,
_, _, X,
X, _, X,
_, _, X,
_, _, X,
},
@ -92,4 +92,11 @@ bool SYMBOLS[SYMBOL_COUNT][DISPLAY_CHAR_WIDTH * DISPLAY_CHAR_HEIGHT] = {
X, X, X,
X, _, X,
},
{
_, _, _,
_, _, _,
_, _, _,
_, _, _,
_, _, _,
},
};

View File

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

View File

@ -11,7 +11,6 @@
#include "mode/Test/Border.h"
#include "mode/Clock/Clock.h"
#include "mode/SpaceInvaders/SpaceInvaders.h"
#include "mode/NewYear/NewYear.h"
enum ModeId {
@ -31,7 +30,7 @@ WebServer server(80);
Display display(32, 8);
ModeId newModeId = SPACE_INVADERS;
ModeId newModeId = NEW_YEAR;
ModeId currentModeId = NONE;
@ -163,9 +162,9 @@ void wifi_loop() {
if (!connected) {
if (hasIp) {
connected = true;
configTime(3600, 3600, WiFi.gatewayIP().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 {
if (!hasIp) {

View File

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

View File

@ -21,37 +21,103 @@ class NewYear : public Mode<NewYear> {
}
void step(Timer *timer, uint32_t counter, uint32_t currentCount) {
tm t{};
getLocalTime(&t);
tm info{};
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();
if (t.tm_mon + 1 == 12 && t.tm_mday == 31) {
drawCountdown(t);
} else {
drawYear(counter, t.tm_year + 1900);
if (!ok) {
drawNoTime();
} else if (info.tm_mon == 1 && info.tm_mday == 1 && info.tm_hour == 0) {
drawYear(counter, info.tm_year + 1900);
drawFirework(timer->interval);
} else {
drawCountdown(info);
}
}
void drawCountdown(const tm &time) {
size_t h = (24 - time.tm_hour - (time.tm_min > 0 || time.tm_sec > 0 ? 1 : 0));
size_t m = (60 - time.tm_min - (time.tm_sec > 0 ? 1 : 0)) % 60;
size_t s = (60 - time.tm_sec) % 60;
uint8_t x = 0;
if (h >= 10) {
display->print(&x, 1, h / 10, COLOR_WHITE);
} else {
x += 3;
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);
}
x++;
display->print(&x, 1, h % 10, COLOR_WHITE);
void drawCountdown(const tm &time) {
size_t days = getDayCountForYear(time.tm_year) - time.tm_yday - 1;
size_t hours = (24 - time.tm_hour - (time.tm_min > 0 || time.tm_sec > 0 ? 1 : 0));
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;
if (days > 0) {
drawDay(days, &x);
display->print(&x, 1, 10, COLOR_WHITE);
display->print(&x, 1, m / 10, COLOR_WHITE);
x++;
display->print(&x, 1, m % 10, COLOR_WHITE);
} else {
x += 2;
}
drawHour(days, hours, &x);
display->print(&x, 1, 10, COLOR_WHITE);
display->print(&x, 1, s / 10, COLOR_WHITE);
x++;
display->print(&x, 1, s % 10, COLOR_WHITE);
draw2Digit(minutes, &x);
if (days <= 0) {
display->print(&x, 1, 10, COLOR_WHITE);
draw2Digit(seconds, &x);
}
}
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) {