Crate blinkt

source ·
Expand description

A Rust library that provides an interface for the Pimoroni Blinkt!, and any similar APA102 or SK9822 LED strips or boards, on a Raspberry Pi. The library supports bitbanging mode on any GPIO pins, and hardware SPI mode on GPIO 10 (physical pin 19) for data, and GPIO 11 (physical pin 23) for clock.

For bitbanging mode, Blinkt gains access to the BCM283x GPIO peripheral either through /dev/gpiomem or /dev/mem. Hardware SPI mode is controlled through /dev/spidev0.0.

Both the original APA102 and the SK9822 clone are supported. The APA102 RGB LED/driver ICs are referred to as pixels throughout the code and documentation.

Each pixel has a red, green and blue LED with possible values between 0-255. Additionally, the overall brightness of each pixel can be set to 0.0-1.0, which is converted to a 5-bit value.

Blinkt stores all color and brightness changes in a local buffer. Use show() to send the buffered values to the pixels.

By default, all pixels are cleared when Blinkt goes out of scope. Use set_clear_on_drop(false) to disable this behavior. Note that drop methods aren’t called when a process is abnormally terminated (for instance when a SIGINT isn’t caught).

Examples

Blinkt! board

A complete example that cycles all pixels on a Blinkt! board through red, green and blue.

use std::error::Error;
use std::time::Duration;
use std::{thread, mem};

use blinkt::Blinkt;

fn main() -> Result<(), Box<dyn Error>> {
    let mut blinkt = Blinkt::new()?;
    let (red, green, blue) = (&mut 255, &mut 0, &mut 0);

    loop {
        blinkt.set_all_pixels(*red, *green, *blue);
        blinkt.show()?;

        thread::sleep(Duration::from_millis(250));

        mem::swap(red, green);
        mem::swap(red, blue);
    }
}

APA102 or SK9822 LED strip

The recommended way to control an LED strip is to use the hardware SPI interface through Blinkt::with_spi(), with the data line connected to GPIO 10 (physical pin 19), and clock on GPIO 11 (physical pin 23).

let mut blinkt = Blinkt::with_spi(BlinktSpi::default(), 144);

Alternatively, you can use the bitbanging mode through Blinkt::with_settings() to connect the LED strip to any available GPIO pins. However, this is less reliable than using the hardware SPI interface, and may cause issues on longer strips.

let mut blinkt = Blinkt::with_settings(23, 24, 8)?;

Iterators

Besides the various set_ methods available on Blinkt, pixels can also be accessed and modified through an iterator. Blinkt::iter_mut() returns a mutable iterator over all Pixels stored in Blinkt.

blinkt.iter_mut().for_each(|pixel| {
    pixel.set_rgb(255, 0, 255);
});

For more idiomatic for loops, you can access the same iterator through a mutable reference of a Blinkt instance.

for pixel in &mut blinkt {
    pixel.set_rgb(255, 255, 0);
}

Modules

Structs

  • Interface for the Pimoroni Blinkt!, and any similar APA102 or SK9822 LED strips or boards.
  • A pixel on an LED strip or board.

Enums

  • Errors that can occur while using Blinkt.
  • Errors that can occur when accessing the GPIO peripheral.
  • Errors that can occur when accessing the SPI peripheral.

Type Definitions

  • Result type returned from methods that can have blinkt::Errors.