Struct LedMatrix

Source
pub struct LedMatrix<SPI, const BUFFER_LENGTH: usize = 64, const DEVICE_COUNT: usize = 1> { /* private fields */ }
Available on crate feature led-matrix only.
Expand description

A high-level abstraction for controlling an LED matrix display using the MAX7219 driver.

Implementations§

Source§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>
where SPI: SpiDevice,

Source

pub fn from_spi(spi: SPI) -> Result<Self, Error>

Simplifies initialization by creating a new LedMatrix instance from the given SPI device and number of connected displays.

Internally, this constructs and initializes the Max7219 driver, making setup easier for typical use cases.

§Arguments
  • spi - The SPI device used for communication.
§Returns

Returns a LedMatrix instance on success, or an error if the display count is invalid

§Example
let spi = /* your SPI device */;
let mut matrix = SingleMatrix::from_spi(spi, 4).unwrap();
Source

pub fn from_driver(driver: Max7219<SPI>) -> Result<Self, Error>

Creates a new LedMatrix instance from an existing Max7219 driver.

This method is useful if you have already created and configured a Max7219 driver manually. In most cases, it is recommended to use Self::from_spi instead, which creates the driver and matrix together in one step.

§Arguments
  • driver - An initialized Max7219 driver instance.
§Error

Returns Err(Error::InvalidDeviceCount) if the driver’s device count does not match the generic DEVICE_COUNT parameter of this matrix type.

§Warning

This method is more error-prone than Self::from_spi because it is easy to configure a driver with one device count (e.g., .with_device_count(4)) and then call from_driver on a LedMatrix type instantiated with a different generic parameter (e.g., LedMatrix<_, 1>). This mismatch will result in an error.

§Example
let driver = Max7219::new(spi).with_device_count(1).expect("device count 1 should not panic");
let mut matrix = SingleMatrix::new(driver).unwrap();
Source

pub fn driver(&mut self) -> &mut Max7219<SPI>

Provides mutable access to the underlying MAX7219 driver.

This allows users to call low-level functions directly

Source

pub fn clear(&mut self, device_index: usize) -> Result<(), Error>

Clear a specific device

Source

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

Clear all device

Source

pub fn write_buffer( &mut self, device_index: usize, buffer: &MatrixBuffer, ) -> Result<(), Error>

Write a complete buffer to a specific display

Source

pub fn draw_char(&mut self, device_index: usize, ch: char) -> Result<(), Error>

Draws a single 8x8 character on the specified display device.

The character is converted into an 8-byte bitmap using a predefined font. If the character is unsupported, it will be replaced with “?” char.

Each byte in the bitmap corresponds to one row of the 8x8 LED matrix (from D0 to D7), and is written to the digit registers of the specified device_index.

§Arguments
  • device_index - Index of the target MAX7219 device in the daisy chain.
  • c - The character to display.
§Errors

Returns an error if the digit conversion fails or if SPI communication fails.

Source

pub fn draw_char_with_font( &mut self, device_index: usize, ch: char, font: &LedFont, ) -> Result<(), Error>

Draws a single 8x8 character on the specified display device using a provided font.

This function is similar to Self::draw_char, but allows overriding the font used for rendering. The character is mapped to an 8-byte bitmap. Each byte represents a row on the matrix, with the most significant bit (bit 7) on the left and the least significant bit (bit 0) on the right.

§Arguments
  • device_index - Index of the MAX7219 device to write to.
  • ch - The character to render on the display.
  • font - The font to use for character lookup and rendering.
Source

pub fn draw_text(&mut self, text: &str) -> Result<(), Error>

Draw a string of text on the LED matrix using the default font. Each character is displayed on one device in the daisy chain. If the string is longer than the number of devices, the extra characters are ignored.

Source

pub fn draw_text_with_font( &mut self, text: &str, font: &LedFont, ) -> Result<(), Error>

Draw a string of text on the LED matrix using a specified font. Each character is displayed on one device in the daisy chain. If the string is longer than the number of devices, the extra characters are ignored.

Source

pub fn scroll_text<D: DelayNs>( &mut self, delay: &mut D, text: &str, config: ScrollConfig, ) -> Result<(), Error>

Scroll the given text across the LED matrix.

This will render text using the current font and step through each frame at the delay specified by config.step_delay_ns. If config.loop_text is true, the text will repeat with config.loop_padding pixels of blank space between repetitions.

§Parameters
  • delay: delay provider implementing embedded_hal::delay::DelayNs.
  • text: the string slice to scroll.
  • config: scrolling configuration (speed, step size, looping).
§Errors

Returns a MatrixError if updating the display buffer fails.

Source

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

Flush the internal display buffer to the actual LED matrix hardware.

This function goes row by row (0 to 7), and for each row, it builds an array of SPI operations (ops) to send to all devices in the daisy-chained display. It packs each row of pixels into a single byte for each device, then sends the data using the driver’s write_all_registers method.

§Example logic (DEVICE_COUNT = 2, row = 0):

Assume self.framebuffer contains pixel bits for 2 devices (128 total):

Device 0, row 0 pixels: [1, 0, 1, 0, 1, 0, 1, 0] => 0b10101010 => 0xAA Device 1, row 0 pixels: [1, 1, 1, 1, 0, 0, 0, 0] => 0b11110000 => 0xF0

Since SPI sends left to right, we must reverse the device order in the ops array: ops[0] = (Digit0, 0xF0) // Device 1 ops[1] = (Digit0, 0xAA) // Device 0

These are sent out in one SPI write for Digit0, and similarly repeated for Digit1 through Digit7.

Source

pub fn clear_buffer(&mut self)

Clear the internal framebuffer (sets all pixels to 0).

Source

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

Clear screen by resetting buffer and flushing

Trait Implementations§

Source§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> DrawTarget for LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>
where SPI: SpiDevice,

Available on crate feature graphics only.
Source§

type Color = BinaryColor

The pixel color type the targetted display supports.
Source§

type Error = Infallible

Error type to return when a drawing operation fails. Read more
Source§

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where I: IntoIterator<Item = Pixel<Self::Color>>,

Draw individual pixels to the display without a defined order. Read more
Source§

fn fill_contiguous<I>( &mut self, area: &Rectangle, colors: I, ) -> Result<(), Self::Error>
where I: IntoIterator<Item = Self::Color>,

Fill a given area with an iterator providing a contiguous stream of pixel colors. Read more
Source§

fn fill_solid( &mut self, area: &Rectangle, color: Self::Color, ) -> Result<(), Self::Error>

Fill a given area with a solid color. Read more
Source§

fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error>

Fill the entire display with a solid color. Read more
Source§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> OriginDimensions for LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>

Available on crate feature graphics only.
Source§

fn size(&self) -> Size

Returns the size of the bounding box.

Auto Trait Implementations§

§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> Freeze for LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>
where SPI: Freeze,

§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> RefUnwindSafe for LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>
where SPI: RefUnwindSafe,

§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> Send for LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>
where SPI: Send,

§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> Sync for LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>
where SPI: Sync,

§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> Unpin for LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>
where SPI: Unpin,

§

impl<SPI, const BUFFER_LENGTH: usize, const DEVICE_COUNT: usize> UnwindSafe for LedMatrix<SPI, BUFFER_LENGTH, DEVICE_COUNT>
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> Dimensions for T

Source§

fn bounding_box(&self) -> Rectangle

Returns the bounding box.
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.