Zum Inhalt springen

LED-Matrix ansteuern

Schaltungaufbau

Anschluss des 8x8 Matrix-Displays

Codebeispiele

Beispiel 1

python
from adafruit_max7219 import matrices
import board
import busio
import digitalio
import time

# Maxtrixdisplay initialisieren
cs = digitalio.DigitalInOut(board.A4)
spi = busio.SPI(board.RX, MOSI=board.TX)
matrix = matrices.Matrix8x8(spi, cs)

while True:
    matrix.brightness(3)

    matrix.fill(1)
    matrix.pixel(3, 3)
    matrix.pixel(3, 4)
    matrix.pixel(4, 3)
    matrix.pixel(4, 4)
    matrix.show()
    time.sleep(1.0)
    matrix.scroll(-1, -1)
    matrix.show()
    time.sleep(1.0)

    matrix.clear_all()
    s = 'Hello, World!'
    for c in range(len(s)*8):
        matrix.fill(0)
        matrix.text(s,-c,0)
        matrix.show()
        time.sleep(0.05)

Beispiel 2

python
from adafruit_max7219 import matrices
import board
import busio
import digitalio
import time

# Maxtrixdisplay initialisieren
cs = digitalio.DigitalInOut(board.A4)
spi = busio.SPI(board.RX, MOSI=board.TX)
matrix = matrices.Matrix8x8(spi, cs)

# Alle LED-Pixel ausschalten
matrix.fill(0)
matrix.show()

# Pause zwischen den Bildern
frame_delay = 0.5

# Aktuelle Bildnummer
bild_nummer = 0

# Bilder der Animation als Liste von Listen von Listen
animation = [
    # frame 0 - alien #1 frame 1
    [
        [0, 0, 0, 1, 1, 0, 0, 0],
        [0, 0, 1, 1, 1, 1, 0, 0],
        [0, 1, 1, 1, 1, 1, 1, 0],
        [1, 1, 0, 1, 1, 0, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 1, 0, 0, 1, 0, 0],
        [0, 1, 0, 1, 1, 0, 1, 0],
        [1, 0, 1, 0, 0, 1, 0, 1]
    ],
    # frame 1 - alien #1 frame 2
    [
        [0, 0, 0, 1, 1, 0, 0, 0],
        [0, 0, 1, 1, 1, 1, 0, 0],
        [0, 1, 1, 1, 1, 1, 1, 0],
        [1, 1, 0, 1, 1, 0, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 1, 0, 0, 1, 0, 0],
        [0, 1, 0, 1, 1, 0, 1, 0],
        [0, 1, 0, 0, 0, 0, 1, 0]
    ]
]

while True:

    for x in range(8):
        for y in range(8):
            matrix.pixel(x, y, animation[bild_nummer][x][y])
    
    # next animation frame
    bild_nummer = bild_nummer + 1

    # show animation
    matrix.show()

    # pause for effect
    time.sleep(frame_delay)

    if bild_nummer == len(animation):
        bild_nummer = 0

Beispiel 3 - Snake Spiel

Bild
python
from adafruit_max7219 import matrices
import board
import busio
import digitalio
from adafruit_debouncer import Debouncer
import time
import random

button1 = digitalio.DigitalInOut(board.D10)
button2 = digitalio.DigitalInOut(board.D11)
button3 = digitalio.DigitalInOut(board.D12)
button4 = digitalio.DigitalInOut(board.D13)
button1.switch_to_input(pull=digitalio.Pull.DOWN)
button2.switch_to_input(pull=digitalio.Pull.DOWN)
button3.switch_to_input(pull=digitalio.Pull.DOWN)
button4.switch_to_input(pull=digitalio.Pull.DOWN)
button1_d = Debouncer(button1)
button2_d = Debouncer(button2)
button3_d = Debouncer(button3)
button4_d = Debouncer(button4)

# Maxtrixdisplay initialisieren
cs = digitalio.DigitalInOut(board.A4)
spi = busio.SPI(board.RX, MOSI=board.TX)
matrix = matrices.Matrix8x8(spi, cs)

# Maximale Helligkeit
matrix.brightness(3)
# Alle LED-Pixel ausschalten
matrix.clear_all()
matrix.show()

# Eine Liste speichert die Koordinaten der Schlangensegmente.
# Erstes Element ist der Kopf.
snake = [(3,3),(3,4)]
direction = (0,-1)
foods = []
maxfood = 4

def drawSnake():
    for segment in snake:
        matrix.pixel(segment[0],segment[1], 1)
    matrix.show()

def moveHead(x, y):
    global direction
    (oldhead_x, oldhead_y) = snake[0]
    newhead = ((oldhead_x + x) % 8, (oldhead_y + y) % 8)

    for segment in snake:
        if segment == newhead: # Game Over
            gameOver()
            return

    direction = (x, y)
    snake.insert(0, newhead)
    matrix.pixel(newhead[0],newhead[1], 1)
    matrix.show()

    if not eatFood():
        removeTail()


def removeTail():
    tail = snake.pop()
    matrix.pixel(tail[0], tail[1], 0)
    matrix.show()

def eatFood():
    for food in foods:
        if snake[0] == food:
            foods.remove(food)
            return True
    return False

def newFood():
    x = random.randint(0, 7)
    y = random.randint(0, 7)
    for segment in snake:
        if segment == (x, y):
            return
    if len(foods) >= maxfood:
        return
    foods.append((x,y))
    matrix.pixel(x, y, 1)
    matrix.show()

def gameOver():
    global snake
    global direction
    global food

    matrix.clear_all()
    s = 'Game Over!'
    for c in range(len(s)*8):
        matrix.fill(0)
        matrix.text(s,-c,0)
        matrix.show()
        time.sleep(0.05)
    matrix.clear_all()

    snake = [(3,3),(3,4)]
    direction = (0, -1)
    food = []
    drawSnake()
    lastFood = time.monotonic()
    lastMove = time.monotonic()


lastFood = time.monotonic()
lastMove = time.monotonic()
drawSnake()

while True:
    button1_d.update()
    button2_d.update()
    button3_d.update()
    button4_d.update()

    if button1_d.rose:
        direction = (0, 1)

    if button2_d.rose:
        direction = (1, 0)

    if button3_d.rose:
        direction = (-1, 0)

    if button4_d.rose:
        direction = (0, -1)

    if (time.monotonic() - lastFood) > 5:
        newFood()
        lastFood = time.monotonic()

    if (time.monotonic() - lastMove) > 0.3:
        moveHead(*direction)
        lastMove = time.monotonic()

Passende Aufgaben zu „Messen und Bewegen“

Hängst du fest? Probestunde anfragen

Inhalte von Serlo Education e.V., lizenziert unter CC BY-SA 4.0. Original auf Serlo ansehen ↗