from machine import Pin, SoftSPI
from max7219 import Max7219
import time
print("=" * 40)
print("MAX7219 双屏滚动显示")
print("上方: hello 下方: world")
print("=" * 40)
# ========== 硬件初始化 ==========
# 上方矩阵 (matrix2) - 显示 "hello"
# CLK: GPIO12, DIN: GPIO11, CS: GPIO10
spi_top = SoftSPI(
baudrate=10000000,
polarity=0,
phase=0,
sck=Pin(12),
mosi=Pin(11),
miso=Pin(13) # 不使用,但需要指定
)
display_top = Max7219(32, 8, spi_top, Pin(10))
# 下方矩阵 (matrix1) - 显示 "world"
# CLK: GPIO6, DIN: GPIO7, CS: GPIO5
spi_bottom = SoftSPI(
baudrate=10000000,
polarity=0,
phase=0,
sck=Pin(6),
mosi=Pin(7),
miso=Pin(8) # 不使用,但需要指定
)
display_bottom = Max7219(32, 8, spi_bottom, Pin(5))
# 设置亮度 (0-15)
display_top.brightness(7)
display_bottom.brightness(7)
# ========== 显示配置 ==========
text_top = "hello"
text_bottom = "world"
# 滚动速度 (毫秒,越小越快)
SCROLL_DELAY = 60
# 字符宽度 (framebuf内置8x8字体)
CHAR_WIDTH = 8
# 屏幕宽度
SCREEN_WIDTH = 32
# ========== 启动动画 ==========
print("启动动画...")
# 全亮测试
display_top.fill(1)
display_bottom.fill(1)
display_top.show()
display_bottom.show()
time.sleep_ms(300)
# 清空
display_top.fill(0)
display_bottom.fill(0)
display_top.show()
display_bottom.show()
time.sleep_ms(200)
# ========== 滚动显示函数 ==========
def scroll_text(text_top, text_bottom, delay_ms):
"""同步滚动两行文字"""
# 计算最长文字的像素宽度
width_top = len(text_top) * CHAR_WIDTH
width_bottom = len(text_bottom) * CHAR_WIDTH
max_width = max(width_top, width_bottom)
# 从右侧滚入,到左侧完全滚出
# 起始: x = SCREEN_WIDTH (文字在屏幕右侧外)
# 结束: x = -max_width (文字完全离开左侧)
for x in range(SCREEN_WIDTH, -max_width - 1, -1):
# 清除显示缓冲区
display_top.fill(0)
display_bottom.fill(0)
# 绘制文字 (text方法: string, x, y, color)
display_top.text(text_top, x, 0, 1)
display_bottom.text(text_bottom, x, 0, 1)
# 更新到LED矩阵
display_top.show()
display_bottom.show()
# 控制滚动速度
time.sleep_ms(delay_ms)
# ========== 主循环 ==========
print("开始滚动显示...")
print(f" 上方矩阵: '{text_top}'")
print(f" 下方矩阵: '{text_bottom}'")
print("-" * 40)
loop_count = 0
while True:
loop_count += 1
print(f"循环 #{loop_count}")
# 执行滚动
scroll_text(text_top, text_bottom, SCROLL_DELAY)
# 循环间隔 (可选)
time.sleep_ms(500)