Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active July 14, 2022 20:43
Show Gist options
  • Save todbot/ee3aefa3c0ef75ce8bf815e8d5e6cbc5 to your computer and use it in GitHub Desktop.
Save todbot/ee3aefa3c0ef75ce8bf815e8d5e6cbc5 to your computer and use it in GitHub Desktop.
Demonstrate external SD card in CircuitPython (on LILYGO ESP32-S2 board https://circuitpython.org/board/lilygo_ttgo_t8_s2_st7789/ )
# lilygo_esp32s2_imagedisplay.py
# 25 Jul 2021 - todbot / Tod Kurt
# SD card handling from:
# https://learn.adafruit.com/adafruit-micro-sd-breakout-board-card-tutorial/circuitpython
import busio
import sdcardio
import storage
import os
sd_spi = busio.SPI(clock=board.SD_CLK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
img_files=[]
img_tilegrids=displayio.Group()
img_bitmaps=[]
try:
sdcard = sdcardio.SDCard(sd_spi, cs=board.SD_CS)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
for fn in os.listdir('/sd'):
print(fn)
if fn.lower().endswith('.bmp') and not fn.startswith('.'):
ffn = "/sd/"+fn
print("using ",ffn)
img_files.append(ffn)
bitmap, palette = adafruit_imageload.load(ffn,
bitmap=displayio.Bitmap,
palette=displayio.Palette)
tilegrid = displayio.TileGrid(bitmap, pixel_shader=palette)
img_tilegrids.append(tilegrid)
except OSError as e:
print("error:",e)
print("Hello World!", sd_spi.frequency)
print("imgs:",img_files)
print("img tiles:",img_tilegrids)
display.auto_refresh = False
screen.append(img_tilegrids)
while True:
for t in img_tilegrids:
#print("boop", gc.mem_free(), t.x, t.y, t.pixel_shader)
t.hidden = False
display.refresh()
t.hidden = True
#time.sleep(0.1)
while True:
for t in img_tilegrids:
print("boop", gc.mem_free(), t.x, t.y, t.pixel_shader)
screen.append(t)
display.refresh()
#time.sleep(0.01)
screen.pop()
while True:
for fn in img_files:
print("displaying", fn, "at:", sd_spi.frequency)
with open(fn,'rb') as f:
odb = displayio.OnDiskBitmap(f)
bitmap = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader)
screen.append(bitmap)
# Wait for the image to load.
display.refresh() # target_frames_per_second=5)
time.sleep(5)
screen.pop() # remove bitmap so we don't run out of memory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment