|
1 | 1 | """ |
2 | 2 | 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. |
5 | 6 | """ |
6 | 7 |
|
7 | | -import time |
8 | 8 | import board |
9 | 9 | from adafruit_pyportal import PyPortal |
10 | 10 |
|
|
20 | 20 | WORD_LOCATION = ['word'] |
21 | 21 | PART_OF_SPEECH = ['definitions', 0, 'partOfSpeech'] |
22 | 22 | DEF_LOCATION = ['definitions', 0, 'text'] |
| 23 | +EXAMPLE_LOCATION = ['examples', 0, 'text'] |
23 | 24 | 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 |
24 | 28 |
|
25 | 29 | # determine the current working directory |
26 | 30 | # needed so we know where to find files |
|
29 | 33 | # Initialize the pyportal object and let us know what data to fetch and where |
30 | 34 | # to display it |
31 | 35 | pyportal = PyPortal(url=DATA_SOURCE, |
32 | | - json_path=(WORD_LOCATION, PART_OF_SPEECH, DEF_LOCATION), |
33 | 36 | status_neopixel=board.NEOPIXEL, |
34 | 37 | default_bg=cwd+"/wordoftheday_background.bmp", |
35 | 38 | text_font=cwd+"/fonts/Arial-ItalicMT-17.bdf", |
|
53 | 56 | pyportal.set_text("\nWord of the Day") # show title |
54 | 57 |
|
55 | 58 | 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