Implement setting alarms
This commit is contained in:
parent
1bc7e66389
commit
14e608d397
41
ds3231.hpp
41
ds3231.hpp
@ -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()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user