]> sigrok.org Git - libsigrokdecode.git/commitdiff
pwm: Add frequency measurement
authorlezerb <redacted>
Tue, 5 Dec 2023 09:29:39 +0000 (10:29 +0100)
committerSoeren Apel <redacted>
Tue, 1 Oct 2024 11:35:39 +0000 (13:35 +0200)
decoders/pwm/pd.py

index 2eda02ca42a1c174b48aa66e19bf74d7f36137b5..3a4153698769645a2b8eb3485316e110f63d2bf3 100644 (file)
@@ -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