diff --git a/config.example.py b/config.example.py index 8aacab1..21fd9af 100644 --- a/config.example.py +++ b/config.example.py @@ -2,5 +2,6 @@ WIFI_SSID = "YourSSID" # Set this to your Wi-Fi network name 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 diff --git a/main.py b/main.py index 5de48ce..57f1a48 100644 --- a/main.py +++ b/main.py @@ -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) +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 --- DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] @@ -83,29 +91,37 @@ while True: local_epoch = time.time() + (Config.UTC_OFFSET * 3600) 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) - # 2. Draw 2x scaled time (starts at x=14) - time_x = 14 - time_y = 12 - draw_scaled_text(oled, time_str, x=time_x, y=time_y, scale=2) + # Draw 24-hour time + if Config.USE_24_HOUR: + # Time string (HH:MM:SS is 8 chars -> 8 chars * 8px * 2x = 128px wide) + time_str = f"{hour_24:02d}:{minute:02d}" - # 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, time_y + 8) + # Draw time at 2x scale (16px high, spans 128px across) + 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 - date_x = (128 - (len(date_str) * 8)) // 2 - oled.text(date_str, date_x, 42) + date_str = f"{DAYS[weekday]} {year}-{month:02d}-{day:02d}" + oled.text(date_str, calculate_x_spacing(date_str), 42) oled.show() time.sleep(0.5)