|
| 1 | +# SPDX-FileCopyrightText: 2023 John Park for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# LCARS MatrixPortal Display |
| 5 | + |
| 6 | +import time |
| 7 | +import os |
| 8 | +import board |
| 9 | +import displayio |
| 10 | +from digitalio import DigitalInOut, Pull |
| 11 | +from adafruit_matrixportal.matrix import Matrix |
| 12 | +from adafruit_debouncer import Debouncer |
| 13 | + |
| 14 | +SPRITESHEET_FOLDER = "/bmps" |
| 15 | +DEFAULT_FRAME_DURATION = 0.7 # 100ms |
| 16 | +AUTO_ADVANCE_LOOPS = 3 |
| 17 | + |
| 18 | +# --- Display setup --- |
| 19 | +matrix = Matrix(bit_depth=1, width=128, height=64) |
| 20 | +sprite_group = displayio.Group() |
| 21 | +matrix.display.show(sprite_group) |
| 22 | + |
| 23 | +# --- Button setup --- |
| 24 | +pin_down = DigitalInOut(board.BUTTON_DOWN) |
| 25 | +pin_down.switch_to_input(pull=Pull.UP) |
| 26 | +button_down = Debouncer(pin_down) |
| 27 | +pin_up = DigitalInOut(board.BUTTON_UP) |
| 28 | +pin_up.switch_to_input(pull=Pull.UP) |
| 29 | +button_up = Debouncer(pin_up) |
| 30 | + |
| 31 | +auto_advance = False |
| 32 | + |
| 33 | +file_list = sorted( |
| 34 | + [ |
| 35 | + f |
| 36 | + for f in os.listdir(SPRITESHEET_FOLDER) |
| 37 | + if (f.endswith(".bmp") and not f.startswith(".")) |
| 38 | + ] |
| 39 | +) |
| 40 | + |
| 41 | +if len(file_list) == 0: |
| 42 | + raise RuntimeError("No images found") |
| 43 | + |
| 44 | +current_image = None |
| 45 | +current_frame = 0 |
| 46 | +current_loop = 0 |
| 47 | +frame_count = 0 |
| 48 | +frame_duration = DEFAULT_FRAME_DURATION |
| 49 | + |
| 50 | + |
| 51 | +def load_image(): |
| 52 | + """ |
| 53 | + Load an image as a sprite |
| 54 | + """ |
| 55 | + # pylint: disable=global-statement |
| 56 | + global current_frame, current_loop, frame_count, frame_duration |
| 57 | + while sprite_group: |
| 58 | + sprite_group.pop() |
| 59 | + |
| 60 | + filename = SPRITESHEET_FOLDER + "/" + file_list[current_image] |
| 61 | + |
| 62 | + bitmap = displayio.OnDiskBitmap(filename) |
| 63 | + sprite = displayio.TileGrid( |
| 64 | + bitmap, |
| 65 | + pixel_shader=bitmap.pixel_shader, |
| 66 | + tile_width=bitmap.width, |
| 67 | + tile_height=matrix.display.height, |
| 68 | + ) |
| 69 | + |
| 70 | + sprite_group.append(sprite) |
| 71 | + |
| 72 | + current_frame = 0 |
| 73 | + current_loop = 0 |
| 74 | + frame_count = int(bitmap.height / matrix.display.height) |
| 75 | + frame_duration = DEFAULT_FRAME_DURATION |
| 76 | + |
| 77 | + |
| 78 | +def advance_image(): |
| 79 | + """ |
| 80 | + Advance to the next image in the list and loop back at the end |
| 81 | + """ |
| 82 | + # pylint: disable=global-statement |
| 83 | + global current_image |
| 84 | + if current_image is not None: |
| 85 | + current_image += 1 |
| 86 | + if current_image is None or current_image >= len(file_list): |
| 87 | + current_image = 0 |
| 88 | + load_image() |
| 89 | + |
| 90 | + |
| 91 | +def advance_frame(): |
| 92 | + """ |
| 93 | + Advance to the next frame and loop back at the end |
| 94 | + """ |
| 95 | + # pylint: disable=global-statement |
| 96 | + global current_frame, current_loop |
| 97 | + current_frame = current_frame + 1 |
| 98 | + if current_frame >= frame_count: |
| 99 | + current_frame = 0 |
| 100 | + current_loop = current_loop + 1 |
| 101 | + sprite_group[0][0] = current_frame |
| 102 | + |
| 103 | +advance_image() |
| 104 | + |
| 105 | +while True: |
| 106 | + button_down.update() |
| 107 | + button_up.update() |
| 108 | + if button_up.fell: |
| 109 | + print("up fell") |
| 110 | + auto_advance = not auto_advance |
| 111 | + if button_down.fell: |
| 112 | + print("down fell") |
| 113 | + advance_image() |
| 114 | + advance_frame() |
| 115 | + time.sleep(frame_duration) |
0 commit comments