该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
文章来源于:http://bbs.ickey.cn/group-topic-id-43142.html
本代码基于Python,通过Python调用WiringPi2库来驱动基于SPI的GPIO扩展板来显示内容。
硬件列表:
1、树莓派B+(感谢Ickey)
2、GPIO扩展板(感谢老邓)
3、LED矩阵(感谢老邓)
完整程序代码如下:
# -*- coding: utf-8 -*-
# -*- code by FishX -*-
# -*- LED Matrix by Mr Old Deng -*_
import wiringpi2 as wiringpi
import time
# Vars
OUTPUT = 1
INPUT = 0
HIGH = 1
LOW = 0
smile = [[0,0,1,1,1,1,0,0]
,[0,1,0,0,0,0,1,0]
,[1,0,1,0,0,1,0,1]
,[1,0,0,0,0,0,0,1]
,[1,0,1,0,0,1,0,1]
,[1,0,0,1,1,0,0,1]
,[0,1,0,0,0,0,1,0]
,[0,0,1,1,1,1,0,0]]
Space = [[0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0]]
Full = [[1,1,1,1,1,1,1,1]
,[1,1,1,1,1,1,1,1]
,[1,1,1,1,1,1,1,1]
,[1,1,1,1,1,1,1,1]
,[1,1,1,1,1,1,1,1]
,[1,1,1,1,1,1,1,1]
,[1,1,1,1,1,1,1,1]
,[1,1,1,1,1,1,1,1]]
F = [[0,0,0,0,0,0,0,0]
,[0,0,1,1,1,1,1,0]
,[0,0,1,0,0,0,0,0]
,[0,0,1,1,1,1,0,0]
,[0,0,1,0,0,0,0,0]
,[0,0,1,0,0,0,0,0]
,[0,0,1,0,0,0,0,0]
,[0,0,0,0,0,0,0,0]]
i = [[0,0,0,0,0,0,0,0]
,[0,0,0,1,1,0,0,0]
,[0,0,0,0,0,0,0,0]
,[0,0,0,0,1,0,0,0]
,[0,0,0,0,1,0,0,0]
,[0,0,0,0,1,0,0,0]
,[0,0,0,1,1,1,0,0]
,[0,0,0,0,0,0,0,0]]
s = [[0,0,0,0,0,0,0,0]
,[0,0,0,1,1,1,0,0]
,[0,0,1,0,0,0,0,0]
,[0,0,0,1,0,0,0,0]
,[0,0,0,0,1,0,0,0]
,[0,0,0,0,0,1,0,0]
,[0,0,1,1,1,0,0,0]
,[0,0,0,0,0,0,0,0]]
h = [[0,0,0,0,0,0,0,0]
,[0,0,1,0,0,0,0,0]
,[0,0,1,0,0,0,0,0]
,[0,0,1,1,0,0,0,0]
,[0,0,1,0,1,0,0,0]
,[0,0,1,0,1,0,0,0]
,[0,0,1,0,1,0,0,0]
,[0,0,0,0,0,0,0,0]]
X = [[0,0,0,0,0,0,0,0]
,[0,1,0,0,0,0,1,0]
,[0,0,1,0,0,1,0,0]
,[0,0,0,1,1,0,0,0]
,[0,0,0,1,1,0,0,0]
,[0,0,1,0,0,1,0,0]
,[0,1,0,0,0,0,1,0]
,[0,0,0,0,0,0,0,0]]
FishX = []
def initWiringPi():
wiringpi.wiringPiSetup()
#(const int pinBase, const int numPins, const int dataPin, const int clockPin, const int latchPin)
wiringpi.sr595Setup(100, 32, 12, 14, 10)
def initMatrix():
for i in range(0,32):
wiringpi.pinMode(100 + i,OUTPUT)
for i in range(0,8):
wiringpi.digitalWrite(100 + i,HIGH)
wiringpi.digitalWrite(108 + i,HIGH)
wiringpi.digitalWrite(116 + i,HIGH)
def clearMatrix(c):
for i in range(0,8):
if c is 'b':
wiringpi.digitalWrite(100 + i,HIGH)
elif c is 'g':
wiringpi.digitalWrite(108 + i,HIGH)
else:
wiringpi.digitalWrite(116 + i,HIGH)
wiringpi.digitalWrite(124 + i,LOW)
def clearDot(x,y,c):
if c is 'b':
wiringpi.digitalWrite(100 + x,HIGH)
elif c is 'g':
wiringpi.digitalWrite(108 + x,HIGH)
else:
wiringpi.digitalWrite(116 + x,HIGH)
wiringpi.digitalWrite(124 + y,LOW)
# 124-131:Y ,100-107 RedX,108-115,GreenX,116-123,RedX
def drawDot(x,y,c):
if c is 'r':
wiringpi.digitalWrite(116 + x,LOW)
elif c is 'g':
wiringpi.digitalWrite(108 + x,LOW)
elif c is 'b':
wiringpi.digitalWrite(100 + x,LOW)
wiringpi.digitalWrite(124 + y,HIGH)
clearDot(x,y,c)
def drawChar(c,l):
global F,i,s,h,X
matrix = []
if c is 'F':
matrix = F
elif c is 'i':
matrix = i
elif c is 's':
matrix = s
elif c is 'h':
matrix = h
elif c is 'X':
matrix = X
for x in range(0,8):
for y in range(0,8):
if matrix[x][y] is 1:
drawDot(y,x,l)
def drawMatrix(m,c,t):
tt = time.time()
while True:
for x in range(0,len(m)):
for y in range(0,len(m[0])):
if m[x][y] is 1:
drawDot(y,x,c)
if time.time() - tt > t:
break;
def timeChar(c,t,l):
tt = time.time()
while True:
drawChar(c,l)
if time.time() - tt > t:
break;
def genFishX():
global FishX
FishX = Space
for j in range(0,8):
FishX[j].extend(F[j][0:5])
for j in range(0,8):
FishX[j].extend(i[j][0:7])
for j in range(0,8):
FishX[j].extend(s[j][0:7])
for j in range(0,8):
FishX[j].extend(h[j][0:7])
for j in range(0,8):
FishX[j].extend(X[j][0:7])
for j in range(0,8):
FishX[j].extend(Space[j][0:5])
def genActualMatrix(arrMatrix,xpos):
r = []
for i in range(0,len(arrMatrix)):
r.append(arrMatrix[i][xpos:xpos+8])
return r
def drawTimeChar():
timeChar('F',0.5,'r')
timeChar('i',0.5,'r')
timeChar('s',0.5,'r')
timeChar('h',0.5,'r')
timeChar('X',0.5,'r')
def drawAniChar():
for z in range(0,len(FishX[0]) - 6):
a = genActualMatrix(FishX,z)
drawMatrix(a,'g',0.3)
initWiringPi()
initMatrix()
genFishX()
while True:
# drawTimeChar()
# time.sleep(1)
drawAniChar()
time.sleep(1)