Add functions to set/get the week day

pull/4/head
Diego Barrios Romero 2018-10-28 13:28:11 +01:00
parent 9e56a48cd3
commit 85c1b6189a
5 changed files with 26 additions and 2 deletions

View File

@ -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

View File

@ -47,6 +47,11 @@ where
}
}
/// Read the day of the week [1-7].
pub fn get_weekday(&mut self) -> Result<u8, Error<E>> {
self.read_register_decimal(Register::DOW)
}
fn read_register_decimal(&mut self, register: u8) -> Result<u8, Error<E>> {
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<E>> {
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<E>> {
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));

View File

@ -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;

View File

@ -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;

View File

@ -92,4 +92,9 @@ mod hours_12h_pm {
set_invalid_param_range_test!(set_hours, Hours::PM(0), Hours::PM(13));
}
}
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);
}