python控制caxa模块_用Python控制硬件12-LED点阵模块(级联)

  • Post author:
  • Post category:python


87372a7a37d01d534a07f48dacd0c756.png

继续上文,这次将4个8X8的模块级联在一起,组成32X8长条形显示屏。

67ae87b51d9cc8205a9948991f6ae90c.png

相应的画布尺寸为32X8,同样基准点从左上角(0,0)开始。

c6c27d7d05cea96e7b0c9743644cd879.png

第一个实验,闪动“爱心”贴图,左右来回移动,遇到边界反弹。

# LED Matrix 32x8 moving heart
import time
from mcush import *
s = Mcush.Mcush('/dev/ttyACM0')
dp = Max7219.LED32x8(s)
cv = Max7219.Canvas(dp)
BITMAPS = [Bitmap.HEART, Bitmap.HEART2]  # 两幅贴图已集成到系统库的Bitmap模块
x, right = 0, True  # 初识位置和移动方向
while True:
    for img in BITMAPS:
        cv.clrScr( flush=False )
        cv.drawBitmap( x, 0, img )
        time.sleep(0.5)
        # 检查是否撞到两侧边界
        if right:
            x += 1  # 右移
            if x + img.width > cv.width:  # 超出边界则反转
                x = cv.width - img.width
                right = False
        else:
            x -= 1  # 右移
            if x < 0:  # 超出边界则反转
                x = 0
                right = True

ce650b68eb0774ae09c9e8d3720e6163.png

LED点阵屏显示移动的闪动爱心https://www.zhihu.com/video/1165231320543502336

第二个实验,显示数字时钟,每半秒钟刷新一次,点亮/关闭中间的“:”符号:

# LED Matrix 32x8 digital clock
import time
from mcush import *
s = Mcush.Mcush('/dev/ttyACM0')
dp = Max7219.LED32x8(s)
cv = Max7219.Canvas(dp)
sign = True  # 符号点亮标志
while True:
    tm = time.strftime("%H:%M" if sign else "%H %M")
    print(tm)
    cv.clrScr( flush=False )  # 清屏
    cv.drawString( 1, 0, tm )  # 显示并刷新
    time.sleep(0.5)
    sign = not sign  # 符号标志反转

84940d1bcff30f743112c685141454cf.png

LED点阵屏显示数字时钟https://www.zhihu.com/video/1165232448647700480

第三个实验,滚动显示字符串:因为字符宽度比显示区域宽,需要左移字符基准位置,才能看到右侧内容,逐渐加大偏移位置,达到滚动文字的效果。

# LED Matrix 32x8 moving string (horizontal)
import time
from mcush import *
s = Mcush.Mcush('/dev/ttyACM0')
dp = Max7219.LED32x8(s)
cv = Max7219.Canvas(dp)
string='Hardware control with Python'
x = cv.width
w, h = cv.getStringRenderSize(string)  # 计算显示文字的宽度
while True:
    cv.clrScr( flush=False )
    cv.drawString( x, 0, string )
    time.sleep(0.1)
    x -= 1  # 左移
    if x < -w:
        x = cv.width  # 整个文字全部超出显示区域则复位,回到最右侧

100081e4bbf059b1013822cde68d72c5.png

LED点阵屏显示滚动文字https://www.zhihu.com/video/1165233149809451008

第四个实验,随机点亮/熄灭像素:

# LED Matrix 32x8 random pixel
import time
from mcush import *
s = Mcush.Mcush('/dev/ttyACM0')
dp = Max7219.LED32x8(s)
# 点亮整个屏幕
for i in range(cv.width):
    cv.drawVLine( i, 0, cv.height-1 ) 
# 清空整个屏幕
for i in range(cv.height):
    cv.drawHLine( i, 0, cv.width-1, 0 ) 
    time.sleep(0.1)  # 延时,否则会太快
# 随机画像素
while True:
    x = randint(0, cv.width-1)
    y = randint(0, cv.height-1)
    c = randint(0, 1)  # 随机颜色
    cv.setPixel( x, y, c )
    #time.sleep(0.1)  # 适当延时

38c7338ed70f68829fdf3f97970fd156.png

LED点阵屏显示随机像素https://www.zhihu.com/video/1165236180844171264



版权声明:本文为weixin_28916013原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。