/* * 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 "Application/Screens/DisplayTimeScreen.h" #include "Application/SystemFonts.h" #include "Bsp/Drivers/RtcDriver.h" #include "Bsp/SystemTime.h" #include "Bsp/Drivers/LowPower.h" using Common::ReturnCode; using Common::Time; using Common::Schedule::NextTime; using Color = BSP::DisplayDriver::Color; 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(true) {} static char get_char_for_digit(uint8_t bcd_digit) { if (bcd_digit > 9) { return '0'; } return bcd_digit + '0'; } ReturnCode DisplayTimeScreen::init() { SET(RCC->CFGR, RCC_CFGR_SW_HSI); 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, Color::WHITE); } void DisplayTimeScreen::display_time() { Common::WallClockTime time; BSP::RtcDriver::get_time(time); const struct font &font = font_large_digits; const uint32_t y_space = (m_driver.get_height() - (2 * font.height)) / 3; const uint32_t x_space = (m_driver.get_width() - (2 * font.width)) / 2; uint32_t x = 0; m_driver.clear(Color::BLACK); x = x_space; display_number(&x, y_space, time.get_hours_12_tens(), time.get_hours_12_ones(), font); x = x_space; display_number(&x, y_space * 2 + font.height, time.get_minutes_tens(), time.get_minutes_ones(), font); m_last_time = time; m_driver.refresh(); } NextTime DisplayTimeScreen::execute() { display_time(); Common::time_t now; BSP::SystemTimer::get_time(now); if (m_display_seconds) { return NextTime::in(Time::seconds(1)); } else { Common::WallClockTime wall_time; BSP::RtcDriver::get_time(wall_time); return NextTime::in(Time::seconds(61 - wall_time.get_seconds())); } } void DisplayTimeScreen::enable() { m_last_time = {}; display_time(); } void DisplayTimeScreen::disable() { } 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() { }