In the embedded code element below, click on the Download Project Bundle button, and save the .zip archive file to your computer.
Then, uncompress the .zip file, it will unpack to a folder named PyPortal_ObliqueStrategies.
Connect your PyPortal to your computer via a known good USB data+power cable. It will show up in your file system as a drive named CIRCUITPY. Copy the contents of the PyPortal_ObliqueStrategies directory to your PyPortal CIRCUITPY drive.
# SPDX-FileCopyrightText: 2019 Collin Cunningham for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
This code will display a random strategy from strategies.py when the
PyPortal screen is pressed. See the original Oblique Strategies
by Brian Eno & Peter Schmidt here: https://www.enoshop.co.uk/product/oblique-strategies
"""
import random
import board
from strategies import strategies
from adafruit_pyportal import PyPortal
cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
# create pyportal object w no data source (we'll feed it text later)
pyportal = PyPortal(url = None,
json_path = None,
status_neopixel = board.NEOPIXEL,
default_bg = None,
text_font = cwd+"fonts/Arial-ItalicMT-17.bdf",
text_position = (30, 120),
text_color = 0xFFFFFF,
)
pyportal.set_text("loading ...") # display while user waits
pyportal.preload_font() # speed things up by preloading font
pyportal.set_text("OBLIQUE STRATEGIES\nBrian Eno / Peter Schmidt") # show title
while True:
if pyportal.touchscreen.touch_point:
# get random string from array and wrap w line breaks
strat = pyportal.wrap_nicely(random.choice(strategies), 35)
outstring = '\n'.join(strat)
# display new text
pyportal.set_text(outstring, 0)
# don't repeat until a new touch begins
while pyportal.touchscreen.touch_point:
continue
How it works
The code.py file is relatively simple, performing a few basic operations …
Import strategies text
from strategies import strategies
All of the strategy text that will be displayed is stored in strategies.py, so we'll import it all as an array of strings at the start.
PyPortal object
pyportal = PyPortal
The pyportal object is created with font path and text formatting info, but no URL or json path - we omit these because we'll be setting the display text within the main loop.
Startup Text
pyportal.set_text("loading ...")
Next, we display loading … on the PyPortal and begin preloading the font so all characters can be displayed quickly from now on. Once preloading is finished, title & author text is displayed.
Main loop
while True:
In the repeating main loop, the following actions are performed:
- Check to see if the screen was touched
- If screen was touched, get a random string from the strategies array and format it so it wraps properly on the display.
- Display the text on PyPortal
Debounce
Lastly, you'll see these two lines at the bottom:
while pyportal.touchscreen.touch_point: continue
This ensures that the code loads only one new strategy when the user touches the screen - even if they keep their finger held down.
Page last edited January 21, 2025
Text editor powered by tinymce.