From 9545c01150b93fe646976ffcd06d5bcbc50f7cc3 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Mon, 7 Sep 2026 22:10:46 -0400 Subject: [PATCH] [Issue #4] - Added blinking colon to signify that the clock is not frozen; also moved to a more "event-driven" approach to refreshing the display to cut down on wasted display cycles --- main.py | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index e1aadd8..dfadba4 100644 --- a/main.py +++ b/main.py @@ -88,30 +88,38 @@ def calculate_x_spacing(text, scale=1): DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] TIME_Y = 12 # Keep vertical placement consistent +last_second = -1 + while True: + # 1. Compute time local_epoch = time.time() + (Config.UTC_OFFSET * 3600) - year, month, day, hour_24, minute, _, weekday, _ = time.localtime(local_epoch) + year, month, day, hour_24, minute, second, weekday, _ = time.localtime(local_epoch) - # 1. Format time string and period - if Config.USE_24_HOUR: - time_str = f"{hour_24:02d}:{minute:02d}" - period = None - else: - hour_12 = hour_24 % 12 or 12 # Replaces the 4-line if hour_12 == 0 block - time_str = f"{hour_12}:{minute:02d}" - period = "AM" if hour_24 < 12 else "PM" + # 2. Only redraw if the second has actually changed + if second != last_second: + last_second = second - # 2. Render time and AM/PM - oled.fill(0) - time_x = calculate_x_spacing(time_str, scale=2) - draw_scaled_text(oled, time_str, x=time_x, y=TIME_Y, scale=2) + sep = ":" if second % 2 == 0 else " " - if period: - oled.text(period, time_x + (len(time_str) * 16) + 4, TIME_Y + 8) + if Config.USE_24_HOUR: + time_str = f"{hour_24:02d}{sep}{minute:02d}" + period = None + else: + hour_12 = hour_24 % 12 or 12 + time_str = f"{hour_12}{sep}{minute:02d}" + period = "AM" if hour_24 < 12 else "PM" - # 3. Render date centered underneath - date_str = f"{DAYS[weekday]} {year}-{month:02d}-{day:02d}" - oled.text(date_str, calculate_x_spacing(date_str), 42) + oled.fill(0) + time_x = calculate_x_spacing(time_str, scale=2) + draw_scaled_text(oled, time_str, x=time_x, y=TIME_Y, scale=2) - oled.show() - time.sleep(0.5) + if period: + oled.text(period, time_x + (len(time_str) * 16) + 4, TIME_Y + 8) + + date_str = f"{DAYS[weekday]} {year}-{month:02d}-{day:02d}" + oled.text(date_str, calculate_x_spacing(date_str), 42) + + oled.show() + + # 3. Sleep in small increments to catch the exact second boundary with minimal latency + time.sleep_ms(20)