"""GIF playback helper for the custom RLCD CircuitPython driver.""" import gc import time import gifio class GIFPlayer: def __init__(self, display): self.display = display def play(self, filename, *, loops=1, x=40, y=0, threshold=1, clear_between_frames=False, max_frames=-1): """Decode a GIF from disk and push frames to the reflective LCD. CircuitPython gifio currently supports GIFs up to 320 pixels wide, so x defaults to 40 to center a 320-wide GIF on this 400-wide display. The ST7305/RLCD display is monochrome in this project, so frames are thresholded from palette indexes: palette index 0 is white; any index >= threshold is black. """ gif = gifio.OnDiskGif(filename) try: count = 0 while loops < 0 or count < loops: frame_count = 0 while True: if max_frames > 0 and frame_count >= max_frames: break delay = gif.next_frame() if delay is None: break if clear_between_frames: self.display.clear(0) if hasattr(self.display, "draw_bitmap_color"): self.display.draw_bitmap_color(gif.bitmap, gif.palette, x, y) else: self.display.draw_bitmap_threshold(gif.bitmap, x, y, threshold=threshold) self.display.show() frame_count += 1 # gifio delay is seconds in modern CircuitPython builds. If # an older build plays 100x too slowly, divide by 100 here. time.sleep(max(0.0, delay)) count += 1 if loops < 0 or count < loops: # OnDiskGif has no reliable seek/rewind API; reopen to loop. gif.deinit() gc.collect() gif = gifio.OnDiskGif(filename) finally: try: gif.deinit() except AttributeError: pass gc.collect()