]> sigrok.org Git - libsigrokdecode.git/blame - decoders/pwm/pd.py
license: remove FSF postal address from boiler plate license text
[libsigrokdecode.git] / decoders / pwm / pd.py
CommitLineData
b4332f0f
UH
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2014 Torsten Duwe <duwe@suse.de>
9658c710 5## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
b4332f0f
UH
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
4539e9ca 18## along with this program; if not, see <http://www.gnu.org/licenses/>.
b4332f0f
UH
19##
20
21import sigrokdecode as srd
22
23class Decoder(srd.Decoder):
24 api_version = 2
25 id = 'pwm'
26 name = 'PWM'
27 longname = 'Pulse-width modulation'
28 desc = 'Analog level encoded in duty cycle percentage.'
29 license = 'gplv2+'
30 inputs = ['logic']
31 outputs = ['pwm']
32 channels = (
9658c710 33 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
b4332f0f
UH
34 )
35 options = (
9658c710
SB
36 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
37 'values': ('active-low', 'active-high')},
b4332f0f
UH
38 )
39 annotations = (
9658c710 40 ('duty-cycle', 'Duty cycle'),
e4227baf
MP
41 ('period', 'Period'),
42 )
43 annotation_rows = (
44 ('duty-cycle', 'Duty cycle', (0,)),
45 ('period', 'Period', (1,)),
b4332f0f
UH
46 )
47 binary = (
48 ('raw', 'RAW file'),
49 )
50
92b7b49f 51 def __init__(self):
406af217 52 self.ss_block = self.es_block = None
9658c710
SB
53 self.first_transition = True
54 self.first_samplenum = None
55 self.start_samplenum = None
56 self.end_samplenum = None
57 self.oldpin = None
b4332f0f 58 self.num_cycles = 0
9658c710 59 self.average = 0
b4332f0f 60
e4227baf
MP
61 def metadata(self, key, value):
62 if key == srd.SRD_CONF_SAMPLERATE:
63 self.samplerate = value
64
b4332f0f 65 def start(self):
9658c710 66 self.startedge = 0 if self.options['polarity'] == 'active-low' else 1
b4332f0f 67 self.out_ann = self.register(srd.OUTPUT_ANN)
2f370328 68 self.out_binary = self.register(srd.OUTPUT_BINARY)
9658c710
SB
69 self.out_average = \
70 self.register(srd.OUTPUT_META,
71 meta=(float, 'Average', 'PWM base (cycle) frequency'))
b4332f0f
UH
72
73 def putx(self, data):
406af217 74 self.put(self.ss_block, self.es_block, self.out_ann, data)
b4332f0f 75
e4227baf
MP
76 def putp(self, period_t):
77 # Adjust granularity.
78 if period_t == 0 or period_t >= 1:
750e122d 79 period_s = '%.1f s' % (period_t)
e4227baf 80 elif period_t <= 1e-12:
750e122d 81 period_s = '%.1f fs' % (period_t * 1e15)
e4227baf 82 elif period_t <= 1e-9:
750e122d 83 period_s = '%.1f ps' % (period_t * 1e12)
e4227baf 84 elif period_t <= 1e-6:
750e122d 85 period_s = '%.1f ns' % (period_t * 1e9)
e4227baf 86 elif period_t <= 1e-3:
750e122d 87 period_s = '%.1f μs' % (period_t * 1e6)
e4227baf 88 else:
750e122d 89 period_s = '%.1f ms' % (period_t * 1e3)
e4227baf 90
406af217 91 self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]])
e4227baf 92
b4332f0f 93 def putb(self, data):
2f370328 94 self.put(self.num_cycles, self.num_cycles, self.out_binary, data)
b4332f0f
UH
95
96 def decode(self, ss, es, data):
9658c710 97
b4332f0f
UH
98 for (self.samplenum, pins) in data:
99 # Ignore identical samples early on (for performance reasons).
100 if self.oldpin == pins[0]:
101 continue
102
9658c710
SB
103 # Initialize self.oldpins with the first sample value.
104 if self.oldpin is None:
105 self.oldpin = pins[0]
106 continue
b4332f0f 107
9658c710
SB
108 if self.first_transition:
109 # First rising edge
110 if self.oldpin != self.startedge:
111 self.first_samplenum = self.samplenum
112 self.start_samplenum = self.samplenum
113 self.first_transition = False
b4332f0f 114 else:
9658c710
SB
115 if self.oldpin != self.startedge:
116 # Rising edge
117 # We are on a full cycle we can calculate
118 # the period, the duty cycle and its ratio.
119 period = self.samplenum - self.start_samplenum
120 duty = self.end_samplenum - self.start_samplenum
121 ratio = float(duty / period)
122
123 # This interval starts at this edge.
406af217 124 self.ss_block = self.start_samplenum
9658c710
SB
125 # Store the new rising edge position and the ending
126 # edge interval.
406af217 127 self.start_samplenum = self.es_block = self.samplenum
9658c710
SB
128
129 # Report the duty cycle in percent.
130 percent = float(ratio * 100)
e4227baf 131 self.putx([0, ['%f%%' % percent]])
9658c710
SB
132
133 # Report the duty cycle in the binary output.
2824e811 134 self.putb([0, bytes([int(ratio * 256)])])
9658c710 135
e4227baf
MP
136 # Report the period in units of time.
137 period_t = float(period / self.samplerate)
138 self.putp(period_t)
139
9658c710
SB
140 # Update and report the new duty cycle average.
141 self.num_cycles += 1
142 self.average += percent
406af217 143 self.put(self.first_samplenum, self.es_block, self.out_average,
9658c710
SB
144 float(self.average / self.num_cycles))
145 else:
146 # Falling edge
406af217 147 self.end_samplenum = self.ss_block = self.samplenum
b4332f0f 148
b4332f0f 149 self.oldpin = pins[0]