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