Skip to content

Fix missing capture module #309

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

Merged
merged 3 commits into from
Sep 1, 2021
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
164 changes: 82 additions & 82 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,96 @@

/// Non-blocking input capture traits
pub mod nb {
/// Input capture
///
/// # Examples
///
/// You can use this interface to measure the period of (quasi) periodic signals
/// / events
///
/// ```
/// extern crate embedded_hal as hal;
/// #[macro_use(block)]
/// extern crate nb;
///
/// use hal::nb::capture::Capture;
///
/// fn main() {
/// let mut capture: Capture1 = {
/// // ..
/// # Capture1
/// };
///
/// capture.set_resolution(1.ms()).unwrap();
///
/// let before = block!(capture.capture(Channel::_1)).unwrap();
/// let after = block!(capture.capture(Channel::_1)).unwrap();
///
/// let period = after.wrapping_sub(before);
///
/// println!("Period: {} ms", period);
/// }
///
/// # use core::convert::Infallible;
/// # struct MilliSeconds(u32);
/// # trait U32Ext { fn ms(self) -> MilliSeconds; }
/// # impl U32Ext for u32 { fn ms(self) -> MilliSeconds { MilliSeconds(self) } }
/// # struct Capture1;
/// # enum Channel { _1 }
/// # impl hal::nb::capture::Capture for Capture1 {
/// # type Error = Infallible;
/// # type Capture = u16;
/// # type Channel = Channel;
/// # type Time = MilliSeconds;
/// # fn capture(&mut self, _: Channel) -> ::nb::Result<u16, Self::Error> { Ok(0) }
/// # fn disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
/// # fn enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
/// # fn get_resolution(&self) -> Result<MilliSeconds, Self::Error> { unimplemented!() }
/// # fn set_resolution<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<MilliSeconds> { Ok(()) }
/// # }
/// ```
// unproven reason: pre-singletons API. With singletons a `CapturePin` (cf. `PwmPin`) trait seems more
// appropriate
pub trait Capture {
/// Enumeration of `Capture` errors
/// Input capture
///
/// Possible errors:
/// # Examples
///
/// - *overcapture*, the previous capture value was overwritten because it
/// was not read in a timely manner
type Error: core::fmt::Debug;

/// Enumeration of channels that can be used with this `Capture` interface
/// You can use this interface to measure the period of (quasi) periodic signals
/// / events
///
/// ```
/// extern crate embedded_hal as hal;
/// #[macro_use(block)]
/// extern crate nb;
///
/// use hal::capture::nb::Capture;
///
/// fn main() {
/// let mut capture: Capture1 = {
/// // ..
/// # Capture1
/// };
///
/// capture.set_resolution(1.ms()).unwrap();
///
/// let before = block!(capture.capture(Channel::_1)).unwrap();
/// let after = block!(capture.capture(Channel::_1)).unwrap();
///
/// If your `Capture` interface has no channels you can use the type `()`
/// here
type Channel;
/// let period = after.wrapping_sub(before);
///
/// println!("Period: {} ms", period);
/// }
///
/// # use core::convert::Infallible;
/// # struct MilliSeconds(u32);
/// # trait U32Ext { fn ms(self) -> MilliSeconds; }
/// # impl U32Ext for u32 { fn ms(self) -> MilliSeconds { MilliSeconds(self) } }
/// # struct Capture1;
/// # enum Channel { _1 }
/// # impl hal::capture::nb::Capture for Capture1 {
/// # type Error = Infallible;
/// # type Capture = u16;
/// # type Channel = Channel;
/// # type Time = MilliSeconds;
/// # fn capture(&mut self, _: Channel) -> ::nb::Result<u16, Self::Error> { Ok(0) }
/// # fn disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
/// # fn enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
/// # fn get_resolution(&self) -> Result<MilliSeconds, Self::Error> { unimplemented!() }
/// # fn set_resolution<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<MilliSeconds> { Ok(()) }
/// # }
/// ```
// unproven reason: pre-singletons API. With singletons a `CapturePin` (cf. `PwmPin`) trait seems more
// appropriate
pub trait Capture {
/// Enumeration of `Capture` errors
///
/// Possible errors:
///
/// - *overcapture*, the previous capture value was overwritten because it
/// was not read in a timely manner
type Error: core::fmt::Debug;

/// A time unit that can be converted into a human time unit (e.g. seconds)
type Time;
/// Enumeration of channels that can be used with this `Capture` interface
///
/// If your `Capture` interface has no channels you can use the type `()`
/// here
type Channel;

/// The type of the value returned by `capture`
type Capture;
/// A time unit that can be converted into a human time unit (e.g. seconds)
type Time;

/// "Waits" for a transition in the capture `channel` and returns the value
/// of counter at that instant
///
/// NOTE that you must multiply the returned value by the *resolution* of
/// this `Capture` interface to get a human time unit (e.g. seconds)
fn capture(&mut self, channel: Self::Channel) -> nb::Result<Self::Capture, Self::Error>;
/// The type of the value returned by `capture`
type Capture;

/// Disables a capture `channel`
fn disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
/// "Waits" for a transition in the capture `channel` and returns the value
/// of counter at that instant
///
/// NOTE that you must multiply the returned value by the *resolution* of
/// this `Capture` interface to get a human time unit (e.g. seconds)
fn capture(&mut self, channel: Self::Channel) -> nb::Result<Self::Capture, Self::Error>;

/// Enables a capture `channel`
fn enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
/// Disables a capture `channel`
fn disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;

/// Returns the current resolution
fn get_resolution(&self) -> Result<Self::Time, Self::Error>;
/// Enables a capture `channel`
fn enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;

/// Sets the resolution of the capture timer
fn set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error>
where
R: Into<Self::Time>;
}
/// Returns the current resolution
fn get_resolution(&self) -> Result<Self::Time, Self::Error>;

/// Sets the resolution of the capture timer
fn set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error>
where
R: Into<Self::Time>;
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
pub mod fmt;
pub use nb;
pub mod adc;
pub mod capture;
pub mod delay;
pub mod digital;
pub mod i2c;
Expand Down