Here is the code from one of the examples that are included with the library. To run the examples, simply rename them as code.py and place them in the root of your CIRCUITPY drive.
Simple Test
This example was written to use the square 3.4" display, but should be able to work with any of the displays. It uses the top level Qualia layer and makes use of the graphics and network. It connects to your WiFi, downloads some test data and displays the data in the REPL.
# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
#
# NOTE: Make sure you've set up your settings.toml file before running this example
# https://learn.adafruit.com/getting-started-with-web-workflow-using-the-code-editor/
from adafruit_qualia import Qualia
from adafruit_qualia.graphics import Displays
# Set a data source URL
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
# Create the Qualia object
qualia = Qualia(Displays.SQUARE34, url=TEXT_URL)
# Go get that data
print("Fetching text from", TEXT_URL)
data = qualia.fetch()
# Print out what we got
print("-" * 40)
print(data)
print("-" * 40)
Quotes Example
The quotes example is more like how the PyPortal works in that a data source is defined, two text fields are created, and the quote and author data are displayed. This example was also written for the square 3.4" display, but could be modified to run on other displays by adjusting the text field settings such as text_wrap.
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
from adafruit_qualia import Qualia
from adafruit_qualia.graphics import Displays
# Set up where we'll be fetching data from
DATA_SOURCE = "https://www.adafruit.com/api/quotes.php"
QUOTE_LOCATION = [0, "text"]
AUTHOR_LOCATION = [0, "author"]
qualia = Qualia(
Displays.SQUARE34,
url=DATA_SOURCE,
json_path=(QUOTE_LOCATION, AUTHOR_LOCATION),
default_bg=0x333333,
)
qualia.add_text(
text_position=(20, 120), # quote location
text_color=0xFFFFFF, # quote text color
text_wrap=25, # characters to wrap for quote
text_maxlen=180, # max text size for quote
text_scale=3, # quote text size
)
qualia.add_text(
text_position=(5, 240), # author location
text_color=0x8080FF, # author text color
text_wrap=0, # no wrap for author
text_maxlen=180, # max text size for quote & author
text_scale=3, # author text size
)
while True:
try:
value = qualia.fetch()
print("Response is", value)
except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e)
time.sleep(60)
QR Code Example
The QR Code Generation example generates a QR code and displays it in the center of the display. This example was written for the round 2.1" display, but could easily be adapted for the other displays.
# SPDX-FileCopyrightText: 2021 Jose David M.
# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# NOTE: Make sure you've set up your settings.toml file before running this example
# https://learn.adafruit.com/getting-started-with-web-workflow-using-the-code-editor/
"""
This example shows a web address QR on the display
"""
import time
from adafruit_qualia.graphics import Displays, Graphics
from adafruit_qualia.peripherals import Peripherals
# Background Information
base = Graphics(Displays.ROUND21, default_bg=0x990099)
# Set up Peripherals
peripherals = Peripherals(i2c_bus=base.i2c_bus)
# Set display to show
display = base.display
# WebPage to show in the QR
webpage = "http://www.adafruit.com"
# QR size Information
qr_size = 9 # Pixels
scale = 10
# Create a barcode
base.qrcode(
webpage,
qr_size=scale,
x=(display.width // 2) - ((qr_size + 5) * scale),
y=(display.height // 2) - ((qr_size + 4) * scale),
)
while True:
if peripherals.button_up:
peripherals.backlight = True
if peripherals.button_down:
peripherals.backlight = False
time.sleep(0.1)
Paint Example
This last example is the most complex one and will run on any of the displays with a touchscreen. This was adapted from an example included in the FocalTouch library and ends up being around 30 lines less, but supporting many more displays. This example only uses the Graphics layer and shows how to make use of the touch screen.
# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
Simple painting demo that works with on any touch display
"""
import displayio
from adafruit_qualia.graphics import Displays, Graphics
# For other displays:
# 2.1" Round = Displays.ROUND21
# 3.4" Square = Displays.SQUARE34
# 320 x 820 Bar - Displays.BAR320X820
# 320 x 960 Bar - Displays.BAR320X960
graphics = Graphics(Displays.SQUARE40, default_bg=None, auto_refresh=False)
if graphics.touch is None:
raise RuntimeError("This example requires a touch screen.")
# Main Program
pixel_size = 6
palette_width = 160
palette_height = graphics.display.height // 8
bitmap = displayio.Bitmap(graphics.display.width, graphics.display.height, 65535)
# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(
bitmap,
pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565),
)
# Add the TileGrid to the Group
graphics.root_group.append(tile_grid)
# Add the Group to the Display
graphics.display.root_group = graphics.root_group
current_color = displayio.ColorConverter().convert(0xFFFFFF)
for i in range(palette_width):
color_index = i * 255 // palette_width
rgb565 = displayio.ColorConverter().convert(color_index | color_index << 8 | color_index << 16)
r_mask = 0xF800
g_mask = 0x07E0
b_mask = 0x001F
for j in range(palette_height):
bitmap[i, j + palette_height] = rgb565 & b_mask
bitmap[i, j + palette_height * 2] = rgb565 & (b_mask | g_mask)
bitmap[i, j + palette_height * 3] = rgb565 & g_mask
bitmap[i, j + palette_height * 4] = rgb565 & (r_mask | g_mask)
bitmap[i, j + palette_height * 5] = rgb565 & r_mask
bitmap[i, j + palette_height * 6] = rgb565 & (r_mask | b_mask)
bitmap[i, j + palette_height * 7] = rgb565
graphics.display.auto_refresh = True
while True:
if graphics.touch.touched:
try:
for touch in graphics.touch.touches:
x = touch["x"]
y = touch["y"]
if not 0 <= x < graphics.display.width or not 0 <= y < graphics.display.height:
continue # Skip out of bounds touches
if x < palette_width:
current_color = bitmap[x, y]
else:
for i in range(pixel_size):
for j in range(pixel_size):
x_pixel = x - (pixel_size // 2) + i
y_pixel = y - (pixel_size // 2) + j
if (
0 <= x_pixel < graphics.display.width
and 0 <= y_pixel < graphics.display.height
):
bitmap[x_pixel, y_pixel] = current_color
except RuntimeError:
pass
Page last edited January 22, 2025
Text editor powered by tinymce.