From 350cffaf67dd77979026696ac1625aa7e001ab66 Mon Sep 17 00:00:00 2001 From: Michael de Silva Date: Sat, 5 Oct 2024 21:18:17 +0530 Subject: [PATCH] Add `DateTimeBuilder` - `DateTimeBuilder::new()` defaults to the UNIX epoch. --- src/lib.rs | 2 +- src/models.rs | 107 +++++++++++++++++++++++++++++++++++++++++++++- src/rtc/update.rs | 2 +- 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cd60570..280e15b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ pub(crate) mod rtc; pub mod prelude { pub use crate::error::DriverError; pub use crate::log::LoggableClockData; - pub use crate::models::{CurrentYear, Month, Weekday, Year}; + pub use crate::models::{CurrentYear, DateTimeBuilder, Month, Weekday, Year}; pub use crate::rtc::address::SlaveAddress; pub use crate::rtc::AddressingMode; } diff --git a/src/models.rs b/src/models.rs index 87ba28c..04072fc 100644 --- a/src/models.rs +++ b/src/models.rs @@ -63,7 +63,7 @@ impl ClockData { /// Day. The rtc module refers to this as "date". #[must_use] - pub fn day(&self) -> u8 { + pub fn date(&self) -> u8 { self.date } @@ -80,7 +80,11 @@ impl ClockData { } /// Set the date and time. Hundredths is set to 0. - pub fn set(&mut self, value: (u8, u8, u8, Weekday, u8, Month, CurrentYear)) { + pub fn set(&mut self, value: &ClockData) { + *self = *value; + } + + fn _set(&mut self, value: (u8, u8, u8, Weekday, u8, Month, CurrentYear)) { let (hours, minutes, seconds, weekday, day, month, year) = value; self.hundredths = 0; @@ -95,6 +99,7 @@ impl ClockData { } /// Creates a tuple to hold the current year. +#[derive(Debug, Default)] pub struct CurrentYear(u8); impl CurrentYear { @@ -154,8 +159,10 @@ impl defmt::Format for LoggableClockData { } /// Enumerated type values for the weekday register. +#[derive(Debug, Default)] #[allow(dead_code)] pub enum Weekday { + #[default] /// Sunday Sunday = 1, /// Monday @@ -217,7 +224,9 @@ impl defmt::Format for Weekday { /// Enumerated type values for the month register. #[allow(dead_code)] +#[derive(Debug, Default)] pub enum Month { + #[default] /// January January = 1, /// February @@ -307,6 +316,100 @@ impl Default for Year { } } +/// Creates a [`DateTimeBuilder`] to set the time. +#[derive(Debug, Default)] +pub struct DateTimeBuilder { + hours: u8, + minutes: u8, + seconds: u8, + date: u8, + weekday: Weekday, + month: Month, + year: CurrentYear, +} + +impl DateTimeBuilder { + /// Creates a new [`DateTimeBuilder`], defaulting to the UNIX epoch. + #[must_use] + pub fn new() -> Self { + Self { + hours: 0, + minutes: 0, + seconds: 0, + date: 1, + weekday: Weekday::Thursday, + month: Month::January, + year: CurrentYear::new(1970), + } + } + + /// Set the hours. + #[must_use] + pub fn hours(mut self, value: u8) -> Self { + self.hours = value; + self + } + + /// Set the minutes. + #[must_use] + pub fn minutes(mut self, value: u8) -> Self { + self.minutes = value; + self + } + + /// Set the seconds. + #[must_use] + pub fn seconds(mut self, value: u8) -> Self { + self.seconds = value; + self + } + + /// Set the date. + #[must_use] + pub fn date(mut self, value: u8) -> Self { + self.date = value; + self + } + + /// Set the weekday. + #[must_use] + pub fn weekday(mut self, value: Weekday) -> Self { + self.weekday = value; + self + } + + /// Set the month. + #[must_use] + pub fn month(mut self, value: Month) -> Self { + self.month = value; + self + } + + /// Set the year. + #[must_use] + pub fn year(mut self, value: CurrentYear) -> Self { + self.year = value; + self + } + + /// Build the time. + #[must_use] + pub fn build(self) -> ClockData { + let mut data = ClockData::new(); + data._set(( + self.hours, + self.minutes, + self.seconds, + self.weekday, + self.date, + self.month, + self.year, + )); + + data + } +} + fn left_pad<'a>(buf: &'a mut ByteMutWriter<'_>, value: u8) -> &'a str { buf.clear(); write!(buf, "{}{}", common_padding(value), value).unwrap(); diff --git a/src/rtc/update.rs b/src/rtc/update.rs index fb6d633..8f03aae 100644 --- a/src/rtc/update.rs +++ b/src/rtc/update.rs @@ -40,7 +40,7 @@ impl Updatable for ClockData { cu.write_register(i2c, Register::Hours, dec_to_bcd(data.hours()))?; cu.write_register(i2c, Register::Minutes, dec_to_bcd(data.minutes()))?; cu.write_register(i2c, Register::Seconds, dec_to_bcd(data.seconds()))?; - cu.write_register(i2c, Register::Date, dec_to_bcd(data.day()))?; + cu.write_register(i2c, Register::Date, dec_to_bcd(data.date()))?; cu.write_register(i2c, Register::Month, dec_to_bcd(data.month()))?; cu.write_register(i2c, Register::Year, dec_to_bcd(data.year()))?;