LED roulette
Alright, let's build a "real" application. The goal is to get to this display of spinning lights:
Since working with the LED pins separately is quite annoying (especially if you have to use
basically all of them like here) you can use the microbit-v2
BSP crate, discussed previously, to
work with the MB2's LED "display". It works like this (examples/light-it-all.rs
):
#![no_main] #![no_std] use cortex_m_rt::entry; use embedded_hal::delay::DelayNs; use microbit::{board::Board, display::blocking::Display, hal::Timer}; use panic_rtt_target as _; use rtt_target::rtt_init_print; #[entry] fn main() -> ! { rtt_init_print!(); let board = Board::take().unwrap(); let mut timer = Timer::new(board.TIMER0); let mut display = Display::new(board.display_pins); let light_it_all = [ [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], ]; loop { // Show light_it_all for 1000ms display.show(&mut timer, light_it_all, 1000); // clear the display again display.clear(); timer.delay_ms(1000_u32); } }
The Rust array light_it_all
shown in the example contains 1 where the LED is on and 0 where it is
off. The call to show()
takes a timer for the BSP display code to use for delaying, a copy of
the array, and a length of time in milliseconds to show this display before returning.