From 4a3c854ca958340507eb69adaaeaf31c6e678df6 Mon Sep 17 00:00:00 2001 From: Gerhard Sittig Date: Thu, 23 Jul 2020 17:23:24 +0200 Subject: common: add LSB/MSB first bitpack variant with optional index The CAN decoder collects bits in MSB first order. The SIRC decoder keeps lists of tuples with bits and their ss/es. Introduce common logic for LSB and MSB first arguments, and optional array index access, to reduce redundancy at callers'. --- decoders/common/srdhelper/mod.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'decoders') diff --git a/decoders/common/srdhelper/mod.py b/decoders/common/srdhelper/mod.py index 6c45af9..b56cce6 100644 --- a/decoders/common/srdhelper/mod.py +++ b/decoders/common/srdhelper/mod.py @@ -31,6 +31,20 @@ def bin2int(s: str): def bitpack(bits): return sum([b << i for i, b in enumerate(bits)]) +def bitpack_lsb(bits, idx=None): + '''Conversion from LSB first bit sequence to integer.''' + if idx is not None: + bits = [b[idx] for b in bits] + return bitpack(bits) + +def bitpack_msb(bits, idx=None): + '''Conversion from MSB first bit sequence to integer.''' + bits = bits[:] + if idx is not None: + bits = [b[idx] for b in bits] + bits.reverse() + return bitpack(bits) + def bitunpack(num, minbits=0): res = [] while num or minbits > 0: -- cgit v1.2.3-70-g09d2