|
1 | 1 | # python library to interface with panda |
2 | 2 | import os |
| 3 | +import re |
3 | 4 | import sys |
4 | 5 | import time |
5 | 6 | import usb1 |
@@ -42,6 +43,15 @@ def calculate_checksum(data): |
42 | 43 | res ^= b |
43 | 44 | return res |
44 | 45 |
|
| 46 | +def _parse_c_struct(path, name): |
| 47 | + type_to_format = {"uint8_t": "B", "uint16_t": "H", "uint32_t": "I", "float": "f"} |
| 48 | + with open(path) as f: |
| 49 | + lines = [l.strip() for l in f.read().split(f"struct __attribute__((packed)) {name} {{", 1)[1].split("};", 1)[0].splitlines() if l.strip()] |
| 50 | + fields = [re.fullmatch(rf"({'|'.join(type_to_format)})\s+\w+;", l) for l in lines] |
| 51 | + if not all(fields): |
| 52 | + raise ValueError(f"unsupported {name} layout in {path}") |
| 53 | + return struct.Struct("<" + "".join(type_to_format[m[1]] for m in fields)) |
| 54 | + |
45 | 55 | def pack_can_buffer(arr, chunk=False, fd=False): |
46 | 56 | snds = [bytearray(), ] |
47 | 57 | for address, dat, bus in arr: |
@@ -128,7 +138,7 @@ class Panda: |
128 | 138 |
|
129 | 139 | CAN_PACKET_VERSION = compute_version_hash(os.path.join(opendbc.INCLUDE_PATH, "opendbc/safety/can.h")) |
130 | 140 | HEALTH_PACKET_VERSION = compute_version_hash(os.path.join(BASEDIR, "board/health.h")) |
131 | | - HEALTH_STRUCT = struct.Struct("<IIIIIIIIBBBBBHBBBHfBBHHHBH") |
| 141 | + HEALTH_STRUCT = _parse_c_struct(os.path.join(BASEDIR, "board/health.h"), "health_t") |
132 | 142 | CAN_HEALTH_STRUCT = struct.Struct("<BIBBBBBBBBIIIIIIIHHBBBIIII") |
133 | 143 |
|
134 | 144 | H7_DEVICES = [HW_TYPE_RED_PANDA, HW_TYPE_TRES, HW_TYPE_CUATRO, HW_TYPE_BODY] |
|
0 commit comments