Add get alarm helper to reduce code duplication

This commit is contained in:
BlackMark 2020-05-17 20:11:35 +02:00
parent 21f9215bba
commit 8a6170cb10

View File

@ -144,26 +144,11 @@ class DS3231 {
static DateTime getAlarm1()
{
const auto alarmReg = readRegister<ALARM1_REG_ADDR>();
DateTime alarmTime = {};
alarmReg.getDate(alarmTime.day);
alarmTime.hour = alarmReg.getHours();
alarmTime.minute = alarmReg.getMinutes();
alarmTime.second = alarmReg.getSeconds();
return alarmTime;
return getAlarmHelper<ALARM1_REG_ADDR>();
}
static DateTime getAlarm2()
{
const auto alarmReg = readRegister<ALARM2_REG_ADDR>();
DateTime alarmTime = {};
alarmReg.getDate(alarmTime.day);
alarmTime.hour = alarmReg.getHours();
alarmTime.minute = alarmReg.getMinutes();
return alarmTime;
return getAlarmHelper<ALARM2_REG_ADDR>();
}
static void setDate(const Date &date)
@ -323,6 +308,24 @@ class DS3231 {
writePartialRegister<0, sizeof(Register) - 1>(reg);
}
template <uint8_t Address>
static inline auto getAlarmHelper()
{
constexpr auto IsAlarm1 = Address == ALARM1_REG_ADDR;
static_assert(IsAlarm1 || Address == ALARM2_REG_ADDR, "Must use valid alarm address");
const auto alarmReg = readRegister<Address>();
DateTime alarmTime = {};
alarmReg.getDate(alarmTime.day);
alarmTime.hour = alarmReg.getHours();
alarmTime.minute = alarmReg.getMinutes();
if constexpr (IsAlarm1)
alarmTime.second = alarmReg.getSeconds();
return alarmTime;
}
template <typename AlarmRate>
static inline void setAlarmHelper(const DateTime &alarmTime, const AlarmRate &alarmRate)
{