Add get alarm helper to reduce code duplication
This commit is contained in:
parent
21f9215bba
commit
8a6170cb10
37
ds3231.hpp
37
ds3231.hpp
@ -144,26 +144,11 @@ class DS3231 {
|
|||||||
|
|
||||||
static DateTime getAlarm1()
|
static DateTime getAlarm1()
|
||||||
{
|
{
|
||||||
const auto alarmReg = readRegister<ALARM1_REG_ADDR>();
|
return getAlarmHelper<ALARM1_REG_ADDR>();
|
||||||
|
|
||||||
DateTime alarmTime = {};
|
|
||||||
alarmReg.getDate(alarmTime.day);
|
|
||||||
alarmTime.hour = alarmReg.getHours();
|
|
||||||
alarmTime.minute = alarmReg.getMinutes();
|
|
||||||
alarmTime.second = alarmReg.getSeconds();
|
|
||||||
|
|
||||||
return alarmTime;
|
|
||||||
}
|
}
|
||||||
static DateTime getAlarm2()
|
static DateTime getAlarm2()
|
||||||
{
|
{
|
||||||
const auto alarmReg = readRegister<ALARM2_REG_ADDR>();
|
return getAlarmHelper<ALARM2_REG_ADDR>();
|
||||||
|
|
||||||
DateTime alarmTime = {};
|
|
||||||
alarmReg.getDate(alarmTime.day);
|
|
||||||
alarmTime.hour = alarmReg.getHours();
|
|
||||||
alarmTime.minute = alarmReg.getMinutes();
|
|
||||||
|
|
||||||
return alarmTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setDate(const Date &date)
|
static void setDate(const Date &date)
|
||||||
@ -323,6 +308,24 @@ class DS3231 {
|
|||||||
writePartialRegister<0, sizeof(Register) - 1>(reg);
|
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>
|
template <typename AlarmRate>
|
||||||
static inline void setAlarmHelper(const DateTime &alarmTime, const AlarmRate &alarmRate)
|
static inline void setAlarmHelper(const DateTime &alarmTime, const AlarmRate &alarmRate)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user