Week ahead in the weather strip, reverse LCD at night
The weekday strip now shows today and the next six days (icon, max, min), the current temperature and feels-like sit next to the clock, and the rain graph spans the full width with the hour under every bar. From sunset to sunrise the whole frame, seconds patch included, is drawn as a reverse LCD. The rain label is now spelled DÉŠŤ. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
45508b1b68
commit
6c9a1420d2
10 files changed
+341
-122
No files matched your search
+76
-56
@@ -1,18 +1,20 @@
|
||||
"""Casio-style weather clock: time, date, current weather and a 12 hour rain forecast."""
|
||||
from datetime import datetime
|
||||
"""Casio-style weather clock: week ahead, time, date, current weather and a 12 hour rain forecast."""
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
import lcdkit
|
||||
from lcdkit import BG, H, INK, W
|
||||
from lcdkit import BG, GHOST, H, INK, W
|
||||
from poller import Poller
|
||||
from view import Patch
|
||||
from weather_data import Icon, Weather, icon_for
|
||||
from weather_data import Day, Icon, Weather, icon_for
|
||||
|
||||
WEEKDAYS = ["PO", "ÚT", "ST", "ČT", "PÁ", "SO", "NE"]
|
||||
SECONDS_BOX = (370, 52, 466, 104) # region redrawn once per second
|
||||
SECONDS_GEOMETRY = dict(x=378, y=58, w=24, h=42, t=6, gap=6)
|
||||
GRAPH_X, GRAPH_PITCH, GRAPH_BOTTOM = 222, 21, 282
|
||||
CELL = (W - 24) // 7 # one day of the forecast strip
|
||||
SECONDS_BOX = (288, 104, 352, 148) # region redrawn once per second
|
||||
SECONDS_GEOMETRY = dict(x=292, y=108, w=20, h=36, t=5, gap=5)
|
||||
GRAPH_X, GRAPH_PITCH, GRAPH_BOTTOM, BAR_W = 22, 37, 288, 26
|
||||
RAIN_LABEL = "DÉŠŤ % / HOD"
|
||||
|
||||
|
||||
def _cloud(d: ImageDraw.ImageDraw, x: float, base: float, s: float, outline: bool = False) -> None:
|
||||
@@ -31,6 +33,19 @@ def _sun(d: ImageDraw.ImageDraw, cx: float, cy: float, r: float, s: float) -> No
|
||||
fill=INK, width=max(2, int(s / 16)))
|
||||
|
||||
|
||||
def is_night(now: datetime, weather: Weather | None) -> bool:
|
||||
"""True from sunset until sunrise; the frame is then drawn as a reverse LCD.
|
||||
|
||||
Uses today's sun times, or those of the closest day the forecast has (they move only minutes a day).
|
||||
Without any forecast it stays day.
|
||||
"""
|
||||
days = [day for day in (weather.days if weather else ()) if day.sunrise and day.sunset]
|
||||
if not days:
|
||||
return False
|
||||
day = min(days, key=lambda d: abs((d.date - now.date()).days))
|
||||
return not day.sunrise.time() <= now.time() < day.sunset.time()
|
||||
|
||||
|
||||
def draw_icon(d: ImageDraw.ImageDraw, icon: Icon | None, x: float, y: float, s: float) -> None:
|
||||
w = max(2, int(s / 16))
|
||||
if icon is Icon.SUN:
|
||||
@@ -82,83 +97,88 @@ class WeatherView:
|
||||
def frame_key(self, now: datetime):
|
||||
return (now.strftime("%Y%m%d%H%M"), self._poller.version, self._is_stale())
|
||||
|
||||
def patch(self, now: datetime) -> Patch:
|
||||
@staticmethod
|
||||
def _seconds(now: datetime) -> Image.Image:
|
||||
x0, y0, x1, y1 = SECONDS_BOX
|
||||
img = Image.new("RGB", (x1 - x0, y1 - y0), BG)
|
||||
g = SECONDS_GEOMETRY
|
||||
lcdkit.number(ImageDraw.Draw(img), f"{now.second:02d}", g["x"] - x0, g["y"] - y0, g["w"], g["h"],
|
||||
g["t"], g["gap"])
|
||||
return Patch(img, x0, y0)
|
||||
return img
|
||||
|
||||
def patch(self, now: datetime) -> Patch:
|
||||
img = self._seconds(now)
|
||||
if is_night(now, self._poller.value):
|
||||
img = lcdkit.night(img)
|
||||
return Patch(img, SECONDS_BOX[0], SECONDS_BOX[1])
|
||||
|
||||
def render(self, now: datetime) -> Image.Image:
|
||||
weather: Weather | None = self._poller.value
|
||||
img = Image.new("RGB", (W, H), BG)
|
||||
d = ImageDraw.Draw(img)
|
||||
f_small, f_tiny = lcdkit.load_font(15), lcdkit.load_font(12)
|
||||
f_small, f_tiny, f_mini = lcdkit.load_font(15), lcdkit.load_font(12), lcdkit.load_font(11)
|
||||
|
||||
d.rectangle((6, 6, W - 7, H - 7), outline=INK, width=2)
|
||||
self._weekday_strip(d, now, f_small)
|
||||
d.line((12, 46, W - 13, 46), fill=INK, width=2)
|
||||
self._week_strip(d, now, weather, f_small, f_tiny)
|
||||
d.line((12, 100, W - 13, 100), fill=INK, width=2)
|
||||
|
||||
# time row: HH:MM, seconds and date on the right
|
||||
lcdkit.number(d, now.strftime("%H:%M"), 24, 54, 54, 96, 12, 9)
|
||||
x0, y0, _, _ = SECONDS_BOX
|
||||
img.paste(self.patch(now).image, (x0, y0))
|
||||
lcdkit.number(d, now.strftime("%-d.%-m."), 372, 112, 14, 34, 4, 4, ghost=False)
|
||||
d.line((12, 160, W - 13, 160), fill=INK, width=2)
|
||||
# time row: HH:MM, seconds and date, then the weather now
|
||||
lcdkit.number(d, now.strftime("%H:%M"), 22, 108, 48, 82, 11, 8)
|
||||
img.paste(self._seconds(now), SECONDS_BOX[:2])
|
||||
lcdkit.number(d, now.strftime("%-d.%-m."), 292, 156, 13, 30, 4, 3, ghost=False)
|
||||
d.line((376, 108, 376, 192), fill=INK, width=2)
|
||||
self._current_block(d, weather, f_small, f_mini)
|
||||
d.line((12, 200, W - 13, 200), fill=INK, width=2)
|
||||
|
||||
self._current_block(d, weather, f_small, f_tiny)
|
||||
d.line((206, 168, 206, H - 14), fill=INK, width=2)
|
||||
self._rain_graph(d, weather, f_small, f_tiny)
|
||||
if self._is_stale():
|
||||
label_w = d.textlength("ZLÍN NYNÍ", font=f_tiny)
|
||||
lcdkit.tag(d, (int(22 + label_w + 8), 163), "STARÉ", f_tiny)
|
||||
return img
|
||||
lcdkit.tag(d, (int(20 + d.textlength(RAIN_LABEL, font=f_tiny) + 8), 203), "STARÉ", f_tiny)
|
||||
return lcdkit.night(img) if is_night(now, weather) else img
|
||||
|
||||
@staticmethod
|
||||
def _weekday_strip(d: ImageDraw.ImageDraw, now: datetime, font) -> None:
|
||||
cell = (W - 24) // 7
|
||||
for i, name in enumerate(WEEKDAYS):
|
||||
x0 = 12 + i * cell
|
||||
colour = INK
|
||||
if i == now.weekday():
|
||||
d.rectangle((x0, 14, x0 + cell - 6, 38), fill=INK)
|
||||
colour = BG
|
||||
text_w = d.textlength(name, font=font)
|
||||
d.text((x0 + (cell - 6 - text_w) / 2, 17), name, font=font, fill=colour)
|
||||
def _week_strip(d: ImageDraw.ImageDraw, now: datetime, weather: Weather | None, f_small, f_tiny) -> None:
|
||||
"""Today and the next six days: name, icon, max and min. Today is inverted."""
|
||||
by_date: dict = {day.date: day for day in (weather.days if weather else ())}
|
||||
for i in range(7):
|
||||
x0 = 12 + i * CELL
|
||||
cx = x0 + CELL / 2
|
||||
if i:
|
||||
d.line((x0, 16, x0, 92), fill=GHOST, width=1)
|
||||
else:
|
||||
d.rectangle((x0 + 4, 12, x0 + CELL - 4, 32), fill=INK)
|
||||
d.text((cx, 22), WEEKDAYS[(now.weekday() + i) % 7], font=f_small, fill=BG if i == 0 else INK,
|
||||
anchor="mm")
|
||||
day: Day | None = by_date.get(now.date() + timedelta(days=i))
|
||||
if day is not None:
|
||||
draw_icon(d, icon_for(day.code), cx - 15, 36, 30)
|
||||
high = f"{lcdkit.fmt_temp(day.high if day else None)}°"
|
||||
low = f" {lcdkit.fmt_temp(day.low if day else None)}°"
|
||||
x = cx - (d.textlength(high, font=f_small) + d.textlength(low, font=f_tiny)) / 2
|
||||
d.text((x, 90), high, font=f_small, fill=INK, anchor="ls")
|
||||
d.text((x + d.textlength(high, font=f_small), 90), low, font=f_tiny, fill=INK, anchor="ls")
|
||||
|
||||
@staticmethod
|
||||
def _current_block(d: ImageDraw.ImageDraw, weather: Weather | None, f_small, f_tiny) -> None:
|
||||
d.text((22, 166), "ZLÍN NYNÍ", font=f_tiny, fill=INK)
|
||||
def _current_block(d: ImageDraw.ImageDraw, weather: Weather | None, f_small, f_mini) -> None:
|
||||
temp = weather.temp if weather else None
|
||||
if weather:
|
||||
draw_icon(d, icon_for(weather.code), 20, 182, 56)
|
||||
text = lcdkit.fmt_temp(temp)
|
||||
w, gap = (26, 7) if len(text) <= 2 else (22, 5) # a third glyph (-10) must stay clear of the divider
|
||||
end = lcdkit.number(d, text, 90, 184, w, 46, 6, gap, ghost=False)
|
||||
w, gap = (18, 5) if len(text) <= 2 else (14, 4) # a third glyph (-10) must stay inside the frame
|
||||
end = lcdkit.number(d, text, 388, 108, w, 38, 5, gap, ghost=False)
|
||||
if temp is not None:
|
||||
d.ellipse((end + 4, 184, end + 14, 194), outline=INK, width=3)
|
||||
hi = lcdkit.fmt_temp(weather.today_max) if weather else "--"
|
||||
lo = lcdkit.fmt_temp(weather.today_min) if weather else "--"
|
||||
d.text((22, 244), f"MAX {hi}° MIN {lo}°", font=f_small, fill=INK)
|
||||
|
||||
d.text((22, 264), "DEŠŤ", font=f_tiny, fill=INK)
|
||||
chances = [h.rain_pct for h in (weather.hours if weather else ()) if h.rain_pct is not None]
|
||||
text = str(max(chances)) if chances else "--"
|
||||
x = lcdkit.number(d, text, 22, 279, 18, 28, 4, 6, ghost=False)
|
||||
if chances:
|
||||
d.text((x + 4, 288), "%", font=f_small, fill=INK)
|
||||
d.ellipse((end, 108, end + 10, 118), outline=INK, width=3)
|
||||
if weather:
|
||||
draw_icon(d, icon_for(weather.code), 380, 152, 40)
|
||||
d.text((424, 164), "POCIT", font=f_mini, fill=INK)
|
||||
d.text((424, 176), f"{lcdkit.fmt_temp(weather.feels if weather else None)}°", font=f_small, fill=INK)
|
||||
|
||||
@staticmethod
|
||||
def _rain_graph(d: ImageDraw.ImageDraw, weather: Weather | None, f_small, f_tiny) -> None:
|
||||
d.text((220, 166), "DEŠŤ % / HOD", font=f_tiny, fill=INK)
|
||||
d.text((20, 206), RAIN_LABEL, font=f_tiny, fill=INK)
|
||||
hours = weather.hours if weather else ()
|
||||
mm = f"{sum(h.mm or 0 for h in hours):.1f} mm" if hours else "-- mm"
|
||||
d.text((W - 18 - d.textlength(mm, font=f_small), 164), mm, font=f_small, fill=INK)
|
||||
d.text((W - 18 - d.textlength(mm, font=f_small), 204), mm, font=f_small, fill=INK)
|
||||
for i in range(12):
|
||||
bx = GRAPH_X + i * GRAPH_PITCH
|
||||
hour = hours[i] if i < len(hours) else None
|
||||
lcdkit.v_bar(d, bx, GRAPH_BOTTOM, hour.rain_pct if hour else None, 14, 16, 5, 1)
|
||||
if hour is not None and i % 3 == 0:
|
||||
label = f"{hour.time.hour:02d}"
|
||||
d.text((bx + 7 - d.textlength(label, font=f_tiny) / 2, 290), label, font=f_tiny, fill=INK)
|
||||
lcdkit.v_bar(d, bx, GRAPH_BOTTOM, hour.rain_pct if hour else None, BAR_W, 11, 4, 1)
|
||||
if hour is not None:
|
||||
d.text((bx + BAR_W / 2, 301), f"{hour.time.hour:02d}", font=f_tiny, fill=INK, anchor="ms")
|
||||
Reference in new issue
Block a user