/
rgb_leds.rs
112 lines (97 loc) · 3.1 KB
/
rgb_leds.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! # Rainbow Example for Cytron Maker Pi rp2040
//!
//! Runs a rainbow-effect colour wheel on the on-board LEDs.
//!
//! Uses the `ws2812_pio` driver to control the LEDs, which in turns uses the
//! RP2040's PIO block.
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use embedded_hal::timer::CountDown;
use fugit::ExtU32;
use panic_halt as _;
// Make an alias for our board support package so copying examples to other boards is easier
use cytron_maker_pi_rp2040 as bsp;
use bsp::hal::{
clocks::{init_clocks_and_plls, Clock},
gpio::{FunctionPio0, Pin},
pac,
sio::Sio,
timer::Timer,
watchdog::Watchdog,
};
use rp2040_hal::pio::PIOExt;
use smart_leds::{brightness, SmartLedsWrite, RGB8};
use ws2812_pio::Ws2812;
/// 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.
#[entry]
fn main() -> ! {
// Configure the RP2040 peripherals
let mut pac = pac::Peripherals::take().unwrap();
let mut watchdog = Watchdog::new(pac.WATCHDOG);
let clocks = init_clocks_and_plls(
bsp::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 = bsp::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
let smartleds_pin: Pin<_, FunctionPio0, _> = pins.smartleds.into_function();
let timer = Timer::new(pac.TIMER, &mut pac.RESETS, &clocks);
let mut delay = timer.count_down();
// Configure the addressable LED
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let mut ws = Ws2812::new(
smartleds_pin,
&mut pio,
sm0,
clocks.peripheral_clock.freq(),
timer.count_down(),
);
// Infinite colour wheel loop
let mut n: u8 = 128;
loop {
// Put the second LED approximately 1/3 ahead on the color wheel
let second_led_n = n.wrapping_add(85);
let leds = [wheel(n), wheel(second_led_n)];
ws.write(brightness(leds.iter().cloned(), 32)).unwrap();
n = n.wrapping_add(1);
delay.start(25.millis());
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()
}
}