From 2921f46fbf98ee676310e42f07eddab5b22692e7 Mon Sep 17 00:00:00 2001 From: John Edgar Park Date: Fri, 24 Aug 2018 12:09:51 -0700 Subject: [PATCH] Phil B fixes to variable errors, cleaned whitespace arrors. --- .../light_paintstick_cpx.py | 103 +++++++++--------- .../light_paintstick_hallowing.py | 47 ++++---- 2 files changed, 76 insertions(+), 74 deletions(-) diff --git a/Light_Paintstick_HalloWing/light_paintstick_cpx.py b/Light_Paintstick_HalloWing/light_paintstick_cpx.py index 0368170cd..169d90ef5 100644 --- a/Light_Paintstick_HalloWing/light_paintstick_cpx.py +++ b/Light_Paintstick_HalloWing/light_paintstick_cpx.py @@ -4,38 +4,45 @@ import gc import time import board -import neopixel import touchio -from analogio import AnalogIn +import digitalio +from neopixel_write import neopixel_write # uncomment one line only here to select bitmap FILENAME = "bats.bmp" # BMP file to load from flash filesystem -#FILENAME = "jpw01.bmp" -#FILENAME = "digikey.bmp" -#FILENAME = "burger.bmp" -#FILENAME = "afbanner.bmp" -#FILENAME = "blinka.bmp" -#FILENAME = "ghost.bmp" -#FILENAME = "helix-32x30.bmp" -#FILENAME = "wales2-107x30.bmp" -#FILENAME = "pumpkin.bmp" -#FILENAME = "rainbow.bmp" -#FILENAME = "rainbowRoad.bmp" -#FILENAME = "rainbowZig.bmp" -#FILENAME = "skull.bmp" -#FILENAME = "adabot.bmp" -#FILENAME = "green_stripes.bmp" -#FILENAME = "red_blue.bmp" -#FILENAME = "minerva.bmp" +# FILENAME = "digikey.bmp" +# FILENAME = "burger.bmp" +# FILENAME = "afbanner.bmp" +# FILENAME = "blinka.bmp" +# FILENAME = "ghost04.bmp" +# FILENAME = "ghost07.bmp" +# FILENAME = "ghost02.bmp" +# FILENAME = "helix-32x30.bmp" +# FILENAME = "wales2-107x30.bmp" +# FILENAME = "pumpkin.bmp" +# FILENAME = "rainbow.bmp" +# FILENAME = "rainbowRoad.bmp" +# FILENAME = "rainbowZig.bmp" +# FILENAME = "skull.bmp" +# FILENAME = "adabot.bmp" +# FILENAME = "green_stripes.bmp" +# FILENAME = "red_blue.bmp" +# FILENAME = "minerva.bmp" -TOUCH = touchio.TouchIn(board.A5) # capacitive touch pad -SPEED = 50000 -BRIGHTNESS = 1.0 # Set brightness here, NOT in NeoPixel constructor -GAMMA = 2.7 # Adjusts perceived brighthess linearity -NUM_PIXELS = 30 # NeoPixel strip length (in pixels) -NEOPIXEL_PIN = board.A1 # Pin where NeoPixels are connected -STRIP = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=1, auto_write=False) -DELAY_TIME = 0.01 # Timer delay before it starts +TOUCH = touchio.TouchIn(board.A5) # Rightmost capacitive touch pad +BRIGHTNESS = 1.0 # NeoPixel brightness 0.0 (min) to 1.0 (max) +GAMMA = 2.7 # Adjusts perceived brighthess linearity +NUM_PIXELS = 30 # NeoPixel strip length (in pixels) +SPEED = 20000 # adjust this to change the playback speed e.g. 50000 is slow +LOOP = False # set to True for looping +# Switch off onboard NeoPixel... +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 def read_le(value): @@ -57,11 +64,11 @@ def load_bmp(filename): if f.read(2) != b'BM': # check signature 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 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)) bmpHeight = read_le(f.read(4)) # BMPs are traditionally stored bottom-to-top. @@ -72,8 +79,6 @@ def load_bmp(filename): bmpHeight = -bmpHeight flip = False - # print("Size: %d\nImage offset: %d\nHeader size: %d" % - # (bmpFileSize, bmpImageoffset, headerSize)) print("WxH: (%d,%d)" % (bmpWidth, bmpHeight)) if read_le(f.read(2)) != 1: @@ -95,7 +100,7 @@ def load_bmp(filename): clippedHeight = NUM_PIXELS # 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, # this index works incrementally backward in column buffers... @@ -105,22 +110,22 @@ def load_bmp(filename): pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize else: # Bitmap is stored top-to-bottom 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... # BMP files use BGR color order # blue, green, red = bytearray(f.read(3)) blue, green, red = f.read(3) # Rearrange into NeoPixel strip's color order, # while handling brightness & gamma correction: - c[idx + STRIP.order[0]] = int(pow(red / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) - c[idx + STRIP.order[1]] = int(pow(green / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) - c[idx + STRIP.order[2]] = int(pow(blue / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) + c[idx ] = int(pow(green / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) + c[idx+1] = int(pow(red / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) + c[idx+2] = int(pow(blue / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) idx -= 3 # Advance (back) one pixel - # Add one more column, using the NeoPixel strip's normal buffer, - # which is never assigned any color data. This is used to turn - # the strip off at the end of the painting operation. - columns.append(STRIP.buf) + # Add one more column with no color data loaded. This is used + # to turn the strip off at the end of the painting operation. + if not LOOP: + columns.append(bytearray(NUM_PIXELS * 3)) print("Loaded OK!") gc.collect() # Garbage-collect now so playback is smoother @@ -139,28 +144,26 @@ def load_bmp(filename): columns = load_bmp(FILENAME) print("Mem free:", gc.mem_free()) +# Orig code: 10320 bytes free +# New code: 13216 bytes free -# Switch off onboard NeoPixel -neo = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0, auto_write=True) -neo.fill(0) +column_delay = SPEED / 65535.0 / 10.0 # 0.0 to 0.1 seconds +while LOOP: + for c in columns: + neopixel_write(NEOPIXEL_PIN, c) + time.sleep(column_delay) # Column-to-column delay while True: - # Wait for touch pad input: while not TOUCH.value: continue - time.sleep(DELAY_TIME) column_delay = SPEED / 65535.0 / 10.0 # 0.0 to 0.1 seconds # print(column_delay) # Play back color data loaded into each column: for c in columns: - # Rather than replace the data in the NeoPixel strip's buffer - # 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 + neopixel_write(NEOPIXEL_PIN, c) time.sleep(column_delay) # Column-to-column delay # Last column is all 0's, no need to explicitly clear strip diff --git a/Light_Paintstick_HalloWing/light_paintstick_hallowing.py b/Light_Paintstick_HalloWing/light_paintstick_hallowing.py index d203e643e..733c0ed5c 100644 --- a/Light_Paintstick_HalloWing/light_paintstick_hallowing.py +++ b/Light_Paintstick_HalloWing/light_paintstick_hallowing.py @@ -13,30 +13,31 @@ from neopixel_write import neopixel_write # uncomment one line only here to select bitmap FILENAME = "bats.bmp" # BMP file to load from flash filesystem -#FILENAME = "jpw01.bmp" -#FILENAME = "digikey.bmp" -#FILENAME = "burger.bmp" -#FILENAME = "afbanner.bmp" -#FILENAME = "blinka.bmp" -#FILENAME = "ghost.bmp" -#FILENAME = "helix-32x30.bmp" -#FILENAME = "wales2-107x30.bmp" -#FILENAME = "pumpkin.bmp" -#FILENAME = "rainbow.bmp" -#FILENAME = "rainbowRoad.bmp" -#FILENAME = "rainbowZig.bmp" -#FILENAME = "skull.bmp" -#FILENAME = "adabot.bmp" -#FILENAME = "green_stripes.bmp" -#FILENAME = "red_blue.bmp" -#FILENAME = "minerva.bmp" +# FILENAME = "digikey.bmp" +# FILENAME = "burger.bmp" +# FILENAME = "afbanner.bmp" +# FILENAME = "blinka.bmp" +# FILENAME = "ghost04.bmp" +# FILENAME = "ghost07.bmp" +# FILENAME = "ghost02.bmp" +# FILENAME = "helix-32x30.bmp" +# FILENAME = "wales2-107x30.bmp" +# FILENAME = "pumpkin.bmp" +# FILENAME = "rainbow.bmp" +# FILENAME = "rainbowRoad.bmp" +# FILENAME = "rainbowZig.bmp" +# FILENAME = "skull.bmp" +# FILENAME = "adabot.bmp" +# FILENAME = "green_stripes.bmp" +# FILENAME = "red_blue.bmp" +# FILENAME = "minerva.bmp" TOUCH = touchio.TouchIn(board.A2) # Rightmost capacitive touch pad ANALOG = AnalogIn(board.SENSE) # Potentiometer on SENSE pin BRIGHTNESS = 1.0 # NeoPixel brightness 0.0 (min) to 1.0 (max) GAMMA = 2.7 # Adjusts perceived brighthess linearity 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... NEOPIXEL_PIN = digitalio.DigitalInOut(board.NEOPIXEL) NEOPIXEL_PIN.direction = digitalio.Direction.OUTPUT @@ -66,11 +67,11 @@ def load_bmp(filename): if f.read(2) != b'BM': # check signature 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 - bmpImageoffset = read_le(f.read(4)) # Start of image data - headerSize = read_le(f.read(4)) + bmpImageoffset = read_le(f.read(4)) # Start of image data + f.read(4) # Read & ignore header size bmpWidth = read_le(f.read(4)) bmpHeight = read_le(f.read(4)) # BMPs are traditionally stored bottom-to-top. @@ -81,8 +82,6 @@ def load_bmp(filename): bmpHeight = -bmpHeight flip = False - # print("Size: %d\nImage offset: %d\nHeader size: %d" % - # (bmpFileSize, bmpImageoffset, headerSize)) print("WxH: (%d,%d)" % (bmpWidth, bmpHeight)) if read_le(f.read(2)) != 1: @@ -114,7 +113,7 @@ def load_bmp(filename): pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize else: # Bitmap is stored top-to-bottom 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... # BMP files use BGR color order # blue, green, red = bytearray(f.read(3))