2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Torsten Duwe <duwe@suse.de>
5 ## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
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.
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.
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
22 import sigrokdecode as srd
24 class Decoder(srd.Decoder):
28 longname = 'Pulse-width modulation'
29 desc = 'Analog level encoded in duty cycle percentage.'
34 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
37 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
38 'values': ('active-low', 'active-high')},
41 ('duty-cycle', 'Duty cycle'),
45 ('duty-cycle', 'Duty cycle', (0,)),
46 ('period', 'Period', (1,)),
52 def __init__(self, **kwargs):
53 self.ss = self.es = None
54 self.first_transition = True
55 self.first_samplenum = None
56 self.start_samplenum = None
57 self.end_samplenum = None
62 def metadata(self, key, value):
63 if key == srd.SRD_CONF_SAMPLERATE:
64 self.samplerate = value
67 self.startedge = 0 if self.options['polarity'] == 'active-low' else 1
68 self.out_ann = self.register(srd.OUTPUT_ANN)
69 self.out_bin = self.register(srd.OUTPUT_BINARY)
71 self.register(srd.OUTPUT_META,
72 meta=(float, 'Average', 'PWM base (cycle) frequency'))
75 self.put(self.ss, self.es, self.out_ann, data)
77 def putp(self, period_t):
79 if period_t == 0 or period_t >= 1:
80 period_s = u'%u s' % (period_t)
81 elif period_t <= 1e-12:
82 period_s = u'%.1f fs' % (period_t * 1e15)
83 elif period_t <= 1e-9:
84 period_s = u'%.1f ps' % (period_t * 1e12)
85 elif period_t <= 1e-6:
86 period_s = u'%.1f ns' % (period_t * 1e9)
87 elif period_t <= 1e-3:
88 period_s = u'%.1f μs' % (period_t * 1e6)
90 period_s = u'%.1f ms' % (period_t * 1e3)
92 self.put(self.ss, self.es, self.out_ann, [1, [period_s]])
95 self.put(self.num_cycles, self.num_cycles, self.out_bin, data)
97 def decode(self, ss, es, data):
99 for (self.samplenum, pins) in data:
100 # Ignore identical samples early on (for performance reasons).
101 if self.oldpin == pins[0]:
104 # Initialize self.oldpins with the first sample value.
105 if self.oldpin is None:
106 self.oldpin = pins[0]
109 if self.first_transition:
111 if self.oldpin != self.startedge:
112 self.first_samplenum = self.samplenum
113 self.start_samplenum = self.samplenum
114 self.first_transition = False
116 if self.oldpin != self.startedge:
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)
124 # This interval starts at this edge.
125 self.ss = self.start_samplenum
126 # Store the new rising edge position and the ending
128 self.start_samplenum = self.es = self.samplenum
130 # Report the duty cycle in percent.
131 percent = float(ratio * 100)
132 self.putx([0, ['%f%%' % percent]])
134 # Report the duty cycle in the binary output.
135 self.putb((0, bytes([int(ratio * 256)])))
137 # Report the period in units of time.
138 period_t = float(period / self.samplerate)
141 # Update and report the new duty cycle average.
143 self.average += percent
144 self.put(self.first_samplenum, self.es, self.out_average,
145 float(self.average / self.num_cycles))
148 self.end_samplenum = self.ss = self.samplenum
150 self.oldpin = pins[0]