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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin};

/// Represents a logical output switch, such as a LED "switch" or transitor
pub trait OutputSwitch {
    type Error;

    /// Turns the switch on
    /// 
    /// # Examples
    /// 
    /// ```
    /// # use embedded_hal_mock::pin::{Mock, State, Transaction};
    /// use switch_hal::output::{OutputSwitch, Switch, ActiveHigh};
    /// # let expectations = [Transaction::set(State::High)];
    /// # let pin = Mock::new(&expectations);
    /// let mut led = Switch::<_, ActiveHigh>::new(pin);
    /// led.on().ok();
    /// ```
    fn on(&mut self) -> Result<(), Self::Error>;

    /// Turns the switch off
    /// 
    /// # Examples
    /// 
    /// ```
    /// # use embedded_hal_mock::pin::{Mock, State, Transaction};
    /// use switch_hal::output::{OutputSwitch, Switch, ActiveHigh};
    /// # let expectations = [Transaction::set(State::Low)];
    /// # let pin = Mock::new(&expectations);
    /// let mut led = Switch::<_, ActiveHigh>::new(pin);
    /// led.off().ok();
    /// ```
    fn off(&mut self) -> Result<(), Self::Error>;
}

/// Toggles the switch from it's current state to it's opposite state.
/// 
/// # Notes
/// This is only available if the underlying hal has implemented [ToggleableOutputPin](embedded_hal::digital::v2::ToggleableOutputPin)
pub trait ToggleableOutputSwitch {
    type Error;

    /// Toggles the current state of the [OutputSwitch](OutputSwitch)
    /// 
    /// # Examples
    /// 
    /// ```ignore
    /// # use embedded_hal_mock::pin::{Mock, State, Transaction};
    /// use switch_hal::output::{OutputSwitch, ToggleableOutputSwitch, Switch, ActiveHigh};
    /// # let pin = Mock::new(&[]);
    /// let mut led = Switch::<_, ActiveHigh>::new(pin);
    /// led.toggle().ok();
    /// ```
    fn toggle(&mut self) -> Result<(), Self::Error>;
}

use core::marker::PhantomData;

/// Zero sized struct for signaling to [Switch](Switch) that it is active high
pub struct ActiveHigh;
/// Zero sized struct for signaling to [Switch](Switch) that it is active low
pub struct ActiveLow;

/// Concrete implementation of [OutputSwitch](OutputSwitch)
pub struct Switch<T, Activeness>
where
    T: OutputPin,
{
    pin: T,
    active: PhantomData<Activeness>,
}

impl<T: OutputPin, Activeness> Switch<T, Activeness> {
    /// Constructs a new [Switch](Switch) from a concrete implementation of an [OutputPin](embedded_hal::digital::v2::OutputPin).
    /// 
    /// # Examples
    ///
    /// Active High
    ///
    /// ```
    /// # use embedded_hal_mock::pin::Mock;
    /// use switch_hal::output::{OutputSwitch, Switch, ActiveHigh};
    /// # let pin = Mock::new(&[]);
    /// let mut led = Switch::<_, ActiveHigh>::new(pin);
    /// ```
    ///
    /// ActiveLow
    ///
    /// ```
    /// # use embedded_hal_mock::pin::Mock;
    /// use switch_hal::output::{OutputSwitch, Switch, ActiveLow};
    /// # let pin = Mock::new(&[]);
    /// let mut led = Switch::<_, ActiveLow>::new(pin);
    /// ```
    ///
    /// stm32f3xx-hal
    ///
    /// ```ignore
    /// // Example for the stm32f303
    /// use stm32f3xx_hal::gpio::gpioe;
    /// use stm32f3xx_hal::gpio::{PushPull, Output};
    /// use stm32f3xx_hal::stm32;
    /// 
    /// use switch_hal::output::{Switch, ActiveHigh};
    /// 
    /// let device_periphs = stm32::Peripherals::take().unwrap();
    /// let gpioe = device_periphs.GPIOE.split(&mut reset_control_clock.ahb);
    /// 
    /// let led = Switch::<_, ActiveHigh>::new(
    ///     gpioe
    ///     .pe9
    ///     .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper)
    /// )
    /// ```
    pub fn new(pin: T) -> Self {
        Switch {
            pin: pin,
            active: PhantomData::<Activeness>,
        }
    }

    /// Consumes the [Switch](Switch) and returns the underlying [OutputPin](embedded_hal::digital::v2::OutputPin).
    /// 
    /// This is useful fore retrieving the underlying pin to use it for a different purpose.
    /// 
    /// # Examples
    /// 
    /// ```
    /// # use embedded_hal_mock::pin::{Mock, State, Transaction};
    /// use switch_hal::output::{OutputSwitch, Switch, ActiveHigh};
    /// # let expectations = [Transaction::set(State::High)];
    /// # let pin = Mock::new(&expectations);
    /// let mut led = Switch::<_, ActiveHigh>::new(pin);
    /// led.on().ok();
    /// let mut pin = led.into_pin();
    /// // do something else with the pin
    /// ```
    pub fn into_pin(self) -> T {
        self.pin
    }
}

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, Activeness> ToggleableOutputSwitch for Switch<T, Activeness> {
    type Error = <T as ToggleableOutputPin>::Error;

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