sleep version
# Be sure to put WiFi access point info in secrets.py file to connect
import time
+import random
from adafruit_magtag.magtag import MagTag
# Set up where we'll be fetching data from
DATA_SOURCE = "https://www.reddit.com/r/showerthoughts/hot.json?limit=10"
-quote_num = 0
+quote_num = random.randint(0, 9) # we get 10 possibilities, pick one of them
QUOTE_LOCATION = ["data", "children", quote_num, "data", "title"]
AUTHOR_LOCATION = ["data", "children", quote_num, "data", "author"]
-AUTHOR_LOCATION = ["data", "children", 0, "data", "author"]
+
+# in seconds, we can refresh about 100 times on a battery
+TIME_BETWEEN_REFRESHES = 1 * 60 * 60 # one hour delay
magtag = MagTag(
url=DATA_SOURCE,
json_path=(QUOTE_LOCATION, AUTHOR_LOCATION),
- debug = False,
)
-magtag.graphics.set_background("/bmps/magtag_shower_bg.bmp")
-magtag.refresh()
-magtag.network.connect()
+magtag.graphics.set_background("/bmps/magtag_shower_bg.bmp")
# quote in bold text, with text wrapping
magtag.add_text(
@@ -38,22 +38,18 @@ magtag.add_text(
# author in italic text, no wrapping
magtag.add_text(
text_font="/fonts/Arial-Italic-12.bdf",
- text_position=(20, 118),
- text_anchor_point=(0.0, 0.5), # left justify this line
+ text_position=(magtag.graphics.display.width // 2, 118),
+ text_anchor_point=(0.5, 0.5), # left justify this line
)
-timestamp = None
-
-while True:
- if not timestamp or (time.monotonic() - timestamp) > 60: # new one each minute
try:
- quote_num = (quote_num + 1) % 10
- QUOTE_LOCATION = ["data", "children", quote_num, "data", "title"]
- AUTHOR_LOCATION = ["data", "children", quote_num, "data", "author"]
- magtag.json_path = (QUOTE_LOCATION, AUTHOR_LOCATION)
+ magtag.network.connect()
value = magtag.fetch()
print("Response is", value)
except (ValueError, RuntimeError) as e:
+ magtag.set_text(e)
print("Some error occured, retrying! -", e)
- timestamp = time.monotonic()
- print(timestamp)
+
+# wait 2 seconds for display to complete
+time.sleep(2)
+magtag.exit_and_deep_sleep(TIME_BETWEEN_REFRESHES)
