pub struct Max7219<SPI> { /* private fields */ }Expand description
Driver for the MAX7219 LED display controller.
Communicates over SPI using the embedded-hal SpiDevice trait.
Implementations§
Source§impl<SPI> Max7219<SPI>where
SPI: SpiDevice,
impl<SPI> Max7219<SPI>where
SPI: SpiDevice,
Sourcepub fn new(spi: SPI) -> Self
pub fn new(spi: SPI) -> Self
Creates a new MAX7219 driver instance with the given SPI interface.
The SPI interface must use Mode 0, which means the clock is low when idle and data is read on the rising edge of the clock signal.
Defaults to a single device (can be daisy-chained using with_device_count).
The SPI frequency must be 10 MHz or less, as required by the MAX7219 datasheet.
Sourcepub fn device_count(&self) -> usize
pub fn device_count(&self) -> usize
Returns the number of MAX7219 devices managed by this driver.
This corresponds to the number of daisy-chained MAX7219 units initialized during driver setup.
Sourcepub fn with_device_count(self, count: usize) -> Result<Self, Error>
pub fn with_device_count(self, count: usize) -> Result<Self, Error>
Sourcepub fn power_on(&mut self) -> Result<(), Error>
pub fn power_on(&mut self) -> Result<(), Error>
Powers on all displays by writing 0x01 to the Shutdown register.
Sourcepub fn power_off(&mut self) -> Result<(), Error>
pub fn power_off(&mut self) -> Result<(), Error>
Powers off all displays by writing 0x00 to the Shutdown register.
Sourcepub fn power_on_device(&mut self, device_index: usize) -> Result<(), Error>
pub fn power_on_device(&mut self, device_index: usize) -> Result<(), Error>
Powers on a single device by writing 0x01 to the Shutdown register.
§Arguments
device_index- The index of the display to power on.
Sourcepub fn power_off_device(&mut self, device_index: usize) -> Result<(), Error>
pub fn power_off_device(&mut self, device_index: usize) -> Result<(), Error>
Powers off a single device by writing 0x00 to the Shutdown register.
§Arguments
device_index- The index of the device to power off.
Sourcepub fn test_device(
&mut self,
device_index: usize,
enable: bool,
) -> Result<(), Error>
pub fn test_device( &mut self, device_index: usize, enable: bool, ) -> Result<(), Error>
Enables or disables display test mode on a specific device.
When enabled, all LEDs on that device are lit regardless of current device data.
Sourcepub fn test_all(&mut self, enable: bool) -> Result<(), Error>
pub fn test_all(&mut self, enable: bool) -> Result<(), Error>
Enable or disable display test mode on all devices in one SPI transaction.
Sourcepub fn set_device_scan_limit(
&mut self,
device_index: usize,
limit: u8,
) -> Result<(), Error>
pub fn set_device_scan_limit( &mut self, device_index: usize, limit: u8, ) -> Result<(), Error>
Sets how many digits the MAX7219 should actively scan and display.
This tells the chip how many digit outputs (DIG0 to DIG7) should be used. The input value must be between 1 and 8:
- 1 means only digit 0 is used
- 8 means all digits (0 to 7) are used
Internally, the value written to the chip is limit - 1, because the chip expects values from 0 to 7.
This applies to a specific device in the daisy chain, selected by device_index.
§Errors
Returns Error::InvalidScanLimit if the value is not in the range 1 to 8.
Sourcepub fn set_scan_limit_all(&mut self, limit: u8) -> Result<(), Error>
pub fn set_scan_limit_all(&mut self, limit: u8) -> Result<(), Error>
Set scan‐limit on all devices in one go.
limit must be in 1..=8. Internally sends limit - 1 to each chip.
Sourcepub fn set_device_decode_mode(
&mut self,
device_index: usize,
mode: DecodeMode,
) -> Result<(), Error>
pub fn set_device_decode_mode( &mut self, device_index: usize, mode: DecodeMode, ) -> Result<(), Error>
Code B decoding allows the MAX7219 to automatically convert values like 0-9, E, H, L, etc.
into the corresponding 7-segment patterns, instead of requiring manual segment control.
The mode parameter specifies which digits use automatic decoding.
Use DecodeMode variants
such as [NoDecode], [Digit0], [Digits0To3], or [AllDigits] based on which digits
should be decoded automatically.
The device_index selects the target device. For a single device setup, use 0.
Sourcepub fn set_decode_mode_all(&mut self, mode: DecodeMode) -> Result<(), Error>
pub fn set_decode_mode_all(&mut self, mode: DecodeMode) -> Result<(), Error>
Set decode‐mode on all devices in one go.
Sourcepub fn clear_display(&mut self, device_index: usize) -> Result<(), Error>
pub fn clear_display(&mut self, device_index: usize) -> Result<(), Error>
Clears all digits by writing 0 to each digit register (DIG0 to DIG7).
This turns off all segments on the display by sending 0x00 to each of the digit registers (Register::Digit0 to Register::Digit7).
This applies to a specific device in the daisy chain, selected by device_index.
Sourcepub fn clear_all(&mut self) -> Result<(), Error>
pub fn clear_all(&mut self) -> Result<(), Error>
Clears all digits on all connected MAX7219 displays.
Sourcepub fn write_raw_digit(
&mut self,
device_index: usize,
digit: u8,
value: u8,
) -> Result<(), Error>
pub fn write_raw_digit( &mut self, device_index: usize, digit: u8, value: u8, ) -> Result<(), Error>
Writes a raw value to the specified digit register (DIG0 to DIG7).
This function gives you low-level control over the display by sending a
raw 8-bit pattern to the specified digit. Each bit in the value corresponds
to an individual segment (on 7-segment displays) or LED (on an LED matrix).
A typical 7-segment display has the following layout:
A
---
F | | B
| |
---
E | | C
| |
--- . DP
D| Byte | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Segment | DP | A | B | C | D | E | F | G |
For example, to display the number 1, use the byte 0b00110000,
which lights up segments B and C.
§Example
display.write_raw_digit(0, Digit::D0, 0b00110000)?; // Shows '1'On an LED matrix (8x8), each digit register maps to a row, and each bit in the value maps to a column (from left to right).
Note: Wiring and orientation can vary between modules. Some modules map rows top-to-bottom, others bottom-to-top; some wire columns left-to-right, others right-to-left. If the display appears mirrored or rotated, adjust your digit and bit mapping accordingly.
Here is an example layout for the FC-16 module, where DIG0 corresponds to the top row (row 0),
and bit 0 maps to the rightmost column (column 0). So a value like 0b10101010 written to DIG0
would light up every alternate LED across the top row from left to right.
DIG0 -> Row 0: value = 0b10101010
Matrix:
Columns
7 6 5 4 3 2 1 0
+----------------
0 | 1 0 1 0 1 0 1 0
1 | ...
2 | ...
Rows 3 | ...
4 | ...
5 | ...
6 | ...
7 | ...This applies to a specific device in the daisy chain, selected by device_index.
§Arguments
device_index: Index of the display in the daisy chain (0 = closest to the Microcontroller)digit: Which digit register to write to (Digit::D0toDigit::D7)value: The raw 8-bit data to send to the digit register
Sourcepub fn set_intensity(
&mut self,
device_index: usize,
intensity: u8,
) -> Result<(), Error>
pub fn set_intensity( &mut self, device_index: usize, intensity: u8, ) -> Result<(), Error>
Sets the brightness intensity (0 to 15) for a specific device.
§Arguments
device_index: Index of the display in the daisy chain (0 = closest to the Microcontroller)intensity: Brightness level from0to15(0x00to0x0F)