我是 python 的初学者,但想创建一个简单的硬件提醒来放置正确的 bin。也许在这个过程中学到一些东西。我的设置是 Pimoroni 的 Raspberry Pico 和 Pico-Unicorn 帽子。目标是每周四滚动提醒。首先我用单一颜色制作它,然后我想做一个特定的渐变
代码的要点是我使用了嵌套列表,其中列表的 [x][y] 对应于 pico-unicorn led 矩阵的 x 和 y。[x][y] 的值将是 0(熄灭)或十六进制颜色(以十六进制颜色点亮)。
缺少学习汇编,我可以用代码做什么?我的方法是否存在根本缺陷,或者只是 pico 很慢?它有效,不要误会我的意思,但如果我能做些什么来让它更好/更快——我只想学习最佳实践。另外,我研究过使用 framebuf 库,但它有一个我不喜欢的固定字体。感谢任何提示。
import picounicorn as display
import utime
import math
utime.sleep(0.5)
display.init()
ledwidth= display.get_width()
ledheight = display.get_height()
def displayoff():
return [[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,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]]
def lettercolumn(letter):
if letter == "H":
return [[0,1,1,1,1,1,0],[0,0,0,1,0,0,0],[0,0,0,1,0,0,0],[0,0,0,1,0,0,0],[0,1,1,1,1,1,0]]
elif letter == "A":
return [[0,1,1,1,1,1,0],[0,1,0,1,0,0,0],[0,1,0,1,0,0,0],[0,1,0,1,0,0,0],[0,1,1,1,1,1,0]]
elif letter == "B":
return [[0,1,1,1,1,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,0,1,1,1,0,0]]
elif letter == "C":
return [[0,1,1,1,1,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0]]
elif letter == "D":
return [[0,1,1,1,1,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,0,1,1,1,0,0]]
elif letter == "E":
return [[0,1,1,1,1,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,0,0,1,0]]
elif letter == "F":
return [[0,1,1,1,1,1,0],[0,1,0,1,0,0,0],[0,1,0,1,0,0,0],[0,1,0,1,0,0,0],[0,1,0,0,0,0,0]]
elif letter == "G":
return [[0,1,1,1,1,1,0],[0,1,0,0,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,1,1,0]]
elif letter == "I":
return [[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,1,1,1,1,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0]]
elif letter == "J":
return [[0,1,0,0,1,1,0],[0,1,0,0,0,1,0],[0,1,1,1,1,1,0],[0,1,0,0,0,0,0],[0,0,0,0,0,0,0]]
elif letter == "K":
return [[0,1,1,1,1,1,0],[0,0,0,1,0,0,0],[0,0,1,0,1,0,0],[0,1,0,0,0,1,0],[0,0,0,0,0,0,0]]
elif letter == "L":
return [[0,1,1,1,1,1,0],[0,0,0,0,0,1,0],[0,0,0,0,0,1,0],[0,0,0,0,0,1,0],[0,0,0,0,0,0,0]]
elif letter == "M":
return [[0,1,1,1,1,1,0],[0,0,1,0,0,0,0],[0,0,0,1,0,0,0],[0,0,1,0,0,0,0],[0,1,1,1,1,1,0]]
elif letter == "N":
return [[0,1,1,1,1,1,0],[0,0,1,0,0,0,0],[0,0,0,1,0,0,0],[0,0,0,0,1,0,0],[0,1,1,1,1,1,0]]
elif letter == "O":
return [[0,1,1,1,1,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,1,1,1,1,1,0]]
elif letter == "P":
return [[0,1,1,1,1,1,0],[0,1,0,1,0,0,0],[0,1,0,1,0,0,0],[0,1,0,1,0,0,0],[0,1,1,1,0,0,0]]
elif letter == "Q":
return [[0,1,1,1,1,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,1,0,0,1,1,0],[0,1,1,1,1,1,0]]
elif letter == "R":
return [[0,1,1,1,1,1,0],[0,1,0,1,0,0,0],[0,1,0,1,0,0,0],[0,1,0,1,1,0,0],[0,1,1,1,0,1,0]]
elif letter == "S":
return [[0,1,1,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,1,1,0]]
elif letter == "T":
return [[0,1,0,0,0,0,0],[0,1,0,0,0,0,0],[0,1,1,1,1,1,0],[0,1,0,0,0,0,0],[0,1,0,0,0,0,0]]
elif letter == "U":
return [[0,1,1,1,1,1,0],[0,0,0,0,0,1,0],[0,0,0,0,0,1,0],[0,0,0,0,0,1,0],[0,1,1,1,1,1,0]]
elif letter == "V":
return [[0,1,1,0,0,0,0],[0,0,0,1,1,0,0],[0,0,0,0,0,1,0],[0,0,0,1,1,0,0],[0,1,1,0,0,0,0]]
elif letter == "W":
return [[0,1,1,1,1,1,0],[0,0,0,0,0,1,0],[0,0,0,0,1,0,0],[0,0,0,0,0,1,0],[0,1,1,1,1,1,0]]
elif letter == "X":
return [[0,1,0,0,0,1,0],[0,0,1,0,1,0,0],[0,0,0,1,0,0,0],[0,0,1,0,1,0,0],[0,1,0,0,0,1,0]]
elif letter == "Y":
return [[0,1,0,0,0,0,0],[0,0,1,0,0,0,0],[0,0,0,1,1,1,0],[0,0,1,0,0,0,0],[0,1,0,0,0,0,0]]
elif letter == "Z":
return [[0,1,0,0,0,1,0],[0,1,0,0,1,1,0],[0,1,0,1,0,1,0],[0,1,1,0,0,1,0],[0,1,0,0,0,1,0]]
elif letter == "0":
return [[0,0,1,1,1,0,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,1,0,0,0,1,0],[0,0,1,1,1,0,0]]
elif letter == "1":
return [[0,0,0,0,0,0,0],[0,1,0,0,0,0,0],[0,1,1,1,1,1,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
elif letter == "2":
return [[0,0,1,0,0,1,0],[0,1,0,0,1,1,0],[0,1,0,1,0,1,0],[0,0,1,0,0,1,0],[0,0,0,0,0,0,0]]
elif letter == "3":
return [[0,1,0,0,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,1,1,1,1,0],[0,0,0,0,0,0,0]]
elif letter == "4":
return [[0,1,1,1,0,0,0],[0,0,0,1,0,0,0],[0,0,0,1,0,0,0],[0,1,1,1,1,1,0],[0,0,0,0,0,0,0]]
elif letter == "5":
return [[0,0,1,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,1,1,0],[0,0,0,0,0,0,0]]
elif letter == "6":
return [[0,1,1,1,1,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,1,1,0],[0,0,0,0,0,0,0]]
elif letter == "7":
return [[0,1,0,0,0,1,0],[0,1,0,0,1,0,0],[0,1,0,1,0,0,0],[0,1,1,0,0,0,0],[0,0,0,0,0,0,0]]
elif letter == "8":
return [[0,1,1,0,1,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,1,0,1,1,0],[0,0,0,0,0,0,0]]
elif letter == "9":
return [[0,1,1,1,0,1,0],[0,1,0,1,0,1,0],[0,1,0,1,0,1,0],[0,1,1,1,1,1,0],[0,0,0,0,0,0,0]]
elif letter == " ":
return [[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]]
elif letter == "!":
return [[0,0,1,1,0,0,0],[0,1,0,0,1,0,0],[0,0,1,0,0,1,0],[0,1,0,0,1,0,0],[0,0,1,1,0,0,0]]
elif letter == "?":
return [[0,0,1,1,0,0,0],[0,1,1,1,1,0,0],[0,0,1,1,1,1,0],[0,1,1,1,1,0,0],[0,0,1,1,0,0,0]]
elif letter == "$":
return [[0,1,1,1,1,1,1],[0,1,1,1,1,1,0],[0,1,1,1,1,1,0],[0,1,1,1,1,1,0],[0,1,1,1,1,1,0]]
hexcolorlist = ['#de1616','#de6f16','#dede16','#16de16','#16dede','#166fde','#1616de','#6f16de','#de16de','#de166f']
#de1616 - (222, 22, 22) - red
#dede16 - (222, 222, 22) - yellow
#16de16 - (22, 222, 22) - green
#16dede - (22, 222, 222) - cyan
#1616de - (22, 22, 222) - blue
#de16de - (222, 22, 222) - magenta)
def text2arraywithcolor(text):
#create gradient color index list from red to magenta and repeat.
#3 list items, [0] and [1] value is x and y (of the picounicord LED matrix),
#while [2] value is color in hex
colorindexarray = []
for colorindex in range (0,math.ceil((len(text)*6+33)/4),1):
for y in range (6,-1,-1):
for x in range (0,4,1):
colorindexarray += [[x+2*(6-y)+4*colorindex,y,hexcolorlist[colorindex%10]]]
#create text array with [x][y] corresponding to picounicorn x,y led address
#value of [x][y] field is either 1 or 0 indicating led on or off
textarray = displayoff()
for i in range(0,len(text)):
textarray += [[0,0,0,0,0,0,0]]
textarray += lettercolumn(text[i])
textarray += displayoff()
#mapping text array with gradient color array to have color effect on text
for xxx in range (16,len(text)*6+16,1):
for yyy in range (1,6,1):
resultx = [search for search in colorindexarray if search[0] == xxx]
resulttotal = [search2 for search2 in resultx if search2[1] == yyy]
if resulttotal != [] and textarray[xxx][yyy] == 1:
textarray[xxx][yyy] = resulttotal[0][2]
return textarray
def lightupdisplay(ledmatrix,text):
for xx in range (0,16,1):
for yy in range (1,6,1):
if ledmatrix[xx][yy] == 0:
display.set_pixel(xx,yy,0,0,0)
else:
hexvalue = ledmatrix[xx][yy].lstrip('#')
rgb = tuple(int(hexvalue[i:i+2], 16) for i in (0, 2, 4))
display.set_pixel(xx,yy,rgb[0],rgb[1],rgb[2])
text = "BLUE BIN"
textarraywithcolor = text2arraywithcolor(text)
#while utime.localtime()[6] == 3:
while True:
for i in range (0,len(text)*6+33-16,1):
lightupdisplay(textarraywithcolor[i:i+16],text)