]> sigrok.org Git - libsigrokdecode.git/blob - decoders/transitioncounter.py
Update transitioncounter.py to the streaming PD API.
[libsigrokdecode.git] / decoders / 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 import sigrok
21
22 lastsample = None
23 oldbit = None
24 transitions = None
25 rising = None
26 falling = None
27
28
29 def decode(sampledata):
30         """Counts the low->high and high->low transitions in the specified
31            channel(s) of the signal."""
32         global lastsample
33         global oldbit, transitions, rising, falling
34
35         # TODO: Don't hardcode the number of channels.
36         channels = 8    
37
38         # FIXME: Get the data in the correct format in the first place.
39         s = ord(sampledata['data'])
40
41         if lastsample == None:
42                 oldbit = [0] * channels
43                 transitions = [0] * channels
44                 rising = [0] * channels
45                 falling = [0] * channels
46
47                 # Initial values.
48                 lastsample = s
49                 for i in range(channels):
50                         oldbit[i] = (lastsample & (1 << i)) >> i
51                 
52         # TODO: Handle LAs with more/less than 8 channels.
53         # Optimization: Skip identical bytes (no transitions).
54         if lastsample != s:
55                 for i in range(channels):
56                         curbit = (s & (1 << i)) >> i
57                         # Optimization: Skip identical bits (no transitions).
58                         if oldbit[i] == curbit:
59                                 continue
60                         elif (oldbit[i] == 0 and curbit == 1):
61                                 rising[i] += 1
62                         elif (oldbit[i] == 1 and curbit == 0):
63                                 falling[i] += 1
64                         oldbit[i] = curbit
65
66                 # Total number of transitions is the sum of rising and falling edges.
67                 for i in range(channels):
68                         transitions[i] = rising[i] + falling[i]
69                 
70                 lastsample = s
71                 print(transitions)
72
73         sigrok.put(sampledata)
74
75 register = {
76         'id': 'transitioncounter',
77         'name': 'Transition counter',
78         'longname': '...',
79         'desc': 'Counts rising/falling edges in the signal.',
80         'longdesc': '...',
81         'author': 'Uwe Hermann',
82         'email': 'uwe@hermann-uwe.de',
83         'license': 'gplv2+',
84         'in': ['logic'],
85         'out': ['transitioncounts'],
86         'probes': [
87                 # All probes.
88         ],
89         'options': {
90                 # No options so far.
91         },
92         # 'start': start,
93         # 'report': report,
94 }
95
96 # Use psyco (if available) as it results in huge performance improvements.
97 try:
98         import psyco
99         psyco.bind(decode)
100 except ImportError:
101         pass
102