Class LedMatrixComponent


public class LedMatrixComponent extends MAX7219
Implementation of the CrowPi LED matrix using SPI with Pi4J
  • Field Details

    • DEFAULT_CHANNEL

      protected static final int DEFAULT_CHANNEL
      Default SPI channel for the LED matrix on the CrowPi
      See Also:
    • DEFAULT_BAUD_RATE

      protected static final int DEFAULT_BAUD_RATE
      Default SPI baud rate for the LED matrix on the CrowPi
      See Also:
    • DEFAULT_SCROLL_DELAY

      protected static final long DEFAULT_SCROLL_DELAY
      Default delay between scroll operations in milliseconds
      See Also:
    • DEFAULT_SCROLL_DIRECTION

      protected static final Direction DEFAULT_SCROLL_DIRECTION
      Default direction for scroll operations
  • Constructor Details

    • LedMatrixComponent

      public LedMatrixComponent(com.pi4j.context.Context pi4j)
      Creates a new LED matrix component with the default channel and baud rate.
      Parameters:
      pi4j - Pi4J context
    • LedMatrixComponent

      public LedMatrixComponent(com.pi4j.context.Context pi4j, int channel, int baud)
      Creates a new LED matrix component with a custom channel and baud rate.
      Parameters:
      pi4j - Pi4J context
      channel - SPI channel
      baud - SPI baud rate
  • Method Details

    • scroll

      public void scroll(Direction direction)
      Scrolls the display towards the given direction and leaves the now empty row/column empty.
      Parameters:
      direction - Desired scroll direction
    • rotate

      public void rotate(Direction direction)
      Rotates the display towards the given direction and wraps around the affected row/column. E.g. if Direction.LEFT is used, the column which falls out on the left will be the new rightmost column.
      Parameters:
      direction - Desired scroll direction
    • scroll

      protected void scroll(Direction direction, LedMatrixComponent.ScrollMode scrollMode, byte[] newBuffer, int newOffset)
      Scrolls the display towards the given direction and fills the empty row/column based on scroll mode. This helper method calls the appropriate internal functions and MUST not be exposed as it contains internal logic. The scrolling operating will be immediately visible on the display.
      Parameters:
      direction - Desired scroll direction
      scrollMode - Desired scroll mode
      newBuffer - Only if LedMatrixComponent.ScrollMode.REPLACE: New buffer for replacement values
      newOffset - Only if LedMatrixComponent.ScrollMode.REPLACE: Desired row/column offset for new buffer
    • print

      public void print(String string)
      Prints the given string to the LED matrix by scrolling each character in from left to right with the default scroll delay. This method is blocking until the string has been fully printed and will both start and end with an empty display.

      A pattern in the format "{SYMBOL-NAME}" can be used to include a symbol with the given name in the string. E.g. if "{HEART}" occurs within the string, it will be automatically replaced with the LedMatrixComponent.Symbol.HEART symbol. If a pattern includes a symbol which could not be found, it is silently ignored and added as-is.

      Parameters:
      string - String to be displayed
    • print

      public void print(String string, Direction scrollDirection)
      Prints the given string to the LED matrix by scrolling each character in towards the given direction with the default scroll delay. This method is blocking until the string has been fully printed and will both start and end with an empty display.

      A pattern in the format "{SYMBOL-NAME}" can be used to include a symbol with the given name in the string. E.g. if "{HEART}" occurs within the string, it will be automatically replaced with the LedMatrixComponent.Symbol.HEART symbol. If a pattern includes a symbol which could not be found, it is silently ignored and added as-is.

      Parameters:
      string - String to be displayed
      scrollDirection - Direction towards character should be scrolled in
    • print

      public void print(String string, Direction scrollDirection, long scrollDelay)
      Prints the given string to the LED matrix by scrolling each character in towards the given direction with a custom scroll delay. This method is blocking until the string has been fully printed and will both start and end with an empty display.

      A pattern in the format "{SYMBOL-NAME}" can be used to include a symbol with the given name in the string. E.g. if "{HEART}" occurs within the string, it will be automatically replaced with the LedMatrixComponent.Symbol.HEART symbol. If a pattern includes a symbol which could not be found, it is silently ignored and added as-is.

      Parameters:
      string - String to be displayed
      scrollDirection - Direction towards character should be scrolled in
      scrollDelay - Delay in milliseconds between scroll operations
    • convertToSymbols

      protected List<LedMatrixComponent.Symbol> convertToSymbols(String string)
      Converts a string into a list of symbols to print on the 8x8 LED matrix. Any characters not supported by the symbol table will throw an IllegalArgumentException.

      This method will also search for Symbol reference patterns in the provided string, which are represented as "{SYMBOL-NAME}". If this pattern is found within the string, this method will try to lookup the symbol and if found add it instead of the pattern. If no symbol with a given name is found, it gets silently ignored and added as-is to the list of output symbols.

      Parameters:
      string - String to parse and convert to symbols
      Returns:
      List of symbols to print for representing the given string
    • print

      public void print(char c)
      Prints the given character on the LED matrix, which will be immediately displayed. If no symbol associated with the given character can be found, an IllegalArgumentException will be thrown.
      Parameters:
      c - Character to display
    • print

      public void print(LedMatrixComponent.Symbol symbol)
      Prints the given symbol on the LED matrix, which will be immediately displayed.
      Parameters:
      symbol - Symbol to display
    • transition

      public void transition(LedMatrixComponent.Symbol symbol)
      Transitions the current LED matrix display to the given symbol by gradually scrolling the symbol in. This works by scrolling each column in one-by-one towards the default scroll direction with the default scroll delay.
      Parameters:
      symbol - New symbol to display
    • transition

      public void transition(LedMatrixComponent.Symbol symbol, Direction scrollDirection)
      Transitions the current LED matrix display to the given symbol by gradually scrolling the symbol in with the default scroll delay. This works by scrolling each column in one-by-one towards the given scroll direction with the default scroll delay.
      Parameters:
      symbol - New symbol to display
      scrollDirection - Desired scrolling direction, e.g. Direction.LEFT means the new symbol scrolls in from right towards left
    • transition

      public void transition(LedMatrixComponent.Symbol symbol, Direction scrollDirection, long scrollDelay)
      Transitions the current LED matrix display to the given symbol by gradually scrolling the symbol in. This works by scrolling each column in one-by-one towards the given scroll direction with the specified scroll delay.
      Parameters:
      symbol - New symbol to display
      scrollDirection - Desired scrolling direction, e.g. Direction.LEFT means the new symbol scrolls in from right towards left
      scrollDelay - Delay in milliseconds between each scrolled column
    • lookupSymbol

      protected LedMatrixComponent.Symbol lookupSymbol(char c)
      Returns a LedMatrixComponent.Symbol which is associated with the given ASCII character. Throws an IllegalArgumentException if no symbol associated with this character was found.
      Parameters:
      c - Character to lookup
      Returns:
      Symbol associated to character
    • draw

      public void draw(Consumer<Graphics2D> drawer)
      Initializes a blank image with the same size as the LED matrix and calls the given consumer with a Graphics2D instance. This allows to easily draw on the screen using regular drawing commands like Graphics.drawLine(int, int, int, int). The drawn image will be immediately displayed on the LED matrix.
      Parameters:
      drawer - Lambda function which draws on new image
    • draw

      public void draw(BufferedImage image, int x, int y)
      Displays a specific area of the given BufferedImage on the LED matrix by enabling LEDs for non-black colors. The area will start at the given X/Y position and has the same width and height as the LED matrix. You MUST ensure that the full width/height is still within bounds or a RasterFormatException will be thrown. The drawn image will be immediately displayed on the LED matrix.
      Parameters:
      image - Image to partially display on the LED matrix
      x - X coordinate where visible area should start
      y - Y coordinate where visible area should start
    • draw

      public void draw(BufferedImage image)
      Displays the given BufferedImage on the LED matrix by enabling LEDs for non-black colors. The passed image MUST have the same size as the LED matrix and of type BufferedImage.TYPE_BYTE_BINARY. Use the overloaded method draw(BufferedImage, int, int) to only display a specific area of a bigger image. The drawn image will be immediately displayed on the LED matrix.
      Parameters:
      image - Image to display on the LED matrix
    • getSpi

      protected com.pi4j.io.spi.Spi getSpi()
      Returns the current SPI instance for the LED matrix.
      Returns:
      SPI instance