X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fpwm%2Fpd.py;h=a2fbb0fe26df4b0419e5c0c17b3438f61bedf870;hp=999b49612a9349dd13d8d5af84ad9e970ecda72b;hb=0172a1661a3addc958269d4251246d2d03cf6368;hpb=b4332f0fdd7706d9caf39a5dcd8a1b5b06a626ab diff --git a/decoders/pwm/pd.py b/decoders/pwm/pd.py index 999b496..a2fbb0f 100644 --- a/decoders/pwm/pd.py +++ b/decoders/pwm/pd.py @@ -2,6 +2,7 @@ ## This file is part of the libsigrokdecode project. ## ## Copyright (C) 2014 Torsten Duwe +## Copyright (C) 2014 Sebastien Bourdelin ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -14,14 +15,13 @@ ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## along with this program; if not, see . ## import sigrokdecode as srd class Decoder(srd.Decoder): - api_version = 2 + api_version = 3 id = 'pwm' name = 'PWM' longname = 'Pulse-width modulation' @@ -30,68 +30,104 @@ class Decoder(srd.Decoder): inputs = ['logic'] outputs = ['pwm'] channels = ( - {'id': 'pwm', 'name': 'PWM in', 'desc': 'Modulation pulses'}, + {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, ) options = ( - {'id': 'new_cycle_edge', 'desc': 'New cycle on which edge', - 'default': 'rising', 'values': ('rising', 'falling')}, + {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high', + 'values': ('active-low', 'active-high')}, ) annotations = ( - ('value', 'PWM value'), + ('duty-cycle', 'Duty cycle'), + ('period', 'Period'), + ) + annotation_rows = ( + ('duty-cycle', 'Duty cycle', (0,)), + ('period', 'Period', (1,)), ) binary = ( ('raw', 'RAW file'), ) - def __init__(self, **kwargs): - self.ss = self.es = -1 - self.high = 1 - self.low = 1 - self.lastedge = 0 - self.oldpin = 0 - self.startedge = 0 + def __init__(self): + 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.out_python = self.register(srd.OUTPUT_PYTHON) self.out_ann = self.register(srd.OUTPUT_ANN) - self.out_bin = self.register(srd.OUTPUT_BINARY) - self.out_freq = self.register(srd.OUTPUT_META, - meta=(int, 'Frequency', 'PWM base (cycle) frequency')) - self.startedge = 0 - if self.options['new_cycle_edge'] == 'falling': - self.startedge = 1 + self.out_binary = self.register(srd.OUTPUT_BINARY) + self.out_average = \ + self.register(srd.OUTPUT_META, + meta=(float, 'Average', 'PWM base (cycle) frequency')) def putx(self, data): - self.put(self.ss, self.es, self.out_ann, data) + self.put(self.ss_block, self.es_block, self.out_ann, data) + + def putp(self, period_t): + # Adjust granularity. + if period_t == 0 or period_t >= 1: + period_s = '%.1f s' % (period_t) + elif period_t <= 1e-12: + period_s = '%.1f fs' % (period_t * 1e15) + elif period_t <= 1e-9: + period_s = '%.1f ps' % (period_t * 1e12) + elif period_t <= 1e-6: + period_s = '%.1f ns' % (period_t * 1e9) + elif period_t <= 1e-3: + period_s = '%.1f μs' % (period_t * 1e6) + else: + period_s = '%.1f ms' % (period_t * 1e3) - def putp(self, data): - self.put(self.ss, self.es, self.out_python, data) + 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_bin, data) - - def decode(self, ss, es, data): - for (self.samplenum, pins) in data: - # Ignore identical samples early on (for performance reasons). - if self.oldpin == pins[0]: - continue - - if self.oldpin == 0: # Rising edge. - self.low = self.samplenum - self.lastedge - else: - self.high = self.samplenum - self.lastedge - - if self.oldpin == self.startedge: - self.es = self.samplenum # This interval ends at this edge. - if self.ss >= 0: # Have we completed a hi-lo sequence? - self.putx([0, ["%d%%" % ((100 * self.high) // (self.high + self.low))]]) - self.putb((0, bytes([(256 * self.high) // (self.high + self.low)]))) - self.num_cycles += 1 - else: - # Mid-interval. - # This interval started at the previous edge. - self.ss = self.lastedge - - self.lastedge = self.samplenum - self.oldpin = pins[0] + self.put(self.num_cycles, self.num_cycles, self.out_binary, data) + + def decode(self): + + # 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 + + # Keep getting samples for the period's middle and terminal edges. + # At the same time that last sample starts the next period. + while True: + + # 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 + + # 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) + + # 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))