]> sigrok.org Git - libsigrokdecode.git/blame - decoders/pwm/pd.py
jitter, pwm: Avoid Unicode string literals
[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
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
22import sigrokdecode as srd
23
24class 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 = (
9658c710 34 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
b4332f0f
UH
35 )
36 options = (
9658c710
SB
37 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
38 'values': ('active-low', 'active-high')},
b4332f0f
UH
39 )
40 annotations = (
9658c710 41 ('duty-cycle', 'Duty cycle'),
e4227baf
MP
42 ('period', 'Period'),
43 )
44 annotation_rows = (
45 ('duty-cycle', 'Duty cycle', (0,)),
46 ('period', 'Period', (1,)),
b4332f0f
UH
47 )
48 binary = (
49 ('raw', 'RAW file'),
50 )
51
52 def __init__(self, **kwargs):
9658c710
SB
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
58 self.oldpin = None
b4332f0f 59 self.num_cycles = 0
9658c710 60 self.average = 0
b4332f0f 61
e4227baf
MP
62 def metadata(self, key, value):
63 if key == srd.SRD_CONF_SAMPLERATE:
64 self.samplerate = value
65
b4332f0f 66 def start(self):
9658c710 67 self.startedge = 0 if self.options['polarity'] == 'active-low' else 1
b4332f0f
UH
68 self.out_ann = self.register(srd.OUTPUT_ANN)
69 self.out_bin = self.register(srd.OUTPUT_BINARY)
9658c710
SB
70 self.out_average = \
71 self.register(srd.OUTPUT_META,
72 meta=(float, 'Average', 'PWM base (cycle) frequency'))
b4332f0f
UH
73
74 def putx(self, data):
75 self.put(self.ss, self.es, self.out_ann, data)
76
e4227baf
MP
77 def putp(self, period_t):
78 # Adjust granularity.
79 if period_t == 0 or period_t >= 1:
750e122d 80 period_s = '%.1f s' % (period_t)
e4227baf 81 elif period_t <= 1e-12:
750e122d 82 period_s = '%.1f fs' % (period_t * 1e15)
e4227baf 83 elif period_t <= 1e-9:
750e122d 84 period_s = '%.1f ps' % (period_t * 1e12)
e4227baf 85 elif period_t <= 1e-6:
750e122d 86 period_s = '%.1f ns' % (period_t * 1e9)
e4227baf 87 elif period_t <= 1e-3:
750e122d 88 period_s = '%.1f μs' % (period_t * 1e6)
e4227baf 89 else:
750e122d 90 period_s = '%.1f ms' % (period_t * 1e3)
e4227baf
MP
91
92 self.put(self.ss, self.es, self.out_ann, [1, [period_s]])
93
b4332f0f
UH
94 def putb(self, data):
95 self.put(self.num_cycles, self.num_cycles, self.out_bin, data)
96
97 def decode(self, ss, es, data):
9658c710 98
b4332f0f
UH
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
9658c710
SB
104 # Initialize self.oldpins with the first sample value.
105 if self.oldpin is None:
106 self.oldpin = pins[0]
107 continue
b4332f0f 108
9658c710
SB
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
b4332f0f 115 else:
9658c710
SB
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 = self.start_samplenum
126 # Store the new rising edge position and the ending
127 # edge interval.
128 self.start_samplenum = self.es = self.samplenum
129
130 # Report the duty cycle in percent.
131 percent = float(ratio * 100)
e4227baf 132 self.putx([0, ['%f%%' % percent]])
9658c710
SB
133
134 # Report the duty cycle in the binary output.
135 self.putb((0, bytes([int(ratio * 256)])))
136
e4227baf
MP
137 # Report the period in units of time.
138 period_t = float(period / self.samplerate)
139 self.putp(period_t)
140
9658c710
SB
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, self.out_average,
145 float(self.average / self.num_cycles))
146 else:
147 # Falling edge
148 self.end_samplenum = self.ss = self.samplenum
b4332f0f 149
b4332f0f 150 self.oldpin = pins[0]