aiie/teensy/teensy-clock.cpp

63 lines
1.5 KiB
C++
Raw Normal View History

2017-02-20 18:55:16 +00:00
#include <string.h> // memset
#include <TimeLib.h>
2018-02-17 14:01:08 +00:00
#include "noslotclock.h"
2017-02-20 18:55:16 +00:00
2018-02-17 14:01:08 +00:00
#include "teensy-clock.h"
2017-02-20 18:55:16 +00:00
2018-02-17 14:01:08 +00:00
TeensyClock::TeensyClock(AppleMMU *mmu) : NoSlotClock(mmu)
2017-02-20 18:55:16 +00:00
{
}
TeensyClock::~TeensyClock()
{
}
2018-02-17 14:01:08 +00:00
void TeensyClock::populateClockRegister()
2017-02-20 18:55:16 +00:00
{
tmElements_t tm;
breakTime(now(), tm);
2018-02-17 14:01:08 +00:00
tm.Year %= 100; // 00-99
tm.Month++; // 1-12
tm.Wday++; // 1-7, where 1 = Sunday
2017-02-20 18:55:16 +00:00
2018-02-17 14:01:08 +00:00
writeNibble(tm.Year / 10); // 00-99
writeNibble(tm.Year % 10);
writeNibble(tm.Month / 10); // 1-12
writeNibble(tm.Month % 10);
writeNibble(tm.Day / 10); // 1-31
writeNibble(tm.Day % 10);
writeNibble(0);
writeNibble(tm.Wday); // 1-7, where 1 = Sunday
writeNibble(tm.Hour / 10);
writeNibble(tm.Hour % 10);
writeNibble(tm.Minute / 10);
writeNibble(tm.Minute % 10);
writeNibble(tm.Second / 10);
writeNibble(tm.Second % 10);
writeNibble(0); // 00-99 milliseconds
writeNibble(0);
2017-02-20 18:55:16 +00:00
}
2018-02-17 14:01:08 +00:00
static uint8_t bcdToDecimal(uint8_t v)
2017-02-20 18:55:16 +00:00
{
2018-02-17 14:01:08 +00:00
return ((v & 0x0F) + (((v & 0xF0) >> 4) * 10));
2017-02-20 18:55:16 +00:00
}
2018-02-17 14:01:08 +00:00
void TeensyClock::updateClockFromRegister()
{
uint8_t hours, minutes, seconds, days, months;
uint16_t years;
2017-02-20 18:55:16 +00:00
2018-02-17 14:01:08 +00:00
years = bcdToDecimal(clockReg & 0xFF00000000000000LL >> 56) + 2000;
months = bcdToDecimal(clockReg & 0x00FF000000000000LL >> 48) - 1;
days = bcdToDecimal(clockReg & 0x0000FF0000000000LL >> 40);
hours = bcdToDecimal(clockReg & 0x00000000FF000000LL >> 24);
minutes = bcdToDecimal(clockReg & 0x0000000000FF0000LL >> 16);
seconds = bcdToDecimal(clockReg & 0x000000000000FF00LL >> 8);
2017-02-20 18:55:16 +00:00
2018-02-17 14:01:08 +00:00
setTime(hours, minutes, seconds, days, months, years);
2017-02-20 18:55:16 +00:00
}