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'.
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: