diff --git a/src/main.cpp b/src/main.cpp index c9394c4..e4c4d81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include "wifi.h" #include "mode/Mode.h" #include "mode/ModeTimer.h" +#include "mode/ModeCountdown.h" void stepMode(); @@ -12,7 +13,7 @@ void buttonCallback(ButtonEvent event); Display display; -ModeTimer mode; +ModeCountdown mode; Button button(23, buttonCallback); diff --git a/src/mode/ModeCountdown.h b/src/mode/ModeCountdown.h new file mode 100644 index 0000000..3a09e59 --- /dev/null +++ b/src/mode/ModeCountdown.h @@ -0,0 +1,68 @@ +#ifndef MODE_COUNTDOWN_H +#define MODE_COUNTDOWN_H + +#include + +#include "Mode.h" +#include "Rest.h" + +class ModeCountdown final : public Mode { + + uint32_t targetEpochSeconds = 0; + + bool bars = false; + +public: + + explicit ModeCountdown() : Mode("Countdown") { + // + } + + void init() override { + Preferences preferences; + preferences.begin("Countdown", true); + targetEpochSeconds = preferences.getULong("targetEpochSeconds", 1767222000); + bars = preferences.getBool("bars", false); + preferences.end(); + } + + void draw(Display &display) override { + const auto rest = Rest(targetEpochSeconds - time(nullptr)); + if (rest.isPositive()) { + if (bars) { + drawCountdownBars(display, rest); + } else { + drawCountdownNumbers(display, rest); + } + } else { + drawAnimation(display); + } + } + +private: + + static void drawCountdownBars(const Display &display, const Rest &rest) { + // TODO + } + + static void drawCountdownNumbers(Display &display, const Rest &rest) { + display.clear(); + if (rest.daysPart > 9) { + display.print(15, 0, ALIGN_CENTER, FONT7, WHITE, "%d Tage", rest.daysPart); + } else if (rest.daysPart > 0) { + display.print(15, 0, ALIGN_CENTER, FONT7, WHITE, "%d T %d h", rest.daysPart, rest.hoursPart); + } else if (rest.hoursPart > 0) { + display.print(15, 0, ALIGN_CENTER, FONT7, WHITE, "%2d:%02d:%02d", rest.hoursPart, rest.minutesPart, rest.secondsPart); + } else if (rest.minutesPart > 0) { + display.print(15, 0, ALIGN_CENTER, FONT7, WHITE, "%2d:%02d", rest.minutesPart, rest.secondsPart); + } else if (rest.secondsPart > 0) { + display.print(15, 0, ALIGN_CENTER, FONT7, WHITE, "%2d.%1d", rest.minutesPart, rest.secondsPart, rest.decisPart); + } + } + + static void drawAnimation(const Display &display) { + // TODO + } + +}; +#endif diff --git a/src/mode/Rest.h b/src/mode/Rest.h index cacd8a3..13da39c 100644 --- a/src/mode/Rest.h +++ b/src/mode/Rest.h @@ -3,6 +3,8 @@ struct Rest { + bool negative; + uint64_t millisTotal; uint64_t decisTotal; @@ -25,8 +27,9 @@ struct Rest { uint32_t daysPart; - explicit Rest(const uint32_t millisTotal) - : millisTotal(millisTotal), + explicit Rest(const int64_t _millisTotal) + : negative(_millisTotal < 0), + millisTotal(negative ? -_millisTotal : _millisTotal), decisTotal(millisTotal / 100), secondsTotal(decisTotal / 10), minutesTotal(secondsTotal / 60), @@ -56,6 +59,10 @@ struct Rest { Serial.printf("%3d.%02d:%02d:%02d.%d\n", daysPart, hoursPart, minutesPart, secondsPart, decisPart); } + bool isPositive() const { + return !negative; + } + }; #endif