一、目的

        这一节我们学习如何使用我们的ESP32开发板来控制控制8组max7219 LED矩阵屏显示。

二、环境

        ESP32 + 8组max7219 LED矩阵屏 + Thonny IDE + 几根杜邦线

接线方法:同65节

三、大字体代码

 代码如下:

import time
import max7219
from machine import Pin, SoftSPI, RTC


class Clock:
    """
    定义可以用来控制32x16的点阵屏类
    """
    def __init__(self):
        self.dp()
        self.se = 0
        self.rtc = RTC()
        self.number_font = {
            0: [
                    (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6), 
                ],
            1: [
                    (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (36, 0), (36, 1), (36, 2), (36, 3), (36, 4), (36, 5), (36, 6), 
                ],
            2: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            3: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            4: [
                    (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6)
                ],
            5: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            6: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            7: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), 
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6)
                ],
            8: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            9: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
        }

    def dp(self):
        spi = SoftSPI(baudrate=100000, polarity=1, phase=0, mosi=Pin(13), sck=Pin(14), miso=Pin(23))
        self.display = max7219.Matrix8x8(spi, Pin(12), 8)

    def show_time(self):
        """
        date = self.rtc.datetime()
        self.m = date[5]
        self.h = date[4]
        self.display.fill(0)
        self.display.text(str(self.h) if len(str(self.h))==2 else '0' + str(self.h) , 0, 1)
        self.display.pixel(16, 2, self.se)        
        self.display.pixel(16, 4, self.se)        
        self.display.text(str(self.m) if len(str(self.m))==2 else '0' + str(self.m) , 17, 1)
        self.se = 0 if self.se == 1 else 1
        self.display.text('abAB456789', 0, 0)
        """
        
        for i in range(10):
            self.display.fill(0)
            for x, y in self.number_font[i]:
                self.display.pixel(x, y, 1)
            self.display.show()
            time.sleep(0.2)
        


# 1. 创建对象
clock = Clock()

# 2. 调用显示
clock.show_time()
"""
while True:
    clock.show_time()
    time.sleep(1)
    break
"""

 显示效果:

四、时间代码

import time
import max7219
from machine import Pin, SoftSPI, RTC


class Clock:
    """
    定义可以用来控制32x16的点阵屏类
    """
    def __init__(self):
        self.dp()
        self.se = 0
        self.rtc = RTC()
        self.number_font = {
            0: [
                    (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6), 
                ],
            1: [
                    (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (36, 0), (36, 1), (36, 2), (36, 3), (36, 4), (36, 5), (36, 6), 
                ],
            2: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            3: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            4: [
                    (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6)
                ],
            5: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            6: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            7: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), 
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6)
                ],
            8: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            9: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
        }

    def dp(self):
        spi = SoftSPI(baudrate=100000, polarity=1, phase=0, mosi=Pin(13), sck=Pin(14), miso=Pin(23))
        self.display = max7219.Matrix8x8(spi, Pin(12), 8)

    def show_time(self):
        """
        date = self.rtc.datetime()
        self.m = date[5]
        self.h = date[4]
        self.display.fill(0)
        self.display.text(str(self.h) if len(str(self.h))==2 else '0' + str(self.h) , 0, 1)
        self.display.pixel(16, 2, self.se)        
        self.display.pixel(16, 4, self.se)        
        self.display.text(str(self.m) if len(str(self.m))==2 else '0' + str(self.m) , 17, 1)
        self.se = 0 if self.se == 1 else 1
        self.display.text('abAB456789', 0, 0)
        
        
        for i in range(10):
            self.display.fill(0)
            for x, y in self.number_font[i]:
                self.display.pixel(x, y, 1)
            self.display.show()
            time.sleep(0.2)
        """
        
        hour = 22
        minute = 33
        
        # 清空
        self.display.fill(0)
        
        # 时(2位)
        for i, hour_temp in enumerate("%02d" % hour):
            for x, y in self.number_font[int(hour_temp)]:
                hour_offset = 7 if i == 1 else 0
                self.display.pixel(x + hour_offset, y, 1)
        # 2个点
        self.display.pixel(16, 6, 1)
        self.display.pixel(48, 1, 1)
        
        # 分(2位)
        for i, minute_temp in enumerate("%02d" % minute):
            for x, y in self.number_font[int(minute_temp)]:
                minute_offset = 24 if i == 1 else 17
                self.display.pixel(x + minute_offset, y, 1)
        
        # 显示
        self.display.show()
        


# 1. 创建对象
clock = Clock()

# 2. 调用显示
clock.show_time()
"""
while True:
    clock.show_time()
    time.sleep(1)
    break
"""

显示效果

五、网络时间代码

import time
import max7219
from machine import Pin, SoftSPI, RTC
import network
import ntptime


class Clock:
    """
    定义可以用来控制32x16的点阵屏类
    """
    def __init__(self, wifi, password):
        self.dp()
        self.se = 0
        self.rtc = RTC()
        self.wifi = wifi
        self.password = password
        self.ntp()  # 调用这个方法来联网设置时间
        self.second_light = False
        self.number_font = {
            0: [
                    (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6), 
                ],
            1: [
                    (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (36, 0), (36, 1), (36, 2), (36, 3), (36, 4), (36, 5), (36, 6), 
                ],
            2: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            3: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            4: [
                    (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6)
                ],
            5: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            6: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            7: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), 
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6)
                ],
            8: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (33, 0), (33, 1), (33, 2), (33, 3), (33, 4), (33, 5), (33, 6), (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
            9: [
                    (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),  (2, 7), (3, 7), (4, 7), (5, 7), (6, 7),
                    (38, 0), (38, 1), (38, 2), (38, 3), (38, 4), (38, 5), (38, 6), (33, 6), (34, 6), (35, 6), (36, 6), (37, 6), (38, 6),
                ],
        }

    def dp(self):
        spi = SoftSPI(baudrate=100000, polarity=1, phase=0, mosi=Pin(13), sck=Pin(14), miso=Pin(23))
        self.display = max7219.Matrix8x8(spi, Pin(12), 8)
        
    def connect_wifi(self):
        wlan = network.WLAN(network.STA_IF)
        wlan.active(True)
        if not wlan.isconnected():
            wlan.connect(self.wifi, self.password)

    def ntp(self):
        self.connect_wifi()
        time.sleep(2)
        ntptime.host="ntp1.aliyun.com"
        ntptime.NTP_DELTA = 3155644800  # 东八区 UTC+8偏移时间(秒)
        try:
            ntptime.settime()
            print("联网成功...")
        except Exception as e:
            pass

    def show_time(self):
        """
        date = self.rtc.datetime()
        self.m = date[5]
        self.h = date[4]
        self.display.fill(0)
        self.display.text(str(self.h) if len(str(self.h))==2 else '0' + str(self.h) , 0, 1)
        self.display.pixel(16, 2, self.se)        
        self.display.pixel(16, 4, self.se)        
        self.display.text(str(self.m) if len(str(self.m))==2 else '0' + str(self.m) , 17, 1)
        self.se = 0 if self.se == 1 else 1
        self.display.text('abAB456789', 0, 0)
        
        
        for i in range(10):
            self.display.fill(0)
            for x, y in self.number_font[i]:
                self.display.pixel(x, y, 1)
            self.display.show()
            time.sleep(0.2)
        """
        
        # 获取真正的时间
        date = self.rtc.datetime()
        hour = date[4]
        minute = date[5]
        
        # 清空
        self.display.fill(0)
        
        # 时(2位)
        for i, hour_temp in enumerate("%02d" % hour):
            for x, y in self.number_font[int(hour_temp)]:
                hour_offset = 7 if i == 1 else 0
                self.display.pixel(x + hour_offset, y, 1)
        # 2个点
        self.display.pixel(16, 6, 1 if self.second_light else 0)
        self.display.pixel(48, 1, 1 if self.second_light else 0)
        self.second_light = not self.second_light
        
        # 分(2位)
        for i, minute_temp in enumerate("%02d" % minute):
            for x, y in self.number_font[int(minute_temp)]:
                minute_offset = 24 if i == 1 else 17
                self.display.pixel(x + minute_offset, y, 1)
        
        # 显示
        self.display.show()
        


# 1. 创建对象
clock = Clock("无线名字", "无线密码")

# 2. 调用显示

while True:
    clock.show_time()
    time.sleep(1)

演示效果:

六、数字滚动

 

import time
import max7219
from machine import Pin, SoftSPI, RTC


class Clock:
    """
    定义可以用来控制32x16的点阵屏类
    """
    def __init__(self):
        self.dp()
        self.se = 0
        self.rtc = RTC()
        self.number_font = {
            0: [
                    [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [33, 0], [33, 1], [33, 2], [33, 3], [33, 4], [33, 5], [33, 6], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6], 
                ],
            1: [
                    [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [4, 7], [36, 0], [36, 1], [36, 2], [36, 3], [36, 4], [36, 5], [36, 6], 
                ],
            2: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [33, 0], [33, 1], [33, 2], [33, 3], [33, 4], [33, 5], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            3: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            4: [
                    [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6]
                ],
            5: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7],  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            6: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7],  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [33, 0], [33, 1], [33, 2], [33, 3], [33, 4], [33, 5], [33, 6], [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            7: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], 
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6]
                ],
            8: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7],  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [33, 0], [33, 1], [33, 2], [33, 3], [33, 4], [33, 5], [33, 6], [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            9: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7],  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
        }

    def dp(self):
        spi = SoftSPI(baudrate=100000, polarity=1, phase=0, mosi=Pin(13), sck=Pin(14), miso=Pin(23))
        self.display = max7219.Matrix8x8(spi, Pin(12), 8)

    def show_time(self):
        for i in range(10):
            # 复制一份当前要显示的文字的编码
            led_point_temp_list = [[x[0], x[1]] for x in self.number_font.get(i)]
            
            print("处理之前", led_point_temp_list)

            # 对每个编码向上提升
            for k, (x, y) in enumerate(led_point_temp_list):
                
                if x < 32:  # 找到上部
                    y -= 16
                    led_point_temp_list[k][1] = y
                else: # 下半部分
                    y -= 8
                    x -= 32
                    led_point_temp_list[k][0] = x
                    led_point_temp_list[k][1] = y
                    
            print("处理之后", led_point_temp_list)
            
            for j in range(32):  # 从中间下落
                print("当前要显示的文字是:", i)
                self.display.fill(0)
                for k, (x, y) in enumerate(led_point_temp_list):
                    
                    if x < 32:  # 找到上部
                        if y == 7:
                            x = x + 32
                            led_point_temp_list[k][0] = x
                        if y >= 0:
                            y = (y + 1) % 8
                        else:
                            y += 1
                        led_point_temp_list[k][1] = y
                    else: # 下半部分
                        y += 1
                        led_point_temp_list[k][1] = y
                    # print("当前显示的点", x, y)
                    if 0 <= y <= 7:
                        self.display.pixel(x, y, 1)
                
                self.display.show()
                
                time.sleep_ms(20 + j)
            
                if j == 15:
                    time.sleep(0.5)
        


# 1. 创建对象
clock = Clock()

# 2. 调用显示
clock.show_time()
"""
while True:
    clock.show_time()
    time.sleep(1)
    break
"""

 演示效果

七、动态滚动显示网络时间

 

import time
import max7219
from machine import Pin, SoftSPI, RTC


class Clock:
    """
    定义可以用来控制32x16的点阵屏类
    """
    def __init__(self):
        self.dp()
        self.se = 0
        self.rtc = RTC()
        self.last_time = None
        self.second_light = False
        self.number_font = {
            0: [
                    [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [33, 0], [33, 1], [33, 2], [33, 3], [33, 4], [33, 5], [33, 6], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6], 
                ],
            1: [
                    [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [4, 7], [36, 0], [36, 1], [36, 2], [36, 3], [36, 4], [36, 5], [36, 6], 
                ],
            2: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [33, 0], [33, 1], [33, 2], [33, 3], [33, 4], [33, 5], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            3: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            4: [
                    [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6]
                ],
            5: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7],  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            6: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7],  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [33, 0], [33, 1], [33, 2], [33, 3], [33, 4], [33, 5], [33, 6], [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            7: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], 
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6]
                ],
            8: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7],  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [33, 0], [33, 1], [33, 2], [33, 3], [33, 4], [33, 5], [33, 6], [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
            9: [
                    [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7],  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
                    [38, 0], [38, 1], [38, 2], [38, 3], [38, 4], [38, 5], [38, 6], [33, 6], [34, 6], [35, 6], [36, 6], [37, 6], [38, 6],
                ],
        }

    def dp(self):
        spi = SoftSPI(baudrate=100000, polarity=1, phase=0, mosi=Pin(13), sck=Pin(14), miso=Pin(23))
        self.display = max7219.Matrix8x8(spi, Pin(12), 8)
        
    def static_one_number(self, number, offset):
        # 显示1位静止不动的数字
        for x, y in self.number_font[int(number)]:
            self.display.pixel(x + offset, y, 1)
        
    def show_1_move_3_static_number(self, old_4_numbers="1234", new_4_numbers="1235"):
        # 滚动显示1位数字 静态显示3位数字
        static_number_list = []
        move_number_list = []
        for i, temp in enumerate(new_4_numbers):
            if i == 0:
                number_offset = 0
            elif i == 1:
                number_offset = 7
            elif i == 2:
                number_offset = 17
            else:
                number_offset = 24
            if temp == old_4_numbers[i]:
                static_number_list.append([int(temp), number_offset])
            else:
                move_number_list.append([int(temp), number_offset])
        
        if len(static_number_list) == 4:
            # 显示所有的4个不变的数字
            self.display.fill(0)
            for number, static_number_offset in static_number_list:
                self.static_one_number(number, static_number_offset)
            # 显示2个点
            self.display.pixel(16, 6, 1 if self.second_light else 0)
            self.display.pixel(48, 1, 1 if self.second_light else 0)
            self.display.show()
            return
        
        led_point_temp_list = []
        for number, number_offset in move_number_list:
            
            # 复制一份当前要显示的文字的编码
            # led_point_temp_list = self.number_font[number][0:]  # 这种方法会出现浅拷贝的问题,假如[[x, y], [x2, y2]] 那么[x, y], [x2, y2]是公用的
            led_point_temp_list.extend([[x[0], x[1], number_offset] for x in self.number_font.get(number)])  # 相当于深拷贝
            # print("id1=,", id(self.number_font[number]), "id2=", id(led_point_temp_list))
            # print("id1[0]=", id(self.number_font[number][0]), "id2[0]=", id(led_point_temp_list[0]))
            
        # print("处理之前", led_point_temp_list)

        # 对每个编码向上提升
        for k, (x, y, number_offset) in enumerate(led_point_temp_list):
            
            if x < 32:  # 找到上部
                y -= 16
                led_point_temp_list[k][1] = y
            else: # 下半部分
                y -= 8
                x -= 32
                led_point_temp_list[k][0] = x
                led_point_temp_list[k][1] = y
                
        # print("处理之后", led_point_temp_list)
        
        for j in range(16):  # 从中间下落
            print("当前要显示的文字是:", number)
            self.display.fill(0)
            
            # 显示2个点
            self.display.pixel(16, 6, 1 if self.second_light else 0)
            self.display.pixel(48, 1, 1 if self.second_light else 0)
            
            # 显示需要移动的数字
            for k, (x, y, number_offset) in enumerate(led_point_temp_list):
                
                if x < 32:  # 找到上部
                    if y == 7:
                        x = x + 32
                        led_point_temp_list[k][0] = x
                    if y >= 0:
                        y = (y + 1) % 8
                    else:
                        y += 1
                    led_point_temp_list[k][1] = y
                else: # 下半部分
                    y += 1
                    led_point_temp_list[k][1] = y
                # print("当前显示的点", x, y)
                if 0 <= y <= 7:
                    self.display.pixel(x + number_offset, y, 1)
            
            # 显示固定不同的另外3位数字
            for number, static_number_offset in static_number_list:
                self.static_one_number(number, static_number_offset)
            
            self.display.show()
            
            time.sleep_ms(20 + j)
        
            if j == 15:
                time.sleep(0.2)

    def show_time(self):
        date = self.rtc.datetime()
        current_time = "%02d%02d" % (date[5], date[6])

        if self.last_time is None:
            self.last_time = current_time
        self.show_1_move_3_static_number(self.last_time, current_time)
        self.last_time = current_time

        # 交替2个点
        self.second_light = not self.second_light


# 1. 创建对象
clock = Clock()

# 2. 调用显示

while True:
    clock.show_time()
    time.sleep(0.2)
    print("----")


八、购买

某宝链接如下:
https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.15.7cba6a4b43bAXC&id=618274380277icon-default.png?t=M85Bhttps://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.15.7cba6a4b43bAXC&id=618274380277

更多推荐