Hi,

I would like to submit my small decoder 'meter'. It was helpful for me in
some protocols decoding, and seems to be some more functional than 'timing'
decoder.

Here is patch generated from latest git clone of libsigrokdecode repository
and screenshot showing decoder options.

P.S. Sorry for repetitive posts: I'm new to mail lists and has overlooked
that posts must be in plain text.
commit bb9e4102682cdf64ecf03c751f529d55f094edac
Author: Andriy Trotsenko <andriy.trotse...@gmail.com>
Date:   Fri Mar 18 13:18:54 2016 +0200

    Added pulse length meter decoder

diff --git a/decoders/meter/__init__.py b/decoders/meter/__init__.py
new file mode 100644
index 0000000..5849cb4
--- /dev/null
+++ b/decoders/meter/__init__.py
@@ -0,0 +1,25 @@
+##
+## This file pretending to be a part of the libsigrokdecode project.
+##
+## Copyright (C) 2015 Andriy Trotsenko <andriy.trotse...@gmail.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
+##
+
+'''
+Pulse length meter
+'''
+
+from .pd import Decoder
diff --git a/decoders/meter/pd.py b/decoders/meter/pd.py
new file mode 100644
index 0000000..76e1603
--- /dev/null
+++ b/decoders/meter/pd.py
@@ -0,0 +1,133 @@
+##
+## This file pretending to be a part of the libsigrokdecode project.
+##
+## Copyright (C) 2015 Andriy Trotsenko <andriy.trotse...@gmail.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 Decoder(srd.Decoder):
+
+    api_version = 2
+    id = 'meter'
+    name = 'Meter'
+    longname = 'Pulse length meter'
+    desc = 'Pulse length meter'
+    license = 'gplv2+'
+
+    CONST_0 = '- 0 -'
+    CONST_1 = '- 1 -'
+    CONST_01 = '0 & 1'
+    CONST_0_1 = '0 - 1'
+    CONST_1_0 = '1 - 0'
+
+    inputs = ['logic']
+    outputs = ['meter']
+    channels = (
+        {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
+    )
+    #optional_channels = ()
+    options = (
+        {'id': 'metering', 'desc': 'Metering', 'default': CONST_01,
+            'values': (CONST_0, CONST_1, CONST_01, CONST_0_1, CONST_1_0)},
+        {'id': 'units', 'desc': 'Units', 'default': 'samples',
+            'values': ('samples', 'seconds')},
+    )
+    annotations = (
+        ('pulse-length', 'Pulse length'),
+    )
+    #annotation_rows = (
+    #    ('components', 'Components', (0,)),
+    #    ('01', '0 -> 1', (1,)),
+    #    ('10', '1 -> 0', (2,)),
+    #)
+    #binary = (
+    #    ('raw', 'RAW file'),
+    #)
+
+    def __init__(self, **kwargs):
+        self.oldpin = None
+        self.toggle_samplenum = self.rise_samplenum = self.fall_samplenum = None
+
+
+    def metadata(self, key, value):
+        if key == srd.SRD_CONF_SAMPLERATE:
+            self.samplerate = value
+
+    def start(self):
+        #self.startedge = 0 if self.options['polarity'] == 'active-low' else 1
+        self.out_ann = self.register(srd.OUTPUT_ANN)
+        #self.out_bin = self.register(srd.OUTPUT_BINARY)
+        #self.out_average = self.register(srd.OUTPUT_META, meta=(float, 'Average', 'PWM base (cycle) frequency'))
+
+    def puta(self, start_samplenum):
+
+        if start_samplenum is None: return
+
+        n = self.samplenum - start_samplenum
+
+        if self.options['units'] != 'samples':
+
+            n *= 1000000 / self.samplerate          # Let's start from microseconds
+
+            for u in ("ยต", "m", ""):                # micro, mini, seconds
+                if n < 10000: break                 # Prefer 1234 over 1.23
+                n /= 1000
+
+            if n < 1: nn = "%.3f" % n               # .123
+            if n >= 1 and n < 10: nn = "%.2f" % n   # 1.23
+            if n >= 10 and n < 100: nn = "%.1f" % n # 12.3
+            if n >= 100: nn = "%.0f" % n            # 123
+                                                    # 1234
+            if nn.find(".")>=0:                     # Strip meaningless 0s after decimal point
+                nn = nn.rstrip("0")
+            nn = nn.rstrip(".")                     # ... and decimal point itself if alone
+            nn += u                                 # Add measurement units
+        else:
+            nn = "%d" % n
+
+        self.put(start_samplenum, self.samplenum, self.out_ann, [0, [nn]])
+
+
+    def decode(self, ss, es, data):
+
+        for (self.samplenum, pins) in data:
+            # Ignore identical samples early on (for performance reasons).
+            if self.oldpin == pins[0]:
+                continue
+
+            # Initialize self.oldpins with the first sample value.
+            if self.oldpin is None:
+                self.oldpin = pins[0]
+                continue
+
+            if self.oldpin != pins[0]:
+
+                if self.options['metering'] == self.CONST_0_1 and pins[0] == 0:
+                    self.puta(self.rise_samplenum)
+                    self.rise_samplenum = self.samplenum
+
+                if self.options['metering'] == self.CONST_1_0 and pins[0] == 1:
+                    self.puta(self.fall_samplenum)
+                    self.fall_samplenum = self.samplenum
+
+                if self.options['metering'] in (self.CONST_0, self.CONST_1, self.CONST_01):
+                    if (self.options['metering'] != self.CONST_0 and pins[0] == 0) or (self.options['metering'] != self.CONST_1 and pins[0] == 1):
+                        self.puta(self.toggle_samplenum)
+                    self.toggle_samplenum = self.samplenum
+
+            self.oldpin = pins[0]
------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
_______________________________________________
sigrok-devel mailing list
sigrok-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to