Reduce code duplication

This commit is contained in:
BlackMark 2020-05-17 19:54:46 +02:00
parent 14e608d397
commit dfd2289aef

View File

@ -189,27 +189,13 @@ class DS3231 {
static void setAlarm1(const DateTime &alarmTime, const Alarm1Rate &alarmRate, bool enableInterrupt = true)
{
detail::Alarm1Reg alarm1Reg;
alarm1Reg.setAlarmRate(alarmRate);
alarm1Reg.setDate(alarmTime.day);
alarm1Reg.setHours(alarmTime.hour);
alarm1Reg.setMinutes(alarmTime.minute);
alarm1Reg.setSeconds(alarmTime.second);
writeRegister(alarm1Reg);
setAlarmHelper(alarmTime, alarmRate);
if (enableInterrupt)
enableInterruptHelper<detail::ControlRegFlags::A1IE>();
}
static void setAlarm2(const DateTime &alarmTime, const Alarm2Rate &alarmRate, bool enableInterrupt = true)
{
detail::Alarm2Reg alarm2Reg;
alarm2Reg.setAlarmRate(alarmRate);
alarm2Reg.setDate(alarmTime.day);
alarm2Reg.setHours(alarmTime.hour);
alarm2Reg.setMinutes(alarmTime.minute);
writeRegister(alarm2Reg);
setAlarmHelper(alarmTime, alarmRate);
if (enableInterrupt)
enableInterruptHelper<detail::ControlRegFlags::A2IE>();
}
@ -313,6 +299,24 @@ class DS3231 {
writePartialRegister<0, sizeof(Register) - 1>(reg);
}
template <typename AlarmRate>
static inline void setAlarmHelper(const DateTime &alarmTime, const AlarmRate &alarmRate)
{
constexpr auto IsAlarm1 = util::is_same_v<AlarmRate, Alarm1Rate>;
static_assert(IsAlarm1 || util::is_same_v<AlarmRate, Alarm2Rate>, "Must use valid alarm rate");
using alarm_reg_t = util::conditional_t<IsAlarm1, detail::Alarm1Reg, detail::Alarm2Reg>;
alarm_reg_t alarmReg;
alarmReg.setAlarmRate(alarmRate);
alarmReg.setDate(alarmTime.day);
alarmReg.setHours(alarmTime.hour);
alarmReg.setMinutes(alarmTime.minute);
if constexpr (IsAlarm1)
alarmReg.setSeconds(alarmTime.second);
writeRegister(alarmReg);
}
template <detail::ControlRegFlags AlarmFlag>
static inline void enableInterruptHelper()
{