Added option to allow user to choose 24-hour or 12-hour time
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2026-09-07 20:36:34 -04:00
parent ec63c2b2d2
commit 7a653c424b
2 changed files with 36 additions and 19 deletions
+2 -1
View File
@@ -2,5 +2,6 @@
WIFI_SSID = "YourSSID" # Set this to your Wi-Fi network name WIFI_SSID = "YourSSID" # Set this to your Wi-Fi network name
WIFI_PASSWORD = "YourPassword" # Set this to your Wi-Fi network password WIFI_PASSWORD = "YourPassword" # Set this to your Wi-Fi network password
# Timezone settings # Time settings
USE_24_HOUR = False # Set this to True for 24-hour time (no AM/PM)
UTC_OFFSET = -5 # Set this to your desired NTP server UTC_OFFSET = -5 # Set this to your desired NTP server
+34 -18
View File
@@ -76,6 +76,14 @@ def draw_scaled_text(display, text, x, y, scale=2):
display.fill_rect(x + (c * scale), y + (r * scale), scale, scale, 1) display.fill_rect(x + (c * scale), y + (r * scale), scale, scale, 1)
def calculate_x_spacing(text, scale=1):
"""
Calculates the amount of horizontal (x) spacing is needed to center a string.
"""
scaled_text = len(text) * scale
return (128 - (scaled_text * 8)) // 2
# --- Main Loop --- # --- Main Loop ---
DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
@@ -83,29 +91,37 @@ while True:
local_epoch = time.time() + (Config.UTC_OFFSET * 3600) local_epoch = time.time() + (Config.UTC_OFFSET * 3600)
year, month, day, hour_24, minute, second, weekday, _ = time.localtime(local_epoch) year, month, day, hour_24, minute, second, weekday, _ = time.localtime(local_epoch)
# 1. Determine AM/PM and 12-hour format
period = "AM" if hour_24 < 12 else "PM"
hour_12 = hour_24 % 12
if hour_12 == 0:
hour_12 = 12
# Format time without leading zero (or use {:2d} for alignment)
time_str = f"{hour_12:2d}:{minute:02d}" # 5 characters -> 80px at 2x
date_str = f"{DAYS[weekday]} {year}-{month:02d}-{day:02d}" # 14 chars -> 112px
oled.fill(0) oled.fill(0)
# 2. Draw 2x scaled time (starts at x=14) # Draw 24-hour time
time_x = 14 if Config.USE_24_HOUR:
time_y = 12 # Time string (HH:MM:SS is 8 chars -> 8 chars * 8px * 2x = 128px wide)
draw_scaled_text(oled, time_str, x=time_x, y=time_y, scale=2) time_str = f"{hour_24:02d}:{minute:02d}"
# 3. Draw AM/PM in standard 1x font (aligned with the bottom of the 16px digits) # Draw time at 2x scale (16px high, spans 128px across)
oled.text(period, time_x + (len(time_str) * 16) + 4, time_y + 8) time_x = calculate_x_spacing(time_str, 2)
draw_scaled_text(oled, time_str, x=time_x, y=14, scale=2)
# Draw 12-hour time (AM/PM)
else:
# 1. Determine AM/PM and 12-hour format
period = "AM" if hour_24 < 12 else "PM"
hour_12 = hour_24 % 12
if hour_12 == 0:
hour_12 = 12
# Format time without leading zero (or use {:2d} for alignment)
time_str = f"{hour_12}:{minute:02d}"
# 2. Draw 2x scaled time
time_x = calculate_x_spacing(time_str, 2)
draw_scaled_text(oled, time_str, x=time_x, y=12, scale=2)
# 3. Draw AM/PM in standard 1x font (aligned with the bottom of the 16px digits)
oled.text(period, time_x + (len(time_str) * 16) + 4, 12 + 8)
# 4. Draw date centered horizontally underneath # 4. Draw date centered horizontally underneath
date_x = (128 - (len(date_str) * 8)) // 2 date_str = f"{DAYS[weekday]} {year}-{month:02d}-{day:02d}"
oled.text(date_str, date_x, 42) oled.text(date_str, calculate_x_spacing(date_str), 42)
oled.show() oled.show()
time.sleep(0.5) time.sleep(0.5)