|
| 1 | +# HalloWing Light Paintbrush |
| 2 | +# Single images only. Filename is set |
| 3 | +# in code, potentiometer is used to tune playback SPEED |
| 4 | +# images should be 30px high, up to 100px wide, 24-bit .bmp files |
| 5 | + |
| 6 | +import gc |
| 7 | +import time |
| 8 | +import board |
| 9 | +import touchio |
| 10 | +import digitalio |
| 11 | +from analogio import AnalogIn |
| 12 | +from neopixel_write import neopixel_write |
| 13 | + |
| 14 | +# uncomment one line only here to select bitmap |
| 15 | +FILENAME = "bats.bmp" # BMP file to load from flash filesystem |
| 16 | +#FILENAME = "jpw01.bmp" |
| 17 | +#FILENAME = "digikey.bmp" |
| 18 | +#FILENAME = "burger.bmp" |
| 19 | +#FILENAME = "afbanner.bmp" |
| 20 | +#FILENAME = "blinka.bmp" |
| 21 | +#FILENAME = "ghost.bmp" |
| 22 | +#FILENAME = "helix-32x30.bmp" |
| 23 | +#FILENAME = "wales2-107x30.bmp" |
| 24 | +#FILENAME = "pumpkin.bmp" |
| 25 | +#FILENAME = "rainbow.bmp" |
| 26 | +#FILENAME = "rainbowRoad.bmp" |
| 27 | +#FILENAME = "rainbowZig.bmp" |
| 28 | +#FILENAME = "skull.bmp" |
| 29 | +#FILENAME = "adabot.bmp" |
| 30 | +#FILENAME = "green_stripes.bmp" |
| 31 | +#FILENAME = "red_blue.bmp" |
| 32 | +#FILENAME = "minerva.bmp" |
| 33 | + |
| 34 | +TOUCH = touchio.TouchIn(board.A2) # Rightmost capacitive touch pad |
| 35 | +ANALOG = AnalogIn(board.SENSE) # Potentiometer on SENSE pin |
| 36 | +BRIGHTNESS = 1.0 # NeoPixel brightness 0.0 (min) to 1.0 (max) |
| 37 | +GAMMA = 2.7 # Adjusts perceived brighthess linearity |
| 38 | +NUM_PIXELS = 30 # NeoPixel strip length (in pixels) |
| 39 | +LOOP = False #set to True for looping |
| 40 | +# Switch off onboard NeoPixel... |
| 41 | +NEOPIXEL_PIN = digitalio.DigitalInOut(board.NEOPIXEL) |
| 42 | +NEOPIXEL_PIN.direction = digitalio.Direction.OUTPUT |
| 43 | +neopixel_write(NEOPIXEL_PIN, bytearray(3)) |
| 44 | +# ...then assign NEOPIXEL_PIN to the external NeoPixel connector: |
| 45 | +NEOPIXEL_PIN = digitalio.DigitalInOut(board.EXTERNAL_NEOPIXEL) |
| 46 | +NEOPIXEL_PIN.direction = digitalio.Direction.OUTPUT |
| 47 | +neopixel_write(NEOPIXEL_PIN, bytearray(NUM_PIXELS * 3)) |
| 48 | + |
| 49 | +# Interpret multi-byte value from file as little-endian value |
| 50 | +def read_le(value): |
| 51 | + result = 0 |
| 52 | + shift = 0 |
| 53 | + for byte in value: |
| 54 | + result += byte << shift |
| 55 | + shift += 8 |
| 56 | + return result |
| 57 | + |
| 58 | +class BMPError(Exception): |
| 59 | + pass |
| 60 | + |
| 61 | +def load_bmp(filename): |
| 62 | + try: |
| 63 | + print("Loading", filename) |
| 64 | + with open("/" + filename, "rb") as f: |
| 65 | + print("File opened") |
| 66 | + if f.read(2) != b'BM': # check signature |
| 67 | + raise BMPError("Not BitMap file") |
| 68 | + |
| 69 | + bmpFileSize = read_le(f.read(4)) |
| 70 | + f.read(4) # Read & ignore creator bytes |
| 71 | + |
| 72 | + bmpImageoffset = read_le(f.read(4)) # Start of image data |
| 73 | + headerSize = read_le(f.read(4)) |
| 74 | + bmpWidth = read_le(f.read(4)) |
| 75 | + bmpHeight = read_le(f.read(4)) |
| 76 | + # BMPs are traditionally stored bottom-to-top. |
| 77 | + # If bmpHeight is negative, image is in top-down order. |
| 78 | + # This is not BMP canon but has been observed in the wild! |
| 79 | + flip = True |
| 80 | + if bmpHeight < 0: |
| 81 | + bmpHeight = -bmpHeight |
| 82 | + flip = False |
| 83 | + |
| 84 | + # print("Size: %d\nImage offset: %d\nHeader size: %d" % |
| 85 | + # (bmpFileSize, bmpImageoffset, headerSize)) |
| 86 | + print("WxH: (%d,%d)" % (bmpWidth, bmpHeight)) |
| 87 | + |
| 88 | + if read_le(f.read(2)) != 1: |
| 89 | + raise BMPError("Not single-plane") |
| 90 | + bmpDepth = read_le(f.read(2)) # bits per pixel |
| 91 | + # print("Bit depth: %d" % (bmpDepth)) |
| 92 | + if bmpDepth != 24: |
| 93 | + raise BMPError("Not 24-bit") |
| 94 | + if read_le(f.read(2)) != 0: |
| 95 | + raise BMPError("Compressed file") |
| 96 | + |
| 97 | + print("Image format OK, reading data...") |
| 98 | + |
| 99 | + rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary |
| 100 | + |
| 101 | + # Constrain rows loaded to pixel strip length |
| 102 | + clippedHeight = bmpHeight |
| 103 | + if clippedHeight > NUM_PIXELS: |
| 104 | + clippedHeight = NUM_PIXELS |
| 105 | + |
| 106 | + # Allocate per-column pixel buffers, sized for NeoPixel strip: |
| 107 | + columns = [bytearray(NUM_PIXELS * 3) for i in range(bmpWidth)] |
| 108 | + |
| 109 | + # Image is displayed at END (not start) of NeoPixel strip, |
| 110 | + # this index works incrementally backward in column buffers... |
| 111 | + idx = (NUM_PIXELS - 1) * 3 |
| 112 | + for row in range(clippedHeight): # For each scanline... |
| 113 | + if flip: # Bitmap is stored bottom-to-top order (normal BMP) |
| 114 | + pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize |
| 115 | + else: # Bitmap is stored top-to-bottom |
| 116 | + pos = bmpImageoffset + row * rowSize |
| 117 | + f.seek(pos) # Start of scanline |
| 118 | + for c in columns: # For each pixel of scanline... |
| 119 | + # BMP files use BGR color order |
| 120 | + # blue, green, red = bytearray(f.read(3)) |
| 121 | + blue, green, red = f.read(3) |
| 122 | + # Rearrange into NeoPixel strip's color order, |
| 123 | + # while handling brightness & gamma correction: |
| 124 | + c[idx ] = int(pow(green / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) |
| 125 | + c[idx+1] = int(pow(red / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) |
| 126 | + c[idx+2] = int(pow(blue / 255, GAMMA) * BRIGHTNESS * 255 + 0.5) |
| 127 | + idx -= 3 # Advance (back) one pixel |
| 128 | + |
| 129 | + # Add one more column with no color data loaded. This is used |
| 130 | + # to turn the strip off at the end of the painting operation. |
| 131 | + if not LOOP: |
| 132 | + columns.append(bytearray(NUM_PIXELS * 3)) |
| 133 | + |
| 134 | + print("Loaded OK!") |
| 135 | + gc.collect() # Garbage-collect now so playback is smoother |
| 136 | + return columns |
| 137 | + |
| 138 | + except OSError as e: |
| 139 | + if e.args[0] == 28: |
| 140 | + raise OSError("OS Error 28 0.25") |
| 141 | + else: |
| 142 | + raise OSError("OS Error 0.5") |
| 143 | + except BMPError as e: |
| 144 | + print("Failed to parse BMP: " + e.args[0]) |
| 145 | + |
| 146 | + |
| 147 | +# Load BMP image, return 'columns' array: |
| 148 | +columns = load_bmp(FILENAME) |
| 149 | + |
| 150 | +print("Mem free:", gc.mem_free()) |
| 151 | +# Orig code: 10320 bytes free |
| 152 | +# New code: 13216 bytes free |
| 153 | + |
| 154 | +column_delay = ANALOG.value / 65535.0 / 10.0 # 0.0 to 0.1 seconds |
| 155 | +while LOOP: |
| 156 | + for c in columns: |
| 157 | + neopixel_write(NEOPIXEL_PIN, c) |
| 158 | + time.sleep(column_delay) # Column-to-column delay |
| 159 | + |
| 160 | +while True: |
| 161 | + # Wait for touch pad input: |
| 162 | + while not TOUCH.value: |
| 163 | + continue |
| 164 | + |
| 165 | + column_delay = ANALOG.value / 65535.0 / 10.0 # 0.0 to 0.1 seconds |
| 166 | + # print(column_delay) |
| 167 | + |
| 168 | + # Play back color data loaded into each column: |
| 169 | + for c in columns: |
| 170 | + neopixel_write(NEOPIXEL_PIN, c) |
| 171 | + time.sleep(column_delay) # Column-to-column delay |
| 172 | + # Last column is all 0's, no need to explicitly clear strip |
| 173 | + |
| 174 | + # Wait for touch pad release, just in case: |
| 175 | + while TOUCH.value: |
| 176 | + continue |
0 commit comments