|
| 1 | +#include "Clock.hpp" |
| 2 | +#include "fw16led/PresetOption.hpp" |
| 3 | +#include <chrono> |
| 4 | +#include <iomanip> |
| 5 | +#include <iostream> |
| 6 | +#include <sstream> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace fw16led::presets |
| 11 | +{ |
| 12 | + constexpr auto ID = "clock"; |
| 13 | + constexpr auto DISPLAY_NAME = "Clock"; |
| 14 | + const auto SETTINGS = std::vector<PresetOptionConfig>{ |
| 15 | + PresetOptionConfig{ |
| 16 | + .type = PresetOptionType::Dropdown, |
| 17 | + .key = "format", |
| 18 | + .label = "Format", |
| 19 | + .dropdownOptions = { |
| 20 | + DropdownOption(0, "12h"), |
| 21 | + DropdownOption(1, "24h")}, |
| 22 | + .defaultDropdown = 1}, |
| 23 | + }; |
| 24 | + |
| 25 | + Clock::Clock() |
| 26 | + : Preset(ID, DISPLAY_NAME) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + void Clock::render() |
| 31 | + { |
| 32 | + auto now = std::chrono::system_clock::now(); |
| 33 | + auto now_time_t = std::chrono::system_clock::to_time_t(now); |
| 34 | + |
| 35 | + std::tm local_time; |
| 36 | +#if defined(_MSC_VER) |
| 37 | + localtime_s(&local_time, &now_time_t); |
| 38 | +#else |
| 39 | + localtime_r(&now_time_t, &local_time); |
| 40 | +#endif |
| 41 | + |
| 42 | + auto format = getOptionValue<int>("format"); |
| 43 | + if (format == 0) |
| 44 | + { |
| 45 | + // Format the time as HH:MM AM/PM |
| 46 | + std::ostringstream time_stream; |
| 47 | + time_stream << std::put_time(&local_time, "%I:%M"); |
| 48 | + std::string current_time = time_stream.str(); |
| 49 | + panel->pattern_text(current_time); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + // Format the time as HH:MM |
| 54 | + std::ostringstream time_stream; |
| 55 | + time_stream << std::put_time(&local_time, "%H:%M"); |
| 56 | + std::string current_time = time_stream.str(); |
| 57 | + panel->pattern_text(current_time); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + void Clock::init(std::shared_ptr<ledmatrix::LedMatrix> panel) |
| 62 | + { |
| 63 | + this->panel = panel; |
| 64 | + |
| 65 | + timer = new QTimer(); |
| 66 | + QObject::connect(timer, &QTimer::timeout, [this]() |
| 67 | + { this->render(); }); |
| 68 | + timer->start(1000); |
| 69 | + |
| 70 | + render(); |
| 71 | + } |
| 72 | + |
| 73 | + void Clock::exit() |
| 74 | + { |
| 75 | + delete timer; |
| 76 | + } |
| 77 | + |
| 78 | + std::vector<PresetOptionConfig> Clock::getOptions() const |
| 79 | + { |
| 80 | + return SETTINGS; |
| 81 | + } |
| 82 | + |
| 83 | + void Clock::registerPreset(std::shared_ptr<PresetRegistry> registry) |
| 84 | + { |
| 85 | + registry->registerPreset(ID, DISPLAY_NAME, []() |
| 86 | + { return std::make_unique<fw16led::presets::Clock>(); }, SETTINGS); |
| 87 | + } |
| 88 | +} // namespace fw16led::presets |
0 commit comments