From cfbb007c719f768b9c55631949e7f98d287e6d0e Mon Sep 17 00:00:00 2001 From: Alex Martens Date: Mon, 9 Aug 2021 13:17:30 -0700 Subject: [PATCH 1/3] add support for defmt --- CHANGELOG.md | 4 ++++ Cargo.toml | 1 + src/clock.rs | 5 +++++ src/duration.rs | 8 ++++++++ src/fraction.rs | 7 +++++++ src/instant.rs | 1 + src/lib.rs | 2 ++ src/timer.rs | 6 ++++++ 8 files changed, 34 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b4d7fe..2a2a480 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Added + +- added optional support for [defmt](https://github.com/knurling-rs/defmt) + [unreleased]: https://github.com/FluenTech/embedded-time/compare/v0.12.0...HEAD ## [0.12.0] - 2021-05-30 diff --git a/Cargo.toml b/Cargo.toml index c83ba0c..81ff46f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ members = ["examples"] [dependencies] num = { version = "0.3.0", default-features = false } serde = { version = "1.0.0", default-features = false, features = ["derive"], optional = true } +defmt = { version = "0.2", optional = true } [dev-dependencies] crossbeam-utils = "0.7.2" diff --git a/src/clock.rs b/src/clock.rs index 2e31ee1..6611459 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -9,6 +9,7 @@ use core::hash::Hash; /// Potential `Clock` errors #[non_exhaustive] #[derive(Debug, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Error { /// Exact cause of failure is unknown Unspecified, @@ -34,7 +35,11 @@ impl Default for Error { /// software [`Timer`]s can be spawned from a `Clock` object. pub trait Clock: Sized { /// The type to hold the tick count + #[cfg(not(feature = "defmt"))] type T: TimeInt + Hash; + /// The type to hold the tick count + #[cfg(feature = "defmt")] + type T: TimeInt + Hash + defmt::Format; /// The duration of one clock tick in seconds, AKA the clock precision. const SCALING_FACTOR: Fraction; diff --git a/src/duration.rs b/src/duration.rs index 33dec7e..2aa7334 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -438,6 +438,7 @@ pub trait Duration: Sized + Copy { /// The purpose of this type is to allow a simple `Duration` object that can be defined at run-time. /// It does this by replacing the `const` _scaling factor_ with a struct field. #[derive(Copy, Clone, Debug, Default)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Generic { integer: T, scaling_factor: Fraction, @@ -560,6 +561,13 @@ pub mod units { } } + #[cfg(feature = "defmt")] + impl defmt::Format for $name { + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "{}", self.0) + } + } + impl ops::Add for $name where Rhs: FixedPoint, diff --git a/src/fraction.rs b/src/fraction.rs index 5201c7a..8c95ba5 100644 --- a/src/fraction.rs +++ b/src/fraction.rs @@ -180,5 +180,12 @@ impl Default for Fraction { } } +#[cfg(feature = "defmt")] +impl defmt::Format for Fraction { + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "{} / {}", self.numerator(), self.denominator()) + } +} + #[cfg(test)] mod tests {} diff --git a/src/instant.rs b/src/instant.rs index 5bd71f1..e4bb092 100644 --- a/src/instant.rs +++ b/src/instant.rs @@ -46,6 +46,7 @@ use num::traits::{WrappingAdd, WrappingSub}; /// Instant::::new(23); /// ``` #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Instant { ticks: Clock::T, diff --git a/src/lib.rs b/src/lib.rs index 85be10a..d12f43b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -295,6 +295,7 @@ pub use timer::Timer; /// Crate errors #[non_exhaustive] #[derive(Debug, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum TimeError { /// Exact cause of failure is unknown Unspecified, @@ -325,6 +326,7 @@ impl Default for TimeError { /// Conversion errors #[non_exhaustive] #[derive(Debug, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum ConversionError { /// Exact cause of failure is unknown Unspecified, diff --git a/src/timer.rs b/src/timer.rs index ff76cf9..c4e37d7 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -13,28 +13,34 @@ use core::{convert::TryFrom, marker::PhantomData, ops::Add, prelude::v1::*}; pub mod param { /// Parameter not set #[derive(Debug, Hash)] + #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct None; /// Timer is ready to start #[derive(Debug, Hash)] + #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Armed; /// Timer is running #[derive(Debug, Hash)] + #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Running; /// Timer will automatically restart when it expires #[derive(Debug, Hash)] + #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Periodic; /// Timer will stop when it expires #[derive(Debug, Hash)] + #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct OneShot; } /// A `Timer` counts toward an expiration, can be polled for elapsed and remaining time, and can be /// one-shot or continuous/periodic. #[derive(Debug, Hash)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Timer<'a, Type, State, Clock: crate::Clock, Dur: Duration> { clock: &'a Clock, duration: Dur, From acd7891ed4c2c5093077c66ec8cfc8c450561fc1 Mon Sep 17 00:00:00 2001 From: Alex <7845120+newAM@users.noreply.github.com> Date: Fri, 13 Aug 2021 11:21:09 -0700 Subject: [PATCH 2/3] Enforce minimum defmt v0.2.3 Co-authored-by: Fabian --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 81ff46f..0b59663 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ members = ["examples"] [dependencies] num = { version = "0.3.0", default-features = false } serde = { version = "1.0.0", default-features = false, features = ["derive"], optional = true } -defmt = { version = "0.2", optional = true } +defmt = { version = "0.2.3", optional = true } [dev-dependencies] crossbeam-utils = "0.7.2" From 59941050b645f31e963b5d7ffb0c0abac2fbb94a Mon Sep 17 00:00:00 2001 From: Alex M Date: Sun, 15 Aug 2021 14:56:19 -0700 Subject: [PATCH 3/3] skip defmt for non-embedded documentation tests --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a91e404..39bd280 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,7 +49,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: --doc --all-features + args: --doc --features serde - name: Clippy uses: actions-rs/cargo@v1