# Capture in embedded_hal::capture - Rust
Input capture
Keywords: rust, rustlang, rust-lang, Capture

# Trait embedded_hal::capture::Capture

source · [−]

    pub trait Capture {
        type Error;
        type Channel;
        type Time;
        type Capture;
        fn try_capture(
            &mut self,
            channel: Self::Channel
        ) -> Result<Self::Capture, Self::Error>;
        fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
        fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
        fn try_get_resolution(&self) -> Result<Self::Time, Self::Error>;
        fn try_set_resolution<R>(
            &mut self,
            resolution: R
        ) -> Result<(), Self::Error>
        where
            R: Into<Self::Time>;
    }

Expand description

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::prelude::*;

    fn main() {
        let mut capture: Capture1 = {
            // ..
        };

        capture.try_set_resolution(1.ms()).unwrap();

        let before = block!(capture.try_capture(Channel::_1)).unwrap();
        let after = block!(capture.try_capture(Channel::_1)).unwrap();

        let period = after.wrapping_sub(before);

        println!("Period: {} ms", period);
    }

## Associated Types

source

#### type Error

Enumeration of `Capture` errors

Possible errors:

- overcapture, the previous capture value was overwritten because it was not read in a timely manner

source

#### type Channel

Enumeration of channels that can be used with this `Capture` interface

If your `Capture` interface has no channels you can use the type `()` here

source

#### type Time

A time unit that can be converted into a human time unit (e.g. seconds)

source

#### type Capture

The type of the value returned by `capture`

## Required methods

source

#### fn try_capture(
&mut self,
channel: Self::Channel
) -> Result<Self::Capture, 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)

source

#### fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>

Disables a capture `channel`

source

#### fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>

Enables a capture `channel`

source

#### fn try_get_resolution(&self) -> Result<Self::Time, Self::Error>

Returns the current resolution

source

#### fn try_set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error> where
R: Into<Self::Time>,

Sets the resolution of the capture timer

## Implementors