]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/can/pd.py
license: remove FSF postal address from boiler plate license text
[libsigrokdecode.git] / decoders / can / pd.py
index bf38b7fb9d0e1d23f9a9b83dd76ba82349ed7a84..1a99c307c94861acf9deca895d6ef181ab81fb2a 100644 (file)
 ## GNU General Public License for more details.
 ##
 ## You should have received a copy of the GNU General Public License
-## along with this program; if not, write to the Free Software
-## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
 ##
 
 import sigrokdecode as srd
 
+class SamplerateError(Exception):
+    pass
+
 class Decoder(srd.Decoder):
     api_version = 2
     id = 'can'
@@ -54,9 +56,14 @@ class Decoder(srd.Decoder):
         ('ack-delimiter', 'ACK delimiter'),
         ('stuff-bit', 'Stuff bit'),
         ('warnings', 'Human-readable warnings'),
+        ('bit', 'Bit'),
+    )
+    annotation_rows = (
+        ('bits', 'Bits', (15, 17)),
+        ('fields', 'Fields', tuple(range(15)) + (16,)),
     )
 
-    def __init__(self, **kwargs):
+    def __init__(self):
         self.samplerate = None
         self.reset_variables()
 
@@ -112,8 +119,6 @@ class Decoder(srd.Decoder):
             return False
 
         # Stuff bit. Keep it in self.rawbits, but drop it from self.bits.
-        self.putx([15, ['Stuff bit: %d' % self.rawbits[-1],
-                        'SB: %d' % self.rawbits[-1], 'SB']])
         self.bits.pop() # Drop last bit.
         return True
 
@@ -302,14 +307,13 @@ class Decoder(srd.Decoder):
         # Get the index of the current CAN frame bit (without stuff bits).
         bitnum = len(self.bits) - 1
 
-        # For debugging.
-        # self.putx([0, ['Bit %d (CAN bit %d): %d' % \
-        #           (self.curbit, bitnum, can_rx)]])
-
         # If this is a stuff bit, remove it from self.bits and ignore it.
         if self.is_stuff_bit():
+            self.putx([15, [str(can_rx)]])
             self.curbit += 1 # Increase self.curbit (bitnum is not affected).
             return
+        else:
+            self.putx([17, [str(can_rx)]])
 
         # Bit 0: Start of frame (SOF) bit
         if bitnum == 0:
@@ -358,8 +362,8 @@ class Decoder(srd.Decoder):
         self.curbit += 1
 
     def decode(self, ss, es, data):
-        if self.samplerate is None:
-            raise Exception("Cannot decode without samplerate.")
+        if not self.samplerate:
+            raise SamplerateError('Cannot decode without samplerate.')
         for (self.samplenum, pins) in data:
 
             (can_rx,) = pins
@@ -376,6 +380,3 @@ class Decoder(srd.Decoder):
                 if not self.reached_bit(self.curbit):
                     continue
                 self.handle_bit(can_rx)
-            else:
-                raise Exception("Invalid state: %s" % self.state)
-