/* * Copyright (C) 2019 Max Regan * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "DisplayTimeScreen.h" #include "RtcDriver.h" #include "SystemTime.h" #include "SystemFonts.h" using Common::ReturnCode; using Common::Time; using Common::Schedule::NextTime; DisplayTimeScreen::DisplayTimeScreen(BSP::DisplayDriver &driver, ScreenManager &manager, Screen &menu_screen) : m_driver(driver) , m_last_time() , m_manager(manager) , m_menu_screen(menu_screen) , m_display_seconds(false) {} static char get_char_for_digit(uint8_t bcd_digit) { if (bcd_digit > 9) { return '0'; } return bcd_digit + '0'; } ReturnCode DisplayTimeScreen::init() { return ReturnCode::OK; } void DisplayTimeScreen::display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones, const struct font &f) { char time_str[3] = { 0 }; time_str[0] = get_char_for_digit(tens); time_str[1] = get_char_for_digit(ones); time_str[2] = '\0'; m_driver.string_at(x, y, time_str, &f); } void DisplayTimeScreen::display_time() { Common::WallClockTime time; BSP::RtcDriver::get_time(time); const struct font &font = large_font; const uint32_t font_width = large_font_width; const uint32_t y_space = (m_driver.get_height() - (2 * font.size)) / 3; const uint32_t x_space = (m_driver.get_width() - (2 * font_width)) / 2; uint32_t x = 0; if (m_last_time.get_hours_24() != time.get_hours_24()) { x = x_space; display_number(&x, y_space, time.get_hours_12_tens(), time.get_hours_12_ones(), font); } if (m_last_time.get_minutes() != time.get_minutes()) { x = x_space; display_number(&x, y_space * 2 + font.size, time.get_minutes_tens(), time.get_minutes_ones(), font); } if (m_display_seconds) { // display_number(&x, y, // time.get_seconds_tens(), time.get_seconds_ones(), font_default); } m_last_time = time; m_driver.refresh(); } NextTime DisplayTimeScreen::execute() { display_time(); Common::time_t now; BSP::SystemTimer::get_time(now); uint32_t next_secs = Time::to_seconds(now) + (m_display_seconds ? 1 : 60); return NextTime::at(Time::seconds(next_secs)); } void DisplayTimeScreen::enable() { m_driver.clear(); m_last_time = {}; } void DisplayTimeScreen::disable() { m_driver.clear(); } void DisplayTimeScreen::notify_up_button() { /* TODO: This should open a menu first */ m_manager.push_screen(m_menu_screen); } void DisplayTimeScreen::notify_middle_button() { } void DisplayTimeScreen::notify_down_button() { }