]> sigrok.org Git - libsigrokdecode.git/blob - decoders/transitioncounter/transitioncounter.py
srd: Properly use append() for appending to lists.
[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 # Transition counter protocol decoder
22
23 import sigrokdecode as srd
24
25 class Decoder(srd.Decoder):
26     api_version = 1
27     id = 'transitioncounter'
28     name = 'Transition counter'
29     longname = 'Pin transition counter'
30     desc = 'Counts rising/falling edges in the signal.'
31     longdesc = '...'
32     license = 'gplv2+'
33     inputs = ['logic']
34     outputs = ['transitioncounts']
35     probes = []
36     optional_probes = []
37     options = {}
38     annotations = [
39         ['TODO', 'TODO'],
40     ]
41
42     def __init__(self, **kwargs):
43         self.channels = -1
44         self.lastsample = None
45
46     def start(self, metadata):
47         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'transitioncounter')
48         self.out_ann = self.add(srd.OUTPUT_ANN, 'transitioncounter')
49
50     def report(self):
51         pass
52
53     def decode(self, ss, es, data):
54
55         for (samplenum, s) in data:
56
57             # ...
58             if self.channels == -1:
59                 self.channels = len(s)
60                 self.oldbit = [0] * self.channels
61                 self.transitions = [0] * self.channels
62                 self.rising = [0] * self.channels
63                 self.falling = [0] * self.channels
64
65             # Optimization: Skip identical samples (no transitions).
66             if self.lastsample == s:
67                 continue
68
69             # Upon the first sample, store the initial values.
70             if self.lastsample == None:
71                 self.lastsample = s
72                 for i in range(self.channels):
73                     self.oldbit[i] = self.lastsample[i]
74
75             # Iterate over all channels/probes in this sample.
76             # Count rising and falling edges for each channel.
77             for i in range(self.channels):
78                 curbit = s[i]
79                 # Optimization: Skip identical bits (no transitions).
80                 if self.oldbit[i] == curbit:
81                     continue
82                 elif (self.oldbit[i] == 0 and curbit == 1):
83                     self.rising[i] += 1
84                 elif (self.oldbit[i] == 1 and curbit == 0):
85                     self.falling[i] += 1
86                 self.oldbit[i] = curbit
87
88             # Save the current sample as 'lastsample' for the next round.
89             self.lastsample = s
90
91         # Total number of transitions = rising + falling edges.
92         for i in range(self.channels):
93             self.transitions[i] = self.rising[i] + self.falling[i]
94
95         # TODO: Which output format?
96         # TODO: How to only output something after the last chunk of data?
97         outdata = []
98         for i in range(self.channels):
99             outdata.append([self.transitions[i], self.rising[i],
100                             self.falling[i]])
101
102         if outdata != []:
103             # self.put(0, 0, self.out_proto, out_proto)
104             self.put(0, 0, self.out_ann, [0, [str(outdata)]])
105