diff options
author | Matt Porter <mporter@konsulko.com> | 2015-03-30 23:59:38 -0400 |
---|---|---|
committer | Uwe Hermann <uwe@hermann-uwe.de> | 2015-04-02 21:14:01 +0200 |
commit | e4227bafd63472d1a0d23fa54f62a2b20e296115 (patch) | |
tree | 77ebddfbdd1099fd1c2730319821c02225806574 | |
parent | 077fa8acbcb8b585af6f5323f16221940a27a72e (diff) | |
download | libsigrokdecode-e4227bafd63472d1a0d23fa54f62a2b20e296115.tar.gz libsigrokdecode-e4227bafd63472d1a0d23fa54f62a2b20e296115.zip |
pwm: add period annotation
-rw-r--r-- | decoders/pwm/pd.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/decoders/pwm/pd.py b/decoders/pwm/pd.py index 148b34b..45e96e2 100644 --- a/decoders/pwm/pd.py +++ b/decoders/pwm/pd.py @@ -39,6 +39,11 @@ class Decoder(srd.Decoder): ) annotations = ( ('duty-cycle', 'Duty cycle'), + ('period', 'Period'), + ) + annotation_rows = ( + ('duty-cycle', 'Duty cycle', (0,)), + ('period', 'Period', (1,)), ) binary = ( ('raw', 'RAW file'), @@ -54,6 +59,10 @@ class Decoder(srd.Decoder): 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) @@ -65,6 +74,23 @@ class Decoder(srd.Decoder): def putx(self, data): self.put(self.ss, self.es, self.out_ann, data) + def putp(self, period_t): + # Adjust granularity. + if period_t == 0 or period_t >= 1: + period_s = u'%u s' % (period_t) + elif period_t <= 1e-12: + period_s = u'%.1f fs' % (period_t * 1e15) + elif period_t <= 1e-9: + period_s = u'%.1f ps' % (period_t * 1e12) + elif period_t <= 1e-6: + period_s = u'%.1f ns' % (period_t * 1e9) + elif period_t <= 1e-3: + period_s = u'%.1f μs' % (period_t * 1e6) + else: + period_s = u'%.1f ms' % (period_t * 1e3) + + self.put(self.ss, self.es, self.out_ann, [1, [period_s]]) + def putb(self, data): self.put(self.num_cycles, self.num_cycles, self.out_bin, data) @@ -103,11 +129,15 @@ class Decoder(srd.Decoder): # Report the duty cycle in percent. percent = float(ratio * 100) - self.putx([0, ["%f%%" % percent]]) + 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 |