"""Builds 12-OBD-Speed/src/watch_dids.h from the DID scan logs: every DID the PCM supports, with its reply length, minus the ones whose meaning is known or that are useless for finding the cruise set speed / speed limit. python tools/make_watch_dids.py 12-OBD-Speed/docs/canlog 12-OBD-Speed/src/watch_dids.h """ import glob import os import re import sys src, out = sys.argv[1], sys.argv[2] dids = {} for path in glob.glob(os.path.join(src, "**", "*.txt"), recursive=True): for line in open(path, encoding="utf-8", errors="replace"): m = re.match(r"^\d+ S 7E0 ([0-9A-F]{4}) ([0-9A-F]+)\s*$", line) if m: dids[int(m.group(1), 16)] = len(m.group(2)) // 2 def skip(did, length): if 0xF100 <= did <= 0xF1FF: # identification (part numbers, VIN) return True if 0xF400 <= did <= 0xF4FF: # copies of the standard mode 01 PIDs return True if 0xDE00 <= did <= 0xDEFF: # large tables return True if did in (0xD01A, 0xDD00, 0xDD01): # text, clock, odometer return True return length > 8 keep = sorted((d, l) for d, l in dids.items() if not skip(d, l)) with open(out, "w", newline="\n") as f: f.write("// Generated by tools/make_watch_dids.py from the DID scan logs (docs/canlog) - do not edit.\n") f.write(f"// PCM (0x7E0) DIDs with unknown meaning that are watched for changes: {len(keep)} of {len(dids)} supported.\n") f.write("#pragma once\n#include \n\n") f.write("struct WatchDid\n{\n uint16_t did;\n uint8_t len; // reply data bytes (from the scan)\n};\n\n") f.write("static const WatchDid WATCH_DIDS[] = {\n") for i in range(0, len(keep), 6): f.write(" " + " ".join(f"{{0x{d:04X}, {l}}}," for d, l in keep[i:i + 6]) + "\n") f.write("};\nstatic const uint16_t WATCH_COUNT = sizeof(WATCH_DIDS) / sizeof(WATCH_DIDS[0]);\n") print(f"{len(keep)} of {len(dids)} DIDs -> {out}")