]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/counter/pd.py
counter: add support for user specified initial counter values
[libsigrokdecode.git] / decoders / counter / pd.py
index 384f978f536b5af2ed3ee10eeb8a8bdc47b45f26..b8811e589bdf89e58ae8748435b536016f490be9 100644 (file)
@@ -19,8 +19,8 @@
 
 import sigrokdecode as srd
 
-(PIN_DATA, PIN_RESET) = range(2)
-(ROW_EDGE, ROW_WORD, ROW_RESET) = range(3)
+PIN_DATA, PIN_RESET = range(2)
+ROW_EDGE, ROW_WORD, ROW_RESET = range(3)
 
 class Decoder(srd.Decoder):
     api_version = 3
@@ -53,6 +53,8 @@ class Decoder(srd.Decoder):
         {'id': 'divider', 'desc': 'Count divider (word width)', 'default': 0},
         {'id': 'reset_edge', 'desc': 'Edge which clears counters (reset)',
             'default': 'falling', 'values': ('rising', 'falling')},
+        {'id': 'edge_off', 'desc': 'Initial edge counter value', 'default': 0},
+        {'id': 'word_off', 'desc': 'Initial word counter value', 'default': 0},
     )
 
     def __init__(self):
@@ -68,37 +70,58 @@ class Decoder(srd.Decoder):
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
 
-    def putc(self, cls, annlist):
-        self.put(self.samplenum, self.samplenum, self.out_ann, [cls, annlist])
+    def putc(self, cls, ss, annlist):
+        self.put(ss, self.samplenum, self.out_ann, [cls, annlist])
 
     def decode(self):
         opt_edge_map = {'rising': 'r', 'falling': 'f', 'any': 'e'}
 
-        self.edge = self.options['data_edge']
-        self.divider = self.options['divider']
-        if self.divider < 0:
-            self.divider = 0
+        data_edge = self.options['data_edge']
+        divider = self.options['divider']
+        if divider < 0:
+            divider = 0
+        reset_edge = self.options['reset_edge']
 
-        condition = [{PIN_DATA: opt_edge_map[self.edge]}]
-        self.have_reset = self.has_channel(PIN_RESET)
-        if self.have_reset:
+        condition = [{PIN_DATA: opt_edge_map[data_edge]}]
+        have_reset = self.has_channel(PIN_RESET)
+        if have_reset:
             cond_reset = len(condition)
-            condition.append({PIN_RESET: opt_edge_map[self.options['reset_edge']]})
+            condition.append({PIN_RESET: opt_edge_map[reset_edge]})
 
-        self.edge_count = 0
-        self.word_count = 0
+        edge_count = int(self.options['edge_off'])
+        edge_start = None
+        word_count = int(self.options['word_off'])
+        word_start = None
         while True:
             self.wait(condition)
+            now = self.samplenum
 
-            if self.have_reset and self.matched[cond_reset]:
-                self.edge_count = 0
-                self.word_count = 0
-                self.putc(ROW_RESET, ['Word reset', 'Reset', 'Rst', 'R'])
+            if have_reset and self.matched[cond_reset]:
+                edge_count = 0
+                edge_start = now
+                word_count = 0
+                word_start = now
+                self.putc(ROW_RESET, now, ['Word reset', 'Reset', 'Rst', 'R'])
                 continue
 
-            self.edge_count += 1
-            self.putc(ROW_EDGE, [str(self.edge_count)])
+            # Implementation note: In the absence of a RESET condition
+            # before the first data edge, any arbitrary choice of where
+            # to start the annotation is valid. One may choose to emit a
+            # narrow annotation (where ss=es), or assume that the cycle
+            # which corresponds to the counter value started at sample
+            # number 0. We decided to go with the latter here, to avoid
+            # narrow annotations (see bug #1210). None of this matters in
+            # the presence of a RESET condition in the input stream.
+            if edge_start is None:
+                edge_start = 0
+            if word_start is None:
+                word_start = 0
 
-            if self.divider > 0 and (self.edge_count % self.divider) == 0:
-                self.word_count += 1
-                self.putc(ROW_WORD, [str(self.word_count)])
+            edge_count += 1
+            self.putc(ROW_EDGE, edge_start, ["{:d}".format(edge_count)])
+            edge_start = now
+
+            if divider and (edge_count % divider) == 0:
+                word_count += 1
+                self.putc(ROW_WORD, word_start, ["{:d}".format(word_count)])
+                word_start = now