]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/i2s/pd.py
all decoders: introduce a reset() method
[libsigrokdecode.git] / decoders / i2s / pd.py
index eeeea2012d6e80211711faca7db7792498ba602a..91801b4e98f9c0c6a62dcb8b92852907513f6092 100644 (file)
 ## GNU General Public License for more details.
 ##
 ## You should have received a copy of the GNU General Public License
-## along with this program; if not, write to the Free Software
-## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
 ##
 
 import sigrokdecode as srd
+import struct
 
 '''
 OUTPUT_PYTHON format:
@@ -37,7 +37,7 @@ class SamplerateError(Exception):
     pass
 
 class Decoder(srd.Decoder):
-    api_version = 2
+    api_version = 3
     id = 'i2s'
     name = 'I²S'
     longname = 'Integrated Interchip Sound'
@@ -59,9 +59,11 @@ class Decoder(srd.Decoder):
         ('wav', 'WAV file'),
     )
 
-    def __init__(self, **kwargs):
+    def __init__(self):
+        self.reset()
+
+    def reset(self):
         self.samplerate = None
-        self.oldsck = 1
         self.oldws = 1
         self.bitcount = 0
         self.data = 0
@@ -73,7 +75,7 @@ class Decoder(srd.Decoder):
 
     def start(self):
         self.out_python = self.register(srd.OUTPUT_PYTHON)
-        self.out_bin = self.register(srd.OUTPUT_BINARY)
+        self.out_binary = self.register(srd.OUTPUT_BINARY)
         self.out_ann = self.register(srd.OUTPUT_ANN)
 
     def metadata(self, key, value):
@@ -84,7 +86,7 @@ class Decoder(srd.Decoder):
         self.put(self.ss_block, self.samplenum, self.out_python, data)
 
     def putbin(self, data):
-        self.put(self.ss_block, self.samplenum, self.out_bin, data)
+        self.put(self.ss_block, self.samplenum, self.out_binary, data)
 
     def putb(self, data):
         self.put(self.ss_block, self.samplenum, self.out_ann, data)
@@ -114,34 +116,23 @@ class Decoder(srd.Decoder):
         h += b'\x01\x00'         # Audio format (0x0001 == PCM)
         h += b'\x02\x00'         # Number of channels (2)
         h += b'\x80\x3e\x00\x00' # Samplerate (16000)
-        h += b'\x00\x7d\x00\x00' # Byterate (32000)
+        h += b'\x00\xfa\x00\x00' # Byterate (64000)
         h += b'\x04\x00'         # Blockalign (4)
-        h += b'\x10\x00'         # Bits per sample (16)
+        h += b'\x20\x00'         # Bits per sample (32)
         # Data subchunk
         h += b'data'
-        h += b'\xff\xff\x00\x00' # Subchunk size (65535 bytes) TODO
+        h += b'\xff\xff\xff\xff' # Subchunk size (4G bytes) TODO
         return h
 
     def wav_sample(self, sample):
-        # TODO: This currently assumes U32 samples, and converts to S16.
-        s = sample >> 16
-        if s >= 0x8000:
-            s -= 0x10000
-        lo, hi = s & 0xff, (s >> 8) & 0xff
-        return bytes([lo, hi])
-
-    def decode(self, ss, es, data):
+        return struct.pack('<I', self.data)
+
+    def decode(self):
         if not self.samplerate:
             raise SamplerateError('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:
-                continue
-
-            self.oldsck = sck
-            if sck == 0:   # Ignore the falling clock edge.
-                continue
+        while True:
+            # Wait for a rising edge on the SCK pin.
+            sck, ws, sd = self.wait({0: 'r'})
 
             self.data = (self.data << 1) | sd
             self.bitcount += 1
@@ -154,7 +145,7 @@ class Decoder(srd.Decoder):
             if self.ss_block is not None:
 
                 if not self.wrote_wav_header:
-                    self.put(0, 0, self.out_bin, [0, self.wav_header()])
+                    self.put(0, 0, self.out_binary, [0, self.wav_header()])
                     self.wrote_wav_header = True
 
                 self.samplesreceived += 1