Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Timer type #21

Merged
merged 5 commits into from
Jul 3, 2020
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
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

### Added

- Add fallibility to `Clock` methods
- A `Timer` type supporting one-shot and periodic software timers utilizing a `Clock` implementation
- `Timer` unit tests
- Fallibility to `Clock` methods
- `Instant::duration_until()` with order checking
- Order checking to `Instant::duration_since()`
- Bounds checking on `Instant` impls of Add/Sub
- Changelog back to v0.5.0 release
- [`crossbeam-utils`](https://crates.io/crates/crossbeam-utils) dev-dependency for scoped threads in tests

### Changed

- Add `&mut self` to `Clock` functions (make stateful, or at least allow stateful implementations)
- Add `&self` to `Clock` functions (make stateful, or at least allow stateful implementations)
- All time-type inner types from signed to unsigned
- `Instant::duration_since()` return type to `Result`
- Refactor `examples/nrf52_dk`
Expand Down
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ panic_rtt = "0.3.0"
nrf52832-hal = { version = "0.10.0", default-features = false, features = ["rt", "xxAA-package"] }
mutex-trait = "0.2.0"

[target.'cfg(not(target_arch = "arm"))'.dev-dependencies]
crossbeam-utils = "0.7.2"

[patch.crates-io]
cortex-m = { git = "https://github.com/rust-embedded/cortex-m", branch = "mutex_add" }
12 changes: 10 additions & 2 deletions examples/nrf52_dk/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,20 @@ where
led2.set_high()?;
led3.set_high()?;
led4.set_low()?;
clock.delay(250_u32.milliseconds()).unwrap();
clock
.new_timer()
.set_duration(250_u32.milliseconds())
.start()
.wait();

led1.set_high()?;
led2.set_low()?;
led3.set_low()?;
led4.set_high()?;
clock.delay(250_u32.milliseconds()).unwrap();
clock
.new_timer()
.set_duration(250_u32.milliseconds())
.start()
.wait();
}
}
19 changes: 5 additions & 14 deletions src/clock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The `Clock` trait can be implemented over hardware timers or other time-keeping device

use crate::{time_int::TimeInt, Duration, Instant, Period};
use core::convert::TryFrom;
use crate::timer::param;
use crate::{time_int::TimeInt, Duration, Instant, Period, Timer};

/// Potential `Clock` errors
#[non_exhaustive]
Expand All @@ -28,17 +28,8 @@ pub trait Clock: Sized {
/// Implementation-specific error returned through [`Error::Other(ImplError)`]
fn now(&self) -> Result<Instant<Self>, Error<Self::ImplError>>;

/// Blocking delay
///
/// # Errors
/// Implementation-specific error returned through [`Error::Other(ImplError)`]
fn delay<Dur: Duration>(&self, dur: Dur) -> Result<(), Error<Self::ImplError>>
where
Self::Rep: TryFrom<Dur::Rep>,
{
let start = self.now()?;
let end = start + dur;
while self.now()? < end {}
Ok(())
/// Spawn a new, `OneShot` [`Timer`] from this clock
fn new_timer<Dur: Duration>(&self) -> Timer<param::OneShot, param::Disarmed, Self, Dur> {
Timer::<param::None, param::None, Self, Dur>::new(&self)
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
//! **Wrapping Clock**: A clock that when at its maximum value, the next count is the minimum
//! value.
//!
//! **Timer**: An entity that counts toward an expiration.
//!
//! **Instant**: A specific instant in time ("time-point") read from a clock.
//!
//! **Duration**: The difference of two instances. The time that has elapsed since an instant. A
Expand Down Expand Up @@ -77,13 +79,15 @@ mod frequency;
mod instant;
mod period;
mod time_int;
mod timer;

pub use clock::Clock;
use core::{convert::Infallible, fmt};
pub use duration::Duration;
pub use instant::Instant;
pub use period::Period;
pub use time_int::TimeInt;
pub use timer::Timer;

/// Public _traits_
///
Expand Down
Loading