Skip to content

Commit

Permalink
Add DateTimeBuilder
Browse files Browse the repository at this point in the history
- `DateTimeBuilder::new()` defaults to the UNIX epoch.
  • Loading branch information
bsodmike committed Oct 6, 2024
1 parent 7c2b3a6 commit 350cffa
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
107 changes: 105 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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;
Expand All @@ -95,6 +99,7 @@ impl ClockData {
}

/// Creates a tuple to hold the current year.
#[derive(Debug, Default)]
pub struct CurrentYear(u8);

impl CurrentYear {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/rtc/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))?;

Expand Down

0 comments on commit 350cffa

Please sign in to comment.