SN3218 LED Driver
A Rust embedded-hal driver for the SN3218 6-channel LED driver chip, commonly used in the Raspberry Pi GFX HAT by Pimoroni.
Overview
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.
Features
- 18-channel PWM control (6 RGB LEDs)
- Automatic gamma correction for natural brightness curves
- Simple API for enabling/disabling outputs
- Mask-based LED control for selective channel enabling
- Built on embedded-hal for platform independence
Hardware
This driver is designed for the SN3218 LED driver chip, which communicates over I2C at address 0x54. It's commonly found on:
- Pimoroni GFX HAT
- Other SN3218-based LED boards
Usage
Add this crate to your Cargo.toml:
[]
= "0.2.0"
= "1.0"
Basic Example
use SN3218;
// Your I2C implementation
use I2c;
Raspberry Pi Example
use SN3218;
use I2c;
API Reference
SN3218<T: I2c>
The main driver struct that wraps your I2C implementation.
Methods
-
new(i2c: T) -> SelfCreates 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:
0x3FFFFenables all 18 channels,0x07enables 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.
Channel Mapping
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 |
Gamma Correction
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.
Error Handling
All methods return Result<(), T::Error> where T::Error is the error type from your I2C implementation. Handle these appropriately for your platform.
Platform Support
This crate works with any platform that implements the embedded-hal I2C traits, including:
- Raspberry Pi (via
rppal) - STM32 microcontrollers
- ESP32/ESP8266
- Arduino-compatible boards
- Linux-based systems
Contributing
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.
References
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
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.