]> sigrok.org Git - libsigrokdecode.git/commitdiff
can: use common MSB first variant of bitpack()
authorGerhard Sittig <redacted>
Thu, 23 Jul 2020 15:26:06 +0000 (17:26 +0200)
committerGerhard Sittig <redacted>
Sun, 26 Jul 2020 12:38:18 +0000 (14:38 +0200)
This eliminates array copies and bit order reversal in the calling
decoder. Prefer common helpers instead.

decoders/can/pd.py

index bf8e10af37f83bfe9853cac203d8691345c610f2..fcd13e6aefba1bdcc3215d108b335cf452750a48 100644 (file)
@@ -18,7 +18,7 @@
 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
 ##
 
-from common.srdhelper import bitpack
+from common.srdhelper import bitpack_msb
 import sigrokdecode as srd
 
 class SamplerateError(Exception):
@@ -205,9 +205,7 @@ class Decoder(srd.Decoder):
 
             x = self.last_databit + 1
             crc_bits = self.bits[x:x + self.crc_len + 1]
-            bits = crc_bits
-            bits.reverse()
-            self.crc = bitpack(bits)
+            self.crc = bitpack_msb(crc_bits)
             self.putb([11, ['%s sequence: 0x%04x' % (crc_type, self.crc),
                             '%s: 0x%04x' % (crc_type, self.crc), '%s' % crc_type]])
             if not self.is_valid_crc(crc_bits):
@@ -297,9 +295,7 @@ class Decoder(srd.Decoder):
 
         # Bits 15-18: Data length code (DLC), in number of bytes (0-8).
         elif bitnum == self.dlc_start + 3:
-            bits = self.bits[self.dlc_start:self.dlc_start + 4]
-            bits.reverse()
-            self.dlc = bitpack(bits)
+            self.dlc = bitpack_msb(self.bits[self.dlc_start:self.dlc_start + 4])
             self.putb([10, ['Data length code: %d' % self.dlc,
                             'DLC: %d' % self.dlc, 'DLC']])
             self.last_databit = self.dlc_start + 3 + (dlc2len(self.dlc) * 8)
@@ -316,9 +312,7 @@ class Decoder(srd.Decoder):
             self.ss_databytebits.append(self.samplenum) # Last databyte bit.
             for i in range(dlc2len(self.dlc)):
                 x = self.dlc_start + 4 + (8 * i)
-                bits = self.bits[x:x + 8]
-                bits.reverse()
-                b = bitpack(bits)
+                b = bitpack_msb(self.bits[x:x + 8])
                 self.frame_bytes.append(b)
                 ss = self.ss_databytebits[i * 8]
                 es = self.ss_databytebits[((i + 1) * 8) - 1]
@@ -342,9 +336,7 @@ class Decoder(srd.Decoder):
 
         # Bits 14-31: Extended identifier (EID[17..0])
         elif bitnum == 31:
-            bits = self.bits[14:]
-            bits.reverse()
-            self.eid = bitpack(bits)
+            self.eid = bitpack_msb(self.bits[14:])
             s = '%d (0x%x)' % (self.eid, self.eid)
             self.putb([4, ['Extended Identifier: %s' % s,
                            'Extended ID: %s' % s, 'Extended ID', 'EID']])
@@ -405,9 +397,7 @@ class Decoder(srd.Decoder):
 
         # Bits 35-38: Data length code (DLC), in number of bytes (0-8).
         elif bitnum == self.dlc_start + 3:
-            bits = self.bits[self.dlc_start:self.dlc_start + 4]
-            bits.reverse()
-            self.dlc = bitpack(bits)
+            self.dlc = bitpack_msb(self.bits[self.dlc_start:self.dlc_start + 4])
             self.putb([10, ['Data length code: %d' % self.dlc,
                             'DLC: %d' % self.dlc, 'DLC']])
             self.last_databit = self.dlc_start + 3 + (dlc2len(self.dlc) * 8)
@@ -422,9 +412,7 @@ class Decoder(srd.Decoder):
             self.ss_databytebits.append(self.samplenum) # Last databyte bit.
             for i in range(dlc2len(self.dlc)):
                 x = self.dlc_start + 4 + (8 * i)
-                bits = self.bits[x:x + 8]
-                bits.reverse()
-                b = bitpack(bits)
+                b = bitpack_msb(self.bits[x:x + 8])
                 self.frame_bytes.append(b)
                 ss = self.ss_databytebits[i * 8]
                 es = self.ss_databytebits[((i + 1) * 8) - 1]
@@ -472,11 +460,9 @@ class Decoder(srd.Decoder):
         # Bits 1-11: Identifier (ID[10..0])
         # The bits ID[10..4] must NOT be all recessive.
         elif bitnum == 11:
-            bits = self.bits[1:]
-            bits.reverse()
             # BEWARE! Don't clobber the decoder's .id field which is
             # part of its boiler plate!
-            self.ident = bitpack(bits)
+            self.ident = bitpack_msb(self.bits[1:])
             self.fullid = self.ident
             s = '%d (0x%x)' % (self.ident, self.ident),
             self.putb([3, ['Identifier: %s' % s, 'ID: %s' % s, 'ID']])