wordclock/libraries/States/ViewDateState.cpp
2019-02-13 19:27:22 +01:00

96 lines
2.3 KiB
C++

#include <ViewStates.h>
ViewDateState::ViewDateState(DS3231 &_ds3231, AT24C32 &_at24c32, char _delimiter) : ViewTextState(at24c32)
{
ds3231 = _ds3231;
at24c32 = _at24c32;
delimiter = _delimiter;
renderOffset = (millis() / TEXT_INTERVAL);
};
void ViewDateState::render(uint8_t matrix[121], ControlStates controlState)
{
bool const blink = (millis() / CONTROL_BLINK_INTERVAL) % 2 == 0;
length = 0;
position = 0;
uint8_t date = ds3231.getDate();
uint8_t month = ds3231.getMonth();
int16_t year = ds3231.getYear();
if (controlState == ControlStates::DATE_DATE && blink)
{
length = 2;
text[0] = (date / 10) + 0x30;
text[1] = (date % 10) + 0x30;
ViewTextState::writeChars(matrix);
}
else if (controlState == ControlStates::DATE_MONTH && blink)
{
length = 2;
text[0] = (month / 10) + 0x30;
text[1] = (month % 10) + 0x30;
ViewTextState::writeChars(matrix);
}
else if (controlState == ControlStates::DATE_YEAR && blink)
{
if (year == 2000 || year == 1900)
{
length = 4;
text[0] = ((year / 100) / 10) + 0x30;
text[1] = ((year / 100) % 10) + 0x30;
position = (millis() / TEXT_INTERVAL) % length;
}
else
{
length = 2;
}
if (year > 1999)
{
year = year - 2000;
}
else
{
year = year - 1900;
}
text[length - 2] = (year / 10) + 0x30;
text[length - 1] = (year % 10) + 0x30;
ViewTextState::writeChars(matrix);
}
else if (controlState != ControlStates::DATE_DATE && controlState != ControlStates::DATE_MONTH && controlState != ControlStates::DATE_YEAR)
{
length = 10;
text[0] = (date / 10) + 0x30;
text[1] = (date % 10) + 0x30;
text[2] = char(delimiter);
text[3] = (month / 10) + 0x30;
text[4] = (month % 10) + 0x30;
text[5] = char(delimiter);
text[6] = ((year / 100) / 10) + 0x30;
text[7] = ((year / 100) % 10) + 0x30;
if (year > 1999)
{
text[8] = ((year - 2000) / 10) + 0x30;
text[9] = ((year - 2000) % 10) + 0x30;
}
else
{
text[8] = ((year - 1900) / 10) + 0x30;
text[9] = ((year - 1900) % 10) + 0x30;
}
position = (millis() / TEXT_INTERVAL - renderOffset - 1) % length;
ViewTextState::writeChars(matrix);
}
renderBinaryClock(matrix, ds3231);
};