]>
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 | ||
15a8a055 GS |
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'] | |
6cbba91f | 34 | outputs = [] |
d6d8a8a4 | 35 | tags = ['Encoding'] |
b4332f0f | 36 | channels = ( |
9658c710 | 37 | {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, |
b4332f0f UH |
38 | ) |
39 | options = ( | |
9658c710 SB |
40 | {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high', |
41 | 'values': ('active-low', 'active-high')}, | |
b4332f0f UH |
42 | ) |
43 | annotations = ( | |
9658c710 | 44 | ('duty-cycle', 'Duty cycle'), |
e4227baf MP |
45 | ('period', 'Period'), |
46 | ) | |
47 | annotation_rows = ( | |
e144452b UH |
48 | ('duty-cycle-vals', 'Duty cycles', (0,)), |
49 | ('periods', 'Periods', (1,)), | |
b4332f0f UH |
50 | ) |
51 | binary = ( | |
52 | ('raw', 'RAW file'), | |
53 | ) | |
54 | ||
92b7b49f | 55 | def __init__(self): |
10aeb8ea GS |
56 | self.reset() |
57 | ||
58 | def reset(self): | |
15a8a055 | 59 | self.samplerate = None |
406af217 | 60 | self.ss_block = self.es_block = None |
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): |
b4332f0f | 67 | self.out_ann = self.register(srd.OUTPUT_ANN) |
2f370328 | 68 | self.out_binary = self.register(srd.OUTPUT_BINARY) |
9658c710 SB |
69 | self.out_average = \ |
70 | self.register(srd.OUTPUT_META, | |
71 | meta=(float, 'Average', 'PWM base (cycle) frequency')) | |
b4332f0f UH |
72 | |
73 | def putx(self, data): | |
406af217 | 74 | self.put(self.ss_block, self.es_block, self.out_ann, data) |
b4332f0f | 75 | |
e4227baf MP |
76 | def putp(self, period_t): |
77 | # Adjust granularity. | |
78 | if period_t == 0 or period_t >= 1: | |
750e122d | 79 | period_s = '%.1f s' % (period_t) |
e4227baf | 80 | elif period_t <= 1e-12: |
750e122d | 81 | period_s = '%.1f fs' % (period_t * 1e15) |
e4227baf | 82 | elif period_t <= 1e-9: |
750e122d | 83 | period_s = '%.1f ps' % (period_t * 1e12) |
e4227baf | 84 | elif period_t <= 1e-6: |
750e122d | 85 | period_s = '%.1f ns' % (period_t * 1e9) |
e4227baf | 86 | elif period_t <= 1e-3: |
750e122d | 87 | period_s = '%.1f μs' % (period_t * 1e6) |
e4227baf | 88 | else: |
750e122d | 89 | period_s = '%.1f ms' % (period_t * 1e3) |
e4227baf | 90 | |
406af217 | 91 | self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]]) |
e4227baf | 92 | |
b4332f0f | 93 | def putb(self, data): |
73dc4831 | 94 | self.put(self.ss_block, self.es_block, self.out_binary, data) |
b4332f0f | 95 | |
bcf6548b | 96 | def decode(self): |
15a8a055 GS |
97 | if not self.samplerate: |
98 | raise SamplerateError('Cannot decode without samplerate.') | |
99 | ||
73dc4831 | 100 | num_cycles = 0 |
eff37627 | 101 | average = 0 |
bcf6548b | 102 | |
0172a166 GS |
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'}) | |
bcf6548b | 106 | self.first_samplenum = self.samplenum |
bcf6548b | 107 | |
0172a166 GS |
108 | # Keep getting samples for the period's middle and terminal edges. |
109 | # At the same time that last sample starts the next period. | |
bcf6548b | 110 | while True: |
bcf6548b | 111 | |
0172a166 GS |
112 | # Get the next two edges. Setup some variables that get |
113 | # referenced in the calculation and in put() routines. | |
eff37627 | 114 | start_samplenum = self.samplenum |
0e48cad6 | 115 | self.wait({0: 'e'}) |
eff37627 | 116 | end_samplenum = self.samplenum |
0e48cad6 | 117 | self.wait({0: 'e'}) |
eff37627 | 118 | self.ss_block = start_samplenum |
0172a166 | 119 | self.es_block = self.samplenum |
06ca8df7 | 120 | |
0172a166 | 121 | # Calculate the period, the duty cycle, and its ratio. |
eff37627 GS |
122 | period = self.samplenum - start_samplenum |
123 | duty = end_samplenum - start_samplenum | |
06ca8df7 UH |
124 | ratio = float(duty / period) |
125 | ||
06ca8df7 UH |
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. | |
73dc4831 | 138 | num_cycles += 1 |
eff37627 | 139 | average += percent |
06ca8df7 | 140 | self.put(self.first_samplenum, self.es_block, self.out_average, |
73dc4831 | 141 | float(average / num_cycles)) |