Files
timely-reference/DisplayTimeTask.cpp

139 lines
3.9 KiB
C++

/*
* 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-29.h"
#include "font-notomono-68.h"
using Common::ReturnCode;
using Common::Time;
using Common::Schedule::NextTime;
static const font &font_large = font_notomono_68;
static const font &font_default = font_notomono_29;
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()
{
SET_TO(GPIOA->MODER, GPIO_MODER_MODE0, 1u << GPIO_MODER_MODE1_Pos);
GPIOA->OTYPER &= ~GPIO_OTYPER_OT_1;
GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD1;
GPIOA->PUPDR |= 2u << GPIO_PUPDR_PUPD1_Pos;
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()
{
BSP::time_bcd time;
BSP::RtcDriver::get_time(time);
if (!m_has_cleared) {
m_driver.clear();
}
int i = 0;
SET(GPIOA->ODR, GPIO_ODR_OD1);
CLR(GPIOA->ODR, GPIO_ODR_OD1);
if (m_last_time.hour_tens != time.hour_tens ||
m_last_time.hour_ones != time.hour_ones ||
!m_has_cleared) {
display_number(8, (i * font_default.size) + (i + 1) * 4, time.hour_tens, time.hour_ones, font_default);
}
SET(GPIOA->ODR, GPIO_ODR_OD1);
CLR(GPIOA->ODR, GPIO_ODR_OD1);
i++;
if (m_last_time.minute_tens != time.minute_tens ||
m_last_time.minute_ones != time.minute_ones ||
!m_has_cleared) {
display_number(8, (i * font_default.size) + (i + 1) * 4, time.minute_tens, time.minute_ones, font_default);
}
SET(GPIOA->ODR, GPIO_ODR_OD1);
CLR(GPIOA->ODR, GPIO_ODR_OD1);
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(8, (i * font_default.size) + (i + 1) * 4, time.second_tens, time.second_ones, font_default);
}
}
// m_has_cleared = true;
m_last_time = time;
SET(GPIOA->ODR, GPIO_ODR_OD1);
CLR(GPIOA->ODR, GPIO_ODR_OD1);
m_driver.refresh();
SET(GPIOA->ODR, GPIO_ODR_OD1);
CLR(GPIOA->ODR, GPIO_ODR_OD1);
}
NextTime DisplayTimeTask::execute()
{
Common::time_t now;
BSP::SystemTimer::get_time(now);
uint32_t next_secs = Time::to_seconds(now) + 1;
display_time();
return NextTime::at(Time::seconds(next_secs));
}