Add functions to set/get the day of the month

pull/4/head
Diego Barrios Romero 2018-10-28 13:32:51 +01:00
parent 85c1b6189a
commit ba5213adf2
5 changed files with 26 additions and 0 deletions

View File

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

View File

@ -52,6 +52,11 @@ where
self.read_register_decimal(Register::DOW)
}
/// Read the day of the month [1-31].
pub fn get_day(&mut self) -> Result<u8, Error<E>> {
self.read_register_decimal(Register::DOM)
}
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))
@ -113,6 +118,16 @@ where
self.iface.write_register(Register::DOW, weekday)
}
/// Set the day of month [1-31].
///
/// Will return an `Error::InvalidInputData` if the day is out of range.
pub fn set_day(&mut self, day: u8) -> Result<(), Error<E>> {
if day < 1 || day > 7 {
return Err(Error::InvalidInputData);
}
self.iface.write_register(Register::DOM, day)
}
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))
}

View File

@ -8,6 +8,7 @@
//! - Read/write the minutes.
//! - Read/write the hours in 24h or AM/PM format.
//! - Read/write the weekday.
//! - Read/write the day.
//!
//! ## The devices
//!
@ -183,6 +184,7 @@ impl Register {
const MINUTES : u8 = 0x01;
const HOURS : u8 = 0x02;
const DOW : u8 = 0x03;
const DOM : u8 = 0x04;
}
struct BitFlags;

View File

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

View File

@ -98,3 +98,10 @@ mod weekday {
set_param_test!(set_weekday, DOW, 1, 1);
set_invalid_param_range_test!(set_weekday, 0, 8);
}
mod day {
use super::*;
get_param_test!(get_day, DOM, 1, 1);
set_param_test!(set_day, DOM, 1, 1);
set_invalid_param_range_test!(set_day, 0, 8);
}