Increasing font size of the time display; changing time to 12-hour time
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import framebuf
|
||||
import network
|
||||
import ntptime
|
||||
import time
|
||||
@@ -51,21 +52,60 @@ except Exception as e:
|
||||
# Disconnect Wi-Fi to conserve power once time is synchronized
|
||||
wlan.active(False)
|
||||
|
||||
|
||||
def draw_scaled_text(display, text, x, y, scale=2):
|
||||
"""
|
||||
Renders text scaled up by an integer factor (scale=2 -> 16x16, scale=3 -> 24x24).
|
||||
Uses a temporary 1-bit FrameBuffer to capture the default 8x8 font.
|
||||
"""
|
||||
width = len(text) * 8
|
||||
height = 8
|
||||
|
||||
# 1-bit MONO_HLSB buffer: (width * height) / 8 bytes
|
||||
buf = bytearray((width * height) // 8)
|
||||
fb = framebuf.FrameBuffer(buf, width, height, framebuf.MONO_HLSB)
|
||||
|
||||
# Render string with built-in font into the scratch buffer
|
||||
fb.fill(0)
|
||||
fb.text(text, 0, 0, 1)
|
||||
|
||||
# Read each pixel and draw a (scale x scale) block on the OLED
|
||||
for r in range(height):
|
||||
for c in range(width):
|
||||
if fb.pixel(c, r):
|
||||
display.fill_rect(x + (c * scale), y + (r * scale), scale, scale, 1)
|
||||
|
||||
|
||||
# --- Main Loop ---
|
||||
DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
|
||||
|
||||
while True:
|
||||
# Apply UTC offset to local epoch
|
||||
local_epoch = time.time() + (Config.UTC_OFFSET * 3600)
|
||||
year, month, day, hour, minute, second, weekday, _ = time.localtime(local_epoch)
|
||||
year, month, day, hour_24, minute, second, weekday, _ = time.localtime(local_epoch)
|
||||
|
||||
time_str = f"{hour:02d}:{minute:02d}:{second:02d}"
|
||||
date_str = f"{DAYS[weekday]} {year}-{month:02d}-{day:02d}"
|
||||
# 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)
|
||||
# Center-align roughly for 8x8 font
|
||||
oled.text(time_str, 32, 20)
|
||||
oled.text(date_str, 12, 38)
|
||||
oled.show()
|
||||
|
||||
# 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)
|
||||
|
||||
# 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)
|
||||
|
||||
# 4. Draw date centered horizontally underneath
|
||||
date_x = (128 - (len(date_str) * 8)) // 2
|
||||
oled.text(date_str, date_x, 42)
|
||||
|
||||
oled.show()
|
||||
time.sleep(0.5)
|
||||
|
||||
Reference in New Issue
Block a user