|
| 1 | +# Scoreboard matrix display |
| 2 | +# uses AdafruitIO to set scores and team names for a scoreboard |
| 3 | +# Perfect for cornhole, ping pong, and other games |
| 4 | + |
| 5 | +import time |
| 6 | +import board |
| 7 | +import terminalio |
| 8 | +from adafruit_matrixportal.matrixportal import MatrixPortal |
| 9 | + |
| 10 | +# --- Display setup --- |
| 11 | +matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=False) |
| 12 | + |
| 13 | +RED_COLOR = 0xAA0000 |
| 14 | +BLUE_COLOR = 0x0000AA |
| 15 | + |
| 16 | +# Red Score |
| 17 | +matrixportal.add_text( |
| 18 | + text_font=terminalio.FONT, |
| 19 | + text_position=(4, int(matrixportal.graphics.display.height * 0.75) - 3), |
| 20 | + text_color=RED_COLOR, |
| 21 | + text_scale=2, |
| 22 | +) |
| 23 | + |
| 24 | +# Blue Score |
| 25 | +matrixportal.add_text( |
| 26 | + text_font=terminalio.FONT, |
| 27 | + text_position=(36, int(matrixportal.graphics.display.height * 0.75) - 3), |
| 28 | + text_color=BLUE_COLOR, |
| 29 | + text_scale=2, |
| 30 | +) |
| 31 | + |
| 32 | +# Red Team name |
| 33 | +matrixportal.add_text( |
| 34 | + text_font=terminalio.FONT, |
| 35 | + text_position=(4, int(matrixportal.graphics.display.height * 0.25) - 4), |
| 36 | + text_color=RED_COLOR, |
| 37 | +) |
| 38 | + |
| 39 | +# Blue Team name |
| 40 | +matrixportal.add_text( |
| 41 | + text_font=terminalio.FONT, |
| 42 | + text_position=(36, int(matrixportal.graphics.display.height * 0.25) - 4), |
| 43 | + text_color=BLUE_COLOR, |
| 44 | +) |
| 45 | + |
| 46 | +# Static 'Connecting' Text |
| 47 | +matrixportal.add_text( |
| 48 | + text_font=terminalio.FONT, |
| 49 | + text_position=(59, 0), |
| 50 | +) |
| 51 | + |
| 52 | +SCORES_RED_FEED = "scores-group.red-team-score-feed" |
| 53 | +SCORES_BLUE_FEED = "scores-group.blue-team-score-feed" |
| 54 | +TEAM_RED_FEED = "scores-group.red-team-name" |
| 55 | +TEAM_BLUE_FEED = "scores-group.blue-team-name" |
| 56 | +UPDATE_DELAY = 4 |
| 57 | + |
| 58 | +matrixportal.set_text_color(RED_COLOR, 0) |
| 59 | +matrixportal.set_text_color(BLUE_COLOR, 1) |
| 60 | + |
| 61 | + |
| 62 | +def show_connecting(show): |
| 63 | + if show: |
| 64 | + matrixportal.set_text(".", 4) |
| 65 | + else: |
| 66 | + matrixportal.set_text(" ", 4) |
| 67 | + |
| 68 | + |
| 69 | +def get_last_data(feed_key): |
| 70 | + feed = matrixportal.get_io_feed(feed_key, detailed=True) |
| 71 | + value = feed["details"]["data"]["last"] |
| 72 | + if value is not None: |
| 73 | + return value["value"] |
| 74 | + return None |
| 75 | + |
| 76 | + |
| 77 | +def customize_team_names(): |
| 78 | + team_red = "Red" |
| 79 | + team_blue = "Blue" |
| 80 | + |
| 81 | + show_connecting(True) |
| 82 | + team_name = get_last_data(TEAM_RED_FEED) |
| 83 | + if team_name is not None: |
| 84 | + print("Team {} is now Team {}".format(team_red, team_name)) |
| 85 | + team_red = team_name |
| 86 | + matrixportal.set_text(team_red, 2) |
| 87 | + team_name = get_last_data(TEAM_BLUE_FEED) |
| 88 | + if team_name is not None: |
| 89 | + print("Team {} is now Team {}".format(team_blue, team_name)) |
| 90 | + team_blue = team_name |
| 91 | + matrixportal.set_text(team_blue, 3) |
| 92 | + show_connecting(False) |
| 93 | + |
| 94 | + |
| 95 | +def update_scores(): |
| 96 | + print("Updating data from Adafruit IO") |
| 97 | + show_connecting(True) |
| 98 | + |
| 99 | + score_red = get_last_data(SCORES_RED_FEED) |
| 100 | + if score_red is None: |
| 101 | + score_red = 0 |
| 102 | + matrixportal.set_text(score_red, 0) |
| 103 | + |
| 104 | + score_blue = get_last_data(SCORES_BLUE_FEED) |
| 105 | + if score_blue is None: |
| 106 | + score_blue = 0 |
| 107 | + matrixportal.set_text(score_blue, 1) |
| 108 | + show_connecting(False) |
| 109 | + |
| 110 | + |
| 111 | +customize_team_names() |
| 112 | +update_scores() |
| 113 | +last_update = time.monotonic() |
| 114 | + |
| 115 | +while True: |
| 116 | + # Set the red score text |
| 117 | + if time.monotonic() > last_update + UPDATE_DELAY: |
| 118 | + update_scores() |
| 119 | + last_update = time.monotonic() |
0 commit comments