Hi,

I needed to decode the timing between pin changes for the WS2811 protocol,
it requires some strict timing and my code is not up to it so I needed to
measure it. I could do that manually but it was a pain in the ass and very
slow so I whipped up this protocol decoder to calculate the time between
pin changes.

Baruch
diff --git a/decoders/timing/__init__.py b/decoders/timing/__init__.py
new file mode 100644
index 0000000..628ad3f
--- /dev/null
+++ b/decoders/timing/__init__.py
@@ -0,0 +1,25 @@
+##
+## This file is part of the libsigrokdecode project.
+##
+## Copyright (C) 2014 Torsten Duwe <d...@suse.de>
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## 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
+##
+
+'''
+Timing decoder, find the time between pulses.
+'''
+
+from .pd import Decoder
diff --git a/decoders/timing/pd.py b/decoders/timing/pd.py
new file mode 100644
index 0000000..d62b569
--- /dev/null
+++ b/decoders/timing/pd.py
@@ -0,0 +1,95 @@
+##
+## This file is part of the libsigrokdecode project.
+##
+## Copyright (C) 2014 Torsten Duwe <d...@suse.de>
+## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourde...@savoirfairelinux.com>
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## 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
+##
+
+import sigrokdecode as srd
+
+class SamplerateError(Exception):
+    pass
+
+def normalize_time(t):
+    if t >= 1.0:
+        return '%.3f sec' % t
+    elif t >= 0.001:
+        return '%.3f msec' % (t*1000.0)
+    elif t >= 0.000001:
+        return '%.3f usec' % (t*1000.0*1000.0)
+    elif t >= 0.000000001:
+        return '%.3f nsec' % (t*1000.0 * 1000.0 * 1000.0)
+    else:
+        return '%f' % t
+
+class Decoder(srd.Decoder):
+    api_version = 2
+    id = 'timing'
+    name = 'Timing'
+    longname = 'Timing Finder'
+    desc = 'Calculate time between pulses.'
+    license = 'gplv2+'
+    inputs = ['logic']
+    outputs = ['timing']
+    channels = (
+        {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
+    )
+    annotations = (
+        ('time', 'Time'),
+    )
+    annotation_rows = (
+        ('time', 'Time', (0,)),
+    )
+
+    def __init__(self, **kwargs):
+        self.samplerate = None
+        self.oldpin = None
+        self.last_samplenum = None
+
+    def metadata(self, key, value):
+        if key == srd.SRD_CONF_SAMPLERATE:
+            self.samplerate = value
+
+    def start(self):
+        self.out_ann = self.register(srd.OUTPUT_ANN)
+
+    def decode(self, ss, es, data):
+        if not self.samplerate:
+            raise SamplerateError('Cannot decode without samplerate.')
+
+        # Initialize self.oldpins with the first sample value.
+        for (samplenum, (pin0,)) in data:
+            # Ignore identical samples early on (for performance reasons).
+            if self.oldpin == pin0:
+                continue
+
+            if self.oldpin is None:
+                self.oldpin = pin0
+                self.last_samplenum = samplenum
+                continue
+
+            if self.oldpin != pin0:
+                samples = samplenum - self.last_samplenum
+                t = samples / self.samplerate
+
+                # Report the timing normalized
+                self.put(self.last_samplenum, samplenum, self.out_ann, [0, [normalize_time(t)]])
+
+                # Store data for next round
+                self.last_samplenum = samplenum
+                self.oldpin = pin0
+
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
sigrok-devel mailing list
sigrok-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to