Skip to content

Commit d2dae44

Browse files
committed
From trait for Pin, PartiallyErasedPin, and ErasedPin structs
1 parent 9595e6a commit d2dae44

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/gpio.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,24 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
341341
}
342342
}
343343

344+
impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>> for ErasedPin<MODE> {
345+
/// Pin-to-erased pin conversion using the [`From`] trait.
346+
///
347+
/// Note that [`From`] is the reciprocal of [`Into`].
348+
fn from(p: Pin<P, N, MODE>) -> Self {
349+
p.erase()
350+
}
351+
}
352+
353+
impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>> for PartiallyErasedPin<P, MODE> {
354+
/// Pin-to-partially erased pin conversion using the [`From`] trait.
355+
///
356+
/// Note that [`From`] is the reciprocal of [`Into`].
357+
fn from(p: Pin<P, N, MODE>) -> Self {
358+
p.erase_number()
359+
}
360+
}
361+
344362
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
345363
/// Set the output of the pin regardless of its mode.
346364
/// Primarily used to set the output value of the pin

src/gpio/partially_erased.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,12 @@ where
136136
unsafe { (*Gpio::<P>::ptr()).idr.read().bits() & (1 << self.i) == 0 }
137137
}
138138
}
139+
140+
impl<const P: char, MODE> From<PartiallyErasedPin<P, MODE>> for ErasedPin<MODE> {
141+
/// Partially erased pin-to-erased pin conversion using the [`From`] trait.
142+
///
143+
/// Note that [`From`] is the reciprocal of [`Into`].
144+
fn from(p: PartiallyErasedPin<P, MODE>) -> Self {
145+
ErasedPin::new(P as u8 - b'A', p.i)
146+
}
147+
}

0 commit comments

Comments
 (0)