| Crates.io | unicorn-hat-extras |
| lib.rs | unicorn-hat-extras |
| version | 0.1.0 |
| created_at | 2025-11-09 23:54:01.597165+00 |
| updated_at | 2025-11-09 23:54:01.597165+00 |
| description | High-level convenience features for unicorn-hat: fonts, drawing primitives, animations |
| homepage | |
| repository | https://github.com/PeterParker/unicorn-hat |
| max_upload_size | |
| id | 1924594 |
| size | 68,518 |
High-level convenience features for the unicorn-hat library.
This crate provides text rendering, scrolling animations, and frame buffers built on top of the core unicorn-hat crate. Perfect for creating ticker-tape displays, notifications, and smooth animations on your Pimoroni Unicorn HAT.
Add both crates to your Cargo.toml:
[dependencies]
unicorn-hat = "0.1"
unicorn-hat-extras = "0.1"
use unicorn_hat::{UnicornHat, RGB8, PixelBuffer};
use unicorn_hat_extras::font::{draw_text, FONT_5X5};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut hat = UnicornHat::new()?;
// Draw "HI" in green at position (0, 1)
draw_text(&mut hat, &FONT_5X5, 0, 1, "HI", RGB8::GREEN)?;
hat.display()?;
Ok(())
}
use std::thread;
use std::time::Duration;
use unicorn_hat::{UnicornHat, RGB8};
use unicorn_hat_extras::font::FONT_5X5;
use unicorn_hat_extras::scroller::{Scroller, Direction};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut hat = UnicornHat::new()?;
let mut scroller = Scroller::new(
"HELLO WORLD",
&FONT_5X5,
RGB8::CYAN,
Direction::Left,
);
// Animate scrolling text
for _ in 0..100 {
hat.clear();
scroller.render(&mut hat)?;
hat.display()?;
scroller.step();
thread::sleep(Duration::from_millis(50));
}
Ok(())
}
use unicorn_hat::{UnicornHat, RGB8, PixelBuffer};
use unicorn_hat_extras::frame::Frame;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut hat = UnicornHat::new()?;
// Prepare frame off-screen
let mut frame = Frame::new();
frame.set_pixel(3, 3, RGB8::RED)?;
frame.set_pixel(4, 4, RGB8::BLUE)?;
// Display the complete frame at once
hat.show_frame(frame.get_all())?;
Ok(())
}
Run the included examples to see what's possible:
# Horizontal scrolling ticker tape
sudo cargo run -p unicorn-hat-extras --example ticker_tape
# Vertical scrolling demonstrations
sudo cargo run -p unicorn-hat-extras --example vertical_scroll
# Frame buffer and animation techniques
sudo cargo run -p unicorn-hat-extras --example double_buffer
font)FONT_5X5: 5×5 pixel bitmap font (ASCII 32-126)draw_char(buffer, font, x, y, ch, color): Draw a single characterdraw_text(buffer, font, x, y, text, color): Draw a text stringCoordinates can be negative for smooth off-screen rendering and scrolling effects.
scroller)Scroller::new(text, font, color, direction): Create a scrolling text animationscroller.step(): Advance the scroll positionscroller.render(buffer): Draw the current framescroller.set_speed(pixels_per_step): Control scroll speedscroller.is_complete(): Check if text has scrolled off-screenSupports all 4 directions: Left, Right, Up, Down.
frame)Frame::new(): Create an 8×8 off-screen bufferFrame::with_color(color): Create a frame filled with a colorframe.set_pixel(x, y, color): Modify pixels off-screenframe.get_all(): Get the complete frame bufferUnicornHat::show_frame(frame_data): Display a frame bufferUse Frame for double-buffering and preparing complex scenes before display.
The included FONT_5X5 font covers printable ASCII (characters 32-126) and is optimized for readability on small 8×8 displays. Each character is 5 pixels wide and 5 pixels tall, with automatic 1-pixel spacing between characters.
Frame for complex scenes to avoid partial updates being visibleScroller speed with set_speed() to match your desired animation frameratehat.clear() before rendering each frame for smooth scrollingAll extras functions work with any type implementing the PixelBuffer trait, including:
UnicornHat (direct hardware rendering)Frame (off-screen rendering)TestBuffer (for unit testing)This means you can mix and match core primitives (lines, rectangles, gradients) with text rendering and scrolling seamlessly.
unicorn-hat core librarysudo (see main unicorn-hat crate docs for details)MIT License - see the workspace root LICENSE file for details.
For more details about the core library, hardware setup, and permissions, see the main README.