|
2 | 2 |
|
3 | 3 | /// Non-blocking input capture traits
|
4 | 4 | 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 |
57 | 6 | ///
|
58 |
| - /// Possible errors: |
| 7 | + /// # Examples |
59 | 8 | ///
|
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(); |
65 | 29 | ///
|
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; |
69 | 63 |
|
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; |
72 | 69 |
|
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; |
75 | 72 |
|
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; |
82 | 75 |
|
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>; |
85 | 82 |
|
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>; |
88 | 85 |
|
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>; |
91 | 88 |
|
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 | + } |
97 | 97 | }
|
0 commit comments