Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- added optional support for [defmt](https://github.com/knurling-rs/defmt)
- add `scaling_factor` method to `FixedPoint` type to retrieve the const scaling-factor [`Fraction`] from any fixed-point object
- Duration and Rate are no longer implemented by the `Generic` duration and rate types
- durations and rates can now be taken generically by a function without having to bind to `FixedPoint` (`FixedPoint` is now bound to the `Duration` and `Rate` traits)
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.3", optional = true }

[dev-dependencies]
crossbeam-utils = "0.7.2"
Expand Down
5 changes: 5 additions & 0 deletions src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Comment thread
PTaylor-us marked this conversation as resolved.
Resolved

/// The duration of one clock tick in seconds, AKA the clock precision.
const SCALING_FACTOR: Fraction;
Expand Down
8 changes: 8 additions & 0 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ pub trait Duration: FixedPoint + 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<T> {
integer: T,
scaling_factor: Fraction,
Expand Down Expand Up @@ -659,6 +660,13 @@ pub mod units {
}
}

#[cfg(feature = "defmt")]
impl<T: TimeInt + defmt::Format> defmt::Format for $name<T> {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "{}", self.0)
Comment thread
PTaylor-us marked this conversation as resolved.
Resolved
}
}

impl<T: TimeInt, Rhs: Duration> ops::Add<Rhs> for $name<T>
where
Self: TryFrom<Rhs>,
Expand Down
7 changes: 7 additions & 0 deletions src/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Comment thread
PTaylor-us marked this conversation as resolved.
Resolved
}
}

#[cfg(test)]
mod tests {}
1 change: 1 addition & 0 deletions src/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use num::traits::{WrappingAdd, WrappingSub};
/// Instant::<SomeClock>::new(23);
/// ```
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instant<Clock: crate::Clock> {
ticks: Clock::T,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,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,
Expand Down