Skip to content

Commit 51d7aff

Browse files
committed
into_mode
1 parent ac44f35 commit 51d7aff

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/gpio/convert.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -276,34 +276,29 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
276276
}
277277

278278
/// Configures the pin to operate as a input pin
279-
pub fn into_input(mut self) -> Pin<P, N, Input> {
280-
self.mode::<Input>();
281-
Pin::new()
279+
pub fn into_input(self) -> Pin<P, N, Input> {
280+
self.into_mode()
282281
}
283282

284283
/// Configures the pin to operate as a floating input pin
285-
pub fn into_floating_input(mut self) -> Pin<P, N, Input> {
286-
self.mode::<Input>();
287-
Pin::new()._internal_resistor(Pull::None)
284+
pub fn into_floating_input(self) -> Pin<P, N, Input> {
285+
self.into_mode()._internal_resistor(Pull::None)
288286
}
289287

290288
/// Configures the pin to operate as a pulled down input pin
291-
pub fn into_pull_down_input(mut self) -> Pin<P, N, Input> {
292-
self.mode::<Input>();
293-
Pin::new()._internal_resistor(Pull::Down)
289+
pub fn into_pull_down_input(self) -> Pin<P, N, Input> {
290+
self.into_mode()._internal_resistor(Pull::Down)
294291
}
295292

296293
/// Configures the pin to operate as a pulled up input pin
297-
pub fn into_pull_up_input(mut self) -> Pin<P, N, Input> {
298-
self.mode::<Input>();
299-
Pin::new()._internal_resistor(Pull::Up)
294+
pub fn into_pull_up_input(self) -> Pin<P, N, Input> {
295+
self.into_mode()._internal_resistor(Pull::Up)
300296
}
301297

302298
/// Configures the pin to operate as an open drain output pin
303299
/// Initial state will be low.
304-
pub fn into_open_drain_output(mut self) -> Pin<P, N, Output<OpenDrain>> {
305-
self.mode::<Output<OpenDrain>>();
306-
Pin::new()
300+
pub fn into_open_drain_output(self) -> Pin<P, N, Output<OpenDrain>> {
301+
self.into_mode()
307302
}
308303

309304
/// Configures the pin to operate as an open-drain output pin.
@@ -313,16 +308,14 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
313308
initial_state: PinState,
314309
) -> Pin<P, N, Output<OpenDrain>> {
315310
self._set_state(initial_state);
316-
self.mode::<Output<OpenDrain>>();
317-
Pin::new()
311+
self.into_mode()
318312
}
319313

320314
/// Configures the pin to operate as an push pull output pin
321315
/// Initial state will be low.
322316
pub fn into_push_pull_output(mut self) -> Pin<P, N, Output<PushPull>> {
323317
self._set_low();
324-
self.mode::<Output<PushPull>>();
325-
Pin::new()
318+
self.into_mode()
326319
}
327320

328321
/// Configures the pin to operate as an push-pull output pin.
@@ -332,14 +325,12 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
332325
initial_state: PinState,
333326
) -> Pin<P, N, Output<PushPull>> {
334327
self._set_state(initial_state);
335-
self.mode::<Output<PushPull>>();
336-
Pin::new()
328+
self.into_mode()
337329
}
338330

339331
/// Configures the pin to operate as an analog input pin
340-
pub fn into_analog(mut self) -> Pin<P, N, Analog> {
341-
self.mode::<Analog>();
342-
Pin::new()
332+
pub fn into_analog(self) -> Pin<P, N, Analog> {
333+
self.into_mode()
343334
}
344335

345336
/// Configures the pin as a pin that can change between input
@@ -375,6 +366,12 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
375366
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (M::MODER << offset)));
376367
}
377368
}
369+
370+
#[inline(always)]
371+
pub(super) fn into_mode<M: PinMode>(mut self) -> Pin<P, N, M> {
372+
self.mode::<M>();
373+
Pin::new()
374+
}
378375
}
379376

380377
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
@@ -401,7 +398,7 @@ where
401398
///
402399
/// The closure `f` is called with the reconfigured pin. After it returns,
403400
/// the pin will be configured back.
404-
pub fn with_floating_input<R>(&mut self, f: impl FnOnce(&mut Pin<P, N, Input>) -> R) -> R {
401+
pub fn with_input<R>(&mut self, f: impl FnOnce(&mut Pin<P, N, Input>) -> R) -> R {
405402
self.with_mode(f)
406403
}
407404

0 commit comments

Comments
 (0)