]> sigrok.org Git - libsigrokdecode.git/commitdiff
srd/i2s: Added sample-rate and bit-depth to report
authorJoel Holdsworth <redacted>
Fri, 6 Apr 2012 21:01:30 +0000 (22:01 +0100)
committerJoel Holdsworth <redacted>
Fri, 6 Apr 2012 21:29:49 +0000 (22:29 +0100)
decoders/i2s/i2s.py

index 23f625d3a54f6e2fded6aa56307e83cf972cd469..d627ebb6867e85b3e5820383e02b206b0b2614de 100644 (file)
@@ -50,16 +50,29 @@ class Decoder(srd.Decoder):
         self.bitcount = 0
         self.data = 0
         self.samplesreceived = 0
+        self.first_sample = None
         self.start_sample = None
         self.samplenum = -1
         self.wordlength = -1
 
     def start(self, metadata):
+        self.samplerate = metadata['samplerate']
         self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2s')
         self.out_ann = self.add(srd.OUTPUT_ANN, 'i2s')
 
     def report(self):
-        return 'I2S: %d samples received' % self.samplesreceived
+
+        # Calculate the sample rate
+        samplerate = '?'
+        if self.start_sample != None and \
+            self.first_sample != None and \
+            self.start_sample > self.first_sample:
+            samplerate = "%d" % (self.samplesreceived *
+                self.samplerate / (self.start_sample -
+                self.first_sample))
+
+        return 'I2S: %d %d-bit samples received at %sHz' % \
+            (self.samplesreceived, self.wordlength, samplerate)
 
     def decode(self, ss, es, data):
         for samplenum, (sck, ws, sd) in data:
@@ -101,6 +114,10 @@ class Decoder(srd.Decoder):
             self.data = 0
             self.bitcount = 0
             self.start_sample = self.samplenum
-                
+
+            # Save the first sample position
+            if self.first_sample == None:
+                self.first_sample = self.samplenum
+
             self.oldws = ws
             
\ No newline at end of file