Michael that seems to be mostly correct. Though the matrix is 25 columns by 8 rows. Pixel 0 is indeed in the top right position. As suggested by Jeff105671 I have run test to confirm that the linear pixel is being correctly calculated. The code seems to be correctly calculating the linear pixel. Here is the update code.
You can ignore the block commented code. I also added a function to calculate the linear pixel from row/column coordinates.
import machine, neopixel, time, random, sys, os
import framebuf, struct
ROWS = 8
COLS = 25
LED_PIN = 16
FRAMEBUF = COLS * ROWS
pattern = bytearray(8)
pattern[0] = 0xFF
pattern[1] = 0x81
pattern[2] = 0x81
pattern[3] = 0x81
pattern[4] = 0x81
pattern[5] = 0x81
pattern[6] = 0x81
pattern[7] = 0xFF
def rgbto(r,g,b):
v = (g << 16) + (r << 8) + b
return(v)
def rgbfrom(v):
b = v & 255
g = (v >> 8) & 255
r = (v >> 16) & 255
#print("V={} R={} G={} B={}".format(v,r,g,b))
return (r,g,b)
# Calculate linear pixel from Column/Row coordinates
def linearPixel(row,col):
#Determine if row is odd or even
#Right to left pattern 0 to 24
if row % 2 == 0:
i = (row * COLS) + col
else:
i = (row * COLS) + (COLS - 1) - col
#Left to right pattern 24 to 0
"""
if r % 2 == 0:
i = (r * COLS) + (COLS - 1) - c
else:
i = (r * COLS) + c
"""
return(i)
try:
msg = "Micropython Rules!"
np = neopixel.NeoPixel(machine.Pin(LED_PIN), COLS*ROWS, bpp=3)
""" Comment out framebuf code while performing simple Neopixel patter tests
print(id(np))
FRAMEBUF = len(msg) * ROWS * 8
fbuf = framebuf.FrameBuffer(bytearray(FRAMEBUF), COLS, ROWS, framebuf.MONO_VLSB)
fbuf.fill(0x00)
#fbuf.hline(0,1,COLS,0x01)
#fbuf.line(1,0,COLS,1,rgbto(0,255,0))
fbuf.text(msg,0,0,0x01)
i = 0
m = 0
"""
"""
for r in range(ROWS):
for c in range(len(msg)*8):
if fbuf.pixel(r,c) != 0:
l = l + "*"
else:
l = l + " "
print(l)
l = ""
"""
"""
while m <= FRAMEBUF:
for c in range(COLS-1,0,-1):
for r in range(0,ROWS):
rgb = fbuf.pixel(m,r)
#print("C={} R={} RGB={} Pixel={}".format(c,r,rgb,fbuf.pixel(c,r)))
if r % 2 == 0:
i = (r * COLS) + (COLS - 1) - c
else:
i = (r * COLS) + c
if rgb != 0:
np[i] = (255,0,0)
else:
np[i] = (0,0,0)
print("I={0} C={1} R={2} FRAMEBUF={3} M={4}".format(i,c,r,FRAMEBUF,m))
np.write()
time.sleep(.1)
m += 1
#fbuf.scroll(-1,0)
"""
# Simple line test Row 0
for c in range(COLS):
i = linearPixel(0,c)
np[i] = (255,0,0)
np.write()
time.sleep(.1)
# Simple line test Row 1
for c in range(COLS):
i = linearPixel(1,c)
np[i] = (0,0,255)
np.write()
time.sleep(.1)
# Simple column test Column 1(2)
np.fill((0,0,0))
for r in range(ROWS):
i = linearPixel(r,1)
np[i] = (128,0,255)
np.write()
time.sleep(.1)
# Simple diaginal line test start at 0,0
np.fill((0,0,0))
c = 0
for r in range(ROWS):
i = linearPixel(r,c)
np[i] = (0,255,0)
np.write()
time.sleep(.1)
c += 1
# Simple reverse diaginal line test start at 7,7
c = 7
for r in range(ROWS-1,-1,-1):
i = linearPixel(r,c)
np[i] = (0,255,128)
np.write()
time.sleep(.1)
c -= 1
# Make a cross
c = 0
for r in range(ROWS-1,-1,-1):
i = linearPixel(r,c)
np[i] = (0,255,128)
np.write()
time.sleep(.1)
c += 1
# Display simple box pattern from bitmap
for r in range(ROWS):
bt = 1
for c in range(ROWS):
i = linearPixel(r,c)
#print(f'pattern[{r}]={pattern[r]:#010b} bit[{c}]={pattern[r]&bt} {bt}')
if pattern[r]&bt != 0:
np[i] = (128,0,128)
else:
np[i] = (0,0,0)
bt = bt << 1
np.write()
time.sleep(.1)
# Move simple box pattern from bitmap one column to the left (scrolling)
np.fill((0,0,0))
for p in range(COLS):
for r in range(ROWS):
bt = 1
for c in range(p):
i = linearPixel(r,c)
print(f'pattern[{r}]={pattern[r]:#010b} bit[{c}]={pattern[r]&bt} {bt}')
if pattern[r]&bt != 0:
np[i] = (0,0,128)
else:
np[i] = (0,0,0)
if bt <= 128:
bt = bt << 1
else:
bt = 1
np.write()
time.sleep(.1)
time.sleep(3)
except KeyboardInterrupt as e:
sys.print_exception(e)
print("Exiting...")
except IndexError as e:
sys.print_exception(e)
print("4:Index out of range C={} R={} I={}".format(c,r,i))
except (Exception, NameError, RuntimeError) as e:
sys.print_exception(e)
print(f"Unexpected {e=}, {type(e)=}")
finally:
np.fill((0,0,0))
np.write()