|
| 1 | +# Quote board matrix display |
| 2 | +# uses AdafruitIO to serve up a quote text feed and color feed |
| 3 | +# random quotes are displayed, updates periodically to look for new quotes |
| 4 | +# avoids repeating the same quote twice in a row |
| 5 | + |
| 6 | +import time |
| 7 | +import random |
| 8 | +import board |
| 9 | +import terminalio |
| 10 | +from adafruit_matrixportal.matrixportal import MatrixPortal |
| 11 | + |
| 12 | +# --- Display setup --- |
| 13 | +matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=True) |
| 14 | + |
| 15 | +# Create a new label with the color and text selected |
| 16 | +matrixportal.add_text( |
| 17 | + text_font=terminalio.FONT, |
| 18 | + text_position=(0, (matrixportal.graphics.display.height // 2) - 1), |
| 19 | + scrolling=True, |
| 20 | +) |
| 21 | + |
| 22 | +# Static 'Connecting' Text |
| 23 | +matrixportal.add_text( |
| 24 | + text_font=terminalio.FONT, |
| 25 | + text_position=(2, (matrixportal.graphics.display.height // 2) - 1), |
| 26 | +) |
| 27 | + |
| 28 | +QUOTES_FEED = "sign-quotes.signtext" |
| 29 | +COLORS_FEED = "sign-quotes.signcolor" |
| 30 | +SCROLL_DELAY = 0.02 |
| 31 | +UPDATE_DELAY = 600 |
| 32 | + |
| 33 | +quotes = [] |
| 34 | +colors = [] |
| 35 | +last_color = None |
| 36 | +last_quote = None |
| 37 | + |
| 38 | + |
| 39 | +def update_data(): |
| 40 | + print("Updating data from Adafruit IO") |
| 41 | + matrixportal.set_text("Connecting", 1) |
| 42 | + |
| 43 | + try: |
| 44 | + quotes_data = matrixportal.get_io_data(QUOTES_FEED) |
| 45 | + quotes.clear() |
| 46 | + for json_data in quotes_data: |
| 47 | + quotes.append(matrixportal.network.json_traverse(json_data, ["value"])) |
| 48 | + print(quotes) |
| 49 | + # pylint: disable=broad-except |
| 50 | + except Exception as error: |
| 51 | + print(error) |
| 52 | + |
| 53 | + try: |
| 54 | + color_data = matrixportal.get_io_data(COLORS_FEED) |
| 55 | + colors.clear() |
| 56 | + for json_data in color_data: |
| 57 | + colors.append(matrixportal.network.json_traverse(json_data, ["value"])) |
| 58 | + print(colors) |
| 59 | + # pylint: disable=broad-except |
| 60 | + except Exception as error: |
| 61 | + print(error) |
| 62 | + |
| 63 | + if not quotes or not colors: |
| 64 | + raise "Please add at least one quote and color to your feeds" |
| 65 | + matrixportal.set_text(" ", 1) |
| 66 | + |
| 67 | + |
| 68 | +update_data() |
| 69 | +last_update = time.monotonic() |
| 70 | +matrixportal.set_text(" ", 1) |
| 71 | +quote_index = None |
| 72 | +color_index = None |
| 73 | + |
| 74 | +while True: |
| 75 | + # Choose a random quote from quotes |
| 76 | + if len(quotes) > 1 and last_quote is not None: |
| 77 | + while quote_index == last_quote: |
| 78 | + quote_index = random.randrange(0, len(quotes)) |
| 79 | + else: |
| 80 | + quote_index = random.randrange(0, len(quotes)) |
| 81 | + last_quote = quote_index |
| 82 | + |
| 83 | + # Choose a random color from colors |
| 84 | + if len(colors) > 1 and last_color is not None: |
| 85 | + while color_index == last_color: |
| 86 | + color_index = random.randrange(0, len(colors)) |
| 87 | + else: |
| 88 | + color_index = random.randrange(0, len(colors)) |
| 89 | + last_color = color_index |
| 90 | + |
| 91 | + # Set the quote text |
| 92 | + matrixportal.set_text(quotes[quote_index]) |
| 93 | + |
| 94 | + # Set the text color |
| 95 | + matrixportal.set_text_color(colors[color_index]) |
| 96 | + |
| 97 | + # Scroll it |
| 98 | + matrixportal.scroll_text(SCROLL_DELAY) |
| 99 | + |
| 100 | + if time.monotonic() > last_update + UPDATE_DELAY: |
| 101 | + update_data() |
| 102 | + last_update = time.monotonic() |
0 commit comments