Skip to content

Commit 7c32240

Browse files
committed
Introduce Board struct
Introduce a Board struct similar to those in other nrf-rs board support crates. This makes for a much nicer API and allows for pretty much all of the feature switching to be removed from the examples.
1 parent bb76d1f commit 7c32240

File tree

19 files changed

+612
-295
lines changed

19 files changed

+612
-295
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818
APIs to be more aligned with each other.
1919
- Add BLE Beacon demo.
2020
- Add a simple speaker demo for micro:bit V2.
21+
- Add Board struct following the pattern used in other nrf board support crates.
2122

2223
## [0.10.1] - 2021-05-25
2324

examples/display-blocking/src/main.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,16 @@ use panic_halt as _;
77
use cortex_m_rt::entry;
88

99
use microbit::{
10-
display_pins,
11-
hal::{gpio::p0::Parts as P0Parts, prelude::*, Timer},
10+
board::Board,
11+
display::blocking::Display,
12+
hal::{prelude::*, Timer},
1213
};
1314

14-
#[cfg(feature = "v2")]
15-
use microbit::hal::gpio::p1::Parts as P1Parts;
16-
17-
use microbit::display::blocking::Display;
18-
1915
#[entry]
2016
fn main() -> ! {
21-
if let Some(p) = microbit::pac::Peripherals::take() {
22-
let mut timer = Timer::new(p.TIMER0);
23-
24-
// Set up pins
25-
#[cfg(feature = "v1")]
26-
let pins = {
27-
let p0parts = P0Parts::new(p.GPIO);
28-
display_pins!(p0parts)
29-
};
30-
31-
#[cfg(feature = "v2")]
32-
let pins = {
33-
let p0parts = P0Parts::new(p.P0);
34-
let p1parts = P1Parts::new(p.P1);
35-
display_pins!(p0parts, p1parts)
36-
};
37-
38-
// Display
39-
let mut display = Display::new(pins);
17+
if let Some(board) = Board::take() {
18+
let mut timer = Timer::new(board.TIMER0);
19+
let mut display = Display::new(board.display_pins);
4020

4121
#[allow(non_snake_case)]
4222
let letter_I = [

examples/display-nonblocking/src/main.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ use cortex_m::peripheral::Peripherals;
1010
use cortex_m_rt::entry;
1111

1212
use microbit::{
13+
board::Board,
1314
display::nonblocking::{Display, GreyscaleImage},
14-
display_pins,
1515
hal::{
16-
gpio::p0::Parts as P0Parts,
16+
clocks::Clocks,
1717
rtc::{Rtc, RtcInterrupt},
1818
},
1919
pac::{self, interrupt, RTC0, TIMER1},
2020
};
2121

22-
#[cfg(feature = "v2")]
23-
use microbit::hal::gpio::p1::Parts as P1Parts;
24-
2522
fn heart_image(inner_brightness: u8) -> GreyscaleImage {
2623
let b = inner_brightness;
2724
GreyscaleImage::new(&[
@@ -41,34 +38,19 @@ static ANIM_TIMER: Mutex<RefCell<Option<Rtc<RTC0>>>> = Mutex::new(RefCell::new(N
4138

4239
#[entry]
4340
fn main() -> ! {
44-
if let Some(p) = pac::Peripherals::take() {
41+
if let Some(board) = Board::take() {
4542
// Starting the low-frequency clock (needed for RTC to work)
46-
p.CLOCK.tasks_lfclkstart.write(|w| unsafe { w.bits(1) });
47-
while p.CLOCK.events_lfclkstarted.read().bits() == 0 {}
48-
p.CLOCK.events_lfclkstarted.reset();
43+
Clocks::new(board.CLOCK).start_lfclk();
4944

5045
// RTC at 16Hz (32_768 / (2047 + 1))
5146
// 62.5ms period
52-
let mut rtc0 = Rtc::new(p.RTC0, 2047).unwrap();
47+
let mut rtc0 = Rtc::new(board.RTC0, 2047).unwrap();
5348
rtc0.enable_event(RtcInterrupt::Tick);
5449
rtc0.enable_interrupt(RtcInterrupt::Tick, None);
5550
rtc0.enable_counter();
5651

57-
// Set up pins
58-
#[cfg(feature = "v1")]
59-
let pins = {
60-
let p0parts = P0Parts::new(p.GPIO);
61-
display_pins!(p0parts)
62-
};
63-
64-
#[cfg(feature = "v2")]
65-
let pins = {
66-
let p0parts = P0Parts::new(p.P0);
67-
let p1parts = P1Parts::new(p.P1);
68-
display_pins!(p0parts, p1parts)
69-
};
70-
71-
let display = Display::new(p.TIMER1, pins);
52+
// Create display
53+
let display = Display::new(board.TIMER1, board.display_pins);
7254

7355
cortex_m::interrupt::free(move |cs| {
7456
*DISPLAY.borrow(cs).borrow_mut() = Some(display);

examples/display-rtic/src/main.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ use defmt_rtt as _;
1111
use panic_halt as _;
1212

1313
use microbit::{
14+
board::Board,
1415
display::nonblocking::{Display, GreyscaleImage},
15-
display_pins,
1616
hal::{
17-
gpio::p0::Parts as P0Parts,
17+
clocks::Clocks,
1818
rtc::{Rtc, RtcInterrupt},
1919
},
2020
pac,
2121
};
2222

23-
#[cfg(feature = "v2")]
24-
use microbit::hal::gpio::p1::Parts as P1Parts;
25-
2623
use rtic::app;
2724

2825
fn heart_image(inner_brightness: u8) -> GreyscaleImage {
@@ -36,45 +33,28 @@ fn heart_image(inner_brightness: u8) -> GreyscaleImage {
3633
])
3734
}
3835

39-
#[app(device = microbit::pac, peripherals = true)]
36+
#[app(device = microbit::pac, peripherals = false)]
4037
const APP: () = {
4138
struct Resources {
4239
display: Display<pac::TIMER1>,
4340
anim_timer: Rtc<pac::RTC0>,
4441
}
4542

4643
#[init]
47-
fn init(cx: init::Context) -> init::LateResources {
48-
let p: pac::Peripherals = cx.device;
44+
fn init(_cx: init::Context) -> init::LateResources {
45+
let board = Board::take().unwrap();
4946

5047
// Starting the low-frequency clock (needed for RTC to work)
51-
p.CLOCK.tasks_lfclkstart.write(|w| unsafe { w.bits(1) });
52-
53-
while p.CLOCK.events_lfclkstarted.read().bits() == 0 {}
54-
p.CLOCK.events_lfclkstarted.reset();
48+
Clocks::new(board.CLOCK).start_lfclk();
5549

5650
// RTC at 16Hz (32_768 / (2047 + 1))
5751
// 16Hz; 62.5ms period
58-
let mut rtc0 = Rtc::new(p.RTC0, 2047).unwrap();
52+
let mut rtc0 = Rtc::new(board.RTC0, 2047).unwrap();
5953
rtc0.enable_event(RtcInterrupt::Tick);
6054
rtc0.enable_interrupt(RtcInterrupt::Tick, None);
6155
rtc0.enable_counter();
6256

63-
// Set up pins
64-
#[cfg(feature = "v1")]
65-
let pins = {
66-
let p0parts = P0Parts::new(p.GPIO);
67-
display_pins!(p0parts)
68-
};
69-
70-
#[cfg(feature = "v2")]
71-
let pins = {
72-
let p0parts = P0Parts::new(p.P0);
73-
let p1parts = P1Parts::new(p.P1);
74-
display_pins!(p0parts, p1parts)
75-
};
76-
77-
let display = Display::new(p.TIMER1, pins);
57+
let display = Display::new(board.TIMER1, board.display_pins);
7858

7959
init::LateResources {
8060
anim_timer: rtc0,

examples/gpio-hal-blinky/src/main.rs

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,21 @@ use panic_halt as _;
55

66
use cortex_m_rt::entry;
77
use embedded_hal::{blocking::delay::DelayMs, digital::v2::OutputPin};
8-
use microbit::hal::{
9-
gpio::{p0, Level},
10-
timer::Timer,
11-
};
8+
use microbit::{board::Board, hal::timer::Timer};
129

1310
#[entry]
1411
fn main() -> ! {
15-
if let Some(p) = microbit::Peripherals::take() {
16-
/* Split GPIO pins */
17-
#[cfg(feature = "v1")]
18-
let p0 = p0::Parts::new(p.GPIO);
12+
let mut board = Board::take().unwrap();
1913

20-
#[cfg(feature = "v2")]
21-
let p0 = p0::Parts::new(p.P0);
14+
let mut timer = Timer::new(board.TIMER0);
2215

23-
let mut timer = Timer::new(p.TIMER0);
24-
25-
#[cfg(feature = "v1")]
26-
let mut led = {
27-
let _ = p0.p0_04.into_push_pull_output(Level::Low);
28-
p0.p0_13.into_push_pull_output(Level::Low)
29-
};
30-
31-
#[cfg(feature = "v2")]
32-
let mut led = {
33-
let _ = p0.p0_28.into_push_pull_output(Level::Low);
34-
p0.p0_21.into_push_pull_output(Level::Low)
35-
};
36-
37-
loop {
38-
let _ = led.set_low();
39-
timer.delay_ms(1_000_u16);
40-
let _ = led.set_high();
41-
timer.delay_ms(1_000_u16);
42-
}
43-
}
16+
let _ = board.display_pins.col1.set_low();
17+
let mut row1 = board.display_pins.row1;
4418

4519
loop {
46-
continue;
20+
let _ = row1.set_low();
21+
timer.delay_ms(1_000_u16);
22+
let _ = row1.set_high();
23+
timer.delay_ms(1_000_u16);
4724
}
4825
}

examples/gpio-hal-ledbutton/src/main.rs

Lines changed: 15 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,28 @@ use defmt_rtt as _;
55
use panic_halt as _;
66

77
use cortex_m_rt::entry;
8-
use microbit::hal::{
9-
gpio::{p0, Level},
10-
prelude::*,
11-
};
8+
use microbit::{board::Board, hal::prelude::*};
129

1310
#[entry]
1411
fn main() -> ! {
15-
if let Some(p) = microbit::Peripherals::take() {
16-
// Split GPIO pins
17-
#[cfg(feature = "v1")]
18-
let p0 = p0::Parts::new(p.GPIO);
12+
let mut board = Board::take().unwrap();
1913

20-
#[cfg(feature = "v2")]
21-
let p0 = p0::Parts::new(p.P0);
14+
board.display_pins.row1.set_high().unwrap();
2215

23-
// Set row of LED matrix to permanent high
24-
#[cfg(feature = "v1")]
25-
let _ = p0.p0_13.into_push_pull_output(Level::Low).set_high();
16+
let mut led1 = board.display_pins.col1;
17+
let mut led2 = board.display_pins.col2;
2618

27-
#[cfg(feature = "v2")]
28-
let _ = p0.p0_21.into_push_pull_output(Level::Low).set_high();
29-
30-
// Set 2 columns to output to control LED states
31-
#[cfg(feature = "v1")]
32-
let (mut led1, mut led2) = {
33-
(
34-
p0.p0_04.into_push_pull_output(Level::Low),
35-
p0.p0_06.into_push_pull_output(Level::Low),
36-
)
37-
};
38-
39-
#[cfg(feature = "v2")]
40-
let (mut led1, mut led2) = {
41-
(
42-
p0.p0_28.into_push_pull_output(Level::Low),
43-
p0.p0_11.into_push_pull_output(Level::Low),
44-
)
45-
};
46-
47-
// Configure button GPIOs as inputs
48-
#[cfg(feature = "v1")]
49-
let (button_a, button_b) = {
50-
(
51-
p0.p0_17.into_floating_input(),
52-
p0.p0_26.into_floating_input(),
53-
)
54-
};
55-
56-
#[cfg(feature = "v2")]
57-
let (button_a, button_b) = {
58-
(
59-
p0.p0_14.into_floating_input(),
60-
p0.p0_23.into_floating_input(),
61-
)
62-
};
63-
64-
loop {
65-
if let Ok(true) = button_a.is_high() {
66-
let _ = led1.set_high();
67-
} else {
68-
let _ = led1.set_low();
69-
}
70-
71-
if let Ok(true) = button_b.is_high() {
72-
let _ = led2.set_high();
73-
} else {
74-
let _ = led2.set_low();
75-
}
19+
loop {
20+
if let Ok(true) = board.buttons.button_a.is_high() {
21+
let _ = led1.set_high();
22+
} else {
23+
let _ = led1.set_low();
7624
}
77-
}
7825

79-
loop {
80-
continue;
26+
if let Ok(true) = board.buttons.button_b.is_high() {
27+
let _ = led2.set_high();
28+
} else {
29+
let _ = led2.set_low();
30+
}
8131
}
8232
}

examples/gpio-hal-printbuttons/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ defmt = "0.2.0"
1212

1313
[dependencies.microbit]
1414
path = "../../microbit"
15+
optional = true
16+
17+
[dependencies.microbit-v2]
18+
path = "../../microbit-v2"
19+
optional = true
1520

1621
[features]
22+
v1 = ["microbit"]
23+
v2 = ["microbit-v2"]
24+
1725
default = [
1826
"defmt-default",
1927
]

0 commit comments

Comments
 (0)