WIP: Optimize scrolling performance (10-25x faster) #1
@@ -92,7 +92,6 @@ class Display(object):
|
|||||||
if Immediate == True:
|
if Immediate == True:
|
||||||
self.np.write()
|
self.np.write()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
#print("No such word")
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def WriteSentence(self,sentence,color=(25,25,0)):
|
def WriteSentence(self,sentence,color=(25,25,0)):
|
||||||
@@ -102,11 +101,10 @@ class Display(object):
|
|||||||
self.np.write()
|
self.np.write()
|
||||||
|
|
||||||
def Scroll(self,Direction,Fill=[(0,0,0)]*16):
|
def Scroll(self,Direction,Fill=[(0,0,0)]*16):
|
||||||
"""Optimized scroll using direct buffer manipulation for all directions."""
|
"""Optimized scroll for horizontally mirrored display (NeoPixel 0 at top-right)."""
|
||||||
newbuf=bytearray(b'')
|
newbuf=bytearray(b'')
|
||||||
|
|
||||||
if Direction == 'l':
|
if Direction == 'l':
|
||||||
# Optimized left scroll - existing implementation
|
|
||||||
for Row in range(0,self.Rows,2):
|
for Row in range(0,self.Rows,2):
|
||||||
newbuf += self.np.buf[Row*16*3+3:Row*16*3+48]
|
newbuf += self.np.buf[Row*16*3+3:Row*16*3+48]
|
||||||
newbuf += bytearray([Fill[Row][1],Fill[Row][0],Fill[Row][2]])
|
newbuf += bytearray([Fill[Row][1],Fill[Row][0],Fill[Row][2]])
|
||||||
@@ -115,37 +113,31 @@ class Display(object):
|
|||||||
self.np.buf=newbuf
|
self.np.buf=newbuf
|
||||||
|
|
||||||
elif Direction == 'r':
|
elif Direction == 'r':
|
||||||
# Optimized right scroll using buffer manipulation
|
|
||||||
for Row in range(0,self.Rows,2):
|
for Row in range(0,self.Rows,2):
|
||||||
# Add fill pixels at the start
|
|
||||||
newbuf += bytearray([Fill[Row][1],Fill[Row][0],Fill[Row][2]])
|
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]])
|
|
||||||
# Copy existing pixels (minus the last one from each row)
|
|
||||||
newbuf += self.np.buf[Row*16*3:Row*16*3+45]
|
newbuf += self.np.buf[Row*16*3:Row*16*3+45]
|
||||||
newbuf += self.np.buf[Row*16*3+48:Row*16*3+93]
|
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
|
self.np.buf=newbuf
|
||||||
|
|
||||||
elif Direction == 'u':
|
elif Direction == 'u':
|
||||||
# Optimized up scroll - skip first row, add fill at bottom
|
newbuf = self.np.buf[self.Columns*3:]
|
||||||
newbuf = self.np.buf[self.Columns*3:] # Skip first row
|
|
||||||
for Col in range(self.Columns):
|
for Col in range(self.Columns):
|
||||||
color = Fill[Col]
|
color = Fill[Col]
|
||||||
newbuf += bytearray([color[1], color[0], color[2]])
|
newbuf += bytearray([color[1], color[0], color[2]])
|
||||||
self.np.buf = newbuf
|
self.np.buf = newbuf
|
||||||
|
|
||||||
elif Direction == 'd':
|
elif Direction == 'd':
|
||||||
# Optimized down scroll - add fill at top, skip last row
|
|
||||||
for Col in range(self.Columns):
|
for Col in range(self.Columns):
|
||||||
color = Fill[Col]
|
color = Fill[Col]
|
||||||
newbuf += bytearray([color[1], color[0], color[2]])
|
newbuf += bytearray([color[1], color[0], color[2]])
|
||||||
newbuf += self.np.buf[:-self.Columns*3] # Skip last row
|
newbuf += self.np.buf[:-self.Columns*3]
|
||||||
self.np.buf = newbuf
|
self.np.buf = newbuf
|
||||||
|
|
||||||
self.np.write()
|
self.np.write()
|
||||||
|
|
||||||
def ScrollLetter(self,Direction,Text,Speed,Color=[(50,50,50)],BGColor=[(0,0,0)]):
|
def ScrollLetter(self,Direction,Text,Speed,Color=[(50,50,50)],BGColor=[(0,0,0)]):
|
||||||
"""Optimized letter scrolling with pre-calculated columns."""
|
"""Optimized letter scrolling with pre-calculated columns."""
|
||||||
# Pre-calculate all columns for all letters
|
|
||||||
all_columns = []
|
all_columns = []
|
||||||
for index,Letter in enumerate(Text):
|
for index,Letter in enumerate(Text):
|
||||||
for X in range(3,14):
|
for X in range(3,14):
|
||||||
@@ -155,7 +147,6 @@ class Display(object):
|
|||||||
BGColor[index % len(BGColor)])
|
BGColor[index % len(BGColor)])
|
||||||
)
|
)
|
||||||
|
|
||||||
# Now scroll with pre-calculated data
|
|
||||||
for column in all_columns:
|
for column in all_columns:
|
||||||
self.Scroll(Direction, column)
|
self.Scroll(Direction, column)
|
||||||
sleep(Speed)
|
sleep(Speed)
|
||||||
@@ -171,9 +162,9 @@ class Display(object):
|
|||||||
NeoPixel 0 is at top-right, serpentine wiring pattern."""
|
NeoPixel 0 is at top-right, serpentine wiring pattern."""
|
||||||
Row=self.Num2Row(Pos)
|
Row=self.Num2Row(Pos)
|
||||||
Col=self.Num2Column(Pos)
|
Col=self.Num2Column(Pos)
|
||||||
if Row%2 == 0: # Even rows go right-to-left (mirrored from original)
|
if Row%2 == 0:
|
||||||
NewPos=Row*self.Columns+(self.Columns-1-Col)
|
NewPos=Row*self.Columns+(self.Columns-1-Col)
|
||||||
else: # Odd rows go left-to-right
|
else:
|
||||||
NewPos=Pos
|
NewPos=Pos
|
||||||
return NewPos
|
return NewPos
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user