|
| 1 | +# SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import time |
| 6 | +import displayio |
| 7 | +import terminalio |
| 8 | +from adafruit_display_text.label import Label |
| 9 | +from adafruit_bitmap_font import bitmap_font |
| 10 | +from adafruit_matrixportal.matrix import Matrix |
| 11 | + |
| 12 | +# set the timer length |
| 13 | +TIMER_LENGTH = 180 # 3 minutes |
| 14 | + |
| 15 | +BLINK = True |
| 16 | +DEBUG = False |
| 17 | + |
| 18 | +# --- Display setup --- |
| 19 | +matrix = Matrix() |
| 20 | +display = matrix.display |
| 21 | + |
| 22 | +# --- Drawing setup --- |
| 23 | +group = displayio.Group() # Create a Group |
| 24 | +bitmap = displayio.Bitmap(64, 32, 2) # Create a bitmap object,width, height, bit depth |
| 25 | +color = displayio.Palette(4) # Create a color palette |
| 26 | +color[0] = 0x000000 # black background |
| 27 | +color[1] = 0xFF0000 # red |
| 28 | +color[2] = 0xFF8C00 # yellow |
| 29 | +color[3] = 0x3DEB34 # green |
| 30 | + |
| 31 | +# Create a TileGrid using the Bitmap and Palette |
| 32 | +tile_grid = displayio.TileGrid(bitmap, pixel_shader=color) |
| 33 | +group.append(tile_grid) # Add the TileGrid to the Group |
| 34 | +display.show(group) |
| 35 | + |
| 36 | +if not DEBUG: |
| 37 | + font = bitmap_font.load_font("/IBMPlexMono-Medium-24_jep.bdf") |
| 38 | +else: |
| 39 | + font = terminalio.FONT |
| 40 | + |
| 41 | +clock_label = Label(font) |
| 42 | + |
| 43 | +def update_time(remaining_time): |
| 44 | + now = time.localtime() # Get the time values we need |
| 45 | + |
| 46 | + # calculate remaining time in seconds |
| 47 | + seconds = remaining_time % 60 |
| 48 | + minutes = remaining_time // 60 |
| 49 | + |
| 50 | + if BLINK: |
| 51 | + colon = ":" if now[5] % 2 else " " |
| 52 | + else: |
| 53 | + colon = ":" |
| 54 | + |
| 55 | + clock_label.text = "{minutes:02d}{colon}{seconds:02d}".format( |
| 56 | + minutes=minutes, seconds=seconds, colon=colon |
| 57 | + ) |
| 58 | + |
| 59 | + if remaining_time < 60: |
| 60 | + clock_label.color = color[1] |
| 61 | + elif remaining_time < 90: |
| 62 | + clock_label.color = color[2] |
| 63 | + elif remaining_time > 90: |
| 64 | + clock_label.color = color[3] |
| 65 | + |
| 66 | + bbx, bby, bbwidth, bbh = clock_label.bounding_box |
| 67 | + # Center the label |
| 68 | + clock_label.x = round(display.width / 2 - bbwidth / 2) |
| 69 | + clock_label.y = display.height // 2 |
| 70 | + if DEBUG: |
| 71 | + print("Label bounding box: {},{},{},{}".format(bbx, bby, bbwidth, bbh)) |
| 72 | + print("Label x: {} y: {}".format(clock_label.x, clock_label.y)) |
| 73 | + |
| 74 | + # decrement remaining time |
| 75 | + remaining_time -= 1 |
| 76 | + if remaining_time < 0: |
| 77 | + remaining_time = TIMER_LENGTH |
| 78 | + |
| 79 | + return remaining_time |
| 80 | + |
| 81 | +def main(): |
| 82 | + remaining_time = TIMER_LENGTH |
| 83 | + update_time(remaining_time) |
| 84 | + group.append(clock_label) |
| 85 | + |
| 86 | + while True: |
| 87 | + remaining_time = update_time(remaining_time) |
| 88 | + time.sleep(1) |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments