Support setting alarms from a NaiveTime

It allows for more concise user code when setting alarm from such object.
pull/5/head
Marc Poulhiès 2020-06-17 14:27:56 +02:00 committed by Diego Barrios Romero
parent 092af13298
commit 044051c2d9
2 changed files with 43 additions and 3 deletions

View File

@ -2,6 +2,7 @@
use super::super::{BitFlags, Ds323x, Error, Hours, Register}; use super::super::{BitFlags, Ds323x, Error, Hours, Register};
use super::{decimal_to_packed_bcd, hours_to_register}; use super::{decimal_to_packed_bcd, hours_to_register};
use crate::ds323x::{NaiveTime, Timelike};
use interface::{ReadData, WriteData}; use interface::{ReadData, WriteData};
/// Parameters for setting Alarm1 on a day of the month /// Parameters for setting Alarm1 on a day of the month
@ -195,6 +196,20 @@ where
self.iface.write_data(&mut data) self.iface.write_data(&mut data)
} }
/// Set Alarm1 for a time (fires when hours, minutes and seconds match).
///
/// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
/// day is not used by the matching strategy but is set to 1.
pub fn set_alarm1_hms(&mut self, when: NaiveTime) -> Result<(), Error<CommE, PinE>> {
let alarm = DayAlarm1 {
day: 1,
hour: Hours::H24(when.hour() as u8),
minute: when.minute() as u8,
second: when.second() as u8,
};
self.set_alarm1_day(alarm, Alarm1Matching::HoursMinutesAndSecondsMatch)
}
/// Set Alarm1 for weekday. /// Set Alarm1 for weekday.
/// ///
/// Will return an `Error::InvalidInputData` if any of the used parameters /// Will return an `Error::InvalidInputData` if any of the used parameters
@ -274,6 +289,19 @@ where
self.iface.write_data(&mut data) self.iface.write_data(&mut data)
} }
/// Set Alarm2 for a time (fires when hours, minutes and seconds match).
///
/// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
/// day is not used by the matching strategy but is set to 1.
pub fn set_alarm2_hm(&mut self, when: NaiveTime) -> Result<(), Error<CommE, PinE>> {
let alarm = DayAlarm2 {
day: 1,
hour: Hours::H24(when.hour() as u8),
minute: when.minute() as u8,
};
self.set_alarm2_day(alarm, Alarm2Matching::HoursAndMinutesMatch)
}
/// Set Alarm2 for weekday. /// Set Alarm2 for weekday.
/// ///
/// Will return an `Error::InvalidInputData` if any of the used parameters /// Will return an `Error::InvalidInputData` if any of the used parameters

View File

@ -8,7 +8,7 @@ use common::{
}; };
extern crate ds323x; extern crate ds323x;
use ds323x::{ use ds323x::{
Alarm1Matching as A1M, Alarm2Matching as A2M, DayAlarm1, DayAlarm2, Error, Hours, Alarm1Matching as A1M, Alarm2Matching as A2M, DayAlarm1, DayAlarm2, Error, Hours, NaiveTime,
WeekdayAlarm1, WeekdayAlarm2, WeekdayAlarm1, WeekdayAlarm2,
}; };
@ -635,7 +635,13 @@ mod alarm1_day {
}, },
A1M::AllMatch A1M::AllMatch
); );
set_alarm_test!(
match_hms_naivetime,
set_alarm1_hms,
ALARM1_SECONDS,
[4, 3, 2, AM | 1],
NaiveTime::from_hms(2, 3, 4)
);
set_alarm_test!( set_alarm_test!(
match_hms, match_hms,
set_alarm1_day, set_alarm1_day,
@ -914,7 +920,13 @@ mod alarm2_day {
}, },
A2M::AllMatch A2M::AllMatch
); );
set_alarm_test!(
match_hm_naivetime,
set_alarm2_hm,
ALARM2_MINUTES,
[3, 2, AM | 1],
NaiveTime::from_hms(2, 3, 0)
);
set_alarm_test!( set_alarm_test!(
match_hm, match_hm,
set_alarm2_day, set_alarm2_day,