]> sigrok.org Git - libsigrokdecode.git/blob - decoders/transitioncounter/transitioncounter.py
srd: Add 'api_version = 1' to all PDs.
[libsigrokdecode.git] / decoders / transitioncounter / transitioncounter.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 import sigrokdecode as srd
22
23 class Decoder(srd.Decoder):
24     api_version = 1
25     id = 'transitioncounter'
26     name = 'Transition counter'
27     longname = 'Pin transition counter'
28     desc = 'Counts rising/falling edges in the signal.'
29     longdesc = '...'
30     license = 'gplv2+'
31     inputs = ['logic']
32     outputs = ['transitioncounts']
33     probes = []
34     options = {}
35     annotations = [
36         ['TODO', 'TODO'],
37     ]
38
39     def __init__(self, **kwargs):
40         self.channels = -1
41         self.lastsample = None
42
43     def start(self, metadata):
44         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'transitioncounter')
45         self.out_ann = self.add(srd.OUTPUT_ANN, 'transitioncounter')
46
47     def report(self):
48         pass
49
50     def decode(self, ss, es, data):
51
52         for (samplenum, s) in data:
53
54             # ...
55             if self.channels == -1:
56                 self.channels = len(s)
57                 self.oldbit = [0] * self.channels
58                 self.transitions = [0] * self.channels
59                 self.rising = [0] * self.channels
60                 self.falling = [0] * self.channels
61
62             # Optimization: Skip identical samples (no transitions).
63             if self.lastsample == s:
64                 continue
65
66             # Upon the first sample, store the initial values.
67             if self.lastsample == None:
68                 self.lastsample = s
69                 for i in range(self.channels):
70                     self.oldbit[i] = self.lastsample[i]
71
72             # Iterate over all channels/probes in this sample.
73             # Count rising and falling edges for each channel.
74             for i in range(self.channels):
75                 curbit = s[i]
76                 # Optimization: Skip identical bits (no transitions).
77                 if self.oldbit[i] == curbit:
78                     continue
79                 elif (self.oldbit[i] == 0 and curbit == 1):
80                     self.rising[i] += 1
81                 elif (self.oldbit[i] == 1 and curbit == 0):
82                     self.falling[i] += 1
83                 self.oldbit[i] = curbit
84
85             # Save the current sample as 'lastsample' for the next round.
86             self.lastsample = s
87
88         # Total number of transitions = rising + falling edges.
89         for i in range(self.channels):
90             self.transitions[i] = self.rising[i] + self.falling[i]
91
92         # TODO: Which output format?
93         # TODO: How to only output something after the last chunk of data?
94         outdata = []
95         for i in range(self.channels):
96             outdata += [[self.transitions[i], self.rising[i], self.falling[i]]]
97
98         if outdata != []:
99             # self.put(0, 0, self.out_proto, out_proto)
100             self.put(0, 0, self.out_ann, [0, [str(outdata)]])
101