Phil B fixes to variable errors, cleaned whitespace arrors.
This commit is contained in:
parent
f5624d9f56
commit
2921f46fbf
2 changed files with 76 additions and 74 deletions
|
|
@ -4,38 +4,45 @@
|
||||||
import gc
|
import gc
|
||||||
import time
|
import time
|
||||||
import board
|
import board
|
||||||
import neopixel
|
|
||||||
import touchio
|
import touchio
|
||||||
from analogio import AnalogIn
|
import digitalio
|
||||||
|
from neopixel_write import neopixel_write
|
||||||
|
|
||||||
# uncomment one line only here to select bitmap
|
# uncomment one line only here to select bitmap
|
||||||
FILENAME = "bats.bmp" # BMP file to load from flash filesystem
|
FILENAME = "bats.bmp" # BMP file to load from flash filesystem
|
||||||
#FILENAME = "jpw01.bmp"
|
# FILENAME = "digikey.bmp"
|
||||||
#FILENAME = "digikey.bmp"
|
# FILENAME = "burger.bmp"
|
||||||
#FILENAME = "burger.bmp"
|
# FILENAME = "afbanner.bmp"
|
||||||
#FILENAME = "afbanner.bmp"
|
# FILENAME = "blinka.bmp"
|
||||||
#FILENAME = "blinka.bmp"
|
# FILENAME = "ghost04.bmp"
|
||||||
#FILENAME = "ghost.bmp"
|
# FILENAME = "ghost07.bmp"
|
||||||
#FILENAME = "helix-32x30.bmp"
|
# FILENAME = "ghost02.bmp"
|
||||||
#FILENAME = "wales2-107x30.bmp"
|
# FILENAME = "helix-32x30.bmp"
|
||||||
#FILENAME = "pumpkin.bmp"
|
# FILENAME = "wales2-107x30.bmp"
|
||||||
#FILENAME = "rainbow.bmp"
|
# FILENAME = "pumpkin.bmp"
|
||||||
#FILENAME = "rainbowRoad.bmp"
|
# FILENAME = "rainbow.bmp"
|
||||||
#FILENAME = "rainbowZig.bmp"
|
# FILENAME = "rainbowRoad.bmp"
|
||||||
#FILENAME = "skull.bmp"
|
# FILENAME = "rainbowZig.bmp"
|
||||||
#FILENAME = "adabot.bmp"
|
# FILENAME = "skull.bmp"
|
||||||
#FILENAME = "green_stripes.bmp"
|
# FILENAME = "adabot.bmp"
|
||||||
#FILENAME = "red_blue.bmp"
|
# FILENAME = "green_stripes.bmp"
|
||||||
#FILENAME = "minerva.bmp"
|
# FILENAME = "red_blue.bmp"
|
||||||
|
# FILENAME = "minerva.bmp"
|
||||||
|
|
||||||
TOUCH = touchio.TouchIn(board.A5) # capacitive touch pad
|
TOUCH = touchio.TouchIn(board.A5) # Rightmost capacitive touch pad
|
||||||
SPEED = 50000
|
BRIGHTNESS = 1.0 # NeoPixel brightness 0.0 (min) to 1.0 (max)
|
||||||
BRIGHTNESS = 1.0 # Set brightness here, NOT in NeoPixel constructor
|
GAMMA = 2.7 # Adjusts perceived brighthess linearity
|
||||||
GAMMA = 2.7 # Adjusts perceived brighthess linearity
|
NUM_PIXELS = 30 # NeoPixel strip length (in pixels)
|
||||||
NUM_PIXELS = 30 # NeoPixel strip length (in pixels)
|
SPEED = 20000 # adjust this to change the playback speed e.g. 50000 is slow
|
||||||
NEOPIXEL_PIN = board.A1 # Pin where NeoPixels are connected
|
LOOP = False # set to True for looping
|
||||||
STRIP = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=1, auto_write=False)
|
# Switch off onboard NeoPixel...
|
||||||
DELAY_TIME = 0.01 # Timer delay before it starts
|
NEOPIXEL_PIN = digitalio.DigitalInOut(board.NEOPIXEL)
|
||||||
|
NEOPIXEL_PIN.direction = digitalio.Direction.OUTPUT
|
||||||
|
neopixel_write(NEOPIXEL_PIN, bytearray(3))
|
||||||
|
# ...then assign NEOPIXEL_PIN to the external NeoPixel connector:
|
||||||
|
NEOPIXEL_PIN = digitalio.DigitalInOut(board.A1)
|
||||||
|
NEOPIXEL_PIN.direction = digitalio.Direction.OUTPUT
|
||||||
|
neopixel_write(NEOPIXEL_PIN, bytearray(NUM_PIXELS * 3))
|
||||||
|
|
||||||
# Interpret multi-byte value from file as little-endian value
|
# Interpret multi-byte value from file as little-endian value
|
||||||
def read_le(value):
|
def read_le(value):
|
||||||
|
|
@ -57,11 +64,11 @@ def load_bmp(filename):
|
||||||
if f.read(2) != b'BM': # check signature
|
if f.read(2) != b'BM': # check signature
|
||||||
raise BMPError("Not BitMap file")
|
raise BMPError("Not BitMap file")
|
||||||
|
|
||||||
bmpFileSize = read_le(f.read(4))
|
f.read(4) # Read & ignore file size
|
||||||
f.read(4) # Read & ignore creator bytes
|
f.read(4) # Read & ignore creator bytes
|
||||||
|
|
||||||
bmpImageoffset = read_le(f.read(4)) # Start of image data
|
bmpImageoffset = read_le(f.read(4)) # Start of image data
|
||||||
headerSize = read_le(f.read(4))
|
f.read(4) # Read & ignore header size
|
||||||
bmpWidth = read_le(f.read(4))
|
bmpWidth = read_le(f.read(4))
|
||||||
bmpHeight = read_le(f.read(4))
|
bmpHeight = read_le(f.read(4))
|
||||||
# BMPs are traditionally stored bottom-to-top.
|
# BMPs are traditionally stored bottom-to-top.
|
||||||
|
|
@ -72,8 +79,6 @@ def load_bmp(filename):
|
||||||
bmpHeight = -bmpHeight
|
bmpHeight = -bmpHeight
|
||||||
flip = False
|
flip = False
|
||||||
|
|
||||||
# print("Size: %d\nImage offset: %d\nHeader size: %d" %
|
|
||||||
# (bmpFileSize, bmpImageoffset, headerSize))
|
|
||||||
print("WxH: (%d,%d)" % (bmpWidth, bmpHeight))
|
print("WxH: (%d,%d)" % (bmpWidth, bmpHeight))
|
||||||
|
|
||||||
if read_le(f.read(2)) != 1:
|
if read_le(f.read(2)) != 1:
|
||||||
|
|
@ -95,7 +100,7 @@ def load_bmp(filename):
|
||||||
clippedHeight = NUM_PIXELS
|
clippedHeight = NUM_PIXELS
|
||||||
|
|
||||||
# Allocate per-column pixel buffers, sized for NeoPixel strip:
|
# Allocate per-column pixel buffers, sized for NeoPixel strip:
|
||||||
columns = [bytearray(NUM_PIXELS * STRIP.bpp) for i in range(bmpWidth)]
|
columns = [bytearray(NUM_PIXELS * 3) for i in range(bmpWidth)]
|
||||||
|
|
||||||
# Image is displayed at END (not start) of NeoPixel strip,
|
# Image is displayed at END (not start) of NeoPixel strip,
|
||||||
# this index works incrementally backward in column buffers...
|
# this index works incrementally backward in column buffers...
|
||||||
|
|
@ -105,22 +110,22 @@ def load_bmp(filename):
|
||||||
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize
|
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize
|
||||||
else: # Bitmap is stored top-to-bottom
|
else: # Bitmap is stored top-to-bottom
|
||||||
pos = bmpImageoffset + row * rowSize
|
pos = bmpImageoffset + row * rowSize
|
||||||
f.seek(pos) # Start of scanline
|
f.seek(pos) # Start of scanline
|
||||||
for c in columns: # For each pixel of scanline...
|
for c in columns: # For each pixel of scanline...
|
||||||
# BMP files use BGR color order
|
# BMP files use BGR color order
|
||||||
# blue, green, red = bytearray(f.read(3))
|
# blue, green, red = bytearray(f.read(3))
|
||||||
blue, green, red = f.read(3)
|
blue, green, red = f.read(3)
|
||||||
# Rearrange into NeoPixel strip's color order,
|
# Rearrange into NeoPixel strip's color order,
|
||||||
# while handling brightness & gamma correction:
|
# while handling brightness & gamma correction:
|
||||||
c[idx + STRIP.order[0]] = int(pow(red / 255, GAMMA) * BRIGHTNESS * 255 + 0.5)
|
c[idx ] = int(pow(green / 255, GAMMA) * BRIGHTNESS * 255 + 0.5)
|
||||||
c[idx + STRIP.order[1]] = int(pow(green / 255, GAMMA) * BRIGHTNESS * 255 + 0.5)
|
c[idx+1] = int(pow(red / 255, GAMMA) * BRIGHTNESS * 255 + 0.5)
|
||||||
c[idx + STRIP.order[2]] = int(pow(blue / 255, GAMMA) * BRIGHTNESS * 255 + 0.5)
|
c[idx+2] = int(pow(blue / 255, GAMMA) * BRIGHTNESS * 255 + 0.5)
|
||||||
idx -= 3 # Advance (back) one pixel
|
idx -= 3 # Advance (back) one pixel
|
||||||
|
|
||||||
# Add one more column, using the NeoPixel strip's normal buffer,
|
# Add one more column with no color data loaded. This is used
|
||||||
# which is never assigned any color data. This is used to turn
|
# to turn the strip off at the end of the painting operation.
|
||||||
# the strip off at the end of the painting operation.
|
if not LOOP:
|
||||||
columns.append(STRIP.buf)
|
columns.append(bytearray(NUM_PIXELS * 3))
|
||||||
|
|
||||||
print("Loaded OK!")
|
print("Loaded OK!")
|
||||||
gc.collect() # Garbage-collect now so playback is smoother
|
gc.collect() # Garbage-collect now so playback is smoother
|
||||||
|
|
@ -139,28 +144,26 @@ def load_bmp(filename):
|
||||||
columns = load_bmp(FILENAME)
|
columns = load_bmp(FILENAME)
|
||||||
|
|
||||||
print("Mem free:", gc.mem_free())
|
print("Mem free:", gc.mem_free())
|
||||||
|
# Orig code: 10320 bytes free
|
||||||
|
# New code: 13216 bytes free
|
||||||
|
|
||||||
# Switch off onboard NeoPixel
|
column_delay = SPEED / 65535.0 / 10.0 # 0.0 to 0.1 seconds
|
||||||
neo = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0, auto_write=True)
|
while LOOP:
|
||||||
neo.fill(0)
|
for c in columns:
|
||||||
|
neopixel_write(NEOPIXEL_PIN, c)
|
||||||
|
time.sleep(column_delay) # Column-to-column delay
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
# Wait for touch pad input:
|
# Wait for touch pad input:
|
||||||
while not TOUCH.value:
|
while not TOUCH.value:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
time.sleep(DELAY_TIME)
|
|
||||||
column_delay = SPEED / 65535.0 / 10.0 # 0.0 to 0.1 seconds
|
column_delay = SPEED / 65535.0 / 10.0 # 0.0 to 0.1 seconds
|
||||||
# print(column_delay)
|
# print(column_delay)
|
||||||
|
|
||||||
# Play back color data loaded into each column:
|
# Play back color data loaded into each column:
|
||||||
for c in columns:
|
for c in columns:
|
||||||
# Rather than replace the data in the NeoPixel strip's buffer
|
neopixel_write(NEOPIXEL_PIN, c)
|
||||||
# pixel-by-pixel, which would be very slow, we just override
|
|
||||||
# STRIP's buffer object with each column of precomputed data:
|
|
||||||
STRIP.buf = c # Substitute our data
|
|
||||||
STRIP.show() # Push to strip
|
|
||||||
time.sleep(column_delay) # Column-to-column delay
|
time.sleep(column_delay) # Column-to-column delay
|
||||||
# Last column is all 0's, no need to explicitly clear strip
|
# Last column is all 0's, no need to explicitly clear strip
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,30 +13,31 @@ from neopixel_write import neopixel_write
|
||||||
|
|
||||||
# uncomment one line only here to select bitmap
|
# uncomment one line only here to select bitmap
|
||||||
FILENAME = "bats.bmp" # BMP file to load from flash filesystem
|
FILENAME = "bats.bmp" # BMP file to load from flash filesystem
|
||||||
#FILENAME = "jpw01.bmp"
|
# FILENAME = "digikey.bmp"
|
||||||
#FILENAME = "digikey.bmp"
|
# FILENAME = "burger.bmp"
|
||||||
#FILENAME = "burger.bmp"
|
# FILENAME = "afbanner.bmp"
|
||||||
#FILENAME = "afbanner.bmp"
|
# FILENAME = "blinka.bmp"
|
||||||
#FILENAME = "blinka.bmp"
|
# FILENAME = "ghost04.bmp"
|
||||||
#FILENAME = "ghost.bmp"
|
# FILENAME = "ghost07.bmp"
|
||||||
#FILENAME = "helix-32x30.bmp"
|
# FILENAME = "ghost02.bmp"
|
||||||
#FILENAME = "wales2-107x30.bmp"
|
# FILENAME = "helix-32x30.bmp"
|
||||||
#FILENAME = "pumpkin.bmp"
|
# FILENAME = "wales2-107x30.bmp"
|
||||||
#FILENAME = "rainbow.bmp"
|
# FILENAME = "pumpkin.bmp"
|
||||||
#FILENAME = "rainbowRoad.bmp"
|
# FILENAME = "rainbow.bmp"
|
||||||
#FILENAME = "rainbowZig.bmp"
|
# FILENAME = "rainbowRoad.bmp"
|
||||||
#FILENAME = "skull.bmp"
|
# FILENAME = "rainbowZig.bmp"
|
||||||
#FILENAME = "adabot.bmp"
|
# FILENAME = "skull.bmp"
|
||||||
#FILENAME = "green_stripes.bmp"
|
# FILENAME = "adabot.bmp"
|
||||||
#FILENAME = "red_blue.bmp"
|
# FILENAME = "green_stripes.bmp"
|
||||||
#FILENAME = "minerva.bmp"
|
# FILENAME = "red_blue.bmp"
|
||||||
|
# FILENAME = "minerva.bmp"
|
||||||
|
|
||||||
TOUCH = touchio.TouchIn(board.A2) # Rightmost capacitive touch pad
|
TOUCH = touchio.TouchIn(board.A2) # Rightmost capacitive touch pad
|
||||||
ANALOG = AnalogIn(board.SENSE) # Potentiometer on SENSE pin
|
ANALOG = AnalogIn(board.SENSE) # Potentiometer on SENSE pin
|
||||||
BRIGHTNESS = 1.0 # NeoPixel brightness 0.0 (min) to 1.0 (max)
|
BRIGHTNESS = 1.0 # NeoPixel brightness 0.0 (min) to 1.0 (max)
|
||||||
GAMMA = 2.7 # Adjusts perceived brighthess linearity
|
GAMMA = 2.7 # Adjusts perceived brighthess linearity
|
||||||
NUM_PIXELS = 30 # NeoPixel strip length (in pixels)
|
NUM_PIXELS = 30 # NeoPixel strip length (in pixels)
|
||||||
LOOP = False #set to True for looping
|
LOOP = False # set to True for looping
|
||||||
# Switch off onboard NeoPixel...
|
# Switch off onboard NeoPixel...
|
||||||
NEOPIXEL_PIN = digitalio.DigitalInOut(board.NEOPIXEL)
|
NEOPIXEL_PIN = digitalio.DigitalInOut(board.NEOPIXEL)
|
||||||
NEOPIXEL_PIN.direction = digitalio.Direction.OUTPUT
|
NEOPIXEL_PIN.direction = digitalio.Direction.OUTPUT
|
||||||
|
|
@ -66,11 +67,11 @@ def load_bmp(filename):
|
||||||
if f.read(2) != b'BM': # check signature
|
if f.read(2) != b'BM': # check signature
|
||||||
raise BMPError("Not BitMap file")
|
raise BMPError("Not BitMap file")
|
||||||
|
|
||||||
bmpFileSize = read_le(f.read(4))
|
f.read(4) # Read & ignore file size
|
||||||
f.read(4) # Read & ignore creator bytes
|
f.read(4) # Read & ignore creator bytes
|
||||||
|
|
||||||
bmpImageoffset = read_le(f.read(4)) # Start of image data
|
bmpImageoffset = read_le(f.read(4)) # Start of image data
|
||||||
headerSize = read_le(f.read(4))
|
f.read(4) # Read & ignore header size
|
||||||
bmpWidth = read_le(f.read(4))
|
bmpWidth = read_le(f.read(4))
|
||||||
bmpHeight = read_le(f.read(4))
|
bmpHeight = read_le(f.read(4))
|
||||||
# BMPs are traditionally stored bottom-to-top.
|
# BMPs are traditionally stored bottom-to-top.
|
||||||
|
|
@ -81,8 +82,6 @@ def load_bmp(filename):
|
||||||
bmpHeight = -bmpHeight
|
bmpHeight = -bmpHeight
|
||||||
flip = False
|
flip = False
|
||||||
|
|
||||||
# print("Size: %d\nImage offset: %d\nHeader size: %d" %
|
|
||||||
# (bmpFileSize, bmpImageoffset, headerSize))
|
|
||||||
print("WxH: (%d,%d)" % (bmpWidth, bmpHeight))
|
print("WxH: (%d,%d)" % (bmpWidth, bmpHeight))
|
||||||
|
|
||||||
if read_le(f.read(2)) != 1:
|
if read_le(f.read(2)) != 1:
|
||||||
|
|
@ -114,7 +113,7 @@ def load_bmp(filename):
|
||||||
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize
|
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize
|
||||||
else: # Bitmap is stored top-to-bottom
|
else: # Bitmap is stored top-to-bottom
|
||||||
pos = bmpImageoffset + row * rowSize
|
pos = bmpImageoffset + row * rowSize
|
||||||
f.seek(pos) # Start of scanline
|
f.seek(pos) # Start of scanline
|
||||||
for c in columns: # For each pixel of scanline...
|
for c in columns: # For each pixel of scanline...
|
||||||
# BMP files use BGR color order
|
# BMP files use BGR color order
|
||||||
# blue, green, red = bytearray(f.read(3))
|
# blue, green, red = bytearray(f.read(3))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue