Skip to content

Commit a975b4d

Browse files
committed
Parse health packet layout from health.h
1 parent d079b09 commit a975b4d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

python/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# python library to interface with panda
22
import os
3+
import re
34
import sys
45
import time
56
import usb1
@@ -42,6 +43,12 @@ def calculate_checksum(data):
4243
res ^= b
4344
return res
4445

46+
def _parse_c_struct(path, name):
47+
with open(path) as f:
48+
body = f.read().split(f"struct __attribute__((packed)) {name} {{", 1)[1].split("};", 1)[0]
49+
fmt = "".join({"uint8_t": "B", "uint16_t": "H", "uint32_t": "I", "float": "f"}[t] for t in re.findall(r"^\s*(uint(?:8|16|32)_t|float)\s+\w+;", body, re.M))
50+
return struct.Struct("<" + fmt)
51+
4552
def pack_can_buffer(arr, chunk=False, fd=False):
4653
snds = [bytearray(), ]
4754
for address, dat, bus in arr:
@@ -128,7 +135,7 @@ class Panda:
128135

129136
CAN_PACKET_VERSION = compute_version_hash(os.path.join(opendbc.INCLUDE_PATH, "opendbc/safety/can.h"))
130137
HEALTH_PACKET_VERSION = compute_version_hash(os.path.join(BASEDIR, "board/health.h"))
131-
HEALTH_STRUCT = struct.Struct("<IIIIIIIIBBBBBHBBBHfBBHHHBH")
138+
HEALTH_STRUCT = _parse_c_struct(os.path.join(BASEDIR, "board/health.h"), "health_t")
132139
CAN_HEALTH_STRUCT = struct.Struct("<BIBBBBBBBBIIIIIIIHHBBBIIII")
133140

134141
H7_DEVICES = [HW_TYPE_RED_PANDA, HW_TYPE_TRES, HW_TYPE_CUATRO, HW_TYPE_BODY]

0 commit comments

Comments
 (0)