diff options
author | Gerhard Sittig <gerhard.sittig@gmx.net> | 2020-07-23 17:26:06 +0200 |
---|---|---|
committer | Gerhard Sittig <gerhard.sittig@gmx.net> | 2020-07-26 14:38:18 +0200 |
commit | ae3ed2954e8c4428c6a43922bd03741136cdb354 (patch) | |
tree | a94b4d74524251ef35e07363061fa832bad232ab | |
parent | 4a3c854ca958340507eb69adaaeaf31c6e678df6 (diff) | |
download | libsigrokdecode-ae3ed2954e8c4428c6a43922bd03741136cdb354.tar.gz libsigrokdecode-ae3ed2954e8c4428c6a43922bd03741136cdb354.zip |
can: use common MSB first variant of bitpack()
This eliminates array copies and bit order reversal in the calling
decoder. Prefer common helpers instead.
-rw-r--r-- | decoders/can/pd.py | 30 |
1 files changed, 8 insertions, 22 deletions
diff --git a/decoders/can/pd.py b/decoders/can/pd.py index bf8e10a..fcd13e6 100644 --- a/decoders/can/pd.py +++ b/decoders/can/pd.py @@ -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']]) |