Skip to content

Commit 5531237

Browse files
committed
Move led pins into separate struct
The main goal here is to make it easier to support the v2 board which has completely different pin layout as the interface will still be similar. As an added benefit it makes the constructor for `led::Display` a bit clearer as rather than a long list of positional arguments you effectively provide named arguments. This is a pattern I've seen used in the nrf-hal.
1 parent c696a0b commit 5531237

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

examples/led_blocking.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ fn main() -> ! {
2121
let p0parts = P0Parts::new(p.GPIO);
2222

2323
// Display
24-
let row1 = p0parts.p0_13.into_push_pull_output(Level::Low);
25-
let row2 = p0parts.p0_14.into_push_pull_output(Level::Low);
26-
let row3 = p0parts.p0_15.into_push_pull_output(Level::Low);
27-
let col1 = p0parts.p0_04.into_push_pull_output(Level::Low);
28-
let col2 = p0parts.p0_05.into_push_pull_output(Level::Low);
29-
let col3 = p0parts.p0_06.into_push_pull_output(Level::Low);
30-
let col4 = p0parts.p0_07.into_push_pull_output(Level::Low);
31-
let col5 = p0parts.p0_08.into_push_pull_output(Level::Low);
32-
let col6 = p0parts.p0_09.into_push_pull_output(Level::Low);
33-
let col7 = p0parts.p0_10.into_push_pull_output(Level::Low);
34-
let col8 = p0parts.p0_11.into_push_pull_output(Level::Low);
35-
let col9 = p0parts.p0_12.into_push_pull_output(Level::Low);
36-
let mut leds = led::Display::new(
37-
col1, col2, col3, col4, col5, col6, col7, col8, col9, row1, row2, row3,
38-
);
24+
let pins = led::Pins {
25+
row1: p0parts.p0_13.into_push_pull_output(Level::Low),
26+
row2: p0parts.p0_14.into_push_pull_output(Level::Low),
27+
row3: p0parts.p0_15.into_push_pull_output(Level::Low),
28+
col1: p0parts.p0_04.into_push_pull_output(Level::Low),
29+
col2: p0parts.p0_05.into_push_pull_output(Level::Low),
30+
col3: p0parts.p0_06.into_push_pull_output(Level::Low),
31+
col4: p0parts.p0_07.into_push_pull_output(Level::Low),
32+
col5: p0parts.p0_08.into_push_pull_output(Level::Low),
33+
col6: p0parts.p0_09.into_push_pull_output(Level::Low),
34+
col7: p0parts.p0_10.into_push_pull_output(Level::Low),
35+
col8: p0parts.p0_11.into_push_pull_output(Level::Low),
36+
col9: p0parts.p0_12.into_push_pull_output(Level::Low),
37+
};
38+
let mut leds = led::Display::new(pins);
3939

4040
#[allow(non_snake_case)]
4141
let letter_I = [

src/led.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
//! On-board user LEDs
22
33
use crate::hal::{
4-
gpio::{p0, Output, Pin, PushPull},
4+
gpio::{Output, Pin, PushPull},
55
prelude::*,
66
};
77

8+
use crate::gpio::{COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, ROW1, ROW2, ROW3};
9+
810
use embedded_hal::blocking::delay::DelayUs;
911

1012
#[allow(clippy::upper_case_acronyms)]
11-
type LED = Pin<Output<PushPull>>;
13+
pub(crate) type LED = Pin<Output<PushPull>>;
1214

1315
const DEFAULT_DELAY_MS: u32 = 2;
1416
const LED_LAYOUT: [[(usize, usize); 5]; 5] = [
@@ -19,6 +21,21 @@ const LED_LAYOUT: [[(usize, usize); 5]; 5] = [
1921
[(2, 2), (1, 6), (2, 0), (1, 5), (2, 1)],
2022
];
2123

24+
pub struct Pins {
25+
pub col1: COL1,
26+
pub col2: COL2,
27+
pub col3: COL3,
28+
pub col4: COL4,
29+
pub col5: COL5,
30+
pub col6: COL6,
31+
pub col7: COL7,
32+
pub col8: COL8,
33+
pub col9: COL9,
34+
pub row1: ROW1,
35+
pub row2: ROW2,
36+
pub row3: ROW3,
37+
}
38+
2239
/// Array of all the LEDs in the 5x5 display on the board
2340
pub struct Display {
2441
delay_ms: u32,
@@ -29,33 +46,24 @@ pub struct Display {
2946
impl Display {
3047
/// Initializes all the user LEDs
3148
#[allow(clippy::too_many_arguments)]
32-
pub fn new(
33-
col1: p0::P0_04<Output<PushPull>>,
34-
col2: p0::P0_05<Output<PushPull>>,
35-
col3: p0::P0_06<Output<PushPull>>,
36-
col4: p0::P0_07<Output<PushPull>>,
37-
col5: p0::P0_08<Output<PushPull>>,
38-
col6: p0::P0_09<Output<PushPull>>,
39-
col7: p0::P0_10<Output<PushPull>>,
40-
col8: p0::P0_11<Output<PushPull>>,
41-
col9: p0::P0_12<Output<PushPull>>,
42-
row1: p0::P0_13<Output<PushPull>>,
43-
row2: p0::P0_14<Output<PushPull>>,
44-
row3: p0::P0_15<Output<PushPull>>,
45-
) -> Self {
49+
pub fn new(pins: Pins) -> Self {
4650
let mut retval = Display {
4751
delay_ms: DEFAULT_DELAY_MS,
48-
rows: [row1.degrade(), row2.degrade(), row3.degrade()],
52+
rows: [
53+
pins.row1.degrade(),
54+
pins.row2.degrade(),
55+
pins.row3.degrade(),
56+
],
4957
cols: [
50-
col1.degrade(),
51-
col2.degrade(),
52-
col3.degrade(),
53-
col4.degrade(),
54-
col5.degrade(),
55-
col6.degrade(),
56-
col7.degrade(),
57-
col8.degrade(),
58-
col9.degrade(),
58+
pins.col1.degrade(),
59+
pins.col2.degrade(),
60+
pins.col3.degrade(),
61+
pins.col4.degrade(),
62+
pins.col5.degrade(),
63+
pins.col6.degrade(),
64+
pins.col7.degrade(),
65+
pins.col8.degrade(),
66+
pins.col9.degrade(),
5967
],
6068
};
6169
// This is needed to reduce flickering on reset

0 commit comments

Comments
 (0)