| Crates.io | sn3218-hal |
| lib.rs | sn3218-hal |
| version | 0.2.0 |
| created_at | 2025-07-21 15:35:28.658795+00 |
| updated_at | 2025-07-21 15:35:28.658795+00 |
| description | Embedded Rust driver for SN3218 18-channel LED controller with gamma correction |
| homepage | https://github.com/judahzf/sn3218 |
| repository | https://github.com/judahzf/sn3218 |
| max_upload_size | |
| id | 1762185 |
| size | 36,303 |
A Rust embedded-hal driver for the SN3218 6-channel LED driver chip, commonly used in the Raspberry Pi GFX HAT by Pimoroni.
The SN3218 is an 18-channel LED driver with PWM control, arranged as 6 RGB channels. This driver provides a simple interface to control LED brightness with automatic gamma correction for smoother brightness transitions.
This driver is designed for the SN3218 LED driver chip, which communicates over I2C at address 0x54. It's commonly found on:
Add this crate to your Cargo.toml:
[dependencies]
sn3218 = "0.2.0"
embedded-hal = "1.0"
use sn3218::SN3218;
// Your I2C implementation
use your_hal::I2c;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let i2c = I2c::new(/* your I2C config */)?;
let mut led_driver = SN3218::new(i2c);
// Enable the output
led_driver.enable()?;
// Enable all LEDs (18 channels)
led_driver.enable_leds(0x3FFFF)?; // All 18 bits set
// Set LED values (18 channels: R,G,B for each of 6 LEDs)
let led_values = [
255, 0, 0, // LED 0: Red
0, 255, 0, // LED 1: Green
0, 0, 255, // LED 2: Blue
255, 255, 0, // LED 3: Yellow
255, 0, 255, // LED 4: Magenta
0, 255, 255, // LED 5: Cyan
];
led_driver.output(&led_values)?;
Ok(())
}
use sn3218::SN3218;
use rppal::i2c::I2c;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let i2c = I2c::new()?;
let mut gfx_hat = SN3218::new(i2c);
// Initialize
gfx_hat.reset()?;
gfx_hat.enable()?;
// Create a rainbow effect
let mut led_values = [0u8; 18];
for i in 0..6 {
let hue = i as f32 * 60.0; // 60 degrees apart
let (r, g, b) = hsv_to_rgb(hue, 1.0, 1.0);
led_values[i * 3] = (r * 255.0) as u8;
led_values[i * 3 + 1] = (g * 255.0) as u8;
led_values[i * 3 + 2] = (b * 255.0) as u8;
}
gfx_hat.enable_leds(0x3FFFF)?;
gfx_hat.output(&led_values)?;
Ok(())
}
SN3218<T: I2c>The main driver struct that wraps your I2C implementation.
new(i2c: T) -> Self
Creates a new SN3218 driver instance with automatic gamma correction table generation.
enable(&mut self) -> Result<(), T::Error>
Enables the LED driver output.
disable(&mut self) -> Result<(), T::Error>
Disables the LED driver output.
reset(&mut self) -> Result<(), T::Error>
Resets the LED driver to its default state.
enable_leds(&mut self, mask: u32) -> Result<(), T::Error>
Enables specific LED channels using a bitmask. Each bit corresponds to one of the 18 channels.
Example: 0x3FFFF enables all 18 channels, 0x07 enables only the first 3 channels.
output(&mut self, values: &[u8]) -> Result<(), T::Error>
Sets the PWM values for all 18 channels. The array must be exactly 18 elements long. Values are automatically gamma-corrected for natural brightness perception.
The 18 channels are organized as 6 RGB LEDs:
| LED | Red Channel | Green Channel | Blue Channel |
|---|---|---|---|
| 0 | 0 | 1 | 2 |
| 1 | 3 | 4 | 5 |
| 2 | 6 | 7 | 8 |
| 3 | 9 | 10 | 11 |
| 4 | 12 | 13 | 14 |
| 5 | 15 | 16 | 17 |
This driver automatically applies gamma correction to all output values using the formula:
corrected_value = 255^(input_value / 255)
This provides more natural brightness transitions, especially important for LED displays.
All methods return Result<(), T::Error> where T::Error is the error type from your I2C implementation. Handle these appropriately for your platform.
This crate works with any platform that implements the embedded-hal I2C traits, including:
rppal)Contributions are welcome! Please feel free to submit a Pull Request. This crate aims to provide a simple, safe interface to the SN3218 chip while maintaining compatibility with the embedded-hal ecosystem.
Licensed under either of
at your option.
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.