diff --git a/demo_animated_sprite.py b/demo_animated_sprite.py new file mode 100644 index 0000000..ce4d4c9 --- /dev/null +++ b/demo_animated_sprite.py @@ -0,0 +1,51 @@ +"""ILI9341 demo (animated sprite). + + Note: This demo requires a board with additional PSRAM. +""" +from ili9341 import Display +from machine import Pin, SPI # type: ignore +from micropython import const # type: ignore +from utime import sleep_us, ticks_us, ticks_diff # type: ignore + +SPRITE_WIDTH = const(221) +SPRITE_HEIGHT = const(154) +SPRITE_COUNT = const(8) +SIZE = const(68068) # width (221) x height (154) x bytes of color (2) + + +def test(): + """Animated sprite.""" + try: + # Baud rate of 40000000 seems about the max + spi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13)) + + display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17)) + display.clear() + + # Load sprite + cat = display.load_sprite('images/Cat221x1232.raw', + SPRITE_WIDTH, SPRITE_HEIGHT * 8) + # Use memoryview to improve memory usage + mv_cat = memoryview(cat) + + x = (display.width - SPRITE_WIDTH) // 2 + y = (display.height - SPRITE_HEIGHT) // 2 + index = 0 # Sprite frame index + + while True: + timer = ticks_us() + offset = SIZE * index + display.draw_sprite(mv_cat[offset: offset + SIZE], x, y, + SPRITE_WIDTH, SPRITE_HEIGHT) + index = (index + 1) % 8 # Set current index + + # Attempt to set framerate to 30 FPS + timer_dif = 33333 - ticks_diff(ticks_us(), timer) + if timer_dif > 0: + sleep_us(timer_dif) + + except KeyboardInterrupt: + display.cleanup() + + +test() diff --git a/images/Cat221x1232.raw b/images/Cat221x1232.raw new file mode 100644 index 0000000..83c46d9 Binary files /dev/null and b/images/Cat221x1232.raw differ