Created
August 9, 2020 19:06
-
-
Save FoamyGuy/0be93facafe3e5f321126d76fcfd0e73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gc | |
import time | |
import displayio | |
import terminalio | |
import adafruit_sdcard | |
import busio | |
import digitalio | |
import board | |
import storage | |
import json | |
# Connect to the card and mount the filesystem. | |
spi = busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO) | |
cs = digitalio.DigitalInOut(board.SD_CS) | |
sdcard = adafruit_sdcard.SDCard(spi, cs) | |
vfs = storage.VfsFat(sdcard) | |
storage.mount(vfs, "/sd") | |
# Sample for comparing label and bitmap_label memory usage with Builtin or loaded BDF fonts | |
results = {} | |
# pylint: disable=no-member | |
########## | |
use_builtinfont = True # Set True to use the terminalio.FONT BuiltinFont, | |
# Set False to use a BDF loaded font, see "fontFiles" below | |
########## | |
my_scale = 1 | |
from adafruit_display_text import bitmap_label as bitmap_label | |
from adafruit_display_text import label | |
# Setup the SPI display | |
if "DISPLAY" not in dir(board): | |
# Setup the LCD display with driver | |
# You may need to change this to match the display driver for the chipset | |
# used on your display | |
from adafruit_ili9341 import ILI9341 | |
displayio.release_displays() | |
# setup the SPI bus | |
spi = board.SPI() | |
tft_cs = board.D9 # arbitrary, pin not used | |
tft_dc = board.D10 | |
tft_backlight = board.D12 | |
tft_reset = board.D11 | |
while not spi.try_lock(): | |
spi.configure(baudrate=32000000) | |
spi.unlock() | |
display_bus = displayio.FourWire( | |
spi, | |
command=tft_dc, | |
chip_select=tft_cs, | |
reset=tft_reset, | |
baudrate=32000000, | |
polarity=1, | |
phase=1, | |
) | |
# Number of pixels in the display | |
DISPLAY_WIDTH = 320 | |
DISPLAY_HEIGHT = 240 | |
# create the display | |
display = ILI9341( | |
display_bus, | |
width=DISPLAY_WIDTH, | |
height=DISPLAY_HEIGHT, | |
rotation=180, # The rotation can be adjusted to match your configuration. | |
auto_refresh=True, | |
native_frames_per_second=90, | |
) | |
# reset the display to show nothing. | |
display.show(None) | |
else: | |
# built-in display | |
display = board.DISPLAY | |
print("Display is started") | |
# load all the fonts | |
print("loading fonts...") | |
_font = terminalio.FONT | |
preload_the_glyphs = ( | |
True # set this to True if you want to preload the font glyphs into memory | |
) | |
# preloading the glyphs will help speed up the rendering of text but will use more RAM | |
if preload_the_glyphs: | |
# identify the glyphs to load into memory -> increases rendering speed | |
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! " | |
print("loading glyphs...") | |
if _font is not terminalio.FONT: | |
_font.load_glyphs(glyphs) | |
print("Glyphs are loaded.") | |
print("Fonts completed loading.") | |
# create group | |
gc.collect() | |
print("After creating Group, Memory free: {}".format(gc.mem_free())) | |
my_string = "Welcome to using displayio on CircuitPython!" | |
gc.collect() | |
label_start_memory = gc.mem_free() | |
start_time = time.monotonic() | |
_label = label.Label( | |
font=_font, | |
text=my_string, | |
color=0xFFFFFF, | |
max_glyphs=len(my_string), | |
background_color=0xFF0000, | |
padding_bottom=0, | |
padding_left=0, | |
padding_right=0, | |
padding_top=0, | |
background_tight=False, | |
x=10, | |
y=30, | |
line_spacing=1.25, | |
scale=my_scale, | |
) | |
end_time = time.monotonic() | |
gc.collect() | |
label_end_memory = gc.mem_free() | |
_group = displayio.Group(max_size=2) # Create a group for displaying | |
_group.append(_label) | |
label_memory_used = label_start_memory - label_end_memory | |
print("***") | |
print("{} time to process: {} seconds".format("label", end_time - start_time)) | |
results['label_{}_chars'.format(len(my_string))] = { | |
"mem_used": label_memory_used, | |
"time_to_process": end_time - start_time | |
} | |
display.auto_refresh = True | |
display.show(_group) | |
gc.collect() | |
bitmap_label_start_memory = gc.mem_free() | |
start_time = time.monotonic() | |
_bitmap_label = bitmap_label.Label( | |
font=_font, | |
text=my_string, | |
color=0xFFFFFF, | |
max_glyphs=len(my_string), | |
background_color=0xFF0000, | |
padding_bottom=0, | |
padding_left=0, | |
padding_right=0, | |
padding_top=0, | |
background_tight=False, | |
x=10, | |
y=70, | |
line_spacing=1.25, | |
scale=my_scale, | |
) | |
end_time = time.monotonic() | |
gc.collect() | |
bitmap_label_end_memory = gc.mem_free() | |
_group.append(_bitmap_label) | |
bitmap_label_memory_used = bitmap_label_start_memory - bitmap_label_end_memory | |
results['bitmap_label_{}_chars'.format(len(my_string))] = { | |
"mem_used": bitmap_label_memory_used, | |
"time_to_process": end_time - start_time | |
} | |
print(results) | |
# Use the filesystem as normal. | |
with open("/sd/results.json", "w+") as f: | |
try: | |
existing_obj = json.loads(f.read()) | |
except ValueError: | |
existing_obj = {} | |
pass | |
for key in results.keys(): | |
existing_obj[key] = results[key] | |
f.write(json.dumps(existing_obj)) | |
print("after") | |
while True: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment