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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#[cfg(feature = "esp")]
use esp_idf_hal::gpio::{Input, InputOutput, Pin, PinDriver};

/// An abstraction over different switching APIs.
pub trait PinWrapper {
    /// Is source on?
    fn is_high(&mut self) -> bool;

    /// Is source off?
    fn is_low(&mut self) -> bool {
        !self.is_high()
    }
}

#[cfg(feature = "esp")]
impl<'d, P: Pin> PinWrapper for PinDriver<'d, P, Input> {
    fn is_high(&mut self) -> bool {
        self.is_high()
    }
}

#[cfg(feature = "esp")]
impl<'d, P: Pin> PinWrapper for PinDriver<'d, P, InputOutput> {
    fn is_high(&mut self) -> bool {
        self.is_high()
    }
}

#[cfg(feature = "embedded_hal_old")]
impl<P> PinWrapper for P
where
    Self: embedded_hal_old::digital::v2::InputPin,
{
    fn is_high(&mut self) -> bool {
        embedded_hal_old::digital::v2::InputPin::is_high(self).unwrap_or_default()
    }
}

#[cfg(feature = "embedded_hal")]
impl<P> PinWrapper for P
where
    Self: embedded_hal::digital::InputPin,
{
    fn is_high(&mut self) -> bool {
        embedded_hal::digital::InputPin::is_high(self).unwrap_or_default()
    }
}

#[cfg(all(test, feature = "std"))]
pub(crate) mod tests {
    use std::{
        sync::{
            atomic::{AtomicBool, Ordering},
            Arc,
        },
        thread::sleep,
        time::{Duration, Instant},
    };

    use crate::{Button, ButtonConfig, Mode, PinWrapper, State};

    pub const CONFIG: ButtonConfig = ButtonConfig {
        hold: Duration::from_millis(500),
        debounce: Duration::from_micros(700),
        release: Duration::from_millis(30),
        mode: Mode::PullDown,
    };

    #[derive(Debug, Default, Clone)]
    pub struct MockPin(Arc<AtomicBool>);

    impl PinWrapper for MockPin {
        fn is_high(&mut self) -> bool {
            self.0.load(Ordering::SeqCst)
        }
    }

    impl Button<MockPin, Instant> {
        pub fn press_button(&mut self) {
            self.pin.press();
            self.tick();
            assert!(matches!(self.state, State::Down(_)));

            sleep(CONFIG.debounce);
            self.tick();
        }

        pub fn release_button(&mut self) {
            self.pin.release();
            self.tick();
        }

        pub fn hold_button(&mut self) {
            self.press_button();
            sleep(CONFIG.hold);
            self.tick();
            self.release_button();
        }
    }

    impl MockPin {
        /// Press the pin with debounce.
        pub fn press(&self) {
            self.0.store(true, Ordering::SeqCst);
            sleep(CONFIG.debounce);
        }

        /// Release the pin with debounce.
        pub fn release(&self) {
            self.0.store(false, Ordering::SeqCst);
            sleep(CONFIG.debounce);
        }

        /// Simulate pin state changes corresponding to one full button click with debounce.
        pub fn click(&self) {
            self.press();
            self.release();
        }

        /// Simulate pin state changes corresponding to one full button hold with debounce.
        pub fn hold(&self) {
            self.press();
            sleep(CONFIG.hold);
            self.release();
        }
    }
}