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, see <http://www.gnu.org/licenses/>.
21 import sigrokdecode as srd
23 class Decoder(srd.Decoder):
27 longname = 'Pulse-width modulation'
28 desc = 'Analog level encoded in duty cycle percentage.'
33 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
36 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
37 'values': ('active-low', 'active-high')},
40 ('duty-cycle', 'Duty cycle'),
44 ('duty-cycle', 'Duty cycle', (0,)),
45 ('period', 'Period', (1,)),
52 self.ss_block = self.es_block = None
53 self.first_samplenum = None
54 self.start_samplenum = None
55 self.end_samplenum = None
59 def metadata(self, key, value):
60 if key == srd.SRD_CONF_SAMPLERATE:
61 self.samplerate = value
64 self.startedge = 0 if self.options['polarity'] == 'active-low' else 1
65 self.out_ann = self.register(srd.OUTPUT_ANN)
66 self.out_binary = self.register(srd.OUTPUT_BINARY)
68 self.register(srd.OUTPUT_META,
69 meta=(float, 'Average', 'PWM base (cycle) frequency'))
72 self.put(self.ss_block, self.es_block, self.out_ann, data)
74 def putp(self, period_t):
76 if period_t == 0 or period_t >= 1:
77 period_s = '%.1f s' % (period_t)
78 elif period_t <= 1e-12:
79 period_s = '%.1f fs' % (period_t * 1e15)
80 elif period_t <= 1e-9:
81 period_s = '%.1f ps' % (period_t * 1e12)
82 elif period_t <= 1e-6:
83 period_s = '%.1f ns' % (period_t * 1e9)
84 elif period_t <= 1e-3:
85 period_s = '%.1f μs' % (period_t * 1e6)
87 period_s = '%.1f ms' % (period_t * 1e3)
89 self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]])
92 self.put(self.num_cycles, self.num_cycles, self.out_binary, data)
96 # Wait for an "active" edge (depends on config).
97 self.wait({0: 'f' if self.startedge == 0 else 'r'})
98 self.first_samplenum = self.samplenum
99 self.start_samplenum = self.samplenum
101 # Handle all next edges.
103 pin, = self.wait({0: 'e'})
105 if pin != self.startedge:
107 self.end_samplenum = self.ss_block = self.samplenum
111 # We are on a full cycle we can calculate
112 # the period, the duty cycle and its ratio.
113 period = self.samplenum - self.start_samplenum
114 duty = self.end_samplenum - self.start_samplenum
115 ratio = float(duty / period)
117 # This interval starts at this edge.
118 self.ss_block = self.start_samplenum
119 # Store the new rising edge position and the ending
121 self.start_samplenum = self.es_block = self.samplenum
123 # Report the duty cycle in percent.
124 percent = float(ratio * 100)
125 self.putx([0, ['%f%%' % percent]])
127 # Report the duty cycle in the binary output.
128 self.putb([0, bytes([int(ratio * 256)])])
130 # Report the period in units of time.
131 period_t = float(period / self.samplerate)
134 # Update and report the new duty cycle average.
136 self.average += percent
137 self.put(self.first_samplenum, self.es_block, self.out_average,
138 float(self.average / self.num_cycles))