diff --git a/halloween-leds/src/main.rs b/halloween-leds/src/main.rs index 5dea472..5cd0be2 100644 --- a/halloween-leds/src/main.rs +++ b/halloween-leds/src/main.rs @@ -1,8 +1,8 @@ #![no_main] #![no_std] -use embedded_hal::{delay::DelayNs, digital::OutputPin}; use embedded_hal::spi::SpiBus; +use embedded_hal::{delay::DelayNs, digital::OutputPin}; use panic_halt as _; use rp_pico::hal::gpio::bank0::Gpio0; use rp_pico::{ @@ -21,6 +21,8 @@ use rp_pico::{ pac, Pins, }; +const FPS: u32 = 60; +const MS_PER_FRAME: u32 = 1000 / FPS; const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // MHz, https://forums.raspberrypi.com/viewtopic.php?t=356764 #[entry] @@ -46,8 +48,6 @@ unsafe fn main() -> ! { .ok() .unwrap(); - // let mut power_light: Pin, PullDown> = pins.gpio0.into_function(); - // power_light.set_high(); let mut timer = Timer::new(peripherals.TIMER, &mut peripherals.RESETS, &clocks); let spi_clk = pins.gpio10.into_function(); @@ -60,44 +60,32 @@ unsafe fn main() -> ! { embedded_hal::spi::MODE_1, ); - let mut lights: [u8; 126] = [0; 126]; - lights[124] = 0xff; - lights[125] = 0xff; - for i in 0..30 { - lights[(i + 1) * 4 + 0] = 0xe0 + 1; - lights[(i + 1) * 4 + 1] = 255; - } + // byte count: 4 for the start frame + // 1 * 4 for three lights with 4 bytes per light + // 4 for the end frame + // = 20 bytes + let mut lights: [u8; 12] = [0; 12]; + lights[8] = 0xff; + lights[9] = 0xff; + lights[10] = 0xff; + lights[11] = 0xff; + + lights[4] = 0xe0 + 1; let mut brightness = 1; let mut step = 1; loop { - if brightness == 255 && step == 1 { + if brightness == 64 && step == 1 { step = -1; } else if brightness == 1 && step == -1 { step = 1; }; - for i in 0..30 { - lights[(i + 1) * 4 + 3] = brightness as u8; - } + lights[5] = brightness as u8; + lights[6] = 255; + // lights[7] = brightness as u8; brightness = brightness + step; - + spi.write(lights.as_slice()); - timer.delay_ms(10); + timer.delay_ms(MS_PER_FRAME); } - - /* - let pins = (pins.gpio0.into_function(), pins.gpio1.into_function()); - - let uart = UartPeripheral::new(peripherals.UART0, pins, &mut peripherals.RESETS) - .enable( - UartConfig::new(9600_u32.Hz(), DataBits::Eight, None, StopBits::One), - clocks.peripheral_clock.freq(), - ) - .unwrap(); - - loop { - uart.write_full_blocking(b"Hello World!\r\n"); - timer.delay_ms(1000); - } - */ }