]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/can/pd.py
can: Emit bit value annotations.
[libsigrokdecode.git] / decoders / can / pd.py
index e425575433510c3edea2512d0b707404134af4d9..7638c2f72b6898fc0769465508f1609f5e4b8bb8 100644 (file)
@@ -20,6 +20,9 @@
 
 import sigrokdecode as srd
 
+class SamplerateError(Exception):
+    pass
+
 class Decoder(srd.Decoder):
     api_version = 2
     id = 'can'
@@ -54,6 +57,11 @@ class Decoder(srd.Decoder):
         ('ack-delimiter', 'ACK delimiter'),
         ('stuff-bit', 'Stuff bit'),
         ('warnings', 'Human-readable warnings'),
+        ('bits', 'Bits'),
+    )
+    annotation_rows = (
+        ('bits', 'Bits', (17,)),
+        ('fields', 'Fields', tuple(range(17))),
     )
 
     def __init__(self, **kwargs):
@@ -302,9 +310,8 @@ 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)]])
+        # Emit a bit value annotation.
+        self.putx([17, [str(can_rx)]])
 
         # If this is a stuff bit, remove it from self.bits and ignore it.
         if self.is_stuff_bit():
@@ -358,8 +365,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,4 +383,3 @@ class Decoder(srd.Decoder):
                 if not self.reached_bit(self.curbit):
                     continue
                 self.handle_bit(can_rx)
-