]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/uart/pd.py
uart: minor nit, rename the "databyte" variable
[libsigrokdecode.git] / decoders / uart / pd.py
index 4c37c344ecf341a72f777713c512d0078738d552..a8bf090b48346d59f30ba90784a72a39f9637a19 100644 (file)
@@ -138,6 +138,7 @@ class Decoder(srd.Decoder):
         ('tx', 'TX dump'),
         ('rxtx', 'RX/TX dump'),
     )
+    idle_state = ['WAIT FOR START BIT', 'WAIT FOR START BIT']
 
     def putx(self, rxtx, data):
         s, halfbit = self.startsample[rxtx], self.bit_width / 2.0
@@ -157,26 +158,26 @@ class Decoder(srd.Decoder):
 
     def putbin(self, rxtx, data):
         s, halfbit = self.startsample[rxtx], self.bit_width / 2.0
-        self.put(s - floor(halfbit), self.samplenum + ceil(halfbit), self.out_bin, data)
+        self.put(s - floor(halfbit), self.samplenum + ceil(halfbit), self.out_binary, data)
 
-    def __init__(self, **kwargs):
+    def __init__(self):
         self.samplerate = None
         self.samplenum = 0
         self.frame_start = [-1, -1]
         self.startbit = [-1, -1]
         self.cur_data_bit = [0, 0]
-        self.databyte = [0, 0]
+        self.datavalue = [0, 0]
         self.paritybit = [-1, -1]
         self.stopbit1 = [-1, -1]
         self.startsample = [-1, -1]
         self.state = ['WAIT FOR START BIT', 'WAIT FOR START BIT']
         self.oldbit = [1, 1]
-        self.oldpins = [1, 1]
+        self.oldpins = [-1, -1]
         self.databits = [[], []]
 
     def start(self):
         self.out_python = self.register(srd.OUTPUT_PYTHON)
-        self.out_bin = self.register(srd.OUTPUT_BINARY)
+        self.out_binary = self.register(srd.OUTPUT_BINARY)
         self.out_ann = self.register(srd.OUTPUT_ANN)
 
     def metadata(self, key, value):
@@ -229,7 +230,7 @@ class Decoder(srd.Decoder):
             # TODO: Abort? Ignore rest of the frame?
 
         self.cur_data_bit[rxtx] = 0
-        self.databyte[rxtx] = 0
+        self.datavalue[rxtx] = 0
         self.startsample[rxtx] = -1
 
         self.state[rxtx] = 'GET DATA BITS'
@@ -248,12 +249,12 @@ class Decoder(srd.Decoder):
 
         # Get the next data bit in LSB-first or MSB-first fashion.
         if self.options['bit_order'] == 'lsb-first':
-            self.databyte[rxtx] >>= 1
-            self.databyte[rxtx] |= \
+            self.datavalue[rxtx] >>= 1
+            self.datavalue[rxtx] |= \
                 (signal << (self.options['num_data_bits'] - 1))
         else:
-            self.databyte[rxtx] <<= 1
-            self.databyte[rxtx] |= (signal << 0)
+            self.datavalue[rxtx] <<= 1
+            self.datavalue[rxtx] |= (signal << 0)
 
         self.putg([rxtx + 12, ['%d' % signal]])
 
@@ -269,9 +270,9 @@ class Decoder(srd.Decoder):
         self.state[rxtx] = 'GET PARITY BIT'
 
         self.putpx(rxtx, ['DATA', rxtx,
-            (self.databyte[rxtx], self.databits[rxtx])])
+            (self.datavalue[rxtx], self.databits[rxtx])])
 
-        b, f = self.databyte[rxtx], self.options['format']
+        b, f = self.datavalue[rxtx], self.options['format']
         if f == 'ascii':
             c = chr(b) if b in range(30, 126 + 1) else '[%02X]' % b
             self.putx(rxtx, [rxtx, [c]])
@@ -287,7 +288,7 @@ class Decoder(srd.Decoder):
         self.putbin(rxtx, [rxtx, bytes([b])])
         self.putbin(rxtx, [2, bytes([b])])
 
-        self.databits = [[], []]
+        self.databits[rxtx] = []
 
     def get_parity_bit(self, rxtx, signal):
         # If no parity is used/configured, skip to the next state immediately.
@@ -304,7 +305,7 @@ class Decoder(srd.Decoder):
         self.state[rxtx] = 'GET STOP BITS'
 
         if parity_ok(self.options['parity_type'], self.paritybit[rxtx],
-                     self.databyte[rxtx], self.options['num_data_bits']):
+                     self.datavalue[rxtx], self.options['num_data_bits']):
             self.putp(['PARITYBIT', rxtx, self.paritybit[rxtx]])
             self.putg([rxtx + 4, ['Parity bit', 'Parity', 'P']])
         else:
@@ -338,10 +339,12 @@ class Decoder(srd.Decoder):
             raise SamplerateError('Cannot decode without samplerate.')
         for (self.samplenum, pins) in data:
 
-            # Note: Ignoring identical samples here for performance reasons
-            # is not possible for this PD, at least not in the current state.
-            # if self.oldpins == pins:
-            #     continue
+            # We want to skip identical samples for performance reasons but,
+            # for now, we can only do that when we are in the idle state
+            # (meaning both channels are waiting for the start bit).
+            if self.state == self.idle_state and self.oldpins == pins:
+                continue
+
             self.oldpins, (rx, tx) = pins, pins
 
             if self.options['invert_rx'] == 'yes':