-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi_epaper_agent_selftest.py
More file actions
executable file
·97 lines (74 loc) · 3.46 KB
/
Copy pathpi_epaper_agent_selftest.py
File metadata and controls
executable file
·97 lines (74 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Exercises pi_epaper_agent.py's non-hardware-specific logic against a real
running hub, without needing actual e-paper SPI hardware: registers a
device, fetches a real /render PNG for mono/gray4/bwr color modes, and
runs it through png_to_epd_buffers() with a FakeEPD standing in for the
Waveshare driver. Checks buffer sizes are sane and that the bwr split
actually produced both black and red pixels (not all-white / all-one-
color, which would mean the split logic is broken).
Doesn't and can't validate the real waveshare_epd import or epd.display()
against actual hardware — that part only gets proven by running
pi_epaper_agent.py itself on a Pi with the panel wired up.
Usage: python pi_epaper_agent_selftest.py --hub http://127.0.0.1:8000
"""
import argparse
import sys
import requests
from pi_epaper_agent import png_to_epd_buffers
class FakeEPD:
"""Stands in for a waveshare_epd.EPD instance: getbuffer() just
returns a bytes-like payload so we can inspect what would have been
sent to the panel."""
def getbuffer(self, img):
return img.tobytes()
def getbuffer_4Gray(self, img):
return img.tobytes()
PROFILES = [
("mono", "epaper_2.13_bw", 250, 122),
("gray4", "epaper_7.5_gray4", 800, 480),
("bwr", "epaper_4.2_bwr", 400, 300),
]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--hub", default="http://127.0.0.1:8000")
args = parser.parse_args()
epd = FakeEPD()
failures = []
for color_mode, profile, width, height in PROFILES:
device_id = f"selftest-{color_mode}"
hb = requests.post(f"{args.hub}/api/heartbeat", json={
"device_id": device_id, "profile": profile,
"capabilities": {"width": width, "height": height, "color_mode": color_mode},
}, timeout=10)
hb.raise_for_status()
channel = {"mono": "clock-full", "gray4": "stat-quad", "bwr": "alert-full"}[color_mode]
reg = requests.post(f"{args.hub}/api/devices/{device_id}/register", json={
"label": f"selftest {color_mode}", "dashboards": [channel],
}, timeout=10)
reg.raise_for_status()
render = requests.get(f"{args.hub}/render/{device_id}", timeout=15)
render.raise_for_status()
buffers = png_to_epd_buffers(render.content, color_mode, epd)
print(f"[{color_mode}] fetched {len(render.content)} bytes PNG -> {len(buffers)} buffer(s), "
f"sizes={[len(b) for b in buffers]}")
if color_mode == "bwr":
from PIL import Image
import io
img = Image.open(io.BytesIO(render.content)).convert("RGB")
px = img.load()
black_count = sum(1 for y in range(img.height) for x in range(img.width)
if px[x, y] == (0, 0, 0))
red_count = sum(1 for y in range(img.height) for x in range(img.width)
if px[x, y][0] > 150 and px[x, y][1] < 100 and px[x, y][2] < 100)
print(f" bwr source image: {black_count} black px, {red_count} red px")
if black_count == 0 or red_count == 0:
failures.append(f"bwr split saw {black_count} black / {red_count} red — expected both > 0 for alert-full")
requests.delete(f"{args.hub}/api/devices/{device_id}", timeout=10)
if failures:
print("\nFAILURES:")
for f in failures:
print(f" - {f}")
sys.exit(1)
print("\nAll checks passed.")
if __name__ == "__main__":
main()