]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/pwm/pd.py
decoders: Add/update tags for each PD.
[libsigrokdecode.git] / decoders / pwm / pd.py
... / ...
CommitLineData
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, see <http://www.gnu.org/licenses/>.
19##
20
21import sigrokdecode as srd
22
23class SamplerateError(Exception):
24 pass
25
26class Decoder(srd.Decoder):
27 api_version = 3
28 id = 'pwm'
29 name = 'PWM'
30 longname = 'Pulse-width modulation'
31 desc = 'Analog level encoded in duty cycle percentage.'
32 license = 'gplv2+'
33 inputs = ['logic']
34 outputs = ['pwm']
35 tags = ['Encoding']
36 channels = (
37 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
38 )
39 options = (
40 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
41 'values': ('active-low', 'active-high')},
42 )
43 annotations = (
44 ('duty-cycle', 'Duty cycle'),
45 ('period', 'Period'),
46 )
47 annotation_rows = (
48 ('duty-cycle', 'Duty cycle', (0,)),
49 ('period', 'Period', (1,)),
50 )
51 binary = (
52 ('raw', 'RAW file'),
53 )
54
55 def __init__(self):
56 self.reset()
57
58 def reset(self):
59 self.samplerate = None
60 self.ss_block = self.es_block = None
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.out_ann = self.register(srd.OUTPUT_ANN)
68 self.out_binary = self.register(srd.OUTPUT_BINARY)
69 self.out_average = \
70 self.register(srd.OUTPUT_META,
71 meta=(float, 'Average', 'PWM base (cycle) frequency'))
72
73 def putx(self, data):
74 self.put(self.ss_block, self.es_block, self.out_ann, data)
75
76 def putp(self, period_t):
77 # Adjust granularity.
78 if period_t == 0 or period_t >= 1:
79 period_s = '%.1f s' % (period_t)
80 elif period_t <= 1e-12:
81 period_s = '%.1f fs' % (period_t * 1e15)
82 elif period_t <= 1e-9:
83 period_s = '%.1f ps' % (period_t * 1e12)
84 elif period_t <= 1e-6:
85 period_s = '%.1f ns' % (period_t * 1e9)
86 elif period_t <= 1e-3:
87 period_s = '%.1f μs' % (period_t * 1e6)
88 else:
89 period_s = '%.1f ms' % (period_t * 1e3)
90
91 self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]])
92
93 def putb(self, data):
94 self.put(self.ss_block, self.es_block, self.out_binary, data)
95
96 def decode(self):
97 if not self.samplerate:
98 raise SamplerateError('Cannot decode without samplerate.')
99
100 num_cycles = 0
101 average = 0
102
103 # Wait for an "active" edge (depends on config). This starts
104 # the first full period of the inspected signal waveform.
105 self.wait({0: 'f' if self.options['polarity'] == 'active-low' else 'r'})
106 self.first_samplenum = self.samplenum
107
108 # Keep getting samples for the period's middle and terminal edges.
109 # At the same time that last sample starts the next period.
110 while True:
111
112 # Get the next two edges. Setup some variables that get
113 # referenced in the calculation and in put() routines.
114 start_samplenum = self.samplenum
115 self.wait({0: 'e'})
116 end_samplenum = self.samplenum
117 self.wait({0: 'e'})
118 self.ss_block = start_samplenum
119 self.es_block = self.samplenum
120
121 # Calculate the period, the duty cycle, and its ratio.
122 period = self.samplenum - start_samplenum
123 duty = end_samplenum - start_samplenum
124 ratio = float(duty / period)
125
126 # Report the duty cycle in percent.
127 percent = float(ratio * 100)
128 self.putx([0, ['%f%%' % percent]])
129
130 # Report the duty cycle in the binary output.
131 self.putb([0, bytes([int(ratio * 256)])])
132
133 # Report the period in units of time.
134 period_t = float(period / self.samplerate)
135 self.putp(period_t)
136
137 # Update and report the new duty cycle average.
138 num_cycles += 1
139 average += percent
140 self.put(self.first_samplenum, self.es_block, self.out_average,
141 float(average / num_cycles))