forked from mjoshd/hyperhdr-py
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstream_leds.py
More file actions
102 lines (90 loc) · 2.65 KB
/
Copy pathstream_leds.py
File metadata and controls
102 lines (90 loc) · 2.65 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
98
99
100
101
102
#!/usr/bin/env python
"""Stream LED colors or gradients over HyperHDR websocket."""
from __future__ import annotations
import argparse
import asyncio
import os
from hyperhdr import const
from hyperhdr.stream import HyperHDRLedColorsStream, HyperHDRLedGradientStream
async def run(args: argparse.Namespace) -> None:
"""Run the stream example."""
stream_cls = (
HyperHDRLedGradientStream
if args.mode == "gradient"
else HyperHDRLedColorsStream
)
stream = stream_cls(
args.host,
port=args.port,
token=args.token,
admin_password=args.password,
convert_to_jpeg=args.jpeg,
)
count = 0
try:
async for frame in stream.frames():
count += 1
if frame.raw:
print(
f"frame {count}: raw_bytes={len(frame.raw)} source={frame.source}"
)
elif frame.image:
print(
f"frame {count}: image_bytes={len(frame.image)} source={frame.source}"
)
else:
print(f"frame {count}: empty source={frame.source}")
if count >= args.frames:
break
finally:
await stream.stop()
def parse_args() -> argparse.Namespace:
"""Parse CLI arguments."""
parser = argparse.ArgumentParser(
description="Stream LED colors or gradients from HyperHDR."
)
parser.add_argument(
"--host",
default=os.environ.get("HYPERHDR_HOST", "hyperhdr.local"),
help="HyperHDR host (env: HYPERHDR_HOST)",
)
parser.add_argument(
"--port",
type=int,
default=const.DEFAULT_PORT_UI,
help=f"HyperHDR UI port (default: {const.DEFAULT_PORT_UI})",
)
parser.add_argument(
"--token",
default=os.environ.get("HYPERHDR_TOKEN"),
help="HyperHDR auth token (env: HYPERHDR_TOKEN)",
)
parser.add_argument(
"--password",
default=os.environ.get("HYPERHDR_ADMIN_PASSWORD"),
help="HyperHDR admin password for auth fallback (env: HYPERHDR_ADMIN_PASSWORD)",
)
parser.add_argument(
"--mode",
choices=["colors", "gradient"],
default="colors",
help="Stream mode",
)
parser.add_argument(
"--frames",
type=int,
default=10,
help="Number of frames to print",
)
parser.add_argument(
"--jpeg",
action="store_true",
help="Convert LED bytes to JPEG (requires Pillow)",
)
return parser.parse_args()
def main() -> None:
"""Entry point."""
args = parse_args()
asyncio.run(run(args))
if __name__ == "__main__":
main()