/* * 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 "DisplayTimeTask.h" #include "SystemTime.h" #include "font-notomono-24.h" #include "font-notomono-64.h" using Common::ReturnCode; using Common::Time; using Common::Schedule::NextTime; static const font &font_large = font_notomono_64; static const font &font_default = font_notomono_24; DisplayTimeTask::DisplayTimeTask(BSP::DisplayDriver &driver) : m_driver(driver) , m_has_cleared(false) , m_last_time() , 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 DisplayTimeTask::init() { return ReturnCode::OK; } void DisplayTimeTask::display_number(uint32_t x, uint32_t y, uint32_t tens, uint32_t ones, const 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 DisplayTimeTask::display_time() { uint32_t width = m_driver.get_width(); uint32_t height = m_driver.get_height(); BSP::time_bcd time; BSP::RtcDriver::get_time(time); if (!m_has_cleared) { m_driver.clear(); } int i = 0; if (m_last_time.hour_tens != time.hour_tens || m_last_time.hour_ones != time.hour_ones || !m_has_cleared) { display_number(0, (i * font_large.size) + (i + 1) * 4, time.hour_tens, time.hour_ones, font_large); } i++; if (m_last_time.minute_tens != time.minute_tens || m_last_time.minute_ones != time.minute_ones || !m_has_cleared) { display_number(0, (i * font_large.size) + (i + 1) * 4, time.minute_tens, time.minute_ones, font_large); } i++; if (m_display_seconds) { if (m_last_time.second_tens != time.second_tens || m_last_time.second_ones != time.second_ones || !m_has_cleared) { display_number(0, (i * font_large.size) + (i + 1) * 4, time.second_tens, time.second_ones, font_default); } } m_has_cleared = true; m_last_time = time; m_driver.refresh(); } NextTime DisplayTimeTask::execute() { display_time(); Common::time_t now; BSP::SystemTimer::get_time(now); uint32_t next_secs = Time::to_seconds(now) + 1; return NextTime::at(Time::seconds(next_secs)); }