diff --git a/platformio.ini b/platformio.ini index db9814c..36c09d6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/display/Display.cpp b/src/display/Display.cpp index a46c5be..f825ebe 100644 --- a/src/display/Display.cpp +++ b/src/display/Display.cpp @@ -1,95 +1,102 @@ #include "Display.h" 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, + }, + { + _, _, _, + _, _, _, + _, _, _, + _, _, _, + _, _, _, + }, }; diff --git a/src/display/Display.h b/src/display/Display.h index 4f1aa69..b801642 100644 --- a/src/display/Display.h +++ b/src/display/Display.h @@ -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(); } diff --git a/src/main.cpp b/src/main.cpp index 6a15436..1e80c72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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) { diff --git a/src/mode/Clock/Clock.h b/src/mode/Clock/Clock.h index d5ee527..542b574 100644 --- a/src/mode/Clock/Clock.h +++ b/src/mode/Clock/Clock.h @@ -8,7 +8,7 @@ class Clock : public Mode { public: explicit Clock(Display *display) : - Mode(display) { + Mode(display) { // nothing } @@ -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); } }; diff --git a/src/mode/ModeBase.h b/src/mode/ModeBase.h index 5d333c1..6623fcc 100644 --- a/src/mode/ModeBase.h +++ b/src/mode/ModeBase.h @@ -22,7 +22,7 @@ protected: public: explicit ModeBase(Display *display) : - display(display) { + display(display) { for (Timer **timer = timers; timer < timers + TIMER_COUNT; timer++) { *timer = nullptr; } diff --git a/src/mode/NewYear/NewYear.h b/src/mode/NewYear/NewYear.h index 2a80c55..3da9bce 100644 --- a/src/mode/NewYear/NewYear.h +++ b/src/mode/NewYear/NewYear.h @@ -21,37 +21,103 @@ class NewYear : public Mode { } 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 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) { - 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; + 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 (h >= 10) { - display->print(&x, 1, h / 10, COLOR_WHITE); + + if (days > 0) { + drawDay(days, &x); + display->print(&x, 1, 10, COLOR_WHITE); } else { - x += 3; + x += 2; } - x++; - display->print(&x, 1, h % 10, COLOR_WHITE); + drawHour(days, hours, &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); - 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) { @@ -77,7 +143,7 @@ class NewYear : public Mode { public: explicit NewYear(Display *display) : - Mode(display) { + Mode(display) { 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); }); for (Firework *firework = fireworksBegin; firework < fireworksEnd; firework++) {