Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fetch() for portrait oriented images #60

wants to merge 5 commits into
base: master


Copy link

cogliano commented Jan 6, 2020

This code was added to allow multiple fetch() commands to work correctly when there is a mix of portrait and landscape oriented images to display on the PyPortal. An additional parameter, image_dim_json_path, was added to init() to point to the JSON section that contains the original width and height of the image. If this parameter (a width and height tuple) is not present, the fetch should work as before with no portrait mode considerations.

Here is an example of the feature being used. I commented out some of the lines so the code could run without external file dependencies. Specific landscape and portrait itemids can be uncommented for testing, or use the randomint() function.

More info on the api used in the example is here:

import time
import board
import random
from adafruit_pyportal import PyPortal
from import Circle

WIDTH = board.DISPLAY.width
HEIGHT = board.DISPLAY.height

apiurl = ""

pyportal = PyPortal(
                    # default_bg=background_file,
                    image_resize=(WIDTH, HEIGHT - 15),
                    image_position=(0, 0),
                    #text_position=(4,HEIGHT - 9),
                    #text_color =0xFFFFFF

circle = Circle(312, 233, 5, fill=0)
loopcount = 0
errorcount = 0
while True:
    response = None
        circle.fill = 0xFF0000
        itemid = random.randint(1, 30000)
        # itemid = 20 # portrait mode example
        # itemid = 21 # landscape mode example
        print("retrieving url:", apiurl +str(itemid))
        response = pyportal.fetch(apiurl + str(itemid))
        circle.fill = 0
        print("Response is", response)
        loopcount = loopcount + 1

    except (RuntimeError, KeyError) as e:
        print("An error occured, retrying! -", e)
        print("loop counter:", loopcount)
        assert errorcount < 20, "Too many errors, stopping"
        errorcount = errorcount + 1

    errorcount = 0
    stamp = time.monotonic()
    # wait 5 minutes before getting again
    while (time.monotonic() - stamp) < (5*60):
        # or, if they touch the screen, fetch immediately!
        if pyportal.touchscreen.touch_point:

cogliano added 5 commits Jan 6, 2020
@cogliano cogliano marked this pull request as ready for review Jan 6, 2020
@kattni kattni requested a review from adafruit/circuitpythonlibrarians Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
1 participant
You can’t perform that action at this time.