mirror of https://github.com/eldruin/ds323x-rs
Extract function to convert Hours to a register value
parent
4675e902ad
commit
1465d116ab
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
extern crate embedded_hal as hal;
|
extern crate embedded_hal as hal;
|
||||||
use super::super::{ Ds323x, Register, BitFlags, Error };
|
use super::super::{ Ds323x, Register, BitFlags, Error };
|
||||||
|
use super::{ decimal_to_packed_bcd, packed_bcd_to_decimal, hours_to_register };
|
||||||
use interface::{ ReadData, WriteData };
|
use interface::{ ReadData, WriteData };
|
||||||
|
|
||||||
/// Date and time
|
/// Date and time
|
||||||
|
@ -125,21 +126,10 @@ where
|
||||||
///
|
///
|
||||||
/// Will return an `Error::InvalidInputData` if the hours are out of range.
|
/// Will return an `Error::InvalidInputData` if the hours are out of range.
|
||||||
pub fn set_hours(&mut self, hours: Hours) -> Result<(), Error<E>> {
|
pub fn set_hours(&mut self, hours: Hours) -> Result<(), Error<E>> {
|
||||||
let value = self.get_hours_register_value(&hours)?;
|
let value = hours_to_register(&hours)?;
|
||||||
self.iface.write_register(Register::HOURS, value)
|
self.iface.write_register(Register::HOURS, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_hours_register_value(&mut self, hours: &Hours) -> Result<u8, Error<E>> {
|
|
||||||
match *hours {
|
|
||||||
Hours::H24(h) if h > 23 => Err(Error::InvalidInputData),
|
|
||||||
Hours::H24(h) => Ok(decimal_to_packed_bcd(h)),
|
|
||||||
Hours::AM(h) if h < 1 || h > 12 => Err(Error::InvalidInputData),
|
|
||||||
Hours::AM(h) => Ok(BitFlags::H24_H12 | decimal_to_packed_bcd(h)),
|
|
||||||
Hours::PM(h) if h < 1 || h > 12 => Err(Error::InvalidInputData),
|
|
||||||
Hours::PM(h) => Ok(BitFlags::H24_H12 | BitFlags::AM_PM | decimal_to_packed_bcd(h)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set the day of week [1-7].
|
/// Set the day of week [1-7].
|
||||||
///
|
///
|
||||||
/// Will return an `Error::InvalidInputData` if the day is out of range.
|
/// Will return an `Error::InvalidInputData` if the day is out of range.
|
||||||
|
@ -211,7 +201,7 @@ where
|
||||||
let mut payload = [Register::SECONDS,
|
let mut payload = [Register::SECONDS,
|
||||||
decimal_to_packed_bcd(datetime.second),
|
decimal_to_packed_bcd(datetime.second),
|
||||||
decimal_to_packed_bcd(datetime.minute),
|
decimal_to_packed_bcd(datetime.minute),
|
||||||
self.get_hours_register_value(&datetime.hour)?,
|
hours_to_register(&datetime.hour)?,
|
||||||
decimal_to_packed_bcd(datetime.weekday),
|
decimal_to_packed_bcd(datetime.weekday),
|
||||||
decimal_to_packed_bcd(datetime.day),
|
decimal_to_packed_bcd(datetime.day),
|
||||||
month, year];
|
month, year];
|
||||||
|
|
|
@ -2,6 +2,7 @@ mod configuration;
|
||||||
mod status;
|
mod status;
|
||||||
mod datetime;
|
mod datetime;
|
||||||
pub use self::datetime::{ Hours, DateTime };
|
pub use self::datetime::{ Hours, DateTime };
|
||||||
|
use super::{ BitFlags, Error };
|
||||||
|
|
||||||
// Transforms a decimal number to packed BCD format
|
// Transforms a decimal number to packed BCD format
|
||||||
fn decimal_to_packed_bcd(dec: u8) -> u8 {
|
fn decimal_to_packed_bcd(dec: u8) -> u8 {
|
||||||
|
@ -13,6 +14,18 @@ fn packed_bcd_to_decimal(bcd: u8) -> u8 {
|
||||||
(bcd >> 4) * 10 + (bcd & 0xF)
|
(bcd >> 4) * 10 + (bcd & 0xF)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hours_to_register<E>(hours: &Hours) -> Result<u8, Error<E>> {
|
||||||
|
match *hours {
|
||||||
|
Hours::H24(h) if h > 23 => Err(Error::InvalidInputData),
|
||||||
|
Hours::H24(h) => Ok(decimal_to_packed_bcd(h)),
|
||||||
|
Hours::AM(h) if h < 1 || h > 12 => Err(Error::InvalidInputData),
|
||||||
|
Hours::AM(h) => Ok(BitFlags::H24_H12 | decimal_to_packed_bcd(h)),
|
||||||
|
Hours::PM(h) if h < 1 || h > 12 => Err(Error::InvalidInputData),
|
||||||
|
Hours::PM(h) => Ok(BitFlags::H24_H12 | BitFlags::AM_PM | decimal_to_packed_bcd(h)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Reference in New Issue