The dashboard (Raspberry Pi 3.5" screen + web pages), the end-to-end encrypted sync mailbox (PHP + SQLite) and the Android companion app (Flutter), with docs, clean-install checks and screenshots made from demo data. Built mostly with an AI coding assistant; not yet reviewed line by line by a human (see README and SECURITY.md). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
7.2 KiB
Python
179 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Framebuffer dashboard for a 3.5" ILI9486 SPI panel (/dev/fb0, 480x320, RGB565).
|
||
|
||
Screens, shown as icon buttons along the bottom and switched with number keys (1 = Home,
|
||
then the plugins' screens — Temp, Weather, Calendar … —, the last = Settings; screens.py).
|
||
Left/Right arrows act *within* a screen (the plugin's key(): the next sensor, the next
|
||
calendar page …; Settings: Celsius / Fahrenheit). q quits.
|
||
|
||
Run: python3 dash.py (first run / after a flip: python3 dash.py --calibrate)
|
||
|
||
Layout (split for readability — all sibling modules in this folder):
|
||
config.py paths, THEME, load/save config
|
||
store.py Store + TempSource + hit/dispatch_tap (tap-region registry)
|
||
panel.py framebuffer, fonts, text/draw helpers, nav bar, graph
|
||
touch.py evdev touch device, calibration, raw->screen mapping
|
||
backlight.py auto brightness from sunrise/sunset (render-side dim)
|
||
screens.py Home + Settings, the screen list, render()
|
||
pluginhost.py the plugins (plugins/<name>/) — weather, calendar, the measurement
|
||
stations are plugins too (their workers, screens, Home parts)
|
||
web.py the web pages (Flask, templates/ + static/, webview.py) — the big-screen layout
|
||
dash.py this file: main loop, key + touch event dispatch
|
||
"""
|
||
|
||
import json
|
||
import os
|
||
import select
|
||
import signal
|
||
import sys
|
||
import termios
|
||
import threading
|
||
import time
|
||
import tty
|
||
|
||
import backlight
|
||
import pluginhost
|
||
from config import CALIB_FILE, load_config
|
||
from panel import Panel
|
||
from screens import render, screen_list
|
||
from store import Store, dispatch_tap, goto_screen
|
||
from touch import (HAVE_EVDEV, calibrate, ecodes, find_touch_device,
|
||
raw_to_screen)
|
||
|
||
IDLE_TIMEOUT_S = 10 * 60 # auto-return to Home after this much idle; see main loop
|
||
|
||
|
||
def read_key():
|
||
ch = sys.stdin.read(1)
|
||
if ch == "\x1b": # escape seq (arrow keys)
|
||
r, _, _ = select.select([sys.stdin], [], [], 0.01)
|
||
if r:
|
||
ch += sys.stdin.read(2)
|
||
return ch
|
||
|
||
|
||
def handle_key(ch, store):
|
||
store.last_activity_t = time.monotonic()
|
||
screens = screen_list(store)
|
||
if ch.isdigit() and 1 <= int(ch) <= len(screens):
|
||
goto_screen(store, int(ch) - 1, screens)
|
||
elif ch in ("\x1b[C", "\x1b[D"): # right / left: the screen's own
|
||
sc = screens[min(store.screen, len(screens) - 1)]
|
||
if sc.get("units"):
|
||
store.toggle_units()
|
||
elif sc.get("key"):
|
||
sc["key"]("right" if ch == "\x1b[C" else "left")
|
||
elif ch in ("q", "Q"):
|
||
return False
|
||
return True
|
||
|
||
|
||
def main():
|
||
import system
|
||
system.apply_pending() # Settings ▸ General ▸ Restore: before anything opens a database
|
||
cfg = load_config()
|
||
from panel import find_screen
|
||
screen = None if "--headless" in sys.argv else find_screen(cfg)
|
||
headless = screen is None # no small screen (another computer): workers + web pages only
|
||
panel = None if headless else Panel(screen)
|
||
if panel:
|
||
print(f"screen: {screen} ({panel.w}×{panel.h}, {panel.bpp} bpp)", flush=True)
|
||
store = Store(cfg)
|
||
store.headless = headless
|
||
pluginhost.load(store) # every plugin (weather, calendar, stations …): their workers start here
|
||
if (cfg.get("web") or {}).get("enabled"): # the web pages (web.py)
|
||
from web import web_worker
|
||
threading.Thread(target=web_worker, args=(store,), daemon=True).start()
|
||
if headless:
|
||
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0)) # systemd stop → the finally below
|
||
print("headless: no small screen (Settings ▸ General ▸ Screen) — web pages and workers only", flush=True)
|
||
try:
|
||
while True:
|
||
time.sleep(3600)
|
||
except (KeyboardInterrupt, SystemExit):
|
||
pass
|
||
finally:
|
||
pluginhost.shutdown()
|
||
return
|
||
|
||
# optional touch (tap-to-select); keyboard always works regardless
|
||
touch, calib = None, None
|
||
if HAVE_EVDEV:
|
||
try:
|
||
touch = find_touch_device()
|
||
except Exception as e:
|
||
print("touch disabled:", e)
|
||
else:
|
||
print("touch disabled: python3-evdev not installed (keyboard still works).")
|
||
if touch is not None:
|
||
if "--calibrate" in sys.argv or not os.path.exists(CALIB_FILE):
|
||
calib = calibrate(panel, touch)
|
||
else:
|
||
try:
|
||
with open(CALIB_FILE) as f:
|
||
calib = json.load(f)
|
||
except Exception:
|
||
calib = calibrate(panel, touch)
|
||
|
||
kbd = sys.stdin.isatty() # no terminal (e.g. systemd) -> display + touch only
|
||
fd = sys.stdin.fileno() if kbd else None
|
||
old = termios.tcgetattr(fd) if kbd else None
|
||
if not kbd:
|
||
print("No TTY — keyboard disabled; running display + touch only.")
|
||
|
||
next_draw = 0.0
|
||
pressed, tcx, tcy, psx, psy = False, None, None, 0.0, 0.0
|
||
try:
|
||
if kbd:
|
||
tty.setcbreak(fd)
|
||
while True:
|
||
watch = ([sys.stdin] if kbd else []) + ([touch.fd] if touch else [])
|
||
r, _, _ = select.select(watch, [], [], 0.5)
|
||
if kbd and sys.stdin in r and not handle_key(read_key(), store):
|
||
break
|
||
if touch and touch.fd in r:
|
||
for ev in touch.read():
|
||
if ev.type == ecodes.EV_ABS:
|
||
if ev.code == ecodes.ABS_X:
|
||
tcx = ev.value
|
||
elif ev.code == ecodes.ABS_Y:
|
||
tcy = ev.value
|
||
elif ev.type == ecodes.EV_KEY and ev.code == ecodes.BTN_TOUCH:
|
||
if ev.value == 1 and tcx is not None:
|
||
pressed = True
|
||
psx, psy = raw_to_screen(calib, tcx, tcy)
|
||
elif ev.value == 0 and pressed:
|
||
pressed = False
|
||
rx, ry = raw_to_screen(calib, tcx, tcy)
|
||
if abs(rx - psx) < 25 and abs(ry - psy) < 25: # tap, not a drag
|
||
dispatch_tap(store, rx, ry)
|
||
store.last_activity_t = time.monotonic()
|
||
next_draw = 0.0
|
||
now = time.monotonic()
|
||
|
||
# Idle return-to-Home: after N minutes with no touch/key, jump back
|
||
# to Home unless the screen asks to stay (Home itself, the Temp grid —
|
||
# left open as a passive display: the screen's stay()).
|
||
if now - store.last_activity_t > IDLE_TIMEOUT_S:
|
||
screens = screen_list(store)
|
||
sc = screens[min(store.screen, len(screens) - 1)]
|
||
if not (sc.get("stay") and sc["stay"]()):
|
||
goto_screen(store, 0, screens)
|
||
next_draw = 0.0
|
||
|
||
if r or now >= next_draw:
|
||
store.brightness = backlight.compute(store)
|
||
panel.blit(render(panel, store), store.brightness)
|
||
next_draw = now + 1.0
|
||
except KeyboardInterrupt:
|
||
pass
|
||
finally:
|
||
pluginhost.shutdown() # the plugins save what's in memory (the 24 h history …)
|
||
if kbd:
|
||
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
||
panel.clear()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|