From 1601d2d8069878d2499c3b14274311ed4aed405a Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 2 Nov 2024 13:17:50 -0400 Subject: [PATCH 1/5] Bare-bones control of the first 30 leds --- Cargo.lock | 302 ++++++++++++++++++++++++++++++ Cargo.toml | 2 +- halloween-leds/.cargo/config.toml | 12 ++ halloween-leds/Cargo.toml | 11 ++ halloween-leds/src/main.rs | 103 ++++++++++ 5 files changed, 429 insertions(+), 1 deletion(-) create mode 100644 halloween-leds/.cargo/config.toml create mode 100644 halloween-leds/Cargo.toml create mode 100644 halloween-leds/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index c7874a2..3eb9f25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -350,6 +350,22 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" +[[package]] +name = "bike" +version = "0.1.0" +dependencies = [ + "az", + "cortex-m", + "cortex-m-rt", + "embedded-alloc", + "embedded-hal 0.2.7", + "fixed", + "fugit", + "lights-core", + "panic-halt 0.2.0", + "rp-pico 0.8.0", +] + [[package]] name = "bindgen" version = "0.69.5" @@ -392,6 +408,18 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" +[[package]] +name = "bitfield" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" + +[[package]] +name = "bitfield" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d7e60934ceec538daadb9d8432424ed043a904d8e0243f3c6446bce549a46ac" + [[package]] name = "bitflags" version = "1.3.2" @@ -731,6 +759,38 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cortex-m" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9" +dependencies = [ + "bare-metal", + "bitfield 0.13.2", + "embedded-hal 0.2.7", + "volatile-register", +] + +[[package]] +name = "cortex-m-rt" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee84e813d593101b1723e13ec38b6ab6abbdbaaa4546553f5395ed274079ddb1" +dependencies = [ + "cortex-m-rt-macros", +] + +[[package]] +name = "cortex-m-rt-macros" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f6f3e36f203cfedbc78b357fb28730aa2c6dc1ab060ee5c2405e843988d3c7" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "cpufeatures" version = "0.2.14" @@ -973,6 +1033,66 @@ dependencies = [ "serde 1.0.210", ] +[[package]] +name = "embedded-alloc" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddae17915accbac2cfbc64ea0ae6e3b330e6ea124ba108dada63646fd3c6f815" +dependencies = [ + "critical-section", + "linked_list_allocator", +] + +[[package]] +name = "embedded-dma" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "994f7e5b5cb23521c22304927195f236813053eb9c065dd2226a32ba64695446" +dependencies = [ + "stable_deref_trait", +] + +[[package]] +name = "embedded-hal" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff" +dependencies = [ + "nb 0.1.3", + "void", +] + +[[package]] +name = "embedded-hal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" + +[[package]] +name = "embedded-hal-async" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4c685bbef7fe13c3c6dd4da26841ed3980ef33e841cddfa15ce8a8fb3f1884" +dependencies = [ + "embedded-hal 1.0.0", +] + +[[package]] +name = "embedded-hal-nb" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605" +dependencies = [ + "embedded-hal 1.0.0", + "nb 1.1.0", +] + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + [[package]] name = "emseries" version = "0.6.0" @@ -1825,6 +1945,26 @@ dependencies = [ "crunchy", ] +[[package]] +name = "halloween-leds" +version = "0.1.0" +dependencies = [ + "cortex-m-rt", + "embedded-hal 1.0.0", + "embedded-io", + "panic-halt 1.0.0", + "rp-pico 0.9.0", +] + +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + [[package]] name = "hashbrown" version = "0.14.5" @@ -1874,6 +2014,16 @@ dependencies = [ "http 0.2.12", ] +[[package]] +name = "heapless" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +dependencies = [ + "hash32", + "stable_deref_trait", +] + [[package]] name = "heck" version = "0.4.1" @@ -2885,6 +3035,18 @@ dependencies = [ "system-deps", ] +[[package]] +name = "panic-halt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812" + +[[package]] +name = "panic-halt" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11" + [[package]] name = "parking" version = "2.2.1" @@ -3587,6 +3749,130 @@ dependencies = [ "thiserror", ] +[[package]] +name = "rp-pico" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6341771e6f8e5d130b2b3cbc23435b7847761adf198af09f4b2a60407d43bd56" +dependencies = [ + "cortex-m-rt", + "fugit", + "rp2040-boot2", + "rp2040-hal 0.9.2", + "usb-device 0.2.9", +] + +[[package]] +name = "rp-pico" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9342d3ac7011ac688300979e9b52a81f0add1d05feb02868cf94bfee0705b28" +dependencies = [ + "cortex-m-rt", + "fugit", + "rp2040-boot2", + "rp2040-hal 0.10.2", + "usb-device 0.3.2", +] + +[[package]] +name = "rp2040-boot2" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c92f344f63f950ee36cf4080050e4dce850839b9175da38f9d2ffb69b4dbb21" +dependencies = [ + "crc-any", +] + +[[package]] +name = "rp2040-hal" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ff2b9ae7e6dd6720fd9f64250c9087260e50fe98b6b032ccca65be3581167ca" +dependencies = [ + "cortex-m", + "critical-section", + "embedded-dma", + "embedded-hal 0.2.7", + "frunk", + "fugit", + "itertools 0.10.5", + "nb 1.1.0", + "paste", + "pio", + "rand_core 0.6.4", + "rp2040-hal-macros", + "rp2040-pac 0.5.0", + "usb-device 0.2.9", + "vcell", + "void", +] + +[[package]] +name = "rp2040-hal" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d11e711940087f2cdff8aeae9f4b902e2014c06a00b39a1092686b81ec973d6f" +dependencies = [ + "bitfield 0.14.0", + "cortex-m", + "critical-section", + "embedded-dma", + "embedded-hal 0.2.7", + "embedded-hal 1.0.0", + "embedded-hal-async", + "embedded-hal-nb", + "embedded-io", + "frunk", + "fugit", + "itertools 0.10.5", + "nb 1.1.0", + "paste", + "pio", + "rand_core 0.6.4", + "rp2040-hal-macros", + "rp2040-pac 0.6.0", + "usb-device 0.3.2", + "vcell", + "void", +] + +[[package]] +name = "rp2040-hal-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86479063e497efe1ae81995ef9071f54fd1c7427e04d6c5b84cde545ff672a5e" +dependencies = [ + "cortex-m-rt", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "rp2040-pac" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12d9d8375815f543f54835d01160d4e47f9e2cae75f17ff8f1ec19ce1da96e4c" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "critical-section", + "vcell", +] + +[[package]] +name = "rp2040-pac" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83cbcd3f7a0ca7bbe61dc4eb7e202842bee4e27b769a7bf3a4a72fa399d6e404" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "critical-section", + "vcell", +] + [[package]] name = "rsa" version = "0.9.6" @@ -4781,6 +5067,22 @@ version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" +[[package]] +name = "usb-device" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f6cc3adc849b5292b4075fc0d5fdcf2f24866e88e336dd27a8943090a520508" + +[[package]] +name = "usb-device" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98816b1accafbb09085168b90f27e93d790b4bfa19d883466b5e53315b5f06a6" +dependencies = [ + "heapless", + "portable-atomic", +] + [[package]] name = "utf-8" version = "0.7.6" diff --git a/Cargo.toml b/Cargo.toml index db1cddf..de2074b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,5 +32,5 @@ members = [ "sgf", "timezone-testing", "tree", - "visions/server", "gm-dash/server", + "visions/server", "gm-dash/server", "halloween-leds" ] diff --git a/halloween-leds/.cargo/config.toml b/halloween-leds/.cargo/config.toml new file mode 100644 index 0000000..fead60a --- /dev/null +++ b/halloween-leds/.cargo/config.toml @@ -0,0 +1,12 @@ +[build] +target = "thumbv6m-none-eabi" + +[target.thumbv6m-none-eabi] +rustflags = [ + "-C", "link-arg=--nmagic", + "-C", "link-arg=-Tlink.x", + "-C", "llvm-args=--inline-threshold=5", + "-C", "no-vectorize-loops", +] + +runner = "elf2uf2-rs -d" diff --git a/halloween-leds/Cargo.toml b/halloween-leds/Cargo.toml new file mode 100644 index 0000000..676ac35 --- /dev/null +++ b/halloween-leds/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "halloween-leds" +version = "0.1.0" +edition = "2021" + +[dependencies] +cortex-m-rt = "0.7.3" +embedded-hal = "1.0.0" +embedded-io = "0.6.1" +panic-halt = "1.0.0" +rp-pico = "0.9.0" diff --git a/halloween-leds/src/main.rs b/halloween-leds/src/main.rs new file mode 100644 index 0000000..5dea472 --- /dev/null +++ b/halloween-leds/src/main.rs @@ -0,0 +1,103 @@ +#![no_main] +#![no_std] + +use embedded_hal::{delay::DelayNs, digital::OutputPin}; +use embedded_hal::spi::SpiBus; +use panic_halt as _; +use rp_pico::hal::gpio::bank0::Gpio0; +use rp_pico::{ + entry, + hal::{ + clocks::init_clocks_and_plls, + fugit::RateExtU32, + gpio::{ + bank0::{Gpio2, Gpio4}, + FunctionSio, Pin, PullDown, SioOutput, + }, + spi::{Enabled, Spi, SpiDevice, ValidSpiPinout}, + uart::{DataBits, StopBits, UartConfig, UartPeripheral}, + Clock, Sio, Timer, Watchdog, + }, + pac, Pins, +}; + +const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // MHz, https://forums.raspberrypi.com/viewtopic.php?t=356764 + +#[entry] +unsafe fn main() -> ! { + let mut peripherals = pac::Peripherals::take().unwrap(); + let sio = Sio::new(peripherals.SIO); + let mut watchdog = Watchdog::new(peripherals.WATCHDOG); + let pins = Pins::new( + peripherals.IO_BANK0, + peripherals.PADS_BANK0, + sio.gpio_bank0, + &mut peripherals.RESETS, + ); + let clocks = init_clocks_and_plls( + XOSC_CRYSTAL_FREQ, + peripherals.XOSC, + peripherals.CLOCKS, + peripherals.PLL_SYS, + peripherals.PLL_USB, + &mut peripherals.RESETS, + &mut watchdog, + ) + .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(); + let spi_sdo = pins.gpio11.into_function(); + let spi = Spi::<_, _, _, 8>::new(peripherals.SPI1, (spi_sdo, spi_clk)); + let mut spi = spi.init( + &mut peripherals.RESETS, + clocks.peripheral_clock.freq(), + 1_u32.MHz(), + 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; + } + + let mut brightness = 1; + let mut step = 1; + loop { + if brightness == 255 && 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; + } + brightness = brightness + step; + + spi.write(lights.as_slice()); + timer.delay_ms(10); + } + + /* + 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); + } + */ +} -- 2.44.1 From 82ec50f5191c25562bf22fe4075fc351decd2a7f Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 3 Nov 2024 18:46:06 -0500 Subject: [PATCH 2/5] Set up properly for a single light --- halloween-leds/src/main.rs | 52 +++++++++++++++----------------------- 1 file changed, 20 insertions(+), 32 deletions(-) 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); - } - */ } -- 2.44.1 From 5ed39f814a0c67873b874c309fbdf847b1d2dea2 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 3 Nov 2024 22:08:52 -0500 Subject: [PATCH 3/5] Remove unused imports --- halloween-leds/src/main.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/halloween-leds/src/main.rs b/halloween-leds/src/main.rs index 5cd0be2..1a5d589 100644 --- a/halloween-leds/src/main.rs +++ b/halloween-leds/src/main.rs @@ -1,21 +1,15 @@ #![no_main] #![no_std] +use embedded_hal::delay::DelayNs; 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::{ entry, hal::{ clocks::init_clocks_and_plls, fugit::RateExtU32, - gpio::{ - bank0::{Gpio2, Gpio4}, - FunctionSio, Pin, PullDown, SioOutput, - }, - spi::{Enabled, Spi, SpiDevice, ValidSpiPinout}, - uart::{DataBits, StopBits, UartConfig, UartPeripheral}, + spi::Spi, Clock, Sio, Timer, Watchdog, }, pac, Pins, @@ -85,7 +79,7 @@ unsafe fn main() -> ! { // lights[7] = brightness as u8; brightness = brightness + step; - spi.write(lights.as_slice()); + let _ = spi.write(lights.as_slice()); timer.delay_ms(MS_PER_FRAME); } } -- 2.44.1 From 99573ff7cfac809e5c5f230fa492d296d2a0605e Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 3 Nov 2024 22:35:50 -0500 Subject: [PATCH 4/5] Add extensive explanation of the code. --- halloween-leds/src/main.rs | 58 ++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/halloween-leds/src/main.rs b/halloween-leds/src/main.rs index 1a5d589..9b6ae75 100644 --- a/halloween-leds/src/main.rs +++ b/halloween-leds/src/main.rs @@ -1,14 +1,21 @@ #![no_main] #![no_std] -use embedded_hal::delay::DelayNs; -use embedded_hal::spi::SpiBus; +/// This application demonstrates using a Raspberry Pi Pico to control an individual SK9822 module. +/// Keep in mind that the Pico, though it accepts 5V for power, it runs on 3.3V logic. The GPIO +/// pins will emit only 3.3 volts, and the SK9822 needs 5V logic. So, make sure that the GPIO pins +/// run through a transistor or a logic level lhifter to go from 3.3V logic to 5V logic. +use embedded_hal::{delay::DelayNs, spi::SpiBus}; use panic_halt as _; use rp_pico::{ entry, hal::{ clocks::init_clocks_and_plls, fugit::RateExtU32, + gpio::{ + bank0::{Gpio10, Gpio11}, + FunctionSpi, Pin, PullDown, + }, spi::Spi, Clock, Sio, Timer, Watchdog, }, @@ -21,15 +28,28 @@ const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // MHz, https://forums.raspberrypi.co #[entry] unsafe fn main() -> ! { + // rp_pico::pac::Peripherals is a reference to physical hardware defined on the Pico. let mut peripherals = pac::Peripherals::take().unwrap(); + + // SIO inidcates "Single Cycle IO". I don't know what this means, but it could mean that this + // is a class of IO operations that can be run in a single clock cycle, such as switching a + // GPIO pin on or off. let sio = Sio::new(peripherals.SIO); + + // Many of the following systems require a watchdog. I do not know what this does, either, but + // it may be some failsafe software that will reset operations if the watchdog detects a lack + // of activity. let mut watchdog = Watchdog::new(peripherals.WATCHDOG); + + // Here we grab the GPIO pins in bank 0. let pins = Pins::new( peripherals.IO_BANK0, peripherals.PADS_BANK0, sio.gpio_bank0, &mut peripherals.RESETS, ); + + // Initialize an abstraction of the clock system with a batch of standard hardware clocks. let clocks = init_clocks_and_plls( XOSC_CRYSTAL_FREQ, peripherals.XOSC, @@ -42,15 +62,23 @@ unsafe fn main() -> ! { .ok() .unwrap(); + // An abstraction for a timer which we can use to delay the code. let mut timer = Timer::new(peripherals.TIMER, &mut peripherals.RESETS, &clocks); - let spi_clk = pins.gpio10.into_function(); - let spi_sdo = pins.gpio11.into_function(); - let spi = Spi::<_, _, _, 8>::new(peripherals.SPI1, (spi_sdo, spi_clk)); - let mut spi = spi.init( + // Grab the clock and data pins for SPI1. For Clock pins and for Data pins, there are only two + // pins each on the Pico which can function for SPI1. + let spi_clk: Pin = pins.gpio10.into_function(); + let spi_sdo: Pin = pins.gpio11.into_function(); + + // Now, create the SPI function abstraction for SPI1 with spi_clk and spi_sdo. + let mut spi = Spi::<_, _, _, 8>::new(peripherals.SPI1, (spi_sdo, spi_clk)).init( &mut peripherals.RESETS, + // The SPI system uses the peripheral clock clocks.peripheral_clock.freq(), + // Transmit data at a rate of 1Mbit. 1_u32.MHz(), + // Run with SPI Mode 1. This means that the clock line should start high and that data will + // be sampled starting at the first falling edge. embedded_hal::spi::MODE_1, ); @@ -59,13 +87,27 @@ unsafe fn main() -> ! { // 4 for the end frame // = 20 bytes let mut lights: [u8; 12] = [0; 12]; + // We just skip the first four bytes, because the start frame is four bytes of 0. + + // Set the first byte of the one and only lamp. The first byte follows the pattern of three 1 + // bits followed by five additional bits that indicate an overall brightness level of the + // pixel. The datasheet for the SK9822 doesn't specify the exact effect, but it does mean that + // the higher this number is, the brighter 255 means for an given LED in the array. 1 is the + // lowest brightness that emits light, and 31 is the highest supported brightness. + lights[4] = 0xe0 + 1; + // Set the Blue light of the dotstar to 255, assuming the dotstar frame format is RBG. Note + // that the standard SK9822 datasheed indicates that the format is BGR. Your mileage may vary. + lights[6] = 255; + + // The end frame is four bytes of 255. lights[8] = 0xff; lights[9] = 0xff; lights[10] = 0xff; lights[11] = 0xff; - lights[4] = 0xe0 + 1; + // The rest of this is just a stock pulsating animation which is slightly brightening and + // dimming the *blue* LED (on my set of dotstars). let mut brightness = 1; let mut step = 1; loop { @@ -75,8 +117,6 @@ unsafe fn main() -> ! { step = 1; }; lights[5] = brightness as u8; - lights[6] = 255; - // lights[7] = brightness as u8; brightness = brightness + step; let _ = spi.write(lights.as_slice()); -- 2.44.1 From 39391fb2fecbc4a8e3d4c421809d6fd1a0d0a52d Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 3 Nov 2024 22:42:34 -0500 Subject: [PATCH 5/5] Rename teh dotstar pi project --- {halloween-leds => pico-dotstar-pulsation}/.cargo/config.toml | 0 {halloween-leds => pico-dotstar-pulsation}/Cargo.toml | 0 {halloween-leds => pico-dotstar-pulsation}/src/main.rs | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {halloween-leds => pico-dotstar-pulsation}/.cargo/config.toml (100%) rename {halloween-leds => pico-dotstar-pulsation}/Cargo.toml (100%) rename {halloween-leds => pico-dotstar-pulsation}/src/main.rs (100%) diff --git a/halloween-leds/.cargo/config.toml b/pico-dotstar-pulsation/.cargo/config.toml similarity index 100% rename from halloween-leds/.cargo/config.toml rename to pico-dotstar-pulsation/.cargo/config.toml diff --git a/halloween-leds/Cargo.toml b/pico-dotstar-pulsation/Cargo.toml similarity index 100% rename from halloween-leds/Cargo.toml rename to pico-dotstar-pulsation/Cargo.toml diff --git a/halloween-leds/src/main.rs b/pico-dotstar-pulsation/src/main.rs similarity index 100% rename from halloween-leds/src/main.rs rename to pico-dotstar-pulsation/src/main.rs -- 2.44.1