]>
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 | |
18 | ## along with this program; if not, write to the Free Software | |
19 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | ## | |
21 | ||
22 | import sigrokdecode as srd | |
23 | ||
24 | class Decoder(srd.Decoder): | |
25 | api_version = 2 | |
26 | id = 'pwm' | |
27 | name = 'PWM' | |
28 | longname = 'Pulse-width modulation' | |
29 | desc = 'Analog level encoded in duty cycle percentage.' | |
30 | license = 'gplv2+' | |
31 | inputs = ['logic'] | |
32 | outputs = ['pwm'] | |
33 | channels = ( | |
9658c710 | 34 | {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, |
b4332f0f UH |
35 | ) |
36 | options = ( | |
9658c710 SB |
37 | {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high', |
38 | 'values': ('active-low', 'active-high')}, | |
b4332f0f UH |
39 | ) |
40 | annotations = ( | |
9658c710 | 41 | ('duty-cycle', 'Duty cycle'), |
b4332f0f UH |
42 | ) |
43 | binary = ( | |
44 | ('raw', 'RAW file'), | |
45 | ) | |
46 | ||
47 | def __init__(self, **kwargs): | |
9658c710 SB |
48 | self.ss = self.es = None |
49 | self.first_transition = True | |
50 | self.first_samplenum = None | |
51 | self.start_samplenum = None | |
52 | self.end_samplenum = None | |
53 | self.oldpin = None | |
b4332f0f | 54 | self.num_cycles = 0 |
9658c710 | 55 | self.average = 0 |
b4332f0f UH |
56 | |
57 | def start(self): | |
9658c710 | 58 | self.startedge = 0 if self.options['polarity'] == 'active-low' else 1 |
b4332f0f UH |
59 | self.out_ann = self.register(srd.OUTPUT_ANN) |
60 | self.out_bin = 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): | |
66 | self.put(self.ss, self.es, self.out_ann, data) | |
67 | ||
b4332f0f UH |
68 | def putb(self, data): |
69 | self.put(self.num_cycles, self.num_cycles, self.out_bin, data) | |
70 | ||
71 | def decode(self, ss, es, data): | |
9658c710 | 72 | |
b4332f0f UH |
73 | for (self.samplenum, pins) in data: |
74 | # Ignore identical samples early on (for performance reasons). | |
75 | if self.oldpin == pins[0]: | |
76 | continue | |
77 | ||
9658c710 SB |
78 | # Initialize self.oldpins with the first sample value. |
79 | if self.oldpin is None: | |
80 | self.oldpin = pins[0] | |
81 | continue | |
b4332f0f | 82 | |
9658c710 SB |
83 | if self.first_transition: |
84 | # First rising edge | |
85 | if self.oldpin != self.startedge: | |
86 | self.first_samplenum = self.samplenum | |
87 | self.start_samplenum = self.samplenum | |
88 | self.first_transition = False | |
b4332f0f | 89 | else: |
9658c710 SB |
90 | if self.oldpin != self.startedge: |
91 | # Rising edge | |
92 | # We are on a full cycle we can calculate | |
93 | # the period, the duty cycle and its ratio. | |
94 | period = self.samplenum - self.start_samplenum | |
95 | duty = self.end_samplenum - self.start_samplenum | |
96 | ratio = float(duty / period) | |
97 | ||
98 | # This interval starts at this edge. | |
99 | self.ss = self.start_samplenum | |
100 | # Store the new rising edge position and the ending | |
101 | # edge interval. | |
102 | self.start_samplenum = self.es = self.samplenum | |
103 | ||
104 | # Report the duty cycle in percent. | |
105 | percent = float(ratio * 100) | |
106 | self.putx([0, ["%f%%" % percent]]) | |
107 | ||
108 | # Report the duty cycle in the binary output. | |
109 | self.putb((0, bytes([int(ratio * 256)]))) | |
110 | ||
111 | # Update and report the new duty cycle average. | |
112 | self.num_cycles += 1 | |
113 | self.average += percent | |
114 | self.put(self.first_samplenum, self.es, self.out_average, | |
115 | float(self.average / self.num_cycles)) | |
116 | else: | |
117 | # Falling edge | |
118 | self.end_samplenum = self.ss = self.samplenum | |
b4332f0f | 119 | |
b4332f0f | 120 | self.oldpin = pins[0] |