Skip to content

Commit a5a03e7

Browse files
committed
channel monitor test out example
1 parent afbf09c commit a5a03e7

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#! /usr/bin/python3
2+
##--------------------------------------------------------------------\
3+
# hackrfpy 'examples/channel_monitor.py'
4+
# Watch POWER over time on several frequencies at once, backed by one
5+
# continuous hackrf_sweep (monitor_frequencies). Demonstrates the
6+
# library's third acquisition style, distinct from the other two:
7+
# - capture()/open_receiver() -> IQ samples at ONE frequency
8+
# - scan_frequencies() -> IQ samples at several (re-open gaps)
9+
# - monitor_frequencies() this -> POWER ONLY at several, gap-free via
10+
# the sweep's fast hardware retuning
11+
#
12+
# Prints a live per-channel bar meter; no plotting extra needed.
13+
# Read-only / receive-only.
14+
#
15+
# Usage:
16+
# uv run python examples/channel_monitor.py
17+
# uv run python examples/channel_monitor.py --freq 433.92M --freq 915M \
18+
# --duration 30
19+
#
20+
#
21+
# Author(s): Lauren Linkous
22+
##--------------------------------------------------------------------\
23+
import argparse
24+
import sys
25+
26+
from hackrfpy import HackRF, parse_freq
27+
28+
BAR_FLOOR, BAR_CEIL, BAR_WIDTH = -80.0, -20.0, 40
29+
30+
31+
def bar(db):
32+
if db is None:
33+
return "?" * 3
34+
frac = (min(max(db, BAR_FLOOR), BAR_CEIL) - BAR_FLOOR) / (BAR_CEIL - BAR_FLOOR)
35+
n = int(frac * BAR_WIDTH)
36+
return "#" * n + "." * (BAR_WIDTH - n)
37+
38+
39+
def main():
40+
p = argparse.ArgumentParser(
41+
description="Multi-channel power monitor via one sweep (read-only).")
42+
p.add_argument("--freq", action="append",
43+
help="frequency to watch (repeatable; parse_freq notation "
44+
"like 433.92M). Default: 98M, 433.92M, 915M")
45+
p.add_argument("--span", default="2M",
46+
help="sweep margin around min/max watched freq (default 2M)")
47+
p.add_argument("--duration", type=float, default=None,
48+
help="seconds to run (default: until Ctrl-C)")
49+
p.add_argument("--lna", type=int, default=16)
50+
p.add_argument("--vga", type=int, default=20)
51+
p.add_argument("--tools-dir", default=None)
52+
args = p.parse_args()
53+
freqs = [parse_freq(f) for f in (args.freq or ["98M", "433.92M", "915M"])]
54+
55+
h = HackRF(tools_dir=args.tools_dir)
56+
labels = {f: f"{f/1e6:9.3f} MHz" for f in freqs}
57+
print("[*] monitoring "
58+
+ ", ".join(l.strip() for l in labels.values())
59+
+ " (Ctrl-C to stop)")
60+
61+
def on_update(update):
62+
lines = [f" {labels[f]} {bar(db)} "
63+
f"{'--' if db is None else f'{db:6.1f} dB'}"
64+
for f, db in update.items()]
65+
print("\n".join(lines) + "\n")
66+
67+
try:
68+
h.monitor_frequencies(freqs, span_hz=parse_freq(args.span),
69+
duration=args.duration, on_update=on_update,
70+
lna=args.lna, vga=args.vga)
71+
except KeyboardInterrupt:
72+
pass
73+
print("[*] stopped")
74+
return 0
75+
76+
77+
if __name__ == "__main__":
78+
sys.exit(main())

0 commit comments

Comments
 (0)