.. note::

    Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

    **Why Join?**

    - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
    - **Learn & Share**: Exchange tips and tutorials to enhance your skills.
    - **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
    - **Special Discounts**: Enjoy exclusive discounts on our newest products.
    - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.

    👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!

.. _1.1.6_py:

1.1.6 LED Dot Matrix
=====================

Introduction
--------------------

As the name suggests, an LED dot matrix is a matrix composed of LEDs.
The lighting up and dimming of the LEDs formulate different characters
and patterns.

Required Components
------------------------------

In this project, we need the following components. 

.. image:: ../img/list_dot.png

It's definitely convenient to buy a whole kit, here's the link: 

.. list-table::
    :widths: 20 20 20
    :header-rows: 1

    *   - Name	
        - ITEMS IN THIS KIT
        - LINK
    *   - Raphael Kit
        - 337
        - |link_Raphael_kit|

You can also buy them separately from the links below.

.. list-table::
    :widths: 30 20
    :header-rows: 1

    *   - COMPONENT INTRODUCTION
        - PURCHASE LINK

    *   - :ref:`cpn_gpio_extension_board`
        - |link_gpio_board_buy|
    *   - :ref:`cpn_breadboard`
        - |link_breadboard_buy|
    *   - :ref:`cpn_wires`
        - |link_wires_buy|
    *   - :ref:`cpn_dot_matrix`
        - |link_led_matrix_buy|

Schematic Diagram
-----------------------

============ ======== ======== ====
T-Board Name physical wiringPi BCM
SPIMOSI      Pin 19   12       MOSI
SPICE0       pin 24   10       CE0
SPISCLK      Pin 23   14       SCLK
============ ======== ======== ====

.. image:: ../img/schematic_dot.png

Experimental Procedures
----------------------------

**Step 1:** Build the circuit. 

.. image:: ../img/1.1.6fritzing.png

**Step 2:** Turn on the SPI before starting the experiment, refer to :ref:`spi_configuration` for details. 

**Step 3:** Install :ref:`Luma.LED_Matrix` module.

**Step 4:** Change directory.

.. raw:: html

   <run></run>

.. code-block::

    cd ~/raphael-kit/python-pi5

**Step 5:** Runs the ``1.1.6_LedMatrix.py`` Python script using the Python interpreter from the ``my_env`` virtual environment.

.. raw:: html

   <run></run>

.. code-block::

    sudo ~/my_env/bin/python3 1.1.6_LedMatrix.py

After running the code, the LED Matrix will display a rectangle for two seconds, then the text 'A' for two seconds, and finally scroll to display the text "Hello, Nice to meet you!

**Code**

.. note::

    You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to  source code path like ``raphael-kit/python``. After modifying the code, you can run it directly to see the effect.


.. raw:: html

    <run></run>

.. code-block:: python

   from luma.core.interface.serial import spi, noop
   from luma.core.render import canvas
   from luma.core.virtual import viewport
   from luma.led_matrix.device import max7219
   from luma.core.legacy import text
   from luma.core.legacy.font import proportional, CP437_FONT, LCD_FONT
   import time

   serial = spi(port=0, device=0, gpio=noop())
   device = max7219(serial, rotate=1)
   virtual = viewport(device, width=200, height=400)

   def displayRectangle():
      with canvas(device) as draw:
         draw.rectangle(device.bounding_box, outline="white", fill="black")

   def displayLetter():
      with canvas(device) as draw:
         text(draw, (0, 0), "A", fill="white", font=proportional(CP437_FONT))

   def scrollToDisplayText():
      with canvas(virtual) as draw:
         text(draw, (0, 0), "Hello, Nice to meet you!", fill="white", font=proportional(CP437_FONT))

      for offset in range(150):
         virtual.set_position((offset,0))
         time.sleep(0.1)

   def main():
      while True:
         displayRectangle()
         time.sleep(2)
         displayLetter()
         time.sleep(2)
         scrollToDisplayText()

   def destroy():
      pass

   if __name__ == '__main__':
      try:
         main()
      except KeyboardInterrupt:
         destroy()

**Code Explanation**

.. code-block:: python

    from luma.core.interface.serial import spi, noop
    from luma.core.render import canvas
    from luma.core.virtual import viewport
    from luma.led_matrix.device import max7219
    from luma.core.legacy import text
    from luma.core.legacy.font import proportional, CP437_FONT, LCD_FONT
    import time

Import the relevant libraries, of which ``luma.core`` is a component library that provides a Pillow-compatible canvas for Python 3, as well as other drawing primitives and text rendering features that support small displays on Raspberry Pi and other single-board computers.
You can visit `https://luma-core.readthedocs.io/en/latest/intro.html <https://luma-core.readthedocs.io/en/latest/intro.html>`_ to learn more.


.. code-block:: python

    serial = spi(port=0, device=0, gpio=noop())
    device = max7219(serial, rotate=1)

Initialize the ``luma.led_matrix.device.max7219`` class.

.. note::

    If you want to modify the display direction of the LED Matrix, you can do so by modifying the value of rotate, where 0 means no rotation, 1 means 90° clockwise rotation, 2 means 180° rotation, and 3 means 270° clockwise rotation.

.. code-block:: python

    def displayRectangle():
        with canvas(device) as draw:
            draw.rectangle(device.bounding_box, outline="white", fill="black")

Display a hollow rectangle in the edge area of the LED Matrix and modify the value of ``fill`` to ``white`` to display a solid rectangle.


.. code-block:: python

    def displayLetter():
        with canvas(device) as draw:
            text(draw, (0, 0), "A", fill="white", font=proportional(CP437_FONT))

An "A" is displayed on the (0, 0) coordinate of the LED Matrix, where ``CP437_FONT`` is a font suitable for 8*8 dot matrix screens.

.. code-block:: python

    virtual = viewport(device, width=200, height=400)

There is no way to display a line of text in a single 8x8 LED matrix. We need to use the ``luma.core.virtual.viewport`` method so that the text can be scrolled through the virtual viewport.

.. code-block:: python

    def scrollToDisplayText():
        with canvas(virtual) as draw:
            text(draw, (0, 0), "Hello, Nice to meet you!", fill="white", font=proportional(CP437_FONT))

        for offset in range(150):
            virtual.set_position((offset,0))
            time.sleep(0.1)

``scrollToDisplayText()`` implements "Hello, Nice to meet you!" as a scrolling text on the LED Matrix.

First, we pass virtual as an argument to the ``canvas()`` function, so that we can use the virtual window as the current display window. Then the ``text()`` function displays "Hello, Nice to meet you!" on the LED Matrix.

Using the for loop function, we move the virtual window in the X direction so that we can see the "Hello, Nice to meet you!" text scrolling.


Phenomenon Picture
-----------------------

.. image:: ../img/1.1.6led_dot_matrix.JPG