vl53l8cx_tof.py (145.3 KB)
It looks like there’s quite a binary package for this driver… so you might want to restructure this code. But here’s the result. From the AI:
What it is
A single, self-contained MicroPython driver that reproduces OpenMV’s built-in tof C module (modules/py_tof.c) for the VL53L8CX using only machine.I2C — no firmware rebuild needed. It’s a faithful port of ST’s ULD (drivers/vl53l8cx): vl53l8cx_init, the firmware download, DCI read/write/replace, offset/xtalk buffer fix-ups, start_ranging, check_data_ready, and get_ranging_data.
Per your choices: VL53L8CX only, single self-contained file (the ~86 KB sensor firmware + config/xtalk/NVM blobs are embedded as base64 and decoded at import — file is ~145 KB), core ranging only (no image-drawing helpers).
API (mirrors the tof module)
from vl53l8cx_tof import VL53L8CX
tof = VL53L8CX(bus=3) # AE3 = I2C3; uploads FW, starts 8x8/15Hz continuous
grid, lo, hi = tof.read_depth() # grid = 64 floats (mm), row-major 8x8
tof.width(); tof.height(); tof.refresh() # 8, 8, 15
tof.reset() # stop ranging
read_depth(hmirror=False, vflip=False, transpose=False, timeout=100) returns (list, min, max) and applies the same orientation logic (including the default mirror-on) as py_tof_read_depth. Running the file directly prints the live 8×8 grid.
Verification
The blobs extract byte-exact from the C headers (firmware = 0x15000, config = 972, xtalk = 776, NVM = 40). I unit-tested the trickiest ported paths against the C semantics with a mock I2C:
start_ranging computes data_read_size = 184 and patches the distance block size to 64 ✓
_get_ranging_data word-swap + int16 extraction + header/footer corruption check ✓
- negative distance truncates toward zero (matching C
int16 /= 4) ✓
- orientation + min/max ✓
Caveats worth noting
- Not run on real hardware here — the bring-up is a line-by-line port, but I couldn’t exercise the actual I2C bus from this environment.
- Default wiring is
bus=3 (AE3 I2C3); the sensor’s 8-bit address 0x52 is the 7-bit 0x29 machine.I2C expects (both forms accepted).
- Firmware is chunked to
VL53L8CX.CHUNK = 1024 bytes per I2C burst; lower it if a board’s I2C driver rejects large transfers. Python firmware upload is slower than the C path (one-time, at startup).
- It assumes no XSHUT/reset line;
_recover() re-runs the full firmware bring-up on a ranging timeout.
Note that it took Claude about 10 minutes to make the above driver. If you use the AI tools they can do pretty much all the Python you want.