SXXXXXXX_PyBusMonitor1553/pybusmonitor1553/utils/printer.py

42 lines
1.4 KiB
Python

import inspect
from ..lib1553.fields import Field
def dump_message(msg_obj):
"""
Introspects a Message object and prints all its decoded fields.
"""
cls = msg_obj.__class__
print(f"\n{'='*60}")
print(f"MESSAGE: {cls.__name__} (SA: {cls.SUBADDRESS})")
print(f"{'-'*60}")
# 1. Get all attributes that are instances of our Field descriptors
fields = []
for name, obj in cls.__dict__.items():
if isinstance(obj, Field):
fields.append((name, obj))
# 2. Sort fields by word_index then start_bit to keep print order logical
# (Word 0 Bit 0 comes before Word 0 Bit 5, which comes before Word 1)
fields.sort(key=lambda x: (x[1].word_index, x[1].start_bit))
# 3. Print values
for name, field_desc in fields:
try:
# This triggers the __get__ method of the descriptor
value = getattr(msg_obj, name)
# Format value based on type
if isinstance(value, float):
val_str = f"{value:.4f}"
elif hasattr(value, 'name'): # Enum
val_str = f"{value.name} ({value.value})"
else:
val_str = str(value)
print(f"Word {field_desc.word_index:02d} | {name:<25}: {val_str}")
except Exception as e:
print(f"Word {field_desc.word_index:02d} | {name:<25}: <Error: {e}>")
print(f"{'='*60}\n")