From: Gerhard Sittig Date: Sun, 18 Jun 2017 10:59:31 +0000 (+0200) Subject: pwm: Rephrase edge and period detection, eliminate internal state X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=commitdiff_plain;h=0172a1661a3addc958269d4251246d2d03cf6368 pwm: Rephrase edge and period detection, eliminate internal state With the simplicity of PWM waveforms and the convenience of the v3 API, there is no need to maintain internal state. After the start of the first period was determined based on the input signal's polarity, any period is defined by just the next two edges, and the next period is immediately to follow. --- 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]])