]>
Commit | Line | Data |
---|---|---|
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 | ||
21 | import sigrokdecode as srd | |
22 | ||
23 | class Decoder(srd.Decoder): | |
bcf6548b | 24 | api_version = 3 |
b4332f0f UH |
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 |
b4332f0f | 53 | |
e4227baf MP |
54 | def metadata(self, key, value): |
55 | if key == srd.SRD_CONF_SAMPLERATE: | |
56 | self.samplerate = value | |
57 | ||
b4332f0f | 58 | def start(self): |
b4332f0f | 59 | self.out_ann = self.register(srd.OUTPUT_ANN) |
2f370328 | 60 | self.out_binary = self.register(srd.OUTPUT_BINARY) |
9658c710 SB |
61 | self.out_average = \ |
62 | self.register(srd.OUTPUT_META, | |
63 | meta=(float, 'Average', 'PWM base (cycle) frequency')) | |
b4332f0f UH |
64 | |
65 | def putx(self, data): | |
406af217 | 66 | self.put(self.ss_block, self.es_block, self.out_ann, data) |
b4332f0f | 67 | |
e4227baf MP |
68 | def putp(self, period_t): |
69 | # Adjust granularity. | |
70 | if period_t == 0 or period_t >= 1: | |
750e122d | 71 | period_s = '%.1f s' % (period_t) |
e4227baf | 72 | elif period_t <= 1e-12: |
750e122d | 73 | period_s = '%.1f fs' % (period_t * 1e15) |
e4227baf | 74 | elif period_t <= 1e-9: |
750e122d | 75 | period_s = '%.1f ps' % (period_t * 1e12) |
e4227baf | 76 | elif period_t <= 1e-6: |
750e122d | 77 | period_s = '%.1f ns' % (period_t * 1e9) |
e4227baf | 78 | elif period_t <= 1e-3: |
750e122d | 79 | period_s = '%.1f μs' % (period_t * 1e6) |
e4227baf | 80 | else: |
750e122d | 81 | period_s = '%.1f ms' % (period_t * 1e3) |
e4227baf | 82 | |
406af217 | 83 | self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]]) |
e4227baf | 84 | |
b4332f0f | 85 | def putb(self, data): |
73dc4831 | 86 | self.put(self.ss_block, self.es_block, self.out_binary, data) |
b4332f0f | 87 | |
bcf6548b | 88 | def decode(self): |
73dc4831 | 89 | num_cycles = 0 |
eff37627 | 90 | average = 0 |
bcf6548b | 91 | |
0172a166 GS |
92 | # Wait for an "active" edge (depends on config). This starts |
93 | # the first full period of the inspected signal waveform. | |
94 | self.wait({0: 'f' if self.options['polarity'] == 'active-low' else 'r'}) | |
bcf6548b | 95 | self.first_samplenum = self.samplenum |
bcf6548b | 96 | |
0172a166 GS |
97 | # Keep getting samples for the period's middle and terminal edges. |
98 | # At the same time that last sample starts the next period. | |
bcf6548b | 99 | while True: |
bcf6548b | 100 | |
0172a166 GS |
101 | # Get the next two edges. Setup some variables that get |
102 | # referenced in the calculation and in put() routines. | |
eff37627 | 103 | start_samplenum = self.samplenum |
0e48cad6 | 104 | self.wait({0: 'e'}) |
eff37627 | 105 | end_samplenum = self.samplenum |
0e48cad6 | 106 | self.wait({0: 'e'}) |
eff37627 | 107 | self.ss_block = start_samplenum |
0172a166 | 108 | self.es_block = self.samplenum |
06ca8df7 | 109 | |
0172a166 | 110 | # Calculate the period, the duty cycle, and its ratio. |
eff37627 GS |
111 | period = self.samplenum - start_samplenum |
112 | duty = end_samplenum - start_samplenum | |
06ca8df7 UH |
113 | ratio = float(duty / period) |
114 | ||
06ca8df7 UH |
115 | # Report the duty cycle in percent. |
116 | percent = float(ratio * 100) | |
117 | self.putx([0, ['%f%%' % percent]]) | |
118 | ||
119 | # Report the duty cycle in the binary output. | |
120 | self.putb([0, bytes([int(ratio * 256)])]) | |
121 | ||
122 | # Report the period in units of time. | |
123 | period_t = float(period / self.samplerate) | |
124 | self.putp(period_t) | |
125 | ||
126 | # Update and report the new duty cycle average. | |
73dc4831 | 127 | num_cycles += 1 |
eff37627 | 128 | average += percent |
06ca8df7 | 129 | self.put(self.first_samplenum, self.es_block, self.out_average, |
73dc4831 | 130 | float(average / num_cycles)) |