2021-09-20 23:33:46 +10:00
|
|
|
//! # Rainbow Example for the Pro Micro RP2040
|
|
|
|
//!
|
|
|
|
//! Runs a rainbow-effect colour wheel on the on-board LED.
|
|
|
|
//!
|
|
|
|
//! Uses the `ws2812_pio` driver to control the LED, which in turns uses the
|
|
|
|
//! RP2040's PIO block.
|
|
|
|
|
2021-09-16 16:16:20 +10:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
|
|
|
use core::iter::once;
|
|
|
|
use cortex_m_rt::entry;
|
|
|
|
use embedded_hal::timer::CountDown;
|
|
|
|
use embedded_time::duration::Extensions;
|
|
|
|
use panic_halt as _;
|
|
|
|
|
|
|
|
use pro_micro_rp2040::{
|
|
|
|
hal::{
|
|
|
|
clocks::{init_clocks_and_plls, Clock},
|
|
|
|
gpio::{FunctionPio0, Pin},
|
|
|
|
pac,
|
|
|
|
sio::Sio,
|
|
|
|
timer::Timer,
|
|
|
|
watchdog::Watchdog,
|
|
|
|
},
|
|
|
|
XOSC_CRYSTAL_FREQ,
|
|
|
|
};
|
|
|
|
use smart_leds::{brightness, SmartLedsWrite, RGB8};
|
|
|
|
use ws2812_pio::Ws2812;
|
|
|
|
|
2021-09-28 02:30:50 +10:00
|
|
|
//// The linker will place this boot block at the start of our program image.
|
2021-09-20 23:33:46 +10:00
|
|
|
/// We need this to help the ROM bootloader get our code up and running.
|
2021-09-16 16:16:20 +10:00
|
|
|
#[link_section = ".boot2"]
|
|
|
|
#[used]
|
|
|
|
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER;
|
|
|
|
|
2021-09-20 23:33:46 +10:00
|
|
|
/// Entry point to our bare-metal application.
|
|
|
|
///
|
|
|
|
/// The `#[entry]` macro ensures the Cortex-M start-up code calls this
|
|
|
|
/// function as soon as all global variables are initialised.
|
|
|
|
///
|
|
|
|
/// The function configures the RP2040 peripherals, then the LED, then runs
|
|
|
|
/// the colour wheel in an infinite loop.
|
2021-09-16 16:16:20 +10:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
2021-09-20 23:33:46 +10:00
|
|
|
// Configure the RP2040 peripherals
|
|
|
|
|
2021-09-16 16:16:20 +10:00
|
|
|
let mut pac = pac::Peripherals::take().unwrap();
|
|
|
|
let mut watchdog = Watchdog::new(pac.WATCHDOG);
|
|
|
|
|
|
|
|
let clocks = init_clocks_and_plls(
|
|
|
|
XOSC_CRYSTAL_FREQ,
|
|
|
|
pac.XOSC,
|
|
|
|
pac.CLOCKS,
|
|
|
|
pac.PLL_SYS,
|
|
|
|
pac.PLL_USB,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
&mut watchdog,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let sio = Sio::new(pac.SIO);
|
2021-09-20 23:33:46 +10:00
|
|
|
|
2021-09-16 16:16:20 +10:00
|
|
|
let pins = pro_micro_rp2040::Pins::new(
|
|
|
|
pac.IO_BANK0,
|
|
|
|
pac.PADS_BANK0,
|
|
|
|
sio.gpio_bank0,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
);
|
2021-09-20 23:33:46 +10:00
|
|
|
|
2021-09-16 16:16:20 +10:00
|
|
|
let _led: Pin<_, FunctionPio0> = pins.led.into_mode();
|
2021-09-20 23:33:46 +10:00
|
|
|
|
2021-09-16 16:16:20 +10:00
|
|
|
let timer = Timer::new(pac.TIMER);
|
|
|
|
let mut delay = timer.count_down();
|
|
|
|
|
2021-09-20 23:33:46 +10:00
|
|
|
// Configure the addressable LED
|
|
|
|
|
2021-09-16 16:16:20 +10:00
|
|
|
let mut ws = Ws2812::new(
|
|
|
|
25,
|
|
|
|
pac.PIO0,
|
|
|
|
&mut pac.RESETS,
|
2021-09-28 03:42:36 +10:00
|
|
|
clocks.peripheral_clock.freq(),
|
2021-09-16 16:16:20 +10:00
|
|
|
timer.count_down(),
|
|
|
|
);
|
|
|
|
|
2021-09-20 23:33:46 +10:00
|
|
|
// Infinite colour wheel loop
|
|
|
|
|
2021-09-16 16:16:20 +10:00
|
|
|
let mut n: u8 = 128;
|
|
|
|
loop {
|
|
|
|
ws.write(brightness(once(wheel(n)), 32)).unwrap();
|
|
|
|
n = n.wrapping_add(1);
|
|
|
|
|
|
|
|
delay.start(25.milliseconds());
|
|
|
|
let _ = nb::block!(delay.wait());
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 23:33:46 +10:00
|
|
|
|
|
|
|
/// Convert a number from `0..=255` to an RGB color triplet.
|
|
|
|
///
|
|
|
|
/// The colours are a transition from red, to green, to blue and back to red.
|
2021-09-16 16:16:20 +10:00
|
|
|
fn wheel(mut wheel_pos: u8) -> RGB8 {
|
|
|
|
wheel_pos = 255 - wheel_pos;
|
|
|
|
if wheel_pos < 85 {
|
2021-09-20 23:33:46 +10:00
|
|
|
// No green in this sector - red and blue only
|
|
|
|
(255 - (wheel_pos * 3), 0, wheel_pos * 3).into()
|
|
|
|
} else if wheel_pos < 170 {
|
|
|
|
// No red in this sector - green and blue only
|
2021-09-16 16:16:20 +10:00
|
|
|
wheel_pos -= 85;
|
2021-09-20 23:33:46 +10:00
|
|
|
(0, wheel_pos * 3, 255 - (wheel_pos * 3)).into()
|
|
|
|
} else {
|
|
|
|
// No blue in this sector - red and green only
|
|
|
|
wheel_pos -= 170;
|
|
|
|
(wheel_pos * 3, 255 - (wheel_pos * 3), 0).into()
|
2021-09-16 16:16:20 +10:00
|
|
|
}
|
|
|
|
}
|