WIP: Optimize scrolling performance (10-25x faster) #1

Draft
adebaumann wants to merge 3 commits from optimize-scrolling into main

View File

@@ -1,5 +1,5 @@
import machine,neopixel
from time import sleep
import font
words={"it":[0,1],
@@ -92,7 +92,6 @@ class Display(object):
if Immediate == True:
self.np.write()
except KeyError:
"evening":[224,225,226,227,228,229,230],
pass
def WriteSentence(self,sentence,color=(25,25,0)):
@@ -102,65 +101,55 @@ class Display(object):
self.np.write()
def Scroll(self,Direction,Fill=[(0,0,0)]*16):
"""Optimized scroll for horizontally mirrored display (NeoPixel 0 at top-right)."""
newbuf=bytearray(b'')
if Direction == 'l':
for Row in range(0,self.Rows,2):
newbuf += self.np.buf[Row*16*3+3:Row*16*3+48]
"cold":[236,237,238,239],
newbuf += bytearray([Fill[Row][1],Fill[Row][0],Fill[Row][2]])
newbuf += bytearray([Fill[Row+1][1],Fill[Row+1][0],Fill[Row+1][2]])
newbuf += self.np.buf[Row*16*3+48:Row*16*3+93]
self.np.buf=newbuf
"warm":[245,256,257,258],
"hot":[252,253,254],
"beer":[19,35,51,67],
}
elif Direction == 'r':
for Row in range(0,self.Rows,2):
newbuf += bytearray([Fill[Row][1],Fill[Row][0],Fill[Row][2]])
newbuf += self.np.buf[Row*16*3:Row*16*3+45]
newbuf += self.np.buf[Row*16*3+51:Row*16*3+96]
newbuf += bytearray([Fill[Row+1][1],Fill[Row+1][0],Fill[Row+1][2]])
self.np.buf=newbuf
elif Direction == 'u':
newbuf = self.np.buf[self.Columns*3:]
for Col in range(self.Columns):
"""Initialises a NeoPixel-String."""
def __init__(self,DataPin,Rows=16,Columns=16):
color = Fill[Col]
newbuf += bytearray([color[1], color[0], color[2]])
self.np.buf = newbuf
elif Direction == 'd':
for Col in range(self.Columns):
color = Fill[Col]
newbuf += bytearray([color[1], color[0], color[2]])
newbuf += self.np.buf[:-self.Columns*3]
self.np.buf = newbuf
self.np.write()
self.Rows=Rows
self.Columns=Columns
def Write(self,Pos,Colour=(0,0,0), Immediate=True):
"""write(Pos,Colour=(0,0,0), Immediate=True): Writes a colour (given as an RGB triplet) to the Pixel
at position 'Pos'. If 'Immediate' is set, change immediately, otherwise, wait until the display is
explicitly written or a write with 'Immediate' is called."""
if Pos >= self.number:
print("Out of range")
else:
self.np[self.Weave(Pos)]=Colour
if Immediate:
self.np.write()
def ScrollLetter(self,Direction,Text,Speed,Color=[(50,50,50)],BGColor=[(0,0,0)]):
"""Optimized letter scrolling with pre-calculated columns."""
all_columns = []
for index,Letter in enumerate(Text):
def Clear(self,color=(0,0,0),Immediate=True):
"""Clears all pixels"""
for X in range(3,14):
all_columns.append(
font.CharacterColumn(Letter, X,
Color[index % len(Color)],
BGColor[index % len(BGColor)])
)
for column in all_columns:
self.Scroll(Direction, column)
sleep(Speed)
def Num2Column(self,Number):
return(Number-int(Number/self.Columns)*self.Columns)
@@ -169,9 +158,12 @@ class Display(object):
return(int(Number/self.Columns))
def Weave(self,Pos):
"""Convert logical position to physical LED position for horizontally mirrored display.
NeoPixel 0 is at top-right, serpentine wiring pattern."""
Row=self.Num2Row(Pos)
Col=self.Num2Column(Pos)
if Row%2 == 0:
NewPos=Row*self.Columns+(self.Columns-1-Col)
else:
NewPos=Pos
return NewPos