From 90c1422381f4a798c83146a2125529bb2f761598 Mon Sep 17 00:00:00 2001 From: xoviat Date: Sun, 16 Apr 2023 19:30:42 -0500 Subject: [PATCH] stm32/rtc: remove chrono datetime and add converters --- .../rtc/{datetime_no_deps.rs => datetime.rs} | 56 +++++++++++++++++++ embassy-stm32/src/rtc/mod.rs | 3 - examples/stm32f4/Cargo.toml | 2 +- 3 files changed, 57 insertions(+), 4 deletions(-) rename embassy-stm32/src/rtc/{datetime_no_deps.rs => datetime.rs} (69%) diff --git a/embassy-stm32/src/rtc/datetime_no_deps.rs b/embassy-stm32/src/rtc/datetime.rs similarity index 69% rename from embassy-stm32/src/rtc/datetime_no_deps.rs rename to embassy-stm32/src/rtc/datetime.rs index 173f3837..5be68b89 100644 --- a/embassy-stm32/src/rtc/datetime_no_deps.rs +++ b/embassy-stm32/src/rtc/datetime.rs @@ -1,3 +1,8 @@ +use core::convert::From; + +#[cfg(feature = "chrono")] +use chrono::{self, Datelike, NaiveDate, Timelike, Weekday}; + use super::byte_to_bcd2; use crate::pac::rtc::Rtc; @@ -41,6 +46,35 @@ pub struct DateTime { pub second: u8, } +#[cfg(feature = "chrono")] +impl From for DateTime { + fn from(date_time: chrono::NaiveDateTime) -> Self { + Self { + year: (date_time.year() - 1970) as u16, + month: date_time.month() as u8, + day: date_time.day() as u8, + day_of_week: date_time.weekday().into(), + hour: date_time.hour() as u8, + minute: date_time.minute() as u8, + second: date_time.second() as u8, + } + } +} + +#[cfg(feature = "chrono")] +impl From for chrono::NaiveDateTime { + fn from(date_time: DateTime) -> Self { + NaiveDate::from_ymd_opt( + (date_time.year + 1970) as i32, + date_time.month as u32, + date_time.day as u32, + ) + .unwrap() + .and_hms_opt(date_time.hour as u32, date_time.minute as u32, date_time.second as u32) + .unwrap() + } +} + /// A day of the week #[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] @@ -55,6 +89,28 @@ pub enum DayOfWeek { Sunday = 6, } +#[cfg(feature = "chrono")] +impl From for DayOfWeek { + fn from(weekday: Weekday) -> Self { + day_of_week_from_u8(weekday.number_from_monday() as u8).unwrap() + } +} + +#[cfg(feature = "chrono")] +impl From for chrono::Weekday { + fn from(weekday: DayOfWeek) -> Self { + match weekday { + DayOfWeek::Monday => Weekday::Mon, + DayOfWeek::Tuesday => Weekday::Tue, + DayOfWeek::Wednesday => Weekday::Wed, + DayOfWeek::Thursday => Weekday::Thu, + DayOfWeek::Friday => Weekday::Fri, + DayOfWeek::Saturday => Weekday::Sat, + DayOfWeek::Sunday => Weekday::Sun, + } + } +} + fn day_of_week_from_u8(v: u8) -> Result { Ok(match v { 0 => DayOfWeek::Monday, diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index ee3349b2..170783b2 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs @@ -1,8 +1,5 @@ //! RTC peripheral abstraction use core::marker::PhantomData; - -#[cfg_attr(feature = "chrono", path = "datetime_chrono.rs")] -#[cfg_attr(not(feature = "chrono"), path = "datetime_no_deps.rs")] mod datetime; pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 1736769e..69dcab64 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers", "arch-cortex-m", "executor-thread", "executor-interrupt"] } embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti", "embedded-sdmmc"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti", "embedded-sdmmc", "chrono"] } embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "nightly"] }