mirror of https://github.com/eldruin/ds323x-rs
Generate error when century bit is set in functions already reading the century bit
parent
6874151dec
commit
66fee80b62
|
|
@ -19,11 +19,13 @@ where
|
|||
let mut data = [0; 8];
|
||||
self.iface.read_data(&mut data)?;
|
||||
|
||||
let year = year_from_registers(
|
||||
data[Register::MONTH as usize + 1],
|
||||
data[Register::YEAR as usize + 1],
|
||||
);
|
||||
let month = packed_bcd_to_decimal(data[Register::MONTH as usize + 1] & !BitFlags::CENTURY);
|
||||
let century = data[Register::MONTH as usize + 1] & BitFlags::CENTURY;
|
||||
if century != 0 {
|
||||
return Err(Error::InvalidDeviceCentury);
|
||||
}
|
||||
|
||||
let year = 2000 + (packed_bcd_to_decimal(data[Register::YEAR as usize + 1]) as u16);
|
||||
let month = packed_bcd_to_decimal(data[Register::MONTH as usize + 1]);
|
||||
let day = packed_bcd_to_decimal(data[Register::DOM as usize + 1]);
|
||||
let hour = hours_from_register(data[Register::HOURS as usize + 1]);
|
||||
let minute = packed_bcd_to_decimal(data[Register::MINUTES as usize + 1]);
|
||||
|
|
@ -99,7 +101,15 @@ where
|
|||
let mut data = [0; 3];
|
||||
data[0] = Register::MONTH;
|
||||
self.iface.read_data(&mut data)?;
|
||||
Ok(year_from_registers(data[1], data[2]))
|
||||
|
||||
let century = data[1] & BitFlags::CENTURY;
|
||||
if century != 0 {
|
||||
return Err(Error::InvalidDeviceCentury);
|
||||
}
|
||||
|
||||
let year = 2000 + (packed_bcd_to_decimal(data[2]) as u16);
|
||||
|
||||
Ok(year)
|
||||
}
|
||||
|
||||
fn date(&mut self) -> Result<NaiveDate, Self::Error> {
|
||||
|
|
@ -108,12 +118,15 @@ where
|
|||
self.iface.read_data(&mut data)?;
|
||||
|
||||
let offset = Register::DOM as usize;
|
||||
let year = year_from_registers(
|
||||
data[Register::MONTH as usize + 1 - offset],
|
||||
data[Register::YEAR as usize + 1 - offset],
|
||||
);
|
||||
let month =
|
||||
packed_bcd_to_decimal(data[Register::MONTH as usize + 1 - offset] & !BitFlags::CENTURY);
|
||||
|
||||
let century = data[Register::MONTH as usize + 1 - offset] & BitFlags::CENTURY;
|
||||
if century != 0 {
|
||||
return Err(Error::InvalidDeviceCentury);
|
||||
}
|
||||
|
||||
let year =
|
||||
2000 + (packed_bcd_to_decimal(data[Register::YEAR as usize + 1 - offset]) as u16);
|
||||
let month = packed_bcd_to_decimal(data[Register::MONTH as usize + 1 - offset]);
|
||||
let day = packed_bcd_to_decimal(data[Register::DOM as usize + 1 - offset]);
|
||||
let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into());
|
||||
some_or_invalid_error(date)
|
||||
|
|
@ -231,16 +244,6 @@ fn hours_from_register(data: u8) -> Hours {
|
|||
}
|
||||
}
|
||||
|
||||
fn year_from_registers(month: u8, year: u8) -> u16 {
|
||||
let century = month & BitFlags::CENTURY;
|
||||
let year = packed_bcd_to_decimal(year);
|
||||
if century != 0 {
|
||||
2100 + u16::from(year)
|
||||
} else {
|
||||
2000 + u16::from(year)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_24h_format(hours_data: u8) -> bool {
|
||||
hours_data & BitFlags::H24_H12 == 0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -387,6 +387,11 @@ pub enum Error<E> {
|
|||
/// It was not possible to read a valid date and/or time.
|
||||
/// The device is probably missing initialization.
|
||||
InvalidDeviceState,
|
||||
/// Device century is not the 20th century.
|
||||
///
|
||||
/// The device does not produce valid dates for centuries
|
||||
/// other than the 20th century.
|
||||
InvalidDeviceCentury,
|
||||
}
|
||||
|
||||
/// Square-wave output frequency
|
||||
|
|
|
|||
Loading…
Reference in New Issue