mirror of https://github.com/eldruin/ds323x-rs
Add functions to set/get the week day
parent
9e56a48cd3
commit
85c1b6189a
|
@ -9,6 +9,7 @@ This driver allows you to:
|
||||||
- Read/write the seconds.
|
- Read/write the seconds.
|
||||||
- Read/write the minutes.
|
- Read/write the minutes.
|
||||||
- Read/write the hours in 24h or AM/PM format.
|
- Read/write the hours in 24h or AM/PM format.
|
||||||
|
- Read/write the weekday.
|
||||||
|
|
||||||
## The devices
|
## The devices
|
||||||
|
|
||||||
|
|
|
@ -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>> {
|
fn read_register_decimal(&mut self, register: u8) -> Result<u8, Error<E>> {
|
||||||
let data = self.iface.read_register(register)?;
|
let data = self.iface.read_register(register)?;
|
||||||
Ok(packed_bcd_to_decimal(data))
|
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>> {
|
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))
|
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!(21, packed_bcd_to_decimal(0b0010_0001));
|
||||||
assert_eq!(59, packed_bcd_to_decimal(0b0101_1001));
|
assert_eq!(59, packed_bcd_to_decimal(0b0101_1001));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_convert_decimal_to_packed_bcd() {
|
fn can_convert_decimal_to_packed_bcd() {
|
||||||
assert_eq!(0b0000_0000, decimal_to_packed_bcd( 0));
|
assert_eq!(0b0000_0000, decimal_to_packed_bcd( 0));
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
//! - Read/write the seconds.
|
//! - Read/write the seconds.
|
||||||
//! - Read/write the minutes.
|
//! - Read/write the minutes.
|
||||||
//! - Read/write the hours in 24h or AM/PM format.
|
//! - Read/write the hours in 24h or AM/PM format.
|
||||||
|
//! - Read/write the weekday.
|
||||||
//!
|
//!
|
||||||
//! ## The devices
|
//! ## The devices
|
||||||
//!
|
//!
|
||||||
|
@ -181,6 +182,7 @@ impl Register {
|
||||||
const SECONDS : u8 = 0x00;
|
const SECONDS : u8 = 0x00;
|
||||||
const MINUTES : u8 = 0x01;
|
const MINUTES : u8 = 0x01;
|
||||||
const HOURS : u8 = 0x02;
|
const HOURS : u8 = 0x02;
|
||||||
|
const DOW : u8 = 0x03;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BitFlags;
|
struct BitFlags;
|
||||||
|
|
|
@ -12,6 +12,7 @@ impl Register {
|
||||||
pub const SECONDS : u8 = 0x00;
|
pub const SECONDS : u8 = 0x00;
|
||||||
pub const MINUTES : u8 = 0x01;
|
pub const MINUTES : u8 = 0x01;
|
||||||
pub const HOURS : u8 = 0x02;
|
pub const HOURS : u8 = 0x02;
|
||||||
|
pub const DOW : u8 = 0x03;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DummyOutputPin;
|
pub struct DummyOutputPin;
|
||||||
|
|
|
@ -92,4 +92,9 @@ mod hours_12h_pm {
|
||||||
set_invalid_param_range_test!(set_hours, Hours::PM(0), Hours::PM(13));
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue