Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active January 26, 2022 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anecdata/c59d67a3aea4b3d45bc1326a43938837 to your computer and use it in GitHub Desktop.
Save anecdata/c59d67a3aea4b3d45bc1326a43938837 to your computer and use it in GitHub Desktop.
CircuitPython Espressif Monitor mode ambient frame count per channel
import time
import wifi
QUEUELEN = 128 # ESP32-S2 (ESP32-S3 can be much longer)
NS_PER_CH = 5_000_000_000 # nanoseconds per channel
def monitor_ambient(channel, duration):
report = {}
monitor = wifi.Monitor()
monitor.deinit()
monitor = wifi.Monitor(channel=channel, queue=QUEUELEN)
# maxq is the longest the core queue gets during the channel sampling period
report["maxq"] = 0
# total count of frames lost because the core queue was full
report["lost"] = 0
report["frames"] = 0
report["time"] = 0
start_ns = time.monotonic_ns()
while True:
# queue stats
if monitor.queued() > report["maxq"]:
report["maxq"] = monitor.queued()
report["lost"] += monitor.lost()
# frame stats
received = monitor.packet()
if received != {}:
report["frames"] += 1
elapsed_ns = time.monotonic_ns() - start_ns
if elapsed_ns > duration:
report["time"] = elapsed_ns
report["fps"] = 1_000_000_000 * (report["frames"] / report["time"])
break
return report
while True:
print("-"*49)
print("Count ambient frames...")
print(f' Ch frames/s queued lost')
for channel in range(1, 14):
print(f' {channel:2d}...', end="\r")
report = monitor_ambient(channel, NS_PER_CH)
print(f' {channel:2d}', end="")
print(f'{report["fps"]:9.1f}', end="")
print(f'{report["maxq"]:7d}', end="")
print(f'{report["lost"]:5d}')
@anecdata
Copy link
Author

Sample output:

-------------------------------------------------
Count ambient frames...
 Ch frames/s queued lost
  1     77.6      2    0
  2      4.6      1    0
  3      2.2      1    0
  4      1.0      1    0
  5      0.6      1    0
  6      9.2      1    0
  7      1.0      1    0
  8      1.2      1    0
  9      0.8      1    0
 10      5.6      1    0
 11    155.6     18    0
 12      3.8      1    0
 13      0.0      0    0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment