Struct Max7219

Source
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,

Source

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.

Source

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.

Source

pub fn with_device_count(self, count: usize) -> Result<Self, Error>

Sets the number of daisy-chained devices to control.

§Errors

Returns Error::InvalidDisplayCount if count > MAX_DISPLAYS.

§Example
let driver = Max7219::new(spi).with_device_count(4)?;
Source

pub fn init(&mut self) -> Result<(), Error>

Initializes all configured displays.

Source

pub fn power_on(&mut self) -> Result<(), Error>

Powers on all displays by writing 0x01 to the Shutdown register.

Source

pub fn power_off(&mut self) -> Result<(), Error>

Powers off all displays by writing 0x00 to the Shutdown register.

Source

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.
Source

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.
Source

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.

Source

pub fn test_all(&mut self, enable: bool) -> Result<(), Error>

Enable or disable display test mode on all devices in one SPI transaction.

Source

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.

Source

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.

Source

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.

Source

pub fn set_decode_mode_all(&mut self, mode: DecodeMode) -> Result<(), Error>

Set decode‐mode on all devices in one go.

Source

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.

Source

pub fn clear_all(&mut self) -> Result<(), Error>

Clears all digits on all connected MAX7219 displays.

Source

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
Byte76543210
SegmentDPABCDEFG

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::D0 to Digit::D7)
  • value: The raw 8-bit data to send to the digit register
Source

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 from 0 to 15 (0x00 to 0x0F)
Source

pub fn set_intensity_all(&mut self, intensity: u8) -> Result<(), Error>

Set intensity for all displays

Auto Trait Implementations§

§

impl<SPI> Freeze for Max7219<SPI>
where SPI: Freeze,

§

impl<SPI> RefUnwindSafe for Max7219<SPI>
where SPI: RefUnwindSafe,

§

impl<SPI> Send for Max7219<SPI>
where SPI: Send,

§

impl<SPI> Sync for Max7219<SPI>
where SPI: Sync,

§

impl<SPI> Unpin for Max7219<SPI>
where SPI: Unpin,

§

impl<SPI> UnwindSafe for Max7219<SPI>
where SPI: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.