diff --git a/README.md b/README.md index 60803b4..fcda82c 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ This driver allows you to: - Read/write the seconds. - Read/write the minutes. - Read/write the hours in 24h or AM/PM format. +- Read/write the weekday. ## The devices diff --git a/src/ds323x/datetime.rs b/src/ds323x/datetime.rs index 0e25d23..90b1cd9 100644 --- a/src/ds323x/datetime.rs +++ b/src/ds323x/datetime.rs @@ -47,6 +47,11 @@ where } } + /// Read the day of the week [1-7]. + pub fn get_weekday(&mut self) -> Result> { + self.read_register_decimal(Register::DOW) + } + fn read_register_decimal(&mut self, register: u8) -> Result> { let data = self.iface.read_register(register)?; Ok(packed_bcd_to_decimal(data)) @@ -98,6 +103,16 @@ where } } + /// Set the day of week [1-7]. + /// + /// Will return an `Error::InvalidInputData` if the day is out of range. + pub fn set_weekday(&mut self, weekday: u8) -> Result<(), Error> { + if weekday < 1 || weekday > 7 { + return Err(Error::InvalidInputData); + } + self.iface.write_register(Register::DOW, weekday) + } + fn write_register_decimal(&mut self, register: u8, decimal_number: u8) -> Result<(), Error> { self.iface.write_register(register, decimal_to_packed_bcd(decimal_number)) } @@ -137,7 +152,7 @@ mod tests { assert_eq!(21, packed_bcd_to_decimal(0b0010_0001)); assert_eq!(59, packed_bcd_to_decimal(0b0101_1001)); } - + #[test] fn can_convert_decimal_to_packed_bcd() { assert_eq!(0b0000_0000, decimal_to_packed_bcd( 0)); diff --git a/src/lib.rs b/src/lib.rs index e8b4578..56e669e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ //! - Read/write the seconds. //! - Read/write the minutes. //! - Read/write the hours in 24h or AM/PM format. +//! - Read/write the weekday. //! //! ## The devices //! @@ -181,6 +182,7 @@ impl Register { const SECONDS : u8 = 0x00; const MINUTES : u8 = 0x01; const HOURS : u8 = 0x02; + const DOW : u8 = 0x03; } struct BitFlags; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index ffb9b74..7656b48 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -12,6 +12,7 @@ impl Register { pub const SECONDS : u8 = 0x00; pub const MINUTES : u8 = 0x01; pub const HOURS : u8 = 0x02; + pub const DOW : u8 = 0x03; } pub struct DummyOutputPin; diff --git a/tests/ds323x.rs b/tests/ds323x.rs index dfe5cd6..987ff29 100644 --- a/tests/ds323x.rs +++ b/tests/ds323x.rs @@ -92,4 +92,9 @@ mod hours_12h_pm { set_invalid_param_range_test!(set_hours, Hours::PM(0), Hours::PM(13)); } -} \ No newline at end of file +mod weekday { + use super::*; + get_param_test!(get_weekday, DOW, 1, 1); + set_param_test!(set_weekday, DOW, 1, 1); + set_invalid_param_range_test!(set_weekday, 0, 8); +}