X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fpwm%2Fpd.py;h=a2fbb0fe26df4b0419e5c0c17b3438f61bedf870;hp=5d0b4a5f2fa52df73d83dbdfbcaf01dc6c13869f;hb=0172a1661a3addc958269d4251246d2d03cf6368;hpb=06ca8df7b2994a4dcd480d11b5c30fc8085b0e7d diff --git a/decoders/pwm/pd.py b/decoders/pwm/pd.py index 5d0b4a5..a2fbb0f 100644 --- a/decoders/pwm/pd.py +++ b/decoders/pwm/pd.py @@ -61,7 +61,6 @@ class Decoder(srd.Decoder): self.samplerate = value def start(self): - self.startedge = 0 if self.options['polarity'] == 'active-low' else 1 self.out_ann = self.register(srd.OUTPUT_ANN) self.out_binary = self.register(srd.OUTPUT_BINARY) self.out_average = \ @@ -93,33 +92,29 @@ class Decoder(srd.Decoder): def decode(self): - # Wait for an "active" edge (depends on config). - self.wait({0: 'f' if self.startedge == 0 else 'r'}) + # Wait for an "active" edge (depends on config). This starts + # the first full period of the inspected signal waveform. + self.wait({0: 'f' if self.options['polarity'] == 'active-low' else 'r'}) self.first_samplenum = self.samplenum - self.start_samplenum = self.samplenum - # Handle all next edges. + # Keep getting samples for the period's middle and terminal edges. + # At the same time that last sample starts the next period. while True: - pin, = self.wait({0: 'e'}) - if pin != self.startedge: - # Non-active edge - self.end_samplenum = self.ss_block = self.samplenum - continue + # Get the next two edges. Setup some variables that get + # referenced in the calculation and in put() routines. + self.start_samplenum = self.samplenum + pins = self.wait({0: 'e'}) + self.end_samplenum = self.samplenum + pins = self.wait({0: 'e'}) + self.ss_block = self.start_samplenum + self.es_block = self.samplenum - # Active edge - # We are on a full cycle we can calculate - # the period, the duty cycle and its ratio. + # Calculate the period, the duty cycle, and its ratio. period = self.samplenum - self.start_samplenum duty = self.end_samplenum - self.start_samplenum ratio = float(duty / period) - # This interval starts at this edge. - self.ss_block = self.start_samplenum - # Store the new rising edge position and the ending - # edge interval. - self.start_samplenum = self.es_block = self.samplenum - # Report the duty cycle in percent. percent = float(ratio * 100) self.putx([0, ['%f%%' % percent]])