> For the complete documentation index, see [llms.txt](https://s761111.gitbook.io/raspi-sensor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://s761111.gitbook.io/raspi-sensor/max7219-ju-shi-led.md).

# MAX7219與矩陣式LED

樹莓派透過SPI協定與MAX7219進行溝通，再由這個控制晶片去控制矩陣式LED，日後當面對較為複雜的零組件時，通常不會由樹莓派直接去操控它，而是透過其它晶片去間接操控，這些控制晶片除了官方本身提供相關的驅動程式或是相關函數外，也有許多網路熱心人士提供更多的Python模組，這次使用的是：

{% embed url="<https://github.com/rm-hull/luma.led_matrix>" %}

{% embed url="<https://youtu.be/sB79wyqsbAo>" %}

```python
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi
from luma.core.render import canvas
from luma.core.legacy import text, show_message
from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
import time

serial = spi(port=0, device=0)
device = max7219(serial, cascaded=1, block_orientation=0, rotate=0)
print("Created device")
# start demo
msg = "12345678"
print(msg)
show_message(device, msg, fill="white", font=proportional(CP437_FONT))
time.sleep(1)

msg = "Slow"
print(msg)
show_message(device, msg, fill="white", font=proportional(LCD_FONT), scroll_delay=0.1)

time.sleep(1)
device.contrast(0x60)
with canvas(device) as draw:
    text(draw, (0, 0), "A", fill="white", font=proportional(CP437_FONT))
time.sleep(2)

with canvas(device) as draw:
    draw.rectangle(device.bounding_box, outline="white", fill="black")
    # time.sleep(2)
    # 僅一個 8*8 看不出橢圓的形狀
    # draw.ellipse([(0, 0), (7, 7)], outline="white", fill="black")

time.sleep(2)

for _ in range(5):
    for intensity in range(16):
        device.contrast(intensity * 16)
        time.sleep(0.1)

device.contrast(0x80)
time.sleep(1)
```
