From: lezerb Date: Tue, 5 Dec 2023 09:29:39 +0000 (+0100) Subject: pwm: Add frequency measurement X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=1677e1b5a6c2837bd9441dc7e228b9e67759661e;p=libsigrokdecode.git pwm: Add frequency measurement --- diff --git a/decoders/pwm/pd.py b/decoders/pwm/pd.py index 2eda02c..3a41536 100644 --- a/decoders/pwm/pd.py +++ b/decoders/pwm/pd.py @@ -43,10 +43,12 @@ class Decoder(srd.Decoder): annotations = ( ('duty-cycle', 'Duty cycle'), ('period', 'Period'), + ('frequency', 'Frequency'), ) annotation_rows = ( ('duty-cycle-vals', 'Duty cycles', (0,)), ('periods', 'Periods', (1,)), + ('frequency-vals', 'Frequencies', (2,)), ) binary = ( ('raw', 'RAW file'), @@ -90,6 +92,29 @@ class Decoder(srd.Decoder): self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]]) + def putf(self, period_t): + if period_t != 0: + frequency = 1 / period_t + + # Adjust granularity. + + if frequency >= 1e15: + frequency_s = '%.3f PHz' % (frequency / 1e15) + elif frequency >= 1e12: + frequency_s = '%.3f THz' % (frequency / 1e12) + elif frequency >= 1e9: + frequency_s = '%.3f GHz' % (frequency / 1e9) + elif frequency >= 1e6: + frequency_s = '%.3f MHz' % (frequency / 1e6) + elif frequency >= 1e3: + frequency_s = '%.3f kHz' % (frequency / 1e3) + else: + frequency_s = '%.3f Hz' % (frequency) + + self.put(self.ss_block, self.es_block, self.out_ann, [2, [frequency_s]]) + else: + self.put(self.ss_block, self.es_block, self.out_ann, [2, ["invalid"]]) + def putb(self, data): self.put(self.ss_block, self.es_block, self.out_binary, data) @@ -133,6 +158,7 @@ class Decoder(srd.Decoder): # Report the period in units of time. period_t = float(period / self.samplerate) self.putp(period_t) + self.putf(period_t) # Update and report the new duty cycle average. num_cycles += 1