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