Skip to content

Commit 2c59231

Browse files
committed
add example feature
1 parent 9106f04 commit 2c59231

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

PyPortal_Word_of_the_Day/code.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
This example uses the Wordnik API to display Wordnik's Word of the Day.
3-
Each day a new word, it's part of speech, and definition
4-
will appear automatically on the display.
3+
Each day a new word, its part of speech, and definition
4+
will appear automatically on the display. Tap the screen to start
5+
as well as to switch between the word's definition and an example sentence.
56
"""
67

7-
import time
88
import board
99
from adafruit_pyportal import PyPortal
1010

@@ -20,7 +20,11 @@
2020
WORD_LOCATION = ['word']
2121
PART_OF_SPEECH = ['definitions', 0, 'partOfSpeech']
2222
DEF_LOCATION = ['definitions', 0, 'text']
23+
EXAMPLE_LOCATION = ['examples', 0, 'text']
2324
CAPTION = 'wordnik.com/word-of-the-day'
25+
DEFINITION_EXAMPLE_ARR = [DEF_LOCATION, EXAMPLE_LOCATION]
26+
#defintion and example array variable initialized at 0
27+
definition_example = 0
2428

2529
# determine the current working directory
2630
# needed so we know where to find files
@@ -29,7 +33,6 @@
2933
# Initialize the pyportal object and let us know what data to fetch and where
3034
# to display it
3135
pyportal = PyPortal(url=DATA_SOURCE,
32-
json_path=(WORD_LOCATION, PART_OF_SPEECH, DEF_LOCATION),
3336
status_neopixel=board.NEOPIXEL,
3437
default_bg=cwd+"/wordoftheday_background.bmp",
3538
text_font=cwd+"/fonts/Arial-ItalicMT-17.bdf",
@@ -53,9 +56,18 @@
5356
pyportal.set_text("\nWord of the Day") # show title
5457

5558
while True:
56-
try:
57-
value = pyportal.fetch()
58-
print("Response is", value)
59-
except RuntimeError as e:
60-
print("Some error occured, retrying! -", e)
61-
time.sleep(10*60) # Update every 10 minutes
59+
if pyportal.touchscreen.touch_point:
60+
try:
61+
#set the JSON path here to be able to change between definition and example
62+
pyportal._json_path=(WORD_LOCATION,
63+
PART_OF_SPEECH,
64+
DEFINITION_EXAMPLE_ARR[definition_example])
65+
value = pyportal.fetch()
66+
print("Response is", value)
67+
except RuntimeError as e:
68+
print("Some error occured, retrying! -", e)
69+
#Change between definition and example
70+
if definition_example == 0:
71+
definition_example = 1
72+
elif definition_example == 1:
73+
definition_example = 0

0 commit comments

Comments
 (0)