Add ESP32 LED benchmark test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
05c4bb87f9
commit
d4ffb1561d
1 file changed
+135
@@ -0,0 +1,135 @@
|
|||||||
|
from machine import Pin
|
||||||
|
from neopixel import NeoPixel
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
BENCHMARK_FILE = "/benchmark.json"
|
||||||
|
PIXEL_FILE = "/pixels.bin"
|
||||||
|
|
||||||
|
NUM_STRIPS = 8
|
||||||
|
LEDS_PER_STRIP = 291
|
||||||
|
BYTES_PER_PIXEL = 3
|
||||||
|
PIXEL_BYTES = NUM_STRIPS * LEDS_PER_STRIP * BYTES_PER_PIXEL
|
||||||
|
|
||||||
|
|
||||||
|
def load_pixels(strips):
|
||||||
|
"""Load pixel buffer from flash into strips. Returns load time ms or None if no file."""
|
||||||
|
try:
|
||||||
|
start = time.ticks_ms()
|
||||||
|
with open(PIXEL_FILE, "rb") as f:
|
||||||
|
buf = f.read()
|
||||||
|
if len(buf) != PIXEL_BYTES:
|
||||||
|
return None
|
||||||
|
ofs = 0
|
||||||
|
for strip in strips:
|
||||||
|
for i in range(LEDS_PER_STRIP):
|
||||||
|
strip[i] = (buf[ofs], buf[ofs + 1], buf[ofs + 2])
|
||||||
|
ofs += BYTES_PER_PIXEL
|
||||||
|
return time.ticks_diff(time.ticks_ms(), start)
|
||||||
|
except OSError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def save_pixels(strips):
|
||||||
|
"""Write current strip pixel data to flash."""
|
||||||
|
buf = bytearray(PIXEL_BYTES)
|
||||||
|
ofs = 0
|
||||||
|
for strip in strips:
|
||||||
|
for i in range(LEDS_PER_STRIP):
|
||||||
|
r, g, b = strip[i]
|
||||||
|
buf[ofs] = r
|
||||||
|
buf[ofs + 1] = g
|
||||||
|
buf[ofs + 2] = b
|
||||||
|
ofs += BYTES_PER_PIXEL
|
||||||
|
with open(PIXEL_FILE, "wb") as f:
|
||||||
|
f.write(buf)
|
||||||
|
print(f"Saved {PIXEL_BYTES} bytes to {PIXEL_FILE}")
|
||||||
|
|
||||||
|
|
||||||
|
def flash_stats(path="/"):
|
||||||
|
"""Return (total_bytes, free_bytes, used_bytes) for the filesystem on flash, or None."""
|
||||||
|
try:
|
||||||
|
s = os.statvfs(path)
|
||||||
|
# f_frsize = fragment size, f_blocks = total blocks, f_bfree = free blocks
|
||||||
|
total = s[1] * s[2] # frsize * blocks
|
||||||
|
free = s[1] * s[3] # frsize * bfree
|
||||||
|
used = total - free
|
||||||
|
return (total, free, used)
|
||||||
|
except OSError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def print_flash():
|
||||||
|
st = flash_stats()
|
||||||
|
if st is None:
|
||||||
|
print("Flash: (statvfs not available)")
|
||||||
|
return
|
||||||
|
total, free, used = st
|
||||||
|
total_k = total // 1024
|
||||||
|
free_k = free // 1024
|
||||||
|
used_k = used // 1024
|
||||||
|
print(f"Flash: {used_k} KB used, {free_k} KB free, {total_k} KB total")
|
||||||
|
|
||||||
|
|
||||||
|
def load_benchmark():
|
||||||
|
try:
|
||||||
|
with open(BENCHMARK_FILE) as f:
|
||||||
|
return json.load(f)
|
||||||
|
except (OSError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def save_benchmark(fill_ms, show_ms):
|
||||||
|
try:
|
||||||
|
with open(BENCHMARK_FILE, "w") as f:
|
||||||
|
json.dump({"fill_ms": fill_ms, "show_ms": show_ms}, f)
|
||||||
|
print(f"Saved to {BENCHMARK_FILE}")
|
||||||
|
except OSError as e:
|
||||||
|
print(f"Save failed: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
# set cpu frequency to 240MHz
|
||||||
|
import machine
|
||||||
|
machine.freq(240000000)
|
||||||
|
|
||||||
|
strips = [NeoPixel(Pin(i), 291) for i in range(8)]
|
||||||
|
|
||||||
|
print_flash()
|
||||||
|
|
||||||
|
# load and print previous run
|
||||||
|
prev = load_benchmark()
|
||||||
|
if prev:
|
||||||
|
print(f"Previous: fill {prev['fill_ms']} ms, show {prev['show_ms']} ms")
|
||||||
|
else:
|
||||||
|
print("No previous benchmark stored")
|
||||||
|
|
||||||
|
# optional: time load from flash + show (if we have stored pixels)
|
||||||
|
load_ms = load_pixels(strips)
|
||||||
|
if load_ms is not None:
|
||||||
|
print(f"Load pixels from flash: {load_ms} ms")
|
||||||
|
start = time.ticks_ms()
|
||||||
|
for strip in strips:
|
||||||
|
strip.write()
|
||||||
|
show_ms_loaded = time.ticks_diff(time.ticks_ms(), start)
|
||||||
|
print(f"Show after load: {show_ms_loaded} ms")
|
||||||
|
|
||||||
|
print("Filling strips")
|
||||||
|
start = time.ticks_ms()
|
||||||
|
for i in range(1):
|
||||||
|
for strip in strips:
|
||||||
|
strip.fill((255, 0, 0))
|
||||||
|
end = time.ticks_ms()
|
||||||
|
fill_ms = time.ticks_diff(end, start)
|
||||||
|
print(f"fill time: {fill_ms} ms")
|
||||||
|
|
||||||
|
print("Showing strips")
|
||||||
|
start = time.ticks_ms()
|
||||||
|
for strip in strips:
|
||||||
|
strip.write()
|
||||||
|
end = time.ticks_ms()
|
||||||
|
show_ms = time.ticks_diff(end, start)
|
||||||
|
print(f"show time: {show_ms} ms")
|
||||||
|
|
||||||
|
save_pixels(strips)
|
||||||
|
save_benchmark(fill_ms, show_ms)
|
||||||
Reference in new issue
Block a user