mirror of https://github.com/eldruin/ds323x-rs
69 lines
2.3 KiB
Rust
69 lines
2.3 KiB
Rust
//! Common implementation
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
extern crate embedded_hal as hal;
|
|
use super::super::{ Ds323x, Register, Error };
|
|
use interface::{ ReadRegister, WriteRegister };
|
|
|
|
impl<DI, IC, E> Ds323x<DI, IC>
|
|
where
|
|
DI: ReadRegister<Error = E> + WriteRegister<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))
|
|
}
|
|
|
|
/// Set the seconds [0-59].
|
|
///
|
|
/// Will return an `Error::InvalidInputData` if the seconds are out of range.
|
|
pub fn set_seconds(&mut self, seconds: u8) -> Result<(), Error<E>> {
|
|
if seconds > 59 {
|
|
return Err(Error::InvalidInputData);
|
|
}
|
|
self.iface.write_register(Register::SECONDS, decimal_to_packed_bcd(seconds))
|
|
}
|
|
}
|
|
|
|
// Transforms a decimal number to packed BCD format
|
|
fn decimal_to_packed_bcd(dec: u8) -> u8 {
|
|
((dec / 10) << 4) | (dec % 10)
|
|
}
|
|
|
|
// Transforms a number in packed BCD format to decimal
|
|
fn packed_bcd_to_decimal(bcd: u8) -> u8 {
|
|
(bcd >> 4) * 10 + (bcd & 0xF)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn can_convert_packed_bcd_to_decimal() {
|
|
assert_eq!(0, packed_bcd_to_decimal(0b0000_0000));
|
|
assert_eq!(1, packed_bcd_to_decimal(0b0000_0001));
|
|
assert_eq!(9, packed_bcd_to_decimal(0b0000_1001));
|
|
assert_eq!(10, packed_bcd_to_decimal(0b0001_0000));
|
|
assert_eq!(11, packed_bcd_to_decimal(0b0001_0001));
|
|
assert_eq!(19, packed_bcd_to_decimal(0b0001_1001));
|
|
assert_eq!(20, packed_bcd_to_decimal(0b0010_0000));
|
|
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));
|
|
assert_eq!(0b0000_0001, decimal_to_packed_bcd( 1));
|
|
assert_eq!(0b0000_1001, decimal_to_packed_bcd( 9));
|
|
assert_eq!(0b0001_0000, decimal_to_packed_bcd(10));
|
|
assert_eq!(0b0001_0001, decimal_to_packed_bcd(11));
|
|
assert_eq!(0b0001_1001, decimal_to_packed_bcd(19));
|
|
assert_eq!(0b0010_0000, decimal_to_packed_bcd(20));
|
|
assert_eq!(0b0010_0001, decimal_to_packed_bcd(21));
|
|
assert_eq!(0b0101_1001, decimal_to_packed_bcd(59));
|
|
}
|
|
} |