Add functions to get/set the minutes.

pull/4/head
Diego Barrios Romero 2018-10-28 12:27:40 +01:00
parent 4aa34d8ab4
commit c1b02cb3c0
5 changed files with 73 additions and 16 deletions

View File

@ -7,6 +7,7 @@ extremely accurate real-time clocks, based on the [`embedded-hal`] traits.
This driver allows you to:
- Read/write the seconds.
- Read/write the minutes.
## The devices

View File

@ -6,14 +6,28 @@ use interface::{ ReadRegister, WriteRegister };
impl<DI, IC, E> Ds323x<DI, IC>
where
DI: ReadRegister<Error = E> + WriteRegister<Error = E>
DI: ReadRegister<Error = E>
{
/// Read the seconds.
pub fn get_seconds(&mut self) -> Result<u8, Error<E>> {
let data = self.iface.read_register(Register::SECONDS)?;
Ok(packed_bcd_to_decimal(data))
self.read_register_decimal(Register::SECONDS)
}
/// Read the minutes.
pub fn get_minutes(&mut self) -> Result<u8, Error<E>> {
self.read_register_decimal(Register::MINUTES)
}
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))
}
}
impl<DI, IC, E> Ds323x<DI, IC>
where
DI: WriteRegister<Error = E>
{
/// Set the seconds [0-59].
///
/// Will return an `Error::InvalidInputData` if the seconds are out of range.
@ -21,7 +35,21 @@ where
if seconds > 59 {
return Err(Error::InvalidInputData);
}
self.iface.write_register(Register::SECONDS, decimal_to_packed_bcd(seconds))
self.write_register_decimal(Register::SECONDS, seconds)
}
/// Set the minutes [0-59].
///
/// Will return an `Error::InvalidInputData` if the minutes are out of range.
pub fn set_minutes(&mut self, minutes: u8) -> Result<(), Error<E>> {
if minutes > 59 {
return Err(Error::InvalidInputData);
}
self.write_register_decimal(Register::MINUTES, minutes)
}
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

@ -5,6 +5,7 @@
//!
//! This driver allows you to:
//! - Read/write the seconds.
//! - Read/write the minutes.
//!
//! ## The devices
//!
@ -177,6 +178,7 @@ struct Register;
impl Register {
const SECONDS : u8 = 0x00;
const MINUTES : u8 = 0x01;
}
const DEVICE_ADDRESS: u8 = 0b110_1000;

View File

@ -10,6 +10,7 @@ pub struct Register;
impl Register {
pub const SECONDS : u8 = 0x00;
pub const MINUTES : u8 = 0x01;
}
pub struct DummyOutputPin;

View File

@ -7,21 +7,46 @@ mod common;
use common::{ DEVICE_ADDRESS, Register, new_ds3231,
new_ds3232, new_ds3234 };
get_test!(can_get_seconds_ds3231, get_seconds, new_ds3231, 1,
I2cTrans::write_read(DEVICE_ADDRESS, vec![Register::SECONDS], vec![1]));
mod seconds {
use super::*;
get_test!(can_get_ds3231, get_seconds, new_ds3231, 1,
I2cTrans::write_read(DEVICE_ADDRESS, vec![Register::SECONDS], vec![1]));
get_test!(can_get_seconds_ds3232, get_seconds, new_ds3232, 1,
I2cTrans::write_read(DEVICE_ADDRESS, vec![Register::SECONDS], vec![1]));
get_test!(can_get_ds3232, get_seconds, new_ds3232, 1,
I2cTrans::write_read(DEVICE_ADDRESS, vec![Register::SECONDS], vec![1]));
get_test!(can_get_seconds_ds3234, get_seconds, new_ds3234, 1,
SpiTrans::transfer(vec![Register::SECONDS, 0], vec![Register::SECONDS, 1]));
get_test!(can_get_ds3234, get_seconds, new_ds3234, 1,
SpiTrans::transfer(vec![Register::SECONDS, 0], vec![Register::SECONDS, 1]));
set_test!(can_set_seconds_ds3231, set_seconds, new_ds3231, 1,
I2cTrans::write(DEVICE_ADDRESS, vec![Register::SECONDS, 1]));
set_test!(can_set_ds3231, set_seconds, new_ds3231, 1,
I2cTrans::write(DEVICE_ADDRESS, vec![Register::SECONDS, 1]));
set_test!(can_set_seconds_ds3232, set_seconds, new_ds3232, 1,
I2cTrans::write(DEVICE_ADDRESS, vec![Register::SECONDS, 1]));
set_test!(can_set_ds3232, set_seconds, new_ds3232, 1,
I2cTrans::write(DEVICE_ADDRESS, vec![Register::SECONDS, 1]));
set_test!(can_set_seconds_ds3234, set_seconds, new_ds3234, 1,
SpiTrans::write(vec![Register::SECONDS + 0x80, 1]));
set_test!(can_set_ds3234, set_seconds, new_ds3234, 1,
SpiTrans::write(vec![Register::SECONDS + 0x80, 1]));
}
mod minutes {
use super::*;
get_test!(can_get_ds3231, get_minutes, new_ds3231, 1,
I2cTrans::write_read(DEVICE_ADDRESS, vec![Register::MINUTES], vec![1]));
get_test!(can_get_ds3232, get_minutes, new_ds3232, 1,
I2cTrans::write_read(DEVICE_ADDRESS, vec![Register::MINUTES], vec![1]));
get_test!(can_get_ds3234, get_minutes, new_ds3234, 1,
SpiTrans::transfer(vec![Register::MINUTES, 0], vec![Register::MINUTES, 1]));
set_test!(can_set_ds3231, set_minutes, new_ds3231, 1,
I2cTrans::write(DEVICE_ADDRESS, vec![Register::MINUTES, 1]));
set_test!(can_set_ds3232, set_minutes, new_ds3232, 1,
I2cTrans::write(DEVICE_ADDRESS, vec![Register::MINUTES, 1]));
set_test!(can_set_ds3234, set_minutes, new_ds3234, 1,
SpiTrans::write(vec![Register::MINUTES + 0x80, 1]));
}