]> sigrok.org Git - libsigrokdecode.git/blob - decoders/pwm/pd.py
pwm: Rephrase edge and period detection, eliminate internal state
[libsigrokdecode.git] / decoders / pwm / pd.py
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
21 import sigrokdecode as srd
22
23 class Decoder(srd.Decoder):
24     api_version = 3
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 = (
33         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
34     )
35     options = (
36         {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
37             'values': ('active-low', 'active-high')},
38     )
39     annotations = (
40         ('duty-cycle', 'Duty cycle'),
41         ('period', 'Period'),
42     )
43     annotation_rows = (
44          ('duty-cycle', 'Duty cycle', (0,)),
45          ('period', 'Period', (1,)),
46     )
47     binary = (
48         ('raw', 'RAW file'),
49     )
50
51     def __init__(self):
52         self.ss_block = self.es_block = None
53         self.first_samplenum = None
54         self.start_samplenum = None
55         self.end_samplenum = None
56         self.num_cycles = 0
57         self.average = 0
58
59     def metadata(self, key, value):
60         if key == srd.SRD_CONF_SAMPLERATE:
61             self.samplerate = value
62
63     def start(self):
64         self.out_ann = self.register(srd.OUTPUT_ANN)
65         self.out_binary = self.register(srd.OUTPUT_BINARY)
66         self.out_average = \
67             self.register(srd.OUTPUT_META,
68                           meta=(float, 'Average', 'PWM base (cycle) frequency'))
69
70     def putx(self, data):
71         self.put(self.ss_block, self.es_block, self.out_ann, data)
72
73     def putp(self, period_t):
74         # Adjust granularity.
75         if period_t == 0 or period_t >= 1:
76             period_s = '%.1f s' % (period_t)
77         elif period_t <= 1e-12:
78             period_s = '%.1f fs' % (period_t * 1e15)
79         elif period_t <= 1e-9:
80             period_s = '%.1f ps' % (period_t * 1e12)
81         elif period_t <= 1e-6:
82             period_s = '%.1f ns' % (period_t * 1e9)
83         elif period_t <= 1e-3:
84             period_s = '%.1f μs' % (period_t * 1e6)
85         else:
86             period_s = '%.1f ms' % (period_t * 1e3)
87
88         self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]])
89
90     def putb(self, data):
91         self.put(self.num_cycles, self.num_cycles, self.out_binary, data)
92
93     def decode(self):
94
95         # Wait for an "active" edge (depends on config). This starts
96         # the first full period of the inspected signal waveform.
97         self.wait({0: 'f' if self.options['polarity'] == 'active-low' else 'r'})
98         self.first_samplenum = self.samplenum
99
100         # Keep getting samples for the period's middle and terminal edges.
101         # At the same time that last sample starts the next period.
102         while True:
103
104             # Get the next two edges. Setup some variables that get
105             # referenced in the calculation and in put() routines.
106             self.start_samplenum = self.samplenum
107             pins = self.wait({0: 'e'})
108             self.end_samplenum = self.samplenum
109             pins = self.wait({0: 'e'})
110             self.ss_block = self.start_samplenum
111             self.es_block = self.samplenum
112
113             # Calculate the period, the duty cycle, and its ratio.
114             period = self.samplenum - self.start_samplenum
115             duty = self.end_samplenum - self.start_samplenum
116             ratio = float(duty / period)
117
118             # Report the duty cycle in percent.
119             percent = float(ratio * 100)
120             self.putx([0, ['%f%%' % percent]])
121
122             # Report the duty cycle in the binary output.
123             self.putb([0, bytes([int(ratio * 256)])])
124
125             # Report the period in units of time.
126             period_t = float(period / self.samplerate)
127             self.putp(period_t)
128
129             # Update and report the new duty cycle average.
130             self.num_cycles += 1
131             self.average += percent
132             self.put(self.first_samplenum, self.es_block, self.out_average,
133                      float(self.average / self.num_cycles))