|
| 1 | +# SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import os |
| 5 | +import ssl |
| 6 | +import time |
| 7 | +import wifi |
| 8 | +import board |
| 9 | +import displayio |
| 10 | +import terminalio |
| 11 | +import socketpool |
| 12 | +import adafruit_requests |
| 13 | +from adafruit_display_text import bitmap_label |
| 14 | + |
| 15 | +# Initialize Wi-Fi connection |
| 16 | +try: |
| 17 | + wifi.radio.connect( |
| 18 | + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 19 | + ) |
| 20 | + print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID")) |
| 21 | +except Exception as e: # pylint: disable=broad-except |
| 22 | + print( |
| 23 | + "Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds." |
| 24 | + ) |
| 25 | + |
| 26 | +pool = socketpool.SocketPool(wifi.radio) |
| 27 | +requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 28 | + |
| 29 | +# Set up the URL for fetching time data |
| 30 | +DATA_SOURCE = "http://worldtimeapi.org/api/timezone/" + os.getenv("TIMEZONE") |
| 31 | + |
| 32 | +# Set up display a default image |
| 33 | +display = board.DISPLAY |
| 34 | +default_bitmap = displayio.OnDiskBitmap("/images/blanka-chan.bmp") |
| 35 | +default_tile_grid = displayio.TileGrid(default_bitmap, pixel_shader=default_bitmap.pixel_shader) |
| 36 | + |
| 37 | +group = displayio.Group() |
| 38 | +group.append(default_tile_grid) |
| 39 | + |
| 40 | +# Create label for displaying time |
| 41 | +time_label = bitmap_label.Label(terminalio.FONT, scale=5) |
| 42 | +time_label.anchor_point = (0.2, 0.5) |
| 43 | +time_label.anchored_position = (display.width // 2, display.height // 2) |
| 44 | + |
| 45 | +# Create main group to hold all display groups |
| 46 | +main_group = displayio.Group() |
| 47 | +main_group.append(group) |
| 48 | +main_group.append(time_label) |
| 49 | +# Show the main group on the display |
| 50 | +display.show(main_group) |
| 51 | + |
| 52 | +current_background_image = "/images/blanka-chan.bmp" |
| 53 | + |
| 54 | +def set_background_image(filename): |
| 55 | + global current_background_image # pylint: disable=global-statement |
| 56 | + tile_bitmap = displayio.OnDiskBitmap(filename) |
| 57 | + new_tile_grid = displayio.TileGrid(tile_bitmap, pixel_shader=tile_bitmap.pixel_shader) |
| 58 | + group[0] = new_tile_grid |
| 59 | + current_background_image = filename |
| 60 | + |
| 61 | +def parse_time(datetime_str): |
| 62 | + # Extract the time part from the datetime string |
| 63 | + time_str = datetime_str.split("T")[1].split(".")[0] |
| 64 | + hour, minute, _ = map(int, time_str.split(":")) |
| 65 | + |
| 66 | + # Convert 24-hour format to 12-hour format and determine AM/PM |
| 67 | + period = "AM" |
| 68 | + if hour >= 12: |
| 69 | + period = "PM" |
| 70 | + if hour > 12: |
| 71 | + hour -= 12 |
| 72 | + elif hour == 0: |
| 73 | + hour = 12 |
| 74 | + |
| 75 | + return hour, minute, period |
| 76 | + |
| 77 | +while True: |
| 78 | + # Fetch time data from WorldTimeAPI |
| 79 | + response = requests.get(DATA_SOURCE) |
| 80 | + data = response.json() |
| 81 | + |
| 82 | + # Parse the time from the datetime string |
| 83 | + current_hour, current_minute, current_period = parse_time(data["datetime"]) |
| 84 | + |
| 85 | + # Display the time |
| 86 | + time_label.text = " {:2}{}\n :{:02}".format(current_hour, current_period, current_minute) |
| 87 | + |
| 88 | + # Switch between two images |
| 89 | + if current_background_image == "/images/blanka-chan.bmp": |
| 90 | + set_background_image("/images/blanka-chan-charged.bmp") |
| 91 | + else: |
| 92 | + set_background_image("/images/blanka-chan.bmp") |
| 93 | + |
| 94 | + time.sleep(5) |
0 commit comments