X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fpwm%2Fpd.py;h=d8626ee00824d427a53672e76b298e84f9233191;hp=0b7be458d881302e368bd778af0e5c4b2252ad8a;hb=6cbba91f23b9f9ace75b4722c9c0776b9211008d;hpb=bcf6548ba8b604fa56a9cf4f7f985e9f67dd5bb6 diff --git a/decoders/pwm/pd.py b/decoders/pwm/pd.py index 0b7be45..d8626ee 100644 --- a/decoders/pwm/pd.py +++ b/decoders/pwm/pd.py @@ -20,6 +20,9 @@ import sigrokdecode as srd +class SamplerateError(Exception): + pass + class Decoder(srd.Decoder): api_version = 3 id = 'pwm' @@ -28,7 +31,8 @@ class Decoder(srd.Decoder): desc = 'Analog level encoded in duty cycle percentage.' license = 'gplv2+' inputs = ['logic'] - outputs = ['pwm'] + outputs = [] + tags = ['Encoding'] channels = ( {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, ) @@ -49,19 +53,17 @@ class Decoder(srd.Decoder): ) def __init__(self): + self.reset() + + def reset(self): + self.samplerate = None self.ss_block = self.es_block = None - self.first_samplenum = None - self.start_samplenum = None - self.end_samplenum = None - self.num_cycles = 0 - self.average = 0 def metadata(self, key, value): if key == srd.SRD_CONF_SAMPLERATE: 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 = \ @@ -89,51 +91,51 @@ class Decoder(srd.Decoder): self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]]) def putb(self, data): - self.put(self.num_cycles, self.num_cycles, self.out_binary, data) + self.put(self.ss_block, self.es_block, self.out_binary, data) def decode(self): + if not self.samplerate: + raise SamplerateError('Cannot decode without samplerate.') - # Get the first rising edge. - pin, = self.wait({0: 'e'}) - if pin != self.startedge: - pin, = self.wait({0: 'e'}) + num_cycles = 0 + average = 0 + + # 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: - # Rising edge - # We are on a full cycle we can 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]]) - - # Report the duty cycle in the binary output. - self.putb([0, bytes([int(ratio * 256)])]) - - # Report the period in units of time. - period_t = float(period / self.samplerate) - self.putp(period_t) - - # Update and report the new duty cycle average. - self.num_cycles += 1 - self.average += percent - self.put(self.first_samplenum, self.es_block, self.out_average, - float(self.average / self.num_cycles)) - else: - # Falling edge - self.end_samplenum = self.ss_block = self.samplenum + + # Get the next two edges. Setup some variables that get + # referenced in the calculation and in put() routines. + start_samplenum = self.samplenum + self.wait({0: 'e'}) + end_samplenum = self.samplenum + self.wait({0: 'e'}) + self.ss_block = start_samplenum + self.es_block = self.samplenum + + # Calculate the period, the duty cycle, and its ratio. + period = self.samplenum - start_samplenum + duty = end_samplenum - start_samplenum + ratio = float(duty / period) + + # Report the duty cycle in percent. + percent = float(ratio * 100) + self.putx([0, ['%f%%' % percent]]) + + # Report the duty cycle in the binary output. + self.putb([0, bytes([int(ratio * 256)])]) + + # Report the period in units of time. + period_t = float(period / self.samplerate) + self.putp(period_t) + + # Update and report the new duty cycle average. + num_cycles += 1 + average += percent + self.put(self.first_samplenum, self.es_block, self.out_average, + float(average / num_cycles))