/ MagTag_AdafruitQuotes / code.py
code.py
1 # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 # 3 # SPDX-License-Identifier: Unlicense 4 import time 5 from adafruit_magtag.magtag import MagTag 6 7 # Set up where we'll be fetching data from 8 DATA_SOURCE = "https://www.adafruit.com/api/quotes.php" 9 QUOTE_LOCATION = [0, 'text'] 10 AUTHOR_LOCATION = [0, 'author'] 11 12 magtag = MagTag( 13 url=DATA_SOURCE, 14 json_path=(QUOTE_LOCATION, AUTHOR_LOCATION), 15 ) 16 magtag.network.connect() 17 18 # quote in bold text, with text wrapping 19 magtag.add_text( 20 text_font="Arial-Bold-12.bdf", 21 text_wrap=33, 22 text_maxlen=160, 23 text_position=(10, 15), 24 line_spacing=0.75, 25 ) 26 27 # author in italic text, no wrapping 28 magtag.add_text( 29 text_font="Arial-Italic-12.bdf", 30 text_position=(100, 110), 31 ) 32 33 timestamp = None 34 while True: 35 if not timestamp or (time.monotonic() - timestamp) > 60: # once every 60 seconds... 36 try: 37 value = magtag.fetch() 38 print("Response is", value) 39 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 40 print("Some error occured, retrying! -", e) 41 timestamp = time.monotonic()