Skip to content

Commit 8e55631

Browse files
committed
QDEC refactor and addition of Pins
1 parent 0589cc9 commit 8e55631

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

examples/qdec-demo/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ const APP: () = {
2525
rtt_init_print!();
2626

2727
let p0 = hal::gpio::p0::Parts::new(ctx.device.P0);
28-
let pin_a = p0.p0_31.into_pullup_input().degrade();
29-
let pin_b = p0.p0_30.into_pullup_input().degrade();
28+
let pins = Pins {
29+
a: p0.p0_31.into_pullup_input().degrade(),
30+
b: p0.p0_30.into_pullup_input().degrade(),
31+
led: None,
32+
};
3033

31-
let qdec = Qdec::new(ctx.device.QDEC, pin_a, pin_b, None, SamplePeriod::_128us);
34+
let qdec = Qdec::new(ctx.device.QDEC, pins, SamplePeriod::_128us);
3235
qdec.debounce(true)
3336
.enable_interrupt(NumSamples::_1smpl)
3437
.enable();

nrf-hal-common/src/qdec.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@ pub struct Qdec {
1515

1616
impl Qdec {
1717
/// Takes ownership of the `QDEC` peripheral and associated pins, returning a safe wrapper.
18-
pub fn new(
19-
qdec: QDEC,
20-
pin_a: Pin<Input<PullUp>>,
21-
pin_b: Pin<Input<PullUp>>,
22-
pin_led: Option<Pin<Input<PullUp>>>,
23-
sample_period: SamplePeriod,
24-
) -> Self {
18+
pub fn new(qdec: QDEC, pins: Pins, sample_period: SamplePeriod) -> Self {
2519
qdec.psel.a.write(|w| {
26-
unsafe { w.bits(pin_a.psel_bits()) };
20+
unsafe { w.bits(pins.a.psel_bits()) };
2721
w.connect().connected()
2822
});
2923
qdec.psel.b.write(|w| {
30-
unsafe { w.bits(pin_b.psel_bits()) };
24+
unsafe { w.bits(pins.b.psel_bits()) };
3125
w.connect().connected()
3226
});
3327

34-
if let Some(p) = &pin_led {
28+
if let Some(p) = &pins.led {
3529
qdec.psel.led.write(|w| {
3630
unsafe { w.bits(p.psel_bits()) };
3731
w.connect().connected()
@@ -139,17 +133,10 @@ impl Qdec {
139133

140134
/// Consumes `self` and returns back the raw `QDEC` peripheral.
141135
#[inline]
142-
pub fn free(
143-
self,
144-
) -> (
145-
QDEC,
146-
Pin<Input<PullUp>>,
147-
Pin<Input<PullUp>>,
148-
Option<Pin<Input<PullUp>>>,
149-
) {
150-
let pin_a = unsafe { Pin::from_psel_bits(self.qdec.psel.a.read().bits()) };
151-
let pin_b = unsafe { Pin::from_psel_bits(self.qdec.psel.b.read().bits()) };
152-
let pin_led = {
136+
pub fn free(self) -> (QDEC, Pins) {
137+
let a = unsafe { Pin::from_psel_bits(self.qdec.psel.a.read().bits()) };
138+
let b = unsafe { Pin::from_psel_bits(self.qdec.psel.b.read().bits()) };
139+
let led = {
153140
let led = self.qdec.psel.led.read();
154141
if led.connect().bit_is_set() {
155142
Some(unsafe { Pin::from_psel_bits(led.bits()) })
@@ -161,10 +148,17 @@ impl Qdec {
161148
self.qdec.psel.b.reset();
162149
self.qdec.psel.led.reset();
163150

164-
(self.qdec, pin_a, pin_b, pin_led)
151+
(self.qdec, Pins { a, b, led })
165152
}
166153
}
167154

155+
/// Pins for the QDEC
156+
pub struct Pins {
157+
pub a: Pin<Input<PullUp>>,
158+
pub b: Pin<Input<PullUp>>,
159+
pub led: Option<Pin<Input<PullUp>>>,
160+
}
161+
168162
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
169163
pub enum SamplePeriod {
170164
_128us,

0 commit comments

Comments
 (0)