from machine import Pin, SPI
import max7219
# Configuration for the 128x8 matrix (16 matrices of 8x8 each)
spi = SPI(0, sck=Pin(2), mosi=Pin(3))
cs = Pin(5, Pin.OUT)
# Initialize the matrix display with 16 matrices chained together (16x 8x8 modules)
display = max7219.Matrix8x8(spi, cs, 16)
# 8x8 font for digits 0-9
font = {
'0': [0x00, 0xfe, 0x82, 0x82, 0x82, 0x82, 0xfe, 0x00], # 0
'1': [0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00], # 1
'2': [0x00, 0xf2, 0x92, 0x92, 0x92, 0x9e, 0x00, 0x00], # 2
'3': [0x00, 0x92, 0x92, 0x92, 0x92, 0xfe, 0x00, 0x00], # 3
'4': [0x00, 0x0e, 0x08, 0x08, 0x08, 0xfe, 0x00, 0x00], # 4
'5': [0x00, 0x9e, 0x92, 0x92, 0x92, 0xf2, 0x00, 0x00], # 5
'6': [0x00, 0xfe, 0x92, 0x92, 0x92, 0x92, 0xf2, 0x00], # 6
'7': [0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0xfe, 0x00], # 7
'8': [0x00, 0xfe, 0x92, 0x92, 0x92, 0x92, 0xfe, 0x00], # 8
'9': [0x00, 0x9e, 0x92, 0x92, 0x92, 0x92, 0xfe, 0x00], # 9
}
# Function to convert a number into a byte array using the font table
def number_to_byte_array(number):
number_str = str(number)
byte_array = []
for digit in number_str:
byte_array.extend(font[digit])
byte_array.append(0x00) # Add a column of empty pixels as space between digits
return byte_array
# Function to display the byte arrays on the matrix
def display_data(data1, data2, data3, data4):
display.fill(0) # Clear the entire display chain
# Display data1 on the first 32 columns
for i in range(len(data1)):
for j in range(8):
if data1[i] & (1 << j):
display.pixel(i, j, 1)
else:
display.pixel(i, j, 0)
# Display data2 on the next 32 columns
for i in range(len(data2)):
for j in range(8):
if data2[i] & (1 << j):
display.pixel(i + 32, j, 1)
else:
display.pixel(i + 32, j, 0)
# Display data3 on the next 32 columns
for i in range(len(data3)):
for j in range(8):
if data3[i] & (1 << j):
display.pixel(i + 64, j, 1)
else:
display.pixel(i + 64, j, 0)
# Display data4 on the next 32 columns
for i in range(len(data4)):
for j in range(8):
if data4[i] & (1 << j):
display.pixel(i + 96, j, 1)
else:
display.pixel(i + 96, j, 0)
display.show()
# Example: Displaying the numbers 123, 456, 789, and 101
number1 = 123
number2 = 456
number3 = 789
number4 = 101
# Get the byte arrays for each number
byte_array1 = number_to_byte_array(number1)
byte_array2 = number_to_byte_array(number2)
byte_array3 = number_to_byte_array(number3)
byte_array4 = number_to_byte_array(number4)
# Ensure each number's data fits within 32 columns
byte_array1 = byte_array1[:32]
byte_array2 = byte_array2[:32]
byte_array3 = byte_array3[:32]
byte_array4 = byte_array4[:32]
# Display the data
display_data(byte_array1, byte_array2, byte_array3, byte_array4)