]> sigrok.org Git - libsigrokdecode.git/blob - decoders/pwm/pd.py
Use consistent __init__() format across all PDs.
[libsigrokdecode.git] / decoders / pwm / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2014 Torsten Duwe <duwe@suse.de>
5 ## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
6 ##
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
11 ##
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ## GNU General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20 ##
21
22 import sigrokdecode as srd
23
24 class Decoder(srd.Decoder):
25     api_version = 2
26     id = 'pwm'
27     name = 'PWM'
28     longname = 'Pulse-width modulation'
29     desc = 'Analog level encoded in duty cycle percentage.'
30     license = 'gplv2+'
31     inputs = ['logic']
32     outputs = ['pwm']
33     channels = (
34         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
35     )
36     options = (
37         {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
38             'values': ('active-low', 'active-high')},
39     )
40     annotations = (
41         ('duty-cycle', 'Duty cycle'),
42         ('period', 'Period'),
43     )
44     annotation_rows = (
45          ('duty-cycle', 'Duty cycle', (0,)),
46          ('period', 'Period', (1,)),
47     )
48     binary = (
49         ('raw', 'RAW file'),
50     )
51
52     def __init__(self):
53         self.ss_block = self.es_block = None
54         self.first_transition = True
55         self.first_samplenum = None
56         self.start_samplenum = None
57         self.end_samplenum = None
58         self.oldpin = None
59         self.num_cycles = 0
60         self.average = 0
61
62     def metadata(self, key, value):
63         if key == srd.SRD_CONF_SAMPLERATE:
64             self.samplerate = value
65
66     def start(self):
67         self.startedge = 0 if self.options['polarity'] == 'active-low' else 1
68         self.out_ann = self.register(srd.OUTPUT_ANN)
69         self.out_binary = self.register(srd.OUTPUT_BINARY)
70         self.out_average = \
71             self.register(srd.OUTPUT_META,
72                           meta=(float, 'Average', 'PWM base (cycle) frequency'))
73
74     def putx(self, data):
75         self.put(self.ss_block, self.es_block, self.out_ann, data)
76
77     def putp(self, period_t):
78         # Adjust granularity.
79         if period_t == 0 or period_t >= 1:
80             period_s = '%.1f s' % (period_t)
81         elif period_t <= 1e-12:
82             period_s = '%.1f fs' % (period_t * 1e15)
83         elif period_t <= 1e-9:
84             period_s = '%.1f ps' % (period_t * 1e12)
85         elif period_t <= 1e-6:
86             period_s = '%.1f ns' % (period_t * 1e9)
87         elif period_t <= 1e-3:
88             period_s = '%.1f μs' % (period_t * 1e6)
89         else:
90             period_s = '%.1f ms' % (period_t * 1e3)
91
92         self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]])
93
94     def putb(self, data):
95         self.put(self.num_cycles, self.num_cycles, self.out_binary, data)
96
97     def decode(self, ss, es, data):
98
99         for (self.samplenum, pins) in data:
100             # Ignore identical samples early on (for performance reasons).
101             if self.oldpin == pins[0]:
102                 continue
103
104             # Initialize self.oldpins with the first sample value.
105             if self.oldpin is None:
106                 self.oldpin = pins[0]
107                 continue
108
109             if self.first_transition:
110                 # First rising edge
111                 if self.oldpin != self.startedge:
112                     self.first_samplenum = self.samplenum
113                     self.start_samplenum = self.samplenum
114                     self.first_transition = False
115             else:
116                 if self.oldpin != self.startedge:
117                     # Rising edge
118                     # We are on a full cycle we can calculate
119                     # the period, the duty cycle and its ratio.
120                     period = self.samplenum - self.start_samplenum
121                     duty = self.end_samplenum - self.start_samplenum
122                     ratio = float(duty / period)
123
124                     # This interval starts at this edge.
125                     self.ss_block = self.start_samplenum
126                     # Store the new rising edge position and the ending
127                     # edge interval.
128                     self.start_samplenum = self.es_block = self.samplenum
129
130                     # Report the duty cycle in percent.
131                     percent = float(ratio * 100)
132                     self.putx([0, ['%f%%' % percent]])
133
134                     # Report the duty cycle in the binary output.
135                     self.putb([0, bytes([int(ratio * 256)])])
136
137                     # Report the period in units of time.
138                     period_t = float(period / self.samplerate)
139                     self.putp(period_t)
140
141                     # Update and report the new duty cycle average.
142                     self.num_cycles += 1
143                     self.average += percent
144                     self.put(self.first_samplenum, self.es_block, self.out_average,
145                              float(self.average / self.num_cycles))
146                 else:
147                     # Falling edge
148                     self.end_samplenum = self.ss_block = self.samplenum
149
150             self.oldpin = pins[0]