Due to high demand, expect some shipping delays at this time - orders may not ship for up to 2-3 business days.
0

MatrixPortal Memory Error
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MatrixPortal Memory Error

by Tortilla on Fri Jan 21, 2022 11:36 pm

Hello, I am getting a MemoryError after attempting to import "from adafruit_matrixportal.matrixportal import MatrixPortal."

I inserted some print statements using gc.mem_free() and received the following output:

Code: Select all | TOGGLE FULL SIZE
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Total memory free right before importing matrixportal library is 106160
Total memory free right after importing matrixportal library is 31520
Total memory free right before matrixportal call is 30896
Traceback (most recent call last):
  File "code.py", line 69, in <module>
  File "adafruit_matrixportal/matrixportal.py", line 97, in __init__
  File "adafruit_matrixportal/graphics.py", line 67, in __init__
  File "adafruit_matrixportal/matrix.py", line 192, in __init__
MemoryError:

Code done running.







My entire code is below:
Code: Select all | TOGGLE FULL SIZE
One panel display using 64 X 32 RGB panel to scroll ESPN game information"""
import gc
import time
import json
from adafruit_datetime import datetime, timedelta
import board

Memory_free = gc.mem_free()
print("Total memory free right before importing matrixportal library is", Memory_free)
from adafruit_matrixportal.matrixportal import MatrixPortal

Memory_free = gc.mem_free()
print("Total memory free right after importing matrixportal library is", Memory_free)
# import terminalio


USE_24HR_TIME = False
TIME_ZONE_OFFSET = -8 # hours ahead or behind Zulu time, e.g. Pacific is -8
TIME_ZONE_NAME = "PST"
# SCROLL_DELAY = 0.03

# Create a list of dictionaries for ESPN API's
# Note that College Baseball doesn't start until mid-February,
# so NHL Hockey (Sport 1)is displayed for now
SPORTS = [
{
"name": "College Baseball",
"url": "http://site.api.espn.com/apis/site/v2/sports/baseball/college-"
"baseball/scoreboard",
},
{
"name": "NHL Hockey",
"url": "http://site.api.espn.com/apis/site/v2/sports/hockey/nhl/scoreboard",
},
]
current_game = 0
current_sport = 1
sports_data = []


EVENTS_LOCATION = ["events"]
STATUS_LOCATION = ["status", "type", "description"]
BROADCAST_LOCATION = ["competitions", 0, "broadcasts"]
IS_FINAL_LOCATION = ["competitions", 0, "status", "type", "id"]
SCORES_LOCATION = ["competitions", 0, "competitors"]
SCORE_0_LOCATION = ["competitions", 0, "competitors", 0, "score"]
SCORE_1_LOCATION = ["competitions", 0, "competitors", 1, "score"]

months = [
"Jan",
"Feb",
"March",
"April",
"May",
"June",
"July",
"Aug",
"Sept",
"Oct",
"Nov",
"Dec",
]

Memory_free = gc.mem_free()
print("Total memory free right before matrixportal call is", Memory_free)

matrixportal = MatrixPortal(
url=SPORTS[current_sport]["url"],
status_neopixel=board.NEOPIXEL,
)


def format_date(iso_formatted_date):
if iso_formatted_date is None:
return "When: Unavailable"
date = datetime.fromisoformat(iso_formatted_date[:-1])
date += timedelta(hours=TIME_ZONE_OFFSET)

if USE_24HR_TIME:
timestring = "%d:%d %s" % (date.hour, date.minute, TIME_ZONE_NAME)
elif date.hour > 12:
timestring = "%d:%d pm %s" % (
abs((date.hour - 12) % 12),
date.minute,
TIME_ZONE_NAME,
)
else:
timestring = "%d:%d am %s" % (date.hour, date.minute, TIME_ZONE_NAME)
return "%s %d, %s" % (months[date.month - 1], date.day, timestring)


def format_score(scores, is_final):
home_score = scores[0]["score"]
away_score = scores[1]["score"]
if not home_score or not away_score:
return "Unavailable"
if int(is_final) == 3:
return "%s - %s" % (home_score, away_score)
return " "


def format_available(value):
if value is None:
return "Unavailable"
return value


def format_broadcast(value):
if not value:
value = "N/A"
else:
value = matrixportal.network.json_traverse(value, [0, "names", 0])
return "Airing on: " + value


def get_game_number():
return "Game %d of %d" % (current_game + 1, len(sports_data))


def update_labels():
# Set the labels for the current game data
matrixportal.set_text(SPORTS[current_sport]["name"], 0, False)
matrixportal.set_text(sports_data[current_game]["name"], 1, False)
matrixportal.set_text(sports_data[current_game]["date"], 2, False)
matrixportal.set_text(sports_data[current_game]["broadcast"], 3, False)
matrixportal.set_text(sports_data[current_game]["status"], 4, False)
matrixportal.set_text(get_game_number(), 5, False)
matrixportal.set_text(sports_data[current_game]["score"], 6)
# wait 2 seconds for display to complete
time.sleep(2)


def fetch_sports_data(reset_game_number=True):
# Fetches and parses data for all games for the current sport
# pylint: disable=global-statement
global sports_data, current_game, current_sport
matrixportal.url = SPORTS[current_sport]["url"]
sports_data.clear()
raw_data = json.loads(matrixportal.fetch(auto_refresh=False))
events = raw_data["events"]
for event in events:
game_data = {}
game_data["name"] = format_available(event["name"])
game_data["date"] = format_date(event["date"])
game_data["status"] = "Game status: " + format_available(
matrixportal.network.json_traverse(event, STATUS_LOCATION)
)
game_data["broadcast"] = format_broadcast(
matrixportal.network.json_traverse(event, BROADCAST_LOCATION)
)
scores = matrixportal.network.json_traverse(event, SCORES_LOCATION)
is_final = matrixportal.network.json_traverse(event, IS_FINAL_LOCATION)
game_data["score"] = format_score(scores, is_final)
sports_data.append(game_data)
if reset_game_number or current_game > len(sports_data):
current_game = 0
update_labels()


FONT = "tom-thumb.bdf"
# FONT = terminalio.FONT

# Sports Name
matrixportal.add_text(text_font=FONT, text_position=(5, 3), is_data=False)

# Game Name
matrixportal.add_text(
text_font=FONT,
text_wrap=35,
line_spacing=0.75,
text_position=(5, 16),
is_data=False,
)

# Date
matrixportal.add_text(text_font=FONT, text_position=(5, 10), is_data=False)

# Broadcast Information
matrixportal.add_text(text_font=FONT, text_position=(5, 20), is_data=False)

# Game Status
matrixportal.add_text(text_font=FONT, text_position=(5, 25), is_data=False)

# Game Number
matrixportal.add_text(text_font=FONT, text_position=(40, 10), is_data=False)

# Score
matrixportal.add_text(text_font=FONT, text_position=(32, 16), is_data=False)

last_update = time.monotronic()
UPDATE_DELAY = 7
fetch_sports_data()

while True:
current_sport = 1
if time.monotonic() > last_update + UPDATE_DELAY:
current_game += 1
last_update = time.monotonic()
if current_game >= len(sports_data):
fetch_sports_data(False)
current_game = 0
update_labels()
# time.sleep(7)



I have tried most of the suggestions on the Adafruit FAQ about memory errors, but nothing has worked so far. These have included changing the order of libraries, making sure that all libraries are .mpy files, etc.

The thing I am surprised about is how much memory that MatrixPortal library is using just to load. I have other similar programs importing MatrixPortal that run fine.

I would appreciate any input about my code that might help eliminate the memory errors.

Thanks!
Last edited by dastels on Sat Jan 22, 2022 1:38 pm, edited 1 time in total.
Reason: Fix code tag

Tortilla
 
Posts: 31
Joined: Sun Jan 09, 2022 5:03 am

Re: MatrixPortal Memory Error

by dastels on Sat Jan 22, 2022 1:41 pm

Hmm.. usually it outputs more information after "MemoryError".

You're using the latest version of the libraries.

That is the output when you go into the REPL (i.e. CTRL-C into it)?

Dave

dastels
 
Posts: 9752
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal Memory Error

by Tortilla on Sat Jan 22, 2022 2:06 pm

dastels wrote:Hmm.. usually it outputs more information after "MemoryError".

You're using the latest version of the libraries.

That is the output when you go into the REPL (i.e. CTRL-C into it)?

Dave


Hi,

Yes, the latest libraries from the adafruit-circuitpython-bundle-7.x-mpy-20220119 bundle.

The output is from the serial terminal. I thought it was unusual that error is occurring on the line that first creates the MatrixPortal object which is very early in the code file before any data fetching occurs or even the creation of the text labels.

I have another program that runs a news scroller using MatrixPortal and the difference in memory is only about 2700 before and after the MatrixPortal call.

Tortilla
 
Posts: 31
Joined: Sun Jan 09, 2022 5:03 am

Re: MatrixPortal Memory Error

by dastels on Sat Jan 22, 2022 2:16 pm

My bad.. typo. Should have been *What* is the output when you go into the REPL (i.e. CTRL-C into it)? I.e. the version and board info.

Dave

dastels
 
Posts: 9752
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal Memory Error

by Tortilla on Sat Jan 22, 2022 3:43 pm

dastels wrote:My bad.. typo. Should have been *What* is the output when you go into the REPL (i.e. CTRL-C into it)? I.e. the version and board info.

Dave


Adafruit CircuitPython 7.1.1 on 2022-01-14; Adafruit Matrix Portal M4 with samd51j19

Tortilla
 
Posts: 31
Joined: Sun Jan 09, 2022 5:03 am

Re: MatrixPortal Memory Error

by dastels on Sat Jan 22, 2022 11:57 pm

OK, I'll replicate it tomorrow and see what I can find out.

Dave

dastels
 
Posts: 9752
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal Memory Error

by Tortilla on Sun Jan 23, 2022 1:56 am

dastels wrote:OK, I'll replicate it tomorrow and see what I can find out.

Dave


Thanks Dave, I appreciate it.

Tortilla
 
Posts: 31
Joined: Sun Jan 09, 2022 5:03 am

Re: MatrixPortal Memory Error

by dastels on Sun Jan 23, 2022 1:38 pm

Ugh.. I just noticed that your code was without formatting! And somethign is stripped of the start.

Dave

dastels
 
Posts: 9752
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal Memory Error

by dastels on Sun Jan 23, 2022 1:59 pm

OK, just using up to the MatrixPortal call I get (adding output after the call):
Code: Select all | TOGGLE FULL SIZE
Total memory free right before importing matrixportal library is 108624
Total memory free right after importing matrixportal library is 34432
Total memory free right before matrixportal call is 33808
Total memory free right after matrixportal call is 24304


Dave

dastels
 
Posts: 9752
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal Memory Error

by dastels on Sun Jan 23, 2022 2:27 pm

BTW, same CP as you:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 7.1.1 on 2022-01-14; Adafruit Matrix Portal M4 with samd51j19


So.. hmmmm

Dave

dastels
 
Posts: 9752
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal Memory Error

by Tortilla on Sun Jan 23, 2022 6:05 pm

dastels wrote:BTW, same CP as you:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 7.1.1 on 2022-01-14; Adafruit Matrix Portal M4 with samd51j19


So.. hmmmm

Dave


Thanks Dave for doing this.

It appears that you were able to replicate my results with the same board. Sorry about the formatting thing.

Tortilla
 
Posts: 31
Joined: Sun Jan 09, 2022 5:03 am

Re: MatrixPortal Memory Error

by dastels on Sun Jan 23, 2022 6:12 pm

But I don't get a memory error. Which is odd.

There's no other imports above the "import gc"?

Dave

dastels
 
Posts: 9752
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal Memory Error

by Tortilla on Sun Jan 23, 2022 6:42 pm

dastels wrote:But I don't get a memory error. Which is odd.

There's no other imports above the "import gc"?

Dave


No, gc is the top of the code. Originally, I didn't use gc, and got the same memory error.

Tortilla
 
Posts: 31
Joined: Sun Jan 09, 2022 5:03 am

Re: MatrixPortal Memory Error

by dastels on Mon Jan 24, 2022 12:04 pm

Can you post your code again, making sure the formatting carries over? I want to try with exactly what you have on the board.

Dave

dastels
 
Posts: 9752
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal Memory Error

by danhalbert on Mon Jan 24, 2022 5:18 pm

Make sure you do not have two copies of the MatrixPortal library, one with .py files, and the other with .mpy. The .py will take precedence. This might happen if you have the .py version at the top level in CIRCUITPY, and the .mpy version in lib/

danhalbert
 
Posts: 3179
Joined: Tue Aug 08, 2017 12:37 pm

Please be positive and constructive with your questions and comments.