switch_hal/output/
mod.rs

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
use embedded_hal::digital::v2::{OutputPin, StatefulOutputPin, ToggleableOutputPin};

use crate::{ActiveHigh, ActiveLow, OutputSwitch, Switch, StatefulOutputSwitch, ToggleableOutputSwitch};

impl<T: OutputPin> OutputSwitch for Switch<T, ActiveHigh> {
    type Error = <T as OutputPin>::Error;

    fn on(&mut self) -> Result<(), Self::Error> {
        self.pin.set_high()
    }

    fn off(&mut self) -> Result<(), Self::Error> {
        self.pin.set_low()
    }
}

impl<T: OutputPin> OutputSwitch for Switch<T, ActiveLow> {
    type Error = <T as OutputPin>::Error;

    fn on(&mut self) -> Result<(), Self::Error> {
        self.pin.set_low()
    }

    fn off(&mut self) -> Result<(), Self::Error> {
        self.pin.set_high()
    }
}

impl<T: OutputPin + ToggleableOutputPin, ActiveLevel> ToggleableOutputSwitch
    for Switch<T, ActiveLevel>
{
    type Error = <T as ToggleableOutputPin>::Error;

    fn toggle(&mut self) -> Result<(), Self::Error> {
        self.pin.toggle()
    }
}

impl<T: OutputPin + StatefulOutputPin> StatefulOutputSwitch
    for Switch<T, ActiveLow>
{
    type Error = <T as OutputPin>::Error;

    fn is_on(&mut self) -> Result<bool, Self::Error> {
        self.pin.is_set_low()
    }

    fn is_off(&mut self) -> Result<bool, Self::Error> {
        self.pin.is_set_high()
    }
}

impl<T: OutputPin + StatefulOutputPin> StatefulOutputSwitch
    for Switch<T, ActiveHigh>
{
    type Error = <T as OutputPin>::Error;

    fn is_on(&mut self) -> Result<bool, Self::Error> {
        self.pin.is_set_high()
    }

    fn is_off(&mut self) -> Result<bool, Self::Error> {
        self.pin.is_set_low()
    }
}