dotstar only really works

This commit is contained in:
ladyada 2017-10-01 20:21:01 -04:00
parent 71a573336a
commit c7576d4fa5
2 changed files with 25 additions and 43 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

View file

@ -1,28 +1,21 @@
# Dotstar (or neopixel) painter! # Dotstar painter! Can handle up to ~2300 pixel size image (e.g. 36 x 64)
import board import board
import digitalio import digitalio
import time import time
import gc import gc
import busio
FILENAME = "blinka.bmp" FILENAME = "blinka.bmp"
IMAGE_DELAY = 0.5 IMAGE_DELAY = 0.2
REPEAT = True REPEAT = True
BRIGHTNESS = 0.3 BRIGHTNESS = 0.3
NEOPIXEL = False # we use dotstar by default PIXEL_DELAY = 0.001
if NEOPIXEL: dotstar = busio.SPI(board.SCK, board.MOSI)
from neopixel_write import neopixel_write while not dotstar.try_lock():
neopin = digitalio.DigitalInOut(board.D1) pass
neopin.switch_to_output() dotstar.configure(baudrate=12000000)
PIXEL_DELAY = 0.005
else: # Dotstar
import busio
dotstar = busio.SPI(board.SCK, board.MOSI)
while not dotstar.try_lock():
pass
dotstar.configure(baudrate=12000000)
PIXEL_DELAY = 0.002
# we'll resize this later # we'll resize this later
databuf = bytearray(0) databuf = bytearray(0)
@ -71,11 +64,11 @@ try:
if read_le(f.read(2)) != 0: if read_le(f.read(2)) != 0:
raise BMPError("Compressed file") raise BMPError("Compressed file")
print("Image Format OK!") print("Image OK!")
rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary
databuf = bytearray(bmpWidth * bmpHeight * 4) # its huge! but its also fast :) databuf = bytearray(2250 * 4) # its huge! but its also fast :)
for row in range(bmpHeight): # For each scanline... for row in range(bmpHeight): # For each scanline...
if(flip): # Bitmap is stored bottom-to-top order (normal BMP) if(flip): # Bitmap is stored bottom-to-top order (normal BMP)
@ -88,14 +81,10 @@ try:
for col in range(bmpWidth): for col in range(bmpWidth):
b,g,r = bytearray(f.read(3)) # BMP files store RGB in BGR b,g,r = bytearray(f.read(3)) # BMP files store RGB in BGR
# front load brightness, gamma and reordering here! # front load brightness, gamma and reordering here!
if NEOPIXEL: order = [b, g, r]
order = [g, r, b] idx = (col * bmpHeight + (bmpHeight - row - 1))*4
idx = (col * bmpHeight + (bmpHeight - row - 1))*3 databuf[idx] = 0xFF # first byte is 'brightness'
else: idx += 1
order = [b, g, r]
idx = (col * bmpHeight + (bmpHeight - row - 1))*4
databuf[idx] = 0xFF # first byte is 'brightness'
idx += 1
for color in order: for color in order:
databuf[idx] = int(pow((color * BRIGHTNESS) / 255, 2.7) * 255 + 0.5) databuf[idx] = int(pow((color * BRIGHTNESS) / 255, 2.7) * 255 + 0.5)
idx += 1 idx += 1
@ -114,28 +103,21 @@ print("Ready to go!")
while True: while True:
print("Draw!") print("Draw!")
index = 0 index = 0
for col in range(bmpWidth): for col in range(bmpWidth):
if NEOPIXEL: row = databuf[index:index+bmpHeight*4]
row = databuf[index:index+bmpHeight*3] dotstar.write(bytearray([0x00, 0x00, 0x00, 0x00] ))
neopixel_write(neopin, row) dotstar.write(row)
index += bmpHeight * 3 dotstar.write(bytearray([0x00, 0x00, 0x00, 0x00]))
else: index += bmpHeight * 4
row = databuf[index:index+bmpHeight*4]
dotstar.write(bytearray([0x00, 0x00, 0x00, 0x00] ))
dotstar.write(row)
dotstar.write(bytearray([0x00, 0x00, 0x00, 0x00]))
index += bmpHeight * 4
time.sleep(PIXEL_DELAY) time.sleep(PIXEL_DELAY)
# clear it out # clear it out
if NEOPIXEL: dotstar.write(bytearray([0x00, 0x00, 0x00, 0x00]))
neopixel_write(neopin, bytearray(bmpHeight * [0, 0, 0])) for r in range(bmpHeight * 5):
else: dotstar.write(bytearray([0xFF, 0x00, 0x00, 0x00]))
dotstar.write(bytearray([0x00, 0x00, 0x00, 0x00])) dotstar.write(bytearray([0xff, 0xff, 0xff, 0xff]))
for r in range(bmpHeight * 5): gc.collect()
dotstar.write(bytearray([0xFF, 0x00, 0x00, 0x00]))
dotstar.write(bytearray([0xff, 0xff, 0xff, 0xff]))
if not REPEAT: if not REPEAT:
break break