Add function to get/set the month

pull/4/head
Diego Barrios Romero 2018-10-28 15:44:17 +01:00
parent 63fe08c6b5
commit 2cf7855293
5 changed files with 53 additions and 6 deletions

View File

@ -11,6 +11,7 @@ This driver allows you to:
- Read/write the hours in 24h or AM/PM format. - Read/write the hours in 24h or AM/PM format.
- Read/write the weekday. - Read/write the weekday.
- Read/write the day. - Read/write the day.
- Read/write the month.
## The devices ## The devices

View File

@ -17,7 +17,7 @@ pub enum Hours {
impl<DI, IC, E> Ds323x<DI, IC> impl<DI, IC, E> Ds323x<DI, IC>
where where
DI: ReadRegister<Error = E> DI: ReadRegister<Error = E> + WriteRegister<Error = E>
{ {
/// Read the seconds. /// Read the seconds.
pub fn get_seconds(&mut self) -> Result<u8, Error<E>> { pub fn get_seconds(&mut self) -> Result<u8, Error<E>> {
@ -57,16 +57,18 @@ where
self.read_register_decimal(Register::DOM) self.read_register_decimal(Register::DOM)
} }
/// Read the month [1-12].
pub fn get_month(&mut self) -> Result<u8, Error<E>> {
let data = self.iface.read_register(Register::MONTH)?;
let value = data & !BitFlags::CENTURY;
Ok(packed_bcd_to_decimal(value))
}
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))
} }
}
impl<DI, IC, E> Ds323x<DI, IC>
where
DI: WriteRegister<Error = E>
{
/// Set the seconds [0-59]. /// Set the seconds [0-59].
/// ///
/// Will return an `Error::InvalidInputData` if the seconds are out of range. /// Will return an `Error::InvalidInputData` if the seconds are out of range.
@ -128,6 +130,19 @@ where
self.iface.write_register(Register::DOM, day) self.iface.write_register(Register::DOM, day)
} }
/// Set the month [1-12].
///
/// Will return an `Error::InvalidInputData` if the month is out of range.
pub fn set_month(&mut self, month: u8) -> Result<(), Error<E>> {
if month < 1 || month > 12 {
return Err(Error::InvalidInputData);
}
// keep the century bit
let data = self.iface.read_register(Register::MONTH)?;
let value = (data & BitFlags::CENTURY) | decimal_to_packed_bcd(month);
self.iface.write_register(Register::MONTH, value)
}
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))
} }

View File

@ -9,6 +9,7 @@
//! - Read/write the hours in 24h or AM/PM format. //! - Read/write the hours in 24h or AM/PM format.
//! - Read/write the weekday. //! - Read/write the weekday.
//! - Read/write the day. //! - Read/write the day.
//! - Read/write the month.
//! //!
//! ## The devices //! ## The devices
//! //!
@ -185,6 +186,7 @@ impl Register {
const HOURS : u8 = 0x02; const HOURS : u8 = 0x02;
const DOW : u8 = 0x03; const DOW : u8 = 0x03;
const DOM : u8 = 0x04; const DOM : u8 = 0x04;
const MONTH : u8 = 0x05;
} }
struct BitFlags; struct BitFlags;
@ -192,6 +194,7 @@ struct BitFlags;
impl BitFlags { impl BitFlags {
const H24_H12 : u8 = 0b0100_0000; const H24_H12 : u8 = 0b0100_0000;
const AM_PM : u8 = 0b0010_0000; const AM_PM : u8 = 0b0010_0000;
const CENTURY : u8 = 0b1000_0000;
} }
const DEVICE_ADDRESS: u8 = 0b110_1000; const DEVICE_ADDRESS: u8 = 0b110_1000;

View File

@ -14,6 +14,7 @@ impl Register {
pub const HOURS : u8 = 0x02; pub const HOURS : u8 = 0x02;
pub const DOW : u8 = 0x03; pub const DOW : u8 = 0x03;
pub const DOM : u8 = 0x04; pub const DOM : u8 = 0x04;
pub const MONTH : u8 = 0x05;
} }
pub struct DummyOutputPin; pub struct DummyOutputPin;

View File

@ -34,6 +34,20 @@ macro_rules! set_param_test {
[ SpiTrans::write(vec![Register::$register + 0x80, $binary_value]) ]); [ SpiTrans::write(vec![Register::$register + 0x80, $binary_value]) ]);
}; };
} }
macro_rules! read_set_param_test {
($method:ident, $register:ident, $value:expr, $binary_value_read:expr, $binary_value_write:expr) => {
set_test!(can_read_set_ds3231, $method, new_ds3231, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value_read]),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value_write]) ]);
set_test!(can_read_set_ds3232, $method, new_ds3232, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value_read]),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value_write]) ]);
set_test!(can_read_set_ds3234, $method, new_ds3234, $value,
[ SpiTrans::transfer(vec![Register::$register, 0], vec![Register::$register, $binary_value_read]),
SpiTrans::write(vec![Register::$register + 0x80, $binary_value_write]) ]);
}; };
} }
@ -107,3 +121,16 @@ mod day {
set_param_test!(set_day, DOM, 1, 1); set_param_test!(set_day, DOM, 1, 1);
set_invalid_param_range_test!(set_day, 0, 8); set_invalid_param_range_test!(set_day, 0, 8);
} }
mod month {
use super::*;
get_param_test!(get_month, MONTH, 1, 1);
read_set_param_test!(set_month, MONTH, 12, 0b0000_0010, 0b0001_0010);
set_invalid_param_range_test!(set_month, 0, 13);
mod keeps_century {
use super::*;
get_param_test!(get_month, MONTH, 12, 0b1001_0010);
read_set_param_test!(set_month, MONTH, 12, 0b1000_0010, 0b1001_0010);
}
}