|
| 1 | +//! Input capture |
| 2 | +
|
| 3 | +use nb; |
| 4 | + |
| 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::prelude::*; |
| 18 | +/// |
| 19 | +/// fn main() { |
| 20 | +/// let mut capture: Capture1 = { |
| 21 | +/// // .. |
| 22 | +/// # Capture1 |
| 23 | +/// }; |
| 24 | +/// |
| 25 | +/// capture.try_set_resolution(1.ms()).unwrap(); |
| 26 | +/// |
| 27 | +/// let before = block!(capture.try_capture(Channel::_1)).unwrap(); |
| 28 | +/// let after = block!(capture.try_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::capture::Capture for Capture1 { |
| 42 | +/// # type Error = Infallible; |
| 43 | +/// # type Capture = u16; |
| 44 | +/// # type Channel = Channel; |
| 45 | +/// # type Time = MilliSeconds; |
| 46 | +/// # fn try_capture(&mut self, _: Channel) -> ::nb::Result<u16, Self::Error> { Ok(0) } |
| 47 | +/// # fn try_disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() } |
| 48 | +/// # fn try_enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() } |
| 49 | +/// # fn try_get_resolution(&self) -> Result<MilliSeconds, Self::Error> { unimplemented!() } |
| 50 | +/// # fn try_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; |
| 63 | + |
| 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; |
| 69 | + |
| 70 | + /// A time unit that can be converted into a human time unit (e.g. seconds) |
| 71 | + type Time; |
| 72 | + |
| 73 | + /// The type of the value returned by `capture` |
| 74 | + type Capture; |
| 75 | + |
| 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 try_capture(&mut self, channel: Self::Channel) -> nb::Result<Self::Capture, Self::Error>; |
| 82 | + |
| 83 | + /// Disables a capture `channel` |
| 84 | + fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>; |
| 85 | + |
| 86 | + /// Enables a capture `channel` |
| 87 | + fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>; |
| 88 | + |
| 89 | + /// Returns the current resolution |
| 90 | + fn try_get_resolution(&self) -> Result<Self::Time, Self::Error>; |
| 91 | + |
| 92 | + /// Sets the resolution of the capture timer |
| 93 | + fn try_set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error> |
| 94 | + where |
| 95 | + R: Into<Self::Time>; |
| 96 | +} |
0 commit comments