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