/ MagTag_Quote_Board / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # MagTag Quote Board 6 # Displays Quotes from the Adafruit quotes server 7 # Be sure to put WiFi access point info in secrets.py file to connect 8 9 import time 10 from adafruit_magtag.magtag import MagTag 11 12 # Set up where we'll be fetching data from 13 DATA_SOURCE = "https://www.adafruit.com/api/quotes.php" 14 QUOTE_LOCATION = [0, "text"] 15 AUTHOR_LOCATION = [0, "author"] 16 # in seconds, we can refresh about 100 times on a battery 17 TIME_BETWEEN_REFRESHES = 1 * 60 * 60 # one hour delay 18 19 magtag = MagTag( 20 url=DATA_SOURCE, 21 json_path=(QUOTE_LOCATION, AUTHOR_LOCATION), 22 ) 23 24 magtag.graphics.set_background("/bmps/magtag_quotes_bg.bmp") 25 26 # quote in bold text, with text wrapping 27 magtag.add_text( 28 text_font="/fonts/Arial-Bold-12.bdf", 29 text_wrap=28, 30 text_maxlen=120, 31 text_position=( 32 (magtag.graphics.display.width // 2), 33 (magtag.graphics.display.height // 2) - 10, 34 ), 35 line_spacing=0.75, 36 text_anchor_point=(0.5, 0.5), # center the text on x & y 37 ) 38 39 # author in italic text, no wrapping 40 magtag.add_text( 41 text_font="/fonts/Arial-Italic-12.bdf", 42 text_position=(magtag.graphics.display.width // 2, 118), 43 text_anchor_point=(0.5, 0.5), # center it in the nice scrolly thing 44 ) 45 46 # OK now we're ready to connect to the network, fetch data and update screen! 47 try: 48 magtag.network.connect() 49 value = magtag.fetch() 50 print("Response is", value) 51 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 52 magtag.set_text(e) 53 print("Some error occured, retrying later -", e) 54 # wait 2 seconds for display to complete 55 time.sleep(2) 56 magtag.exit_and_deep_sleep(TIME_BETWEEN_REFRESHES)