@@ -11,7 +11,7 @@ pub trait OutputPin {
1111
1212/// Output pin that can read its output state
1313#[ cfg( feature = "unproven" ) ]
14- trait StatefulOutputPin {
14+ pub trait StatefulOutputPin {
1515 /// Is the pin set to high?
1616 fn is_set_high ( & self ) -> bool ;
1717
@@ -20,23 +20,73 @@ trait StatefulOutputPin {
2020}
2121
2222/// Output pin that can be toggled
23+ ///
24+ /// See [toggleable](toggleable) to use a software implementation if
25+ /// both [OutputPin](trait.OutputPin.html) and
26+ /// [StatefulOutputPin](trait.StatefulOutputPin.html) are
27+ /// implemented. Otherwise, implement this using hardware mechanisms.
2328#[ cfg( feature = "unproven" ) ]
24- trait ToggleableOutputPin {
25- /// Toggle pin output
29+ pub trait ToggleableOutputPin {
30+ /// Toggle pin output.
2631 fn toggle ( & mut self ) ;
2732}
2833
2934/// If you can read **and** write the output state, a pin is
30- /// toggleable by software. You may override the `toggle()` method
31- /// with a hardware implementation.
32- #[ cfg( feature = "unproven" ) ]
33- impl < PIN : OutputPin + StatefulOutputPin > ToggleableOutputPin for PIN {
34- /// Toggle pin output
35- fn toggle ( & mut self ) {
36- if self . is_set_low ( ) {
37- self . set_high ( ) ;
38- } else {
39- self . set_low ( ) ;
35+ /// toggleable by software.
36+ ///
37+ /// ```
38+ /// use embedded_hal::digital::{OutputPin, StatefulOutputPin, ToggleableOutputPin};
39+ /// use embedded_hal::digital::toggleable;
40+ ///
41+ /// /// A virtual output pin that exists purely in software
42+ /// struct MyPin {
43+ /// state: bool
44+ /// }
45+ ///
46+ /// impl OutputPin for MyPin {
47+ /// fn set_low(&mut self) {
48+ /// self.state = false;
49+ /// }
50+ /// fn set_high(&mut self) {
51+ /// self.state = true;
52+ /// }
53+ /// }
54+ ///
55+ /// impl StatefulOutputPin for MyPin {
56+ /// fn is_set_low(&self) -> bool {
57+ /// !self.state
58+ /// }
59+ /// fn is_set_high(&self) -> bool {
60+ /// self.state
61+ /// }
62+ /// }
63+ ///
64+ /// /// Opt-in to the software implementation.
65+ /// impl toggleable::Default for MyPin {}
66+ ///
67+ /// let mut pin = MyPin { state: false };
68+ /// pin.toggle();
69+ /// assert!(pin.is_set_high());
70+ /// pin.toggle();
71+ /// assert!(pin.is_set_low());
72+ /// ```
73+ pub mod toggleable {
74+ use super :: { OutputPin , StatefulOutputPin , ToggleableOutputPin } ;
75+
76+ /// Software-driven `toggle()` implementation.
77+ pub trait Default : OutputPin + StatefulOutputPin { }
78+
79+ impl < P > ToggleableOutputPin for P
80+ where
81+ P : Default ,
82+ {
83+ /// Toggle pin output
84+ fn toggle ( & mut self ) {
85+ if self . is_set_low ( ) {
86+ self . set_high ( ) ;
87+ } else {
88+ self . set_low ( ) ;
89+ }
4090 }
4191 }
4292}
0 commit comments