rp-hal-boards/boards/itsy_bitsy_rp2040/examples/itsy_bitsy_rainbow.rs
9names 0e7abdc705
Make BSPs provide boot2 as a default feature (#153)
* Add boot2 feature. Add boot2 linkage into each BSP optional on feature

* Enable boot2 feature in BSPs by default. Remove boot2 decl from all BSP examples

* Add EXTERN in memory.x for BOOT2_FIRMWARE, rename bootloader static slice to BOOT2_FIRMWARE

* Update new examples and itsy_bitsy BSP to use boot2 feature

* Remove boot2 as a dev-dependency for the BSPs, no longer needed

* Add no_mangle BOOT2_FIRMWARE to adafruit_macropad

* Fix itsy-bitsy blinky - it wasn't using the BSP, so it didn't get BOOT2_FIRMWARE linked in
2021-12-05 01:00:09 +11:00

98 lines
2.4 KiB
Rust

//! Continuously changes the color of the Neopixel on a Adafruit ItsyBitsy RP2040 board
#![no_std]
#![no_main]
use core::iter::once;
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::timer::CountDown;
use embedded_time::duration::Extensions;
use panic_halt as _;
use rp2040_hal::pio::PIOExt;
use smart_leds::{brightness, SmartLedsWrite, RGB8};
use ws2812_pio::Ws2812;
use itsy_bitsy_rp2040::{
hal::{
clocks::{init_clocks_and_plls, Clock},
pac,
sio::Sio,
watchdog::Watchdog,
},
Pins, XOSC_CRYSTAL_FREQ,
};
#[entry]
fn main() -> ! {
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);
let pins = Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
let led = pins.neopixel_data.into_mode();
pins.neopixel_power
.into_push_pull_output()
.set_high()
.unwrap();
let timer = rp2040_hal::timer::Timer::new(pac.TIMER, &mut pac.RESETS);
let mut delay = timer.count_down();
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let mut ws = Ws2812::new(
led,
&mut pio,
sm0,
clocks.peripheral_clock.freq(),
timer.count_down(),
);
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());
}
}
/// 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.
fn wheel(mut wheel_pos: u8) -> RGB8 {
wheel_pos = 255 - wheel_pos;
if wheel_pos < 85 {
// 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
wheel_pos -= 85;
(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()
}
}