From ce519dacbc465323c2095ab5fd29df7dc577517e Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Sun, 14 Apr 2024 23:58:15 +1000 Subject: [PATCH] DFReader.py: add bitmask data to verbose print output --- DFReader.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/DFReader.py b/DFReader.py index c9b7ff90e..a8c234b7a 100644 --- a/DFReader.py +++ b/DFReader.py @@ -293,12 +293,85 @@ def __str__(self): ret = ret[:-2] return ret + '}' + def dump_verbose_bitmask(self, f, c, val, field_metadata): + try: + try: + bitmask = field_metadata["bitmask"] + except Exception: + return + + # work out how many bits to show: + t = field_metadata.get("type") + bit_count = None + if t == "uint8_t": + bit_count = 8 + elif t == "uint16_t": + bit_count = 16 + elif t == "uint32_t": + bit_count = 32 + + if bit_count is None: + return + + highest = -1 + + # we show bit values at least up to the highest bit set: + for i in range(bit_count): + if val & (1< highest: + highest = bit_offset + + for i in range(bit_offset): + bit_value = 1 << i + done = False + for bit in bitmask.bit: + if bit["value"] != bit_value: + continue + if val & bit_value: + bang = "" + else: + bang = "!" + bit_name = bit.get('name') + bit_desc = None + try: + bit_desc = bit["description"] + except KeyError: + pass + if bit_desc is None: + f.write(" %s%s\n" % (bang, bit_name,)) + else: + f.write(" %s%s (%s)\n" % (bang, bit_name, bit_desc)) + done = True + break + if not done: + f.write(" %{s}UNKNOWN_BIT%s\n" % (bang, str(i))) + except Exception as e: + # print(e) + pass + def dump_verbose(self, f): is_py3 = sys.version_info >= (3,0) timestamp = "%s.%03u" % ( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self._timestamp)), int(self._timestamp*1000.0)%1000) f.write("%s: %s\n" % (timestamp, self.fmt.name)) + + field_metadata_by_name = {} + try: + metadata_tree = self._parent.metadata.metadata_tree() + metadata = metadata_tree[self.fmt.name] + for fm in metadata["fields"].field: + field_metadata_by_name[fm.get("name")] = fm + except Exception as e: + # print(e) + pass + for c in self.fmt.columns: # Get the value val = self.__getattr__(c) @@ -325,6 +398,10 @@ def dump_verbose(self, f): # Append the unit f.write(" %s\n" % (unit)) + # if this is a bitmask then print out all bits set: + if c in field_metadata_by_name: + self.dump_verbose_bitmask(f, c, val, field_metadata_by_name[c]) + def get_msgbuf(self): '''create a binary message buffer for a message''' values = []