Skip to content

Commit 833dcf2

Browse files
Merge #309
309: Fix missing `capture` module r=eldruin a=GrantM11235 cc `@eldruin` This must have gotten lost during #298 Co-authored-by: Grant Miller <[email protected]>
2 parents b574c55 + 05afa31 commit 833dcf2

File tree

2 files changed

+83
-82
lines changed

2 files changed

+83
-82
lines changed

src/capture.rs

+82-82
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,96 @@
22
33
/// Non-blocking input capture traits
44
pub mod nb {
5-
/// Input capture
6-
///
7-
/// # Examples
8-
///
9-
/// You can use this interface to measure the period of (quasi) periodic signals
10-
/// / events
11-
///
12-
/// ```
13-
/// extern crate embedded_hal as hal;
14-
/// #[macro_use(block)]
15-
/// extern crate nb;
16-
///
17-
/// use hal::nb::capture::Capture;
18-
///
19-
/// fn main() {
20-
/// let mut capture: Capture1 = {
21-
/// // ..
22-
/// # Capture1
23-
/// };
24-
///
25-
/// capture.set_resolution(1.ms()).unwrap();
26-
///
27-
/// let before = block!(capture.capture(Channel::_1)).unwrap();
28-
/// let after = block!(capture.capture(Channel::_1)).unwrap();
29-
///
30-
/// let period = after.wrapping_sub(before);
31-
///
32-
/// println!("Period: {} ms", period);
33-
/// }
34-
///
35-
/// # use core::convert::Infallible;
36-
/// # struct MilliSeconds(u32);
37-
/// # trait U32Ext { fn ms(self) -> MilliSeconds; }
38-
/// # impl U32Ext for u32 { fn ms(self) -> MilliSeconds { MilliSeconds(self) } }
39-
/// # struct Capture1;
40-
/// # enum Channel { _1 }
41-
/// # impl hal::nb::capture::Capture for Capture1 {
42-
/// # type Error = Infallible;
43-
/// # type Capture = u16;
44-
/// # type Channel = Channel;
45-
/// # type Time = MilliSeconds;
46-
/// # fn capture(&mut self, _: Channel) -> ::nb::Result<u16, Self::Error> { Ok(0) }
47-
/// # fn disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
48-
/// # fn enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
49-
/// # fn get_resolution(&self) -> Result<MilliSeconds, Self::Error> { unimplemented!() }
50-
/// # fn set_resolution<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<MilliSeconds> { Ok(()) }
51-
/// # }
52-
/// ```
53-
// unproven reason: pre-singletons API. With singletons a `CapturePin` (cf. `PwmPin`) trait seems more
54-
// appropriate
55-
pub trait Capture {
56-
/// Enumeration of `Capture` errors
5+
/// Input capture
576
///
58-
/// Possible errors:
7+
/// # Examples
598
///
60-
/// - *overcapture*, the previous capture value was overwritten because it
61-
/// was not read in a timely manner
62-
type Error: core::fmt::Debug;
63-
64-
/// Enumeration of channels that can be used with this `Capture` interface
9+
/// You can use this interface to measure the period of (quasi) periodic signals
10+
/// / events
11+
///
12+
/// ```
13+
/// extern crate embedded_hal as hal;
14+
/// #[macro_use(block)]
15+
/// extern crate nb;
16+
///
17+
/// use hal::capture::nb::Capture;
18+
///
19+
/// fn main() {
20+
/// let mut capture: Capture1 = {
21+
/// // ..
22+
/// # Capture1
23+
/// };
24+
///
25+
/// capture.set_resolution(1.ms()).unwrap();
26+
///
27+
/// let before = block!(capture.capture(Channel::_1)).unwrap();
28+
/// let after = block!(capture.capture(Channel::_1)).unwrap();
6529
///
66-
/// If your `Capture` interface has no channels you can use the type `()`
67-
/// here
68-
type Channel;
30+
/// let period = after.wrapping_sub(before);
31+
///
32+
/// println!("Period: {} ms", period);
33+
/// }
34+
///
35+
/// # use core::convert::Infallible;
36+
/// # struct MilliSeconds(u32);
37+
/// # trait U32Ext { fn ms(self) -> MilliSeconds; }
38+
/// # impl U32Ext for u32 { fn ms(self) -> MilliSeconds { MilliSeconds(self) } }
39+
/// # struct Capture1;
40+
/// # enum Channel { _1 }
41+
/// # impl hal::capture::nb::Capture for Capture1 {
42+
/// # type Error = Infallible;
43+
/// # type Capture = u16;
44+
/// # type Channel = Channel;
45+
/// # type Time = MilliSeconds;
46+
/// # fn capture(&mut self, _: Channel) -> ::nb::Result<u16, Self::Error> { Ok(0) }
47+
/// # fn disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
48+
/// # fn enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
49+
/// # fn get_resolution(&self) -> Result<MilliSeconds, Self::Error> { unimplemented!() }
50+
/// # fn set_resolution<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<MilliSeconds> { Ok(()) }
51+
/// # }
52+
/// ```
53+
// unproven reason: pre-singletons API. With singletons a `CapturePin` (cf. `PwmPin`) trait seems more
54+
// appropriate
55+
pub trait Capture {
56+
/// Enumeration of `Capture` errors
57+
///
58+
/// Possible errors:
59+
///
60+
/// - *overcapture*, the previous capture value was overwritten because it
61+
/// was not read in a timely manner
62+
type Error: core::fmt::Debug;
6963

70-
/// A time unit that can be converted into a human time unit (e.g. seconds)
71-
type Time;
64+
/// Enumeration of channels that can be used with this `Capture` interface
65+
///
66+
/// If your `Capture` interface has no channels you can use the type `()`
67+
/// here
68+
type Channel;
7269

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

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

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

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

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

92-
/// Sets the resolution of the capture timer
93-
fn set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error>
94-
where
95-
R: Into<Self::Time>;
96-
}
89+
/// Returns the current resolution
90+
fn get_resolution(&self) -> Result<Self::Time, Self::Error>;
91+
92+
/// Sets the resolution of the capture timer
93+
fn set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error>
94+
where
95+
R: Into<Self::Time>;
96+
}
9797
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@
418418
pub mod fmt;
419419
pub use nb;
420420
pub mod adc;
421+
pub mod capture;
421422
pub mod delay;
422423
pub mod digital;
423424
pub mod i2c;

0 commit comments

Comments
 (0)