diff --git a/docs/examples.rst b/docs/examples.rst index 8805a84..c7b2206 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -10,6 +10,6 @@ Ensure your device works with this simple test. Other Demos ------------ -.. literalinclude:: ../examples/magtag_bitcoin_demo.py - :caption: examples/magtag_bitcoin_demo.py +.. literalinclude:: ../examples/magtag_quote_demo.py + :caption: examples/magtag_quote_demo.py :linenos: diff --git a/examples/magtag_bitcoin_demo.py b/examples/magtag_bitcoin_demo.py deleted file mode 100644 index 2bf7b3c..0000000 --- a/examples/magtag_bitcoin_demo.py +++ /dev/null @@ -1,37 +0,0 @@ -# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries -# -# SPDX-License-Identifier: Unlicense -from adafruit_magtag.magtag import MagTag - -# Set up where we'll be fetching data from -DATA_SOURCE = "https://api.coindesk.com/v1/bpi/currentprice.json" -DATA_LOCATION = ["bpi", "USD", "rate_float"] - - -def text_transform(val): - return "Bitcoin: $%d" % val - - -magtag = MagTag( - url=DATA_SOURCE, - json_path=DATA_LOCATION, -) - -magtag.network.connect() - -magtag.add_text( - text_position=( - (magtag.graphics.display.width // 2) - 1, - (magtag.graphics.display.height // 2) - 1, - ), - text_scale=3, - text_transform=text_transform, - text_anchor_point=(0.5, 0.5), -) - -try: - value = magtag.fetch() - print("Response is", value) -except (ValueError, RuntimeError) as e: - print("Some error occured, retrying! -", e) -magtag.exit_and_deep_sleep(60) diff --git a/examples/magtag_quote_demo.py b/examples/magtag_quote_demo.py new file mode 100644 index 0000000..5086b4c --- /dev/null +++ b/examples/magtag_quote_demo.py @@ -0,0 +1,64 @@ +# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries +# +# SPDX-License-Identifier: Unlicense +import os +import time +from adafruit_magtag.magtag import MagTag + +# Set up where we'll be fetching data from +DATA_SOURCE = "https://www.adafruit.com/api/quotes.php" +QUOTE_LOCATION = [0, "text"] +AUTHOR_LOCATION = [0, "author"] + +# the current working directory (where this file is) +try: + cwd = os.path.dirname(os.path.realpath(__file__)) +except AttributeError: + cwd = ("/" + __file__).rsplit("/", 1)[0] + +magtag = MagTag( + url=DATA_SOURCE, + json_path=(QUOTE_LOCATION, AUTHOR_LOCATION), + default_bg=0x000000, +) + + +def add_quote_marks(quote_text): + return f'"{quote_text}"' + + +def add_hyphen(author_name): + return f"- {author_name}" + + +magtag.add_text( + text_position=( + (magtag.graphics.display.width // 2) - 1, + (magtag.graphics.display.height // 2) - 20, + ), + text_color=0xFFFFFF, + text_wrap=44, + text_maxlen=180, + line_spacing=1.0, + text_transform=add_quote_marks, + text_anchor_point=(0.5, 0.5), +) + +magtag.add_text( + text_position=( + (magtag.graphics.display.width // 2) - 120, + (magtag.graphics.display.height // 2) + 34, + ), + text_color=0xFFFFFF, + text_wrap=0, + text_maxlen=30, + text_transform=add_hyphen, +) + +while True: + try: + value = magtag.fetch() + print("Response is", value) + except (ValueError, RuntimeError) as e: + print("Some error occured, retrying! -", e) + time.sleep(180)