From 581208b2ffeeb2a2128aee0741fa3fd9e46358e2 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Wed, 6 Aug 2025 17:50:53 +0100 Subject: Run black on all first party python code --- scripts/logextract.py | 373 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 240 insertions(+), 133 deletions(-) (limited to 'scripts/logextract.py') diff --git a/scripts/logextract.py b/scripts/logextract.py index 592f6f8a..3050ff0b 100755 --- a/scripts/logextract.py +++ b/scripts/logextract.py @@ -6,6 +6,7 @@ # This file may be distributed under the terms of the GNU GPLv3 license. import sys, re, collections, ast, itertools + def format_comment(line_num, line): return "# %6d: %s" % (line_num, line) @@ -14,6 +15,7 @@ def format_comment(line_num, line): # Config file extraction ###################################################################### + class GatherConfig: def __init__(self, configs, line_num, recent_lines, logname): self.configs = configs @@ -22,12 +24,14 @@ class GatherConfig: self.filename = "%s.config%04d.cfg" % (logname, self.config_num) self.config_lines = [] self.comments = [] + def add_line(self, line_num, line): - if line != '=======================': + if line != "=======================": self.config_lines.append(line) return True self.finalize() return False + def finalize(self): lines = tuple(self.config_lines) ch = self.configs.get(lines) @@ -36,13 +40,15 @@ class GatherConfig: else: ch.comments.extend(self.comments) ch.comments.append(format_comment(self.line_num, "config file")) + def add_comment(self, comment): if comment is not None: self.comments.append(comment) + def write_file(self): lines = itertools.chain(self.comments, self.config_lines) - lines = ('%s\n' % l for l in lines) - with open(self.filename, 'wt') as f: + lines = ("%s\n" % l for l in lines) + with open(self.filename, "wt") as f: f.writelines(lines) @@ -52,6 +58,7 @@ class GatherConfig: uart_r = re.compile(r"tmcuart_(?:send|response) oid=[0-9]+ (?:read|write)=") + class TMCUartHelper: def _calc_crc8(self, data): # Generate a CRC8-ATM value for a bytearray @@ -61,33 +68,46 @@ class TMCUartHelper: if (crc >> 7) ^ (b & 0x01): crc = (crc << 1) ^ 0x07 else: - crc = (crc << 1) - crc &= 0xff + crc = crc << 1 + crc &= 0xFF b >>= 1 return crc + def _add_serial_bits(self, data): # Add serial start and stop bits to a message in a bytearray out = 0 pos = 0 for d in data: b = (d << 1) | 0x200 - out |= (b << pos) + out |= b << pos pos += 10 res = bytearray() - for i in range((pos+7)//8): - res.append((out >> (i*8)) & 0xff) + for i in range((pos + 7) // 8): + res.append((out >> (i * 8)) & 0xFF) return res + def _encode_read(self, sync, addr, reg): # Generate a uart read register message msg = bytearray([sync, addr, reg]) msg.append(self._calc_crc8(msg)) return self._add_serial_bits(msg) + def _encode_write(self, sync, addr, reg, val): # Generate a uart write register message - msg = bytearray([sync, addr, reg, (val >> 24) & 0xff, - (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff]) + msg = bytearray( + [ + sync, + addr, + reg, + (val >> 24) & 0xFF, + (val >> 16) & 0xFF, + (val >> 8) & 0xFF, + val & 0xFF, + ] + ) msg.append(self._calc_crc8(msg)) return self._add_serial_bits(msg) + def _decode_read(self, data): # Extract a uart read request message if len(data) != 5: @@ -98,13 +118,14 @@ class TMCUartHelper: mval |= d << pos pos += 8 # Extract register value - addr = (mval >> 11) & 0xff - reg = (mval >> 21) & 0xff + addr = (mval >> 11) & 0xFF + reg = (mval >> 21) & 0xFF # Verify start/stop bits and crc - encoded_data = self._encode_read(0xf5, addr, reg) + encoded_data = self._encode_read(0xF5, addr, reg) if data != encoded_data: return "Invalid: %s" % (self.pretty_print(addr, reg),) return self.pretty_print(addr, reg) + def _decode_reg(self, data): # Extract a uart read response message if len(data) != 10: @@ -115,25 +136,31 @@ class TMCUartHelper: mval |= d << pos pos += 8 # Extract register value - addr = (mval >> 11) & 0xff - reg = (mval >> 21) & 0xff - val = ((((mval >> 31) & 0xff) << 24) | (((mval >> 41) & 0xff) << 16) - | (((mval >> 51) & 0xff) << 8) | ((mval >> 61) & 0xff)) - sync = 0xf5 - if addr == 0xff: + addr = (mval >> 11) & 0xFF + reg = (mval >> 21) & 0xFF + val = ( + (((mval >> 31) & 0xFF) << 24) + | (((mval >> 41) & 0xFF) << 16) + | (((mval >> 51) & 0xFF) << 8) + | ((mval >> 61) & 0xFF) + ) + sync = 0xF5 + if addr == 0xFF: sync = 0x05 # Verify start/stop bits and crc encoded_data = self._encode_write(sync, addr, reg, val) if data != encoded_data: - #print("Got %s vs %s" % (repr(data), repr(encoded_data))) + # print("Got %s vs %s" % (repr(data), repr(encoded_data))) return "Invalid:%s" % (self.pretty_print(addr, reg, val),) return self.pretty_print(addr, reg, val) + def pretty_print(self, addr, reg, val=None): if val is None: return "(%x@%x)" % (reg, addr) if reg & 0x80: return "(%x@%x=%08x)" % (reg & ~0x80, addr, val) return "(%x@%x==%08x)" % (reg, addr, val) + def parse_msg(self, msg): data = bytearray(msg) if len(data) == 10: @@ -149,16 +176,28 @@ class TMCUartHelper: # Shutdown extraction ###################################################################### + def add_high_bits(val, ref, mask): half = (mask + 1) // 2 return ref + ((val - (ref & mask) + half) & mask) - half + count_s = r"(?P[0-9]+)" time_s = r"(?P