diff options
| author | xoviat <[email protected]> | 2023-04-16 19:30:42 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-04-16 19:30:42 -0500 |
| commit | 90c1422381f4a798c83146a2125529bb2f761598 (patch) | |
| tree | 0edef2da6350c6e740b91cfdbe73f18703184b2f | |
| parent | e9ede443bca345f8651a7d8229b319432650dfce (diff) | |
stm32/rtc: remove chrono datetime and add converters
| -rw-r--r-- | embassy-stm32/src/rtc/datetime.rs (renamed from embassy-stm32/src/rtc/datetime_no_deps.rs) | 56 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/mod.rs | 3 | ||||
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 2 |
3 files changed, 57 insertions, 4 deletions
diff --git a/embassy-stm32/src/rtc/datetime_no_deps.rs b/embassy-stm32/src/rtc/datetime.rs index 173f38377..5be68b89b 100644 --- a/embassy-stm32/src/rtc/datetime_no_deps.rs +++ b/embassy-stm32/src/rtc/datetime.rs | |||
| @@ -1,3 +1,8 @@ | |||
| 1 | use core::convert::From; | ||
| 2 | |||
| 3 | #[cfg(feature = "chrono")] | ||
| 4 | use chrono::{self, Datelike, NaiveDate, Timelike, Weekday}; | ||
| 5 | |||
| 1 | use super::byte_to_bcd2; | 6 | use super::byte_to_bcd2; |
| 2 | use crate::pac::rtc::Rtc; | 7 | use crate::pac::rtc::Rtc; |
| 3 | 8 | ||
| @@ -41,6 +46,35 @@ pub struct DateTime { | |||
| 41 | pub second: u8, | 46 | pub second: u8, |
| 42 | } | 47 | } |
| 43 | 48 | ||
| 49 | #[cfg(feature = "chrono")] | ||
| 50 | impl From<chrono::NaiveDateTime> for DateTime { | ||
| 51 | fn from(date_time: chrono::NaiveDateTime) -> Self { | ||
| 52 | Self { | ||
| 53 | year: (date_time.year() - 1970) as u16, | ||
| 54 | month: date_time.month() as u8, | ||
| 55 | day: date_time.day() as u8, | ||
| 56 | day_of_week: date_time.weekday().into(), | ||
| 57 | hour: date_time.hour() as u8, | ||
| 58 | minute: date_time.minute() as u8, | ||
| 59 | second: date_time.second() as u8, | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | #[cfg(feature = "chrono")] | ||
| 65 | impl From<DateTime> for chrono::NaiveDateTime { | ||
| 66 | fn from(date_time: DateTime) -> Self { | ||
| 67 | NaiveDate::from_ymd_opt( | ||
| 68 | (date_time.year + 1970) as i32, | ||
| 69 | date_time.month as u32, | ||
| 70 | date_time.day as u32, | ||
| 71 | ) | ||
| 72 | .unwrap() | ||
| 73 | .and_hms_opt(date_time.hour as u32, date_time.minute as u32, date_time.second as u32) | ||
| 74 | .unwrap() | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 44 | /// A day of the week | 78 | /// A day of the week |
| 45 | #[repr(u8)] | 79 | #[repr(u8)] |
| 46 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] | 80 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] |
| @@ -55,6 +89,28 @@ pub enum DayOfWeek { | |||
| 55 | Sunday = 6, | 89 | Sunday = 6, |
| 56 | } | 90 | } |
| 57 | 91 | ||
| 92 | #[cfg(feature = "chrono")] | ||
| 93 | impl From<chrono::Weekday> for DayOfWeek { | ||
| 94 | fn from(weekday: Weekday) -> Self { | ||
| 95 | day_of_week_from_u8(weekday.number_from_monday() as u8).unwrap() | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | #[cfg(feature = "chrono")] | ||
| 100 | impl From<DayOfWeek> for chrono::Weekday { | ||
| 101 | fn from(weekday: DayOfWeek) -> Self { | ||
| 102 | match weekday { | ||
| 103 | DayOfWeek::Monday => Weekday::Mon, | ||
| 104 | DayOfWeek::Tuesday => Weekday::Tue, | ||
| 105 | DayOfWeek::Wednesday => Weekday::Wed, | ||
| 106 | DayOfWeek::Thursday => Weekday::Thu, | ||
| 107 | DayOfWeek::Friday => Weekday::Fri, | ||
| 108 | DayOfWeek::Saturday => Weekday::Sat, | ||
| 109 | DayOfWeek::Sunday => Weekday::Sun, | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 58 | fn day_of_week_from_u8(v: u8) -> Result<DayOfWeek, Error> { | 114 | fn day_of_week_from_u8(v: u8) -> Result<DayOfWeek, Error> { |
| 59 | Ok(match v { | 115 | Ok(match v { |
| 60 | 0 => DayOfWeek::Monday, | 116 | 0 => DayOfWeek::Monday, |
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index ee3349b27..170783b2d 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs | |||
| @@ -1,8 +1,5 @@ | |||
| 1 | //! RTC peripheral abstraction | 1 | //! RTC peripheral abstraction |
| 2 | use core::marker::PhantomData; | 2 | use core::marker::PhantomData; |
| 3 | |||
| 4 | #[cfg_attr(feature = "chrono", path = "datetime_chrono.rs")] | ||
| 5 | #[cfg_attr(not(feature = "chrono"), path = "datetime_no_deps.rs")] | ||
| 6 | mod datetime; | 3 | mod datetime; |
| 7 | 4 | ||
| 8 | pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; | 5 | pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; |
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 1736769ef..69dcab64c 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" | |||
| 8 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 8 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 9 | 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"] } | 9 | 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"] } |
| 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } | 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } |
| 11 | 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"] } | 11 | 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"] } |
| 12 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 12 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } |
| 13 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "nightly"] } | 13 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "nightly"] } |
| 14 | 14 | ||
