|
1 |
| -# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries |
2 |
| -# SPDX-FileCopyrightText: 2024 Tyeth Gundry for Adafruit Industries |
3 |
| -# |
4 |
| -# SPDX-License-Identifier: MIT |
5 |
| - |
6 | 1 | import os
|
7 | 2 | import time
|
8 | 3 | import wifi
|
|
24 | 19 | ## See TZ Identifier column at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
25 | 20 | ## If you want to set the timezone, you can do so with the following line:
|
26 | 21 | timezone = "GB"
|
27 |
| -#timezone = None # Or instead rely on automatic timezone detection based on IP Address |
| 22 | +# timezone = None # Or instead rely on automatic timezone detection based on IP Address |
28 | 23 |
|
29 | 24 |
|
30 | 25 | ## The time of the thing!
|
31 | 26 | EVENT_YEAR = 2024
|
32 | 27 | EVENT_MONTH = 8
|
33 |
| -EVENT_DAY = 16 |
34 |
| -EVENT_HOUR = 0 |
35 |
| -EVENT_MINUTE = 0 |
| 28 | +EVENT_DAY = 11 # 16 |
| 29 | +EVENT_HOUR = 10 # 0 |
| 30 | +EVENT_MINUTE = 28 # 0 |
36 | 31 | ## we'll make a python-friendly structure
|
37 |
| -event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY, |
38 |
| - EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds |
39 |
| - -1, -1, False)) # we dont know day of week/year or DST |
40 |
| - |
41 |
| -wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) |
| 32 | +event_time = time.struct_time( |
| 33 | + ( |
| 34 | + EVENT_YEAR, |
| 35 | + EVENT_MONTH, |
| 36 | + EVENT_DAY, |
| 37 | + EVENT_HOUR, |
| 38 | + EVENT_MINUTE, |
| 39 | + 0, # we don't track seconds |
| 40 | + -1, # we dont know day of week/year or DST |
| 41 | + -1, |
| 42 | + False, |
| 43 | + ) |
| 44 | +) |
| 45 | + |
| 46 | +print("Connecting to WiFi...") |
| 47 | +wifi.radio.connect( |
| 48 | + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 49 | +) |
42 | 50 |
|
43 | 51 | ## Initialize a requests session using the newer connection manager
|
44 | 52 | ## See https://adafruit-playground.com/u/justmobilize/pages/adafruit-connection-manager
|
|
47 | 55 | requests = adafruit_requests.Session(pool, ssl_context)
|
48 | 56 |
|
49 | 57 | ## Create an instance of the Adafruit IO HTTP client
|
50 |
| -io = IO_HTTP(os.getenv("ADAFRUIT_AIO_USERNAME"), os.getenv("ADAFRUIT_AIO_KEY"), requests) |
51 |
| - |
52 |
| -## Setup RGB LEDs - comment out the DotStar import and setup if using NeoPixel |
53 |
| -pixels_length = 1 # Set to the number of pixels in your strip (funhouse has 5) |
54 |
| -pixels_brightness = 0.4 # Set to a value between 0.0 and 1.0 |
55 |
| -# Uncomment the following lines if you are using DotStar and update pins if necessary |
56 |
| -dotstar_clock_pin = board.DOTSTAR_CLOCK |
57 |
| -dotstar_data_pin = board.DOTSTAR_DATA |
58 |
| -pixels = DotStar(dotstar_clock_pin, dotstar_data_pin, pixels_length, brightness=pixels_brightness) |
59 |
| -## Uncomment the following lines if you are using NeoPixel and update pin if necessary |
60 |
| -# neopixel_pin = board.NEOPIXEL |
61 |
| -# pixels = neopixel.NeoPixel(neopixel_pin, pixels_length, brightness=pixels_brightness) |
62 |
| - |
63 |
| -pixels.fill((0, 0, 0)) # Turn off all pixels |
64 |
| - |
65 |
| -# Setup built-in display |
66 |
| -display = board.DISPLAY |
| 58 | +io = IO_HTTP( |
| 59 | + os.getenv("ADAFRUIT_AIO_USERNAME"), os.getenv("ADAFRUIT_AIO_KEY"), requests |
| 60 | +) |
| 61 | + |
| 62 | +## Setup display and size appropriate assets |
| 63 | +if board.board_id == "adafruit_qualia_s3_rgb666": |
| 64 | + # Display Initialisation for 3.2" Bar display (320x820) |
| 65 | + from qualia_bar_display_320x820 import setup_display |
| 66 | + display = setup_display() |
| 67 | + display.rotation = 90 # Rotate the display |
| 68 | + BITMAP_FILE = "/circuitpython_day_2024_820x260_16bit.bmp" |
| 69 | + FONT_FILE = "/font_free_mono_bold_48.pcf" |
| 70 | + FONT_Y_OFFSET = 30 |
| 71 | + blinka_bitmap = displayio.OnDiskBitmap(BITMAP_FILE) |
| 72 | + PIXEL_SHADER = displayio.ColorConverter( |
| 73 | + input_colorspace=displayio.Colorspace.RGB565 |
| 74 | + ) |
| 75 | +else: |
| 76 | + # Setup built-in display |
| 77 | + display = board.DISPLAY |
| 78 | + BITMAP_FILE = "/cpday_tft.bmp" |
| 79 | + FONT_FILE = "/Helvetica-Bold-16.pcf" |
| 80 | + FONT_Y_OFFSET = 13 |
| 81 | + PIXEL_SHADER = displayio.ColorConverter() |
| 82 | + blinka_bitmap = displayio.OnDiskBitmap(BITMAP_FILE) |
| 83 | + PIXEL_SHADER = blinka_bitmap.pixel_shader |
67 | 84 | group = displayio.Group()
|
68 |
| -font = bitmap_font.load_font("/Helvetica-Bold-16.pcf") |
69 |
| -blinka_bitmap = displayio.OnDiskBitmap("/cpday_tft.bmp") |
| 85 | +font = bitmap_font.load_font(FONT_FILE) |
| 86 | +# blinka_bitmap = displayio.OnDiskBitmap(BITMAP_FILE) |
70 | 87 | blinka_grid = displayio.TileGrid(blinka_bitmap, pixel_shader=blinka_bitmap.pixel_shader)
|
71 |
| -scrolling_label = bitmap_label.Label(font, text=" ", y=display.height - 13) |
| 88 | +# blinka_grid.y = -100 |
| 89 | +scrolling_label = bitmap_label.Label(font, text=" ", y=display.height - FONT_Y_OFFSET) |
72 | 90 |
|
73 | 91 | group.append(blinka_grid)
|
74 | 92 | group.append(scrolling_label)
|
|
94 | 112 | print(now)
|
95 | 113 | total_seconds = time.mktime(now)
|
96 | 114 | refresh_clock = ticks_add(refresh_clock, refresh_timer)
|
97 |
| - except Exception as e: # pylint: disable=broad-except |
98 |
| - print("Some error occured, retrying via reset in 5seconds! -", e) |
99 |
| - time.sleep(5) |
| 115 | + except Exception as e: # pylint: disable=broad-except |
| 116 | + print("Some error occured, retrying via reset in 15seconds! -", e) |
| 117 | + time.sleep(15) |
100 | 118 | microcontroller.reset()
|
101 | 119 |
|
102 | 120 | if ticks_diff(ticks_ms(), clock_clock) >= clock_timer:
|
|
113 | 131 | days_remaining = -remaining
|
114 | 132 | finished = True
|
115 | 133 | if not first_run and days_remaining == 0:
|
116 |
| - scrolling_label.text = "It's CircuitPython Day 2024! The snakiest day of the year!" |
117 |
| - # Flash on/off blinka colours (nice purple) each second |
118 |
| - if pixels[0] == (0, 0, 0): |
119 |
| - pixels.fill((0x40, 0x00, 0x80)) |
120 |
| - else: |
121 |
| - pixels.fill((0, 0, 0)) |
| 134 | + scrolling_label.text = ( |
| 135 | + "It's CircuitPython Day 2024! The snakiest day of the year!" |
| 136 | + ) |
122 | 137 |
|
123 | 138 | # Check for the moment of the event to trigger something (a NASA snake launch)
|
124 | 139 | if not triggered and (
|
125 |
| - hours_remaining==0 and mins_remaining == 0 and secs_remaining <= 0 |
| 140 | + hours_remaining == 0 |
| 141 | + and mins_remaining == 0 |
| 142 | + and secs_remaining <= 1 |
| 143 | + # Change at/after xx:yy:01 seconds so we've already updated the display |
126 | 144 | ):
|
127 | 145 | # send a signal to an adafruit IO feed, where an Action is listening
|
128 |
| - print("Launch the snakes!") |
| 146 | + print("Launch the snakes! (sending message to Adafruit IO)") |
129 | 147 | triggered = True
|
130 | 148 | io.send_data("cpday-countdown", "Launch the snakes!")
|
131 |
| - else: |
132 |
| - pixels.fill((0, 0, 0)) # Turn off all pixels |
| 149 | + |
133 | 150 | else:
|
| 151 | + # calculate time until event |
134 | 152 | secs_remaining = remaining % 60
|
135 | 153 | remaining //= 60
|
136 | 154 | mins_remaining = remaining % 60
|
137 | 155 | remaining //= 60
|
138 | 156 | hours_remaining = remaining % 24
|
139 | 157 | remaining //= 24
|
140 | 158 | days_remaining = remaining
|
141 |
| - pixels.fill((0, 0, 0)) # Turn off all pixels |
142 | 159 | if not finished or (finished and days_remaining < 0):
|
143 |
| - scrolling_label.text = (f"{days_remaining} DAYS, {hours_remaining} HOURS," + |
144 |
| - f"{mins_remaining} MINUTES & {secs_remaining} SECONDS") |
| 160 | + # Remove 1 from days_remaining to count from end of day instead of start |
| 161 | + if days_remaining < 0: |
| 162 | + print( |
| 163 | + f"Event time in past: Adding 1 to days_remaining ({days_remaining}) to count from end of day" |
| 164 | + ) |
| 165 | + days_remaining += 1 |
| 166 | + # Update the display with current countdown value |
| 167 | + scrolling_label.text = ( |
| 168 | + f"{days_remaining} DAYS, {hours_remaining} HOURS," |
| 169 | + + f"{mins_remaining} MINUTES & {secs_remaining} SECONDS" |
| 170 | + ) |
| 171 | + |
145 | 172 | total_seconds += 1
|
146 | 173 | clock_clock = ticks_add(clock_clock, clock_timer)
|
147 | 174 | if ticks_diff(ticks_ms(), scroll_clock) >= scroll_timer:
|
|
150 | 177 | scrolling_label.x = display.width + 2
|
151 | 178 | display.refresh()
|
152 | 179 | scroll_clock = ticks_add(scroll_clock, scroll_timer)
|
153 |
| - |
| 180 | + |
154 | 181 | first_run = False
|
0 commit comments