]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/i2s/pd.py
Rename inter-PD output type to SRD_OUTPUT_PYTHON
[libsigrokdecode.git] / decoders / i2s / pd.py
index c4ac4fba2daeafe46d66176d0f4d8ec368868b87..292a4c48ebbd1af8e51816271270d7ac9cf2dfe9 100644 (file)
 
 import sigrokdecode as srd
 
+'''
+Protocol output format:
+
+Packet:
+[<ptype>, <pdata>]
+
+<ptype>, <pdata>:
+ - 'DATA', [<channel>, <value>]
+
+<channel>: 'L' or 'R'
+<value>: integer
+'''
+
 class Decoder(srd.Decoder):
     api_version = 1
     id = 'i2s'
@@ -45,6 +58,7 @@ class Decoder(srd.Decoder):
     ]
 
     def __init__(self, **kwargs):
+        self.samplerate = None
         self.oldsck = 1
         self.oldws = 1
         self.bitcount = 0
@@ -54,11 +68,20 @@ class Decoder(srd.Decoder):
         self.start_sample = None
         self.wordlength = -1
 
-    def start(self, metadata):
-        self.samplerate = metadata['samplerate']
-        self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2s')
+    def start(self):
+        self.out_proto = self.add(srd.OUTPUT_PYTHON, 'i2s')
         self.out_ann = self.add(srd.OUTPUT_ANN, 'i2s')
 
+    def metadata(self, key, value):
+        if key == srd.SRD_CONF_SAMPLERATE:
+            self.samplerate = value
+
+    def putpb(self, data):
+        self.put(self.start_sample, self.samplenum, self.out_proto, data)
+
+    def putb(self, data):
+        self.put(self.start_sample, self.samplenum, self.out_ann, data)
+
     def report(self):
 
         # Calculate the sample rate.
@@ -74,7 +97,9 @@ class Decoder(srd.Decoder):
             (self.samplesreceived, self.wordlength, samplerate)
 
     def decode(self, ss, es, data):
-        for samplenum, (sck, ws, sd) in data:
+        if self.samplerate is None:
+            raise Exception("Cannot decode without samplerate.")
+        for self.samplenum, (sck, ws, sd) in data:
 
             # Ignore sample if the bit clock hasn't changed.
             if sck == self.oldsck:
@@ -94,29 +119,31 @@ class Decoder(srd.Decoder):
             # Only submit the sample, if we received the beginning of it.
             if self.start_sample != None:
                 self.samplesreceived += 1
-                self.put(self.start_sample, samplenum, self.out_proto,
-                         ['data', self.data])
+
                 idx = 0 if self.oldws else 1
-                self.put(self.start_sample, samplenum, self.out_ann,
-                         [idx, ['0x%08x', self.data)]])
+                c1 = 'Left channel' if self.oldws else 'Right channel'
+                c2 = 'Left' if self.oldws else 'Right'
+                c3 = 'L' if self.oldws else 'R'
+                v = '%08x' % self.data
+                self.putpb(['DATA', [c3, self.data]])
+                self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
+                                 '%s: %s' % (c3, v), c3]])
 
                 # Check that the data word was the correct length.
                 if self.wordlength != -1 and self.wordlength != self.bitcount:
-                    self.put(self.start_sample, samplenum, self.out_ann,
-                        [2, ['Received a %d-bit word, when a '
-                        '%d-bit word was expected' % (self.bitcount,
-                        self.wordlength)]])
+                    self.putb([2, ['Received %d-bit word, expected %d-bit '
+                                   'word' % (self.bitcount, self.wordlength)]])
 
                 self.wordlength = self.bitcount
 
             # Reset decoder state.
             self.data = 0
             self.bitcount = 0
-            self.start_sample = samplenum
+            self.start_sample = self.samplenum
 
             # Save the first sample position.
             if self.first_sample == None:
-                self.first_sample = samplenum
+                self.first_sample = self.samplenum
 
             self.oldws = ws