1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! Embedded controls library is based on [switch-hal](https://crates.io/crates/switch-hal)
//! that allows to handle primitive controls like [`DebounceInput`](crate::DebouncedInput),
//! [`Encoder`](crate::Encoder).
//! [`Controls`](crate::Control) are updating by [`timestamps`](crate::Control::Timestamp)
//! that passed to [`update`](crate::Control::update) and return [`event`](crate::Control::Event)
//! or [`error`](crate::Control::Error).

#![no_std]

mod debounced_input;
mod encoder;

pub mod macros;

pub use debounced_input::{DebouncedInput, DebouncedInputConfig, DebouncedInputEvent};
pub use encoder::{Encoder, EncoderConfig, EncoderEvent};

use core::fmt::Debug;

/// Represents an elapsed timer that used for configs.
/// # Example
/// ```
/// # use embedded_controls::ElapsedTimer;
/// pub struct MyElapsedTimer {
///     duration: u32,
/// }
///
/// impl ElapsedTimer for MyElapsedTimer {
///     type Error = ();
///     type Timestamp = u32;
///
///     fn is_timeout(
///         &self,
///         from: &Self::Timestamp,
///         to: &Self::Timestamp,
///     ) -> Result<bool, Self::Error> {
///         if to >= from {
///             Ok((to - from) >= self.duration)
///         } else {
///             Err(())
///         }
///     }
/// }
/// ```
pub trait ElapsedTimer {
    type Error: Debug;
    type Timestamp: Clone;

    /// Returns true if a timer duration is more(equal) then duration between from-to timestamps,
    /// otherwise false.
    /// # Errors
    /// This function will return an error if duration between from-to timestamps is negative.
    fn is_timeout(&self, from: &Self::Timestamp, to: &Self::Timestamp)
        -> Result<bool, Self::Error>;
}

/// Represents a control, such as debounced input, button, encoder and etc.
pub trait Control {
    type Timestamp;
    type Event;
    type Error;

    /// Update a control and return an current event or error after update.
    ///
    /// `now` - the current timestamp upon `update` invoke.
    fn update(&mut self, now: Self::Timestamp) -> Result<Self::Event, Self::Error>;
}