165 lines
7.4 KiB
Python
165 lines
7.4 KiB
Python
"""Casio-style weather clock: time, date, current weather and a 12 hour rain forecast."""
|
|
from datetime import datetime
|
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
import lcdkit
|
|
from lcdkit import BG, H, INK, W
|
|
from poller import Poller
|
|
from view import Patch
|
|
from weather_data import 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
|
|
|
|
|
|
def _cloud(d: ImageDraw.ImageDraw, x: float, base: float, s: float, outline: bool = False) -> None:
|
|
"""A cloud of width ~0.8*s sitting on the line y=base; `outline` first paints a background halo."""
|
|
for pad, colour in ((3, BG), (0, INK)) if outline else ((0, INK),):
|
|
d.ellipse((x + s * 0.30 - pad, base - s * 0.34 - pad, x + s * 0.68 + pad, base + pad), fill=colour)
|
|
d.ellipse((x + s * 0.52 - pad, base - s * 0.26 - pad, x + s * 0.95 + pad, base + pad), fill=colour)
|
|
d.rounded_rectangle((x + s * 0.18 - pad, base - s * 0.20 - pad, x + s * 0.95 + pad, base + pad),
|
|
radius=int(s * 0.1), fill=colour)
|
|
|
|
|
|
def _sun(d: ImageDraw.ImageDraw, cx: float, cy: float, r: float, s: float) -> None:
|
|
d.ellipse((cx - r, cy - r, cx + r, cy + r), fill=INK)
|
|
for dx, dy in ((0, -1), (0, 1), (-1, 0), (1, 0), (-.7, -.7), (.7, -.7), (-.7, .7), (.7, .7)):
|
|
d.line((cx + dx * r * 1.35, cy + dy * r * 1.35, cx + dx * r * 1.8, cy + dy * r * 1.8),
|
|
fill=INK, width=max(2, int(s / 16)))
|
|
|
|
|
|
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:
|
|
_sun(d, x + s / 2, y + s / 2, s * 0.22, s)
|
|
elif icon is Icon.PARTLY:
|
|
_sun(d, x + s * 0.36, y + s * 0.36, s * 0.2, s)
|
|
_cloud(d, x, y + s * 0.82, s, outline=True)
|
|
elif icon is Icon.CLOUDY:
|
|
_cloud(d, x, y + s * 0.75, s)
|
|
elif icon is Icon.RAIN:
|
|
_cloud(d, x, y + s * 0.6, s)
|
|
for i in range(3):
|
|
dx = x + s * (0.3 + 0.2 * i)
|
|
d.line((dx + 4, y + s * 0.68, dx - 2, y + s * 0.92), fill=INK, width=w)
|
|
elif icon is Icon.SNOW:
|
|
_cloud(d, x, y + s * 0.6, s)
|
|
for i in range(3):
|
|
dx, dy = x + s * (0.3 + 0.2 * i), y + s * (0.8 if i % 2 == 0 else 0.9)
|
|
d.rectangle((dx - 2, dy - 2, dx + 2, dy + 2), fill=INK)
|
|
elif icon is Icon.STORM:
|
|
_cloud(d, x, y + s * 0.6, s)
|
|
bolt = [(x + s * 0.52, y + s * 0.62), (x + s * 0.40, y + s * 0.80), (x + s * 0.50, y + s * 0.80),
|
|
(x + s * 0.44, y + s * 0.98), (x + s * 0.62, y + s * 0.74), (x + s * 0.52, y + s * 0.74)]
|
|
d.polygon(bolt, fill=INK)
|
|
elif icon is Icon.FOG:
|
|
for i in range(4):
|
|
off = s * 0.08 if i % 2 else 0
|
|
yy = y + s * (0.25 + 0.17 * i)
|
|
d.line((x + s * 0.12 + off, yy, x + s * 0.88 + off, yy), fill=INK, width=w + 1)
|
|
|
|
|
|
class WeatherView:
|
|
name = "weather"
|
|
STALE_AFTER = 3600.0 # seconds without a successful fetch before the data is marked old
|
|
|
|
def __init__(self, poller: Poller[Weather]) -> None:
|
|
self._poller = poller
|
|
|
|
def start(self) -> None:
|
|
self._poller.start()
|
|
|
|
def stop(self) -> None:
|
|
self._poller.stop()
|
|
|
|
def _is_stale(self) -> bool:
|
|
age = self._poller.age()
|
|
return age is not None and age > self.STALE_AFTER
|
|
|
|
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:
|
|
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)
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
# 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)
|
|
|
|
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
|
|
|
|
@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)
|
|
|
|
@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)
|
|
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)
|
|
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)
|
|
|
|
@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)
|
|
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)
|
|
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)
|