| Crates.io | unicorn-hat |
| lib.rs | unicorn-hat |
| version | 0.1.0 |
| created_at | 2025-11-09 23:49:47.997182+00 |
| updated_at | 2025-11-09 23:49:47.997182+00 |
| description | Rust library for controlling the Pimoroni Unicorn HAT (8x8 LED matrix) on Raspberry Pi |
| homepage | |
| repository | https://github.com/PeterParker/unicorn-hat |
| max_upload_size | |
| id | 1924585 |
| size | 146,209 |
A Rust library for controlling the Pimoroni Unicorn HAT (8x8 LED matrix) on Raspberry Pi.
Add to your Cargo.toml:
[dependencies]
unicorn-hat = "0.1"
Hardware:
System Setup:
# Install dependencies
sudo apt-get install -y libclang-dev llvm-dev
# Enable SPI
sudo raspi-config
# Navigate to: Interface Options -> SPI -> Enable
use unicorn_hat::{UnicornHat, RGB8};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut hat = UnicornHat::new()?;
// Set corner pixels
hat.set_pixel(0, 0, RGB8::RED)?;
hat.set_pixel(7, 0, RGB8::GREEN)?;
hat.set_pixel(0, 7, RGB8::BLUE)?;
hat.set_pixel(7, 7, RGB8::YELLOW)?;
// Display changes
hat.display()?;
Ok(())
}
Run with sudo:
sudo cargo run
use unicorn_hat::{UnicornHat, HSV};
let mut hat = UnicornHat::new()?;
for x in 0..8 {
let hue = (x as f32 / 8.0) * 360.0;
let color = HSV::new(hue, 1.0, 1.0).into();
for y in 0..8 {
hat.set_pixel(x, y, color)?;
}
}
hat.display()?;
use unicorn_hat::{primitives, UnicornHat, RGB8};
let mut hat = UnicornHat::new()?;
// Draw a horizontal line
primitives::draw_hline(&mut hat, 3, RGB8::RED)?;
// Draw a filled rectangle
primitives::fill_rect(&mut hat, 1, 1, 3, 3, RGB8::BLUE)?;
hat.display()?;
use unicorn_hat::{primitives, UnicornHat, RGB8};
let mut hat = UnicornHat::new()?;
// Horizontal gradient from red to blue
primitives::gradient_h(&mut hat, RGB8::RED, RGB8::BLUE)?;
hat.display()?;
For text rendering, scrolling animations, and frame buffers, see the companion crate:
[dependencies]
unicorn-hat = "0.1"
unicorn-hat-extras = "0.1"
See the unicorn-hat-extras documentation for details.
UnicornHat - Main controllerRGB8 - 8-bit RGB color (0-255 per channel)HSV - Hue/Saturation/Value color modelRotate - Display rotationOrigin - Coordinate system origin (TopLeft or BottomLeft)// Initialization
let mut hat = UnicornHat::new()?;
// Pixel operations
hat.set_pixel(x, y, color)?;
hat.get_pixel(x, y)?;
hat.fill(color);
hat.clear();
// Display
hat.display()?;
// Configuration
hat.set_brightness(128); // 0-255
hat.set_rotation(Rotate::RotCW90);
// Bulk operations
hat.set_all(&grid)?;
let grid = hat.get_all()?;
RGB8::BLACK, RGB8::WHITE, RGB8::RED, RGB8::GREEN, RGB8::BLUE, RGB8::YELLOW, RGB8::CYAN, RGB8::MAGENTA, RGB8::ORANGE, RGB8::PURPLE
The library requires hardware access. You have two options:
sudo cargo run
cargo build --release
sudo setcap cap_sys_rawio=ep target/release/your-program
./target/release/your-program # No sudo needed
Note: Capabilities must be reset after each rebuild.
Run the included examples:
sudo cargo run --example simple
sudo cargo run --example rainbow
sudo cargo run --example gradients
Generate full API documentation:
cargo doc --open
sudo or CAP_SYS_RAWIO capabilityMIT License - see LICENSE for details.