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