from machine import Pin, SPI
from time import sleep
# SPI Configuration
spi = SPI(0, baudrate=10000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(19))
cs = Pin(17, Pin.OUT) # Chip Select (CS/LOAD)
# MAX7219 Commands
DECODE_MODE = 0x09
INTENSITY = 0x0A
SCAN_LIMIT = 0x0B
SHUTDOWN = 0x0C
DISPLAY_TEST = 0x0F
# Define letters (for this example, we use 8x8 patterns for the letters)
letters = {
'L': [
0b00011110,
0b00001100,
0b00001100,
0b00001100,
0b10001100,
0b11001100,
0b11111110,
0b00000000,
],
'C': [
0b00111100,
0b01100110,
0b00000011,
0b00000011,
0b00000011,
0b01100110,
0b00111100,
0b00000000,
],
'Y': [
0b01100110,
0b01100110,
0b00111100,
0b00011000,
0b00011000,
0b00011000,
0b00111100,
0b00000000,
]
}
def send_data(register, data):
cs.off()
spi.write(bytearray([register, data]))
cs.on()
def initialize_display():
send_data(SHUTDOWN, 0x01) # Wake up display
send_data(DECODE_MODE, 0x00) # Set decode mode
send_data(SCAN_LIMIT, 0x07) # Scan all 8 rows
send_data(INTENSITY, 0x08) # Set brightness
send_data(DISPLAY_TEST, 0x00) # Disable test mode
clear_display()
def clear_display():
for i in range(1, 9):
send_data(i, 0x00)
# Display a pattern (8x8 array)
def display_pattern(pattern):
for i in range(8):
send_data(i + 1, pattern[i])
# Function to scroll the letters
def scroll_letters(letters_to_scroll, delay=0.2):
# Convert each letter to its corresponding 8x8 pattern
patterns = [letters[char] for char in letters_to_scroll]
# Start scrolling
while True:
for offset in range(len(patterns[0]) + len(patterns) * 8):
# Create an empty 8x8 matrix to hold the combined patterns
combined = [0] * 8 # List to hold the combined data for each row
# Combine the patterns, scrolling them leftward
for i, pattern in enumerate(patterns):
shift = offset - i * 8
if shift < 0 or shift >= 8:
continue # Ignore patterns that are out of bounds
# Copy the shifted data into the combined rows
for row in range(8):
if 0 <= shift < 8:
combined[row] |= (pattern[row] >> shift) & 0xFF
display_pattern(combined)
sleep(delay)
initialize_display()
scroll_letters(['L', 'C', 'Y'])