#apa102 #smart-leds #spi #sk9822 #dotstar #smart-leds-apa102

no-std apa102-spi

Driver for writing to APA102 (DotStar) and SK9822 LEDs over SPI

8 releases (4 breaking)

new 0.5.0 Apr 10, 2025
0.4.0 Jan 14, 2024
0.3.2 Nov 20, 2019
0.3.1 Sep 20, 2019
0.1.0 Mar 5, 2019

#678 in Embedded development

Download history 36/week @ 2024-12-22 152/week @ 2024-12-29 226/week @ 2025-01-05 160/week @ 2025-01-12 216/week @ 2025-01-19 256/week @ 2025-01-26 120/week @ 2025-02-02 192/week @ 2025-02-09 127/week @ 2025-02-16 116/week @ 2025-02-23 153/week @ 2025-03-02 426/week @ 2025-03-09 344/week @ 2025-03-16 427/week @ 2025-03-23 171/week @ 2025-03-30 598/week @ 2025-04-06

1,577 downloads per month
Used in 4 crates (3 directly)

MIT/Apache

34KB
638 lines

APA102 driver for embedded-hal spi traits

For usage with the smart-leds crate.

License

Licensed under either of

at your option.

Todo

  • Implement a WriteIter based version

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Send data to APA102 LEDs (also known as DotStar LEDs) via SPI. This crate is also compatible with SK9822 LEDs which share the same protocol as APA102 LEDs. Both blocking and asynchronous implementations are provided, which require a HAL crate for your microcontroller with an implementation of the embedded_hal::spi::SpiBus or embedded_hal_async::spi::SpiBus trait.

There are several ways to send pixel data:

  • Handle all details of the protocol yourself with the [Apa102Pixel] struct, 8 bit RGB + 5 bits brightness
  • Simply provide [RGB8] values, hardcoding maximum brightness. This may be uncomfortably bright.
  • Use FastLED's pseudo-13-bit gamma correction algorithm to convert [RGB8] + 8 bit brightness to 8 bit RGB + 5 bit brightness.
#
#
#
#
#
use apa102_spi::{u5, Apa102Pixel, Apa102Writer, PixelOrder, SmartLedsWrite, RGB8};

// You only need to specify MOSI and clock pins for your SPI peripheral.
// APA102 LEDs do not send data over MISO and do not have a CS pin.
let spi = get_spi_peripheral_from_your_hal;
let mut led_strip = Apa102Writer::new(spi, 1, PixelOrder::default());

// Specify pixel values as 8 bit RGB + 5 bit brightness
let led_buffer = [Apa102Pixel { red: 255, green: 0, blue: 0, brightness: u5::new(1) }];
led_strip.write(led_buffer);

// Specify pixel values with 8 bit RGB values
let led_buffer_rgb = [RGB8 { r: 255, g: 0, b: 0 }];
// Brightness is set to maximum value (31) in `impl From<RGB8> for Apa102Pixel`
led_strip.write(led_buffer_rgb);

// Convert RGB8 + 8 bit brightness into Apa102Pixels
// using FastLED's pseudo-13-bit gamma correction algorithm.
led_strip.write(led_buffer_rgb.map(
  |p| Apa102Pixel::from_rgb8_with_brightness(p, 255, None)));

Cargo features

Dependencies

~335–490KB