Implement setting alarms

This commit is contained in:
BlackMark 2020-05-17 19:41:53 +02:00
parent 1bc7e66389
commit 14e608d397

View File

@ -187,6 +187,33 @@ class DS3231 {
writePartialRegister<START_OFFSET, END_OFFSET>(timeReg);
}
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);
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);
if (enableInterrupt)
enableInterruptHelper<detail::ControlRegFlags::A2IE>();
}
static bool checkAlarm1()
{
return checkAlarmHelper<detail::ControlStatusRegFlags::A1F>();
@ -286,6 +313,20 @@ class DS3231 {
writePartialRegister<0, sizeof(Register) - 1>(reg);
}
template <detail::ControlRegFlags AlarmFlag>
static inline void enableInterruptHelper()
{
constexpr auto IsAlarm1Flag = AlarmFlag == detail::ControlRegFlags::A1IE;
constexpr auto IsAlarm2Flag = AlarmFlag == detail::ControlRegFlags::A2IE;
static_assert(IsAlarm1Flag || IsAlarm2Flag, "Must use valid alarm flag");
auto controlReg = readRegister<CONTROL_REG_ADDR>();
using Flags = typename decltype(controlReg)::Flags;
controlReg &= ~Flags::BBSQW;
controlReg |= Flags::INTCN | AlarmFlag;
writeRegister(controlReg);
}
template <detail::ControlStatusRegFlags AlarmFlag>
static inline bool checkAlarmHelper()
{