Basic questions about PyPortal display

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Basic questions about PyPortal display

Post by ghulse »

I'm learning to program PyPortal device, starting with very basic stuff. The following code displays the message: Hello World! But what if I want to display a second message, using a separate text label? I know I can display multiple lines of text by adding '\' at the end of each line. But I would like to have a second text label, specifying its x, y coordinates, and not sure how to do that.

Also, do I need another library to specify background color? Currently, the line of code: background = GREEN doesn't change the background color.

Code: Select all


import board
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text import label

# Hex Colors
WHITE = 0xFFFFFF
RED = 0xFF0000
YELLOW = 0xFFFF00
GREEN = 0x00FF00
BLUE = 0x0000FF
PURPLE = 0xFF00FF
BLACK = 0x000000
ORANGE = 0xC91818

display = board.DISPLAY
background = GREEN
message = "Hello World!"
message2 = "Hello Galaxy!"

# Set text, font, and color and !
text = (message)
font = bitmap_font.load_font("fonts/Helvetica-Bold-16.bdf")

color = ORANGE

# Create the text label
text_area = label.Label(font, text=text, color=color)

# Set the location
text_area.x = 90
text_area.y = 120

# Show it
display.show(text_area)

# Loop forever so you can enjoy your text
while True:
    pass

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Basic questions about PyPortal display

Post by dastels »

You create a second label the same way except that you'll need to create a Group to hold both, then Displayshow the group.

See https://learn.adafruit.com/circuitpytho ... -displayio and https://learn.adafruit.com/making-a-pyp ... -displayio for more information and examples.

Dave

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Basic questions about PyPortal display

Post by ghulse »

dastels wrote: Sun Jan 22, 2023 2:25 pm You create a second label the same way except that you'll need to create a Group to hold both, then Displayshow the group.
Sorry if I'm being a bit dense. The first example uses only one text label. The second example has a lot going on, and it's difficult to distinguish between all the various buttons and text labels. There is no Displayshow used in the second example.

I'm experimenting with different fonts, colors before I add the string parsing of the NOAA buoy data.

So with the following code, how exactly do I create a second text label and group it with the first, perhaps using a different color? And also, why doesn't background = GREEN actually change the background color?

Code: Select all

display = board.DISPLAY
background = GREEN
message = "Hello World!"
message2 = "Hello Galaxy!"

# DisplayIO Setup
# Set up fonts
font_large = bitmap_font.load_font("fonts/Helvetica-Bold-16.bdf")
# preload fonts
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: "

font_large.load_glyphs(glyphs)

# Set text, font, and color and !
text = (message)
font = font_large
color = WHITE

# Create the text label
text_area = label.Label(font, text=text, color=color)

# Set the location
text_area.x = 90
text_area.y = 120

# Show it
display.show(text_area)

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Basic questions about PyPortal display

Post by dastels »

There is a show: board.DISPLAY.show(splash). That's how things get displayed.

- create a Group
- create the Labels
- append the Labels to the Group
- show the Group

Read through the different pages in the second guide, not just the final code. It's broken down nicely.

Dave

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Basic questions about PyPortal display

Post by ghulse »

dastels wrote: Tue Jan 24, 2023 3:16 pmRead through the different pages in the second guide, not just the final code. It's broken down nicely.
Okay, checking out out. Thanks!

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Basic questions about PyPortal display

Post by ghulse »

I've figured out how to have multiple text labels and append them to a group. Thank you.

Now comes the next big hurdle. It seems that most, if not all, pyportal projects that get data from a url do so via a json routine. I have to get text from an RSS Feed that is not json and then parse it manually (as it were). Here's how I did it on a MagTag.

Code: Select all

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

while True:
    # print("Fetching and parsing NOAA data
    response = requests.get(noaa_url)

    html_text = response.text

    #-- Find the substring between the start and end strings
    ##-- Find the substring between the start and end strings
    start_string = '<description>'
    end_string = "</description>"
    start_index = html_text.find(start_string)
    start_index = html_text.find(start_string, start_index+ len(start_string))
    end_index = html_text.find(end_string, start_index + len(start_string))

    desc = html_text[start_index: end_index+start_index]
    rows = desc.split('\n')
Any help on how to do this on the pyPortal would be greatly appreciated.

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Basic questions about PyPortal display

Post by dastels »

The same approach should work, Just ignore that part of the pyportal library (if you're using any of it).

Dave

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Basic questions about PyPortal display

Post by ghulse »

dastels wrote: Wed Jan 25, 2023 7:00 pm The same approach should work, Just ignore that part of the pyportal library (if you're using any of it).
Aren't the socketpool and requests calls specifically for the MagTag? Or do I need to install specific libraries to make them work on the PyPortal?

Code: Select all

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

while True:
    # print("Fetching and parsing NOAA data
    response = requests.get(noaa_url)

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Basic questions about PyPortal display

Post by dastels »

Read the PyPortal guide if you haven't https://learn.adafruit.com/adafruit-pyportal, the part specific to wifi is at https://learn.adafruit.com/adafruit-pyp ... et-connect.

It's a little different in the detail since on the PyPortal the ESP32 is accessed as a co-processor via SPI, whereas on the MagTag your code is running directly on the ESP32.

Dave

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Basic questions about PyPortal display

Post by ghulse »

dastels wrote: Sat Jan 28, 2023 10:14 pm Read the PyPortal guide if you haven't https://learn.adafruit.com/adafruit-pyportal, the part specific to wifi is at https://learn.adafruit.com/adafruit-pyp ... et-connect.
This was very helpful. Thanks!

I've now got buoy data displayed to the pyportal. Very thrilled about this!

I'd like to add a background image (bmap) and have various text labels display on top of it. Is there a way to add the bmap image to the group that currently holds the text labels? Or is there a different approach?

I checked out this word of the day project, which uses a background image, but it uses the pyportal.fetch method.

https://learn.adafruit.com/pyportal-wor ... ay-display

Edit: Never mind, figured it out!!

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Basic questions about PyPortal display

Post by ghulse »

My code displays NOAA tide data correctly, but now I want it to refresh every ten minutes or so. When I open the url connect inside of a while loop, it throws up a memory error. This method works great in the python version. What is happening here?

Code: Select all

while True:
    # open rss feed and start parsing text
    print("Getting data from NOAA...")
    r = requests.get(noaa_url)
    text = r.text
    
    <snip>
    
    time.sleep(5*60)   # sleep for 5 minutes
    
MemoryError: memory allocation failed, allocating 47820 bytes

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Basic questions about PyPortal display

Post by dastels »

You're running out of available memory. Maybe try assigning something simple (0 or false) to r and gc.collect() before the sleep. I.e. throw out the old response & data before fetching new ones.

Dave

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Basic questions about PyPortal display

Post by ghulse »

dastels wrote: Wed Feb 01, 2023 11:27 am You're running out of available memory. Maybe try assigning something simple (0 or false) to r and gc.collect() before the sleep. I.e. throw out the old response & data before fetching new ones.

Dave
Thanks. What's gc.collect?

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Basic questions about PyPortal display

Post by dastels »

Code: Select all

import gc
at the start with the other imports and use

Code: Select all

gc.collect()
to reclaim memory that isn't being used.

Also see https://learn.adafruit.com/welcome-to-c ... es-3129414 and https://learn.adafruit.com/Memory-savin ... cuitPython.

Dave

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Basic questions about PyPortal display

Post by ghulse »

The code works on initial run, but then gives a memory error when it tries to refresh. This is the error I'm getting:

Getting data from NOAA...
Traceback (most recent call last):
File "code.py", line 55, in <module>
File "adafruit_requests.py", line 446, in text
File "adafruit_requests.py", line 426, in content
MemoryError: memory allocation failed, allocating 46821 bytes

Line 55 is text = r.text.

I printed the value of gc.mem_free() just before the time.sleep and it comes back with 122080.
I also removed the background image entirely, but that didn't help. I've looked over the links you provided and still trying to figure out what to do.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”