11use super :: * ;
22
3- #[ derive( Clone , Copy ) ]
4- enum Inverted {
5- NonInverted ,
6- Inverted ,
7- }
8-
9- impl Inverted {
10- fn is_inverted ( & self ) -> bool {
11- match self {
12- Inverted :: NonInverted => false ,
13- Inverted :: Inverted => true ,
14- }
15- }
16- }
17-
18- /// Generic pin wrapper for pins which can be Output or Input.
3+ /// A type-erased GPIO pin, with additional configuration options.
4+ ///
5+ /// Note that accessing unsupported pin functions (e.g. trying to use an
6+ /// input-only pin as output) will panic.
197pub struct AnyPin < ' d > {
208 pin : ErasedPin ,
21- inverted : Inverted ,
9+ is_inverted : bool ,
2210 _phantom : PhantomData < & ' d ( ) > ,
2311}
2412
2513impl < ' d > AnyPin < ' d > {
2614 /// Create wrapper for the given pin.
2715 #[ inline]
28- pub fn new < P : OutputPin + InputPin + CreateErasedPin > (
29- pin : impl crate :: peripheral:: Peripheral < P = P > + ' d ,
30- ) -> Self {
16+ pub fn new < P : CreateErasedPin > ( pin : impl crate :: peripheral:: Peripheral < P = P > + ' d ) -> Self {
3117 crate :: into_ref!( pin) ;
3218 let pin = pin. erased_pin ( private:: Internal ) ;
3319
3420 Self {
3521 pin,
36- inverted : Inverted :: NonInverted ,
22+ is_inverted : false ,
3723 _phantom : PhantomData ,
3824 }
3925 }
@@ -49,7 +35,7 @@ impl<'d> AnyPin<'d> {
4935
5036 Self {
5137 pin,
52- inverted : Inverted :: Inverted ,
38+ is_inverted : true ,
5339 _phantom : PhantomData ,
5440 }
5541 }
@@ -61,7 +47,7 @@ impl<'d> crate::peripheral::Peripheral for AnyPin<'d> {
6147 unsafe fn clone_unchecked ( & mut self ) -> Self :: P {
6248 Self {
6349 pin : unsafe { self . pin . clone_unchecked ( ) } ,
64- inverted : self . inverted ,
50+ is_inverted : self . is_inverted ,
6551 _phantom : PhantomData ,
6652 }
6753 }
@@ -114,10 +100,10 @@ impl<'d> OutputPin for AnyPin<'d> {
114100 fn connect_peripheral_to_output ( & mut self , signal : OutputSignal , _internal : private:: Internal ) {
115101 self . pin . connect_peripheral_to_output_with_options (
116102 signal,
117- self . inverted . is_inverted ( ) ,
103+ self . is_inverted ,
118104 false ,
119105 false ,
120- self . inverted . is_inverted ( ) ,
106+ self . is_inverted ,
121107 private:: Internal ,
122108 ) ;
123109 }
@@ -131,7 +117,7 @@ impl<'d> OutputPin for AnyPin<'d> {
131117 force_via_gpio_mux : bool ,
132118 _internal : private:: Internal ,
133119 ) {
134- if self . inverted . is_inverted ( ) {
120+ if self . is_inverted {
135121 self . pin . connect_peripheral_to_output_with_options (
136122 signal,
137123 true ,
@@ -168,8 +154,8 @@ impl<'d> InputPin for AnyPin<'d> {
168154 fn connect_input_to_peripheral ( & mut self , signal : InputSignal , _internal : private:: Internal ) {
169155 self . pin . connect_input_to_peripheral_with_options (
170156 signal,
171- self . inverted . is_inverted ( ) ,
172- self . inverted . is_inverted ( ) ,
157+ self . is_inverted ,
158+ self . is_inverted ,
173159 private:: Internal ,
174160 ) ;
175161 }
@@ -181,7 +167,7 @@ impl<'d> InputPin for AnyPin<'d> {
181167 force_via_gpio_mux : bool ,
182168 _internal : private:: Internal ,
183169 ) {
184- if self . inverted . is_inverted ( ) {
170+ if self . is_inverted {
185171 self . pin . connect_input_to_peripheral_with_options (
186172 signal,
187173 true ,
@@ -198,85 +184,3 @@ impl<'d> InputPin for AnyPin<'d> {
198184 }
199185 }
200186}
201-
202- /// Generic pin wrapper for pins which can only be Input.
203- pub struct AnyInputOnlyPin < ' d > {
204- pin : ErasedPin ,
205- inverted : Inverted ,
206- _phantom : PhantomData < & ' d ( ) > ,
207- }
208-
209- impl < ' d > AnyInputOnlyPin < ' d > {
210- /// Create wrapper for the given pin.
211- #[ inline]
212- pub fn new < P : InputPin + CreateErasedPin > (
213- pin : impl crate :: peripheral:: Peripheral < P = P > + ' d ,
214- ) -> Self {
215- crate :: into_ref!( pin) ;
216- let pin = pin. erased_pin ( private:: Internal ) ;
217-
218- Self {
219- pin,
220- inverted : Inverted :: NonInverted ,
221- _phantom : PhantomData ,
222- }
223- }
224- }
225-
226- impl < ' d > crate :: peripheral:: Peripheral for AnyInputOnlyPin < ' d > {
227- type P = Self ;
228-
229- unsafe fn clone_unchecked ( & mut self ) -> Self :: P {
230- Self {
231- pin : unsafe { self . pin . clone_unchecked ( ) } ,
232- inverted : self . inverted ,
233- _phantom : PhantomData ,
234- }
235- }
236- }
237-
238- impl < ' d > private:: Sealed for AnyInputOnlyPin < ' d > { }
239-
240- impl < ' d > Pin for AnyInputOnlyPin < ' d > {
241- delegate:: delegate! {
242- to self . pin {
243- fn number( & self , _internal: private:: Internal ) -> u8 ;
244- fn sleep_mode( & mut self , on: bool , _internal: private:: Internal ) ;
245- fn set_alternate_function( & mut self , alternate: AlternateFunction , _internal: private:: Internal ) ;
246- fn is_listening( & self , _internal: private:: Internal ) -> bool ;
247- fn listen_with_options(
248- & mut self ,
249- event: Event ,
250- int_enable: bool ,
251- nmi_enable: bool ,
252- wake_up_from_light_sleep: bool ,
253- _internal: private:: Internal ,
254- ) ;
255- fn unlisten( & mut self , _internal: private:: Internal ) ;
256- fn is_interrupt_set( & self , _internal: private:: Internal ) -> bool ;
257- fn clear_interrupt( & mut self , _internal: private:: Internal ) ;
258- fn wakeup_enable( & mut self , enable: bool , event: WakeEvent , _internal: private:: Internal ) ;
259- }
260- }
261- }
262-
263- impl < ' d > InputPin for AnyInputOnlyPin < ' d > {
264- delegate:: delegate! {
265- to self . pin {
266- fn init_input( & self , pull_down: bool , pull_up: bool , _internal: private:: Internal ) ;
267- fn set_to_input( & mut self , _internal: private:: Internal ) ;
268- fn enable_input( & mut self , on: bool , _internal: private:: Internal ) ;
269- fn enable_input_in_sleep_mode( & mut self , on: bool , _internal: private:: Internal ) ;
270- fn is_input_high( & self , _internal: private:: Internal ) -> bool ;
271- fn connect_input_to_peripheral( & mut self , signal: InputSignal , _internal: private:: Internal ) ;
272- fn connect_input_to_peripheral_with_options(
273- & mut self ,
274- signal: InputSignal ,
275- invert: bool ,
276- force_via_gpio_mux: bool ,
277- _internal: private:: Internal ,
278- ) ;
279- fn disconnect_input_from_peripheral( & mut self , signal: InputSignal , _internal: private:: Internal ) ;
280- }
281- }
282- }
0 commit comments