Ok, I hope this shows the formatting:dastels wrote: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
Code: Select all
"""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
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)
Thanks again for helping with this.