]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/i2c/pd.py
s/out_proto/out_python/.
[libsigrokdecode.git] / decoders / i2c / pd.py
index 9f8cbaff36434758f0c5f0d9876c10b9cb14d4b3..4d77920842e319de98593f2f1eab55532aec608b 100644 (file)
@@ -18,8 +18,6 @@
 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 ##
 
-# I2C protocol decoder
-
 # TODO: Look into arbitration, collision detection, clock synchronisation, etc.
 # TODO: Implement support for 10bit slave addresses.
 # TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0).
@@ -28,9 +26,9 @@
 import sigrokdecode as srd
 
 '''
-Protocol output format:
+OUTPUT_PYTHON format:
 
-I2C packet:
+I²C packet:
 [<cmd>, <data>]
 
 <cmd> is one of:
@@ -66,7 +64,7 @@ proto = {
 class Decoder(srd.Decoder):
     api_version = 1
     id = 'i2c'
-    name = 'I2C'
+    name = 'I²C'
     longname = 'Inter-Integrated Circuit'
     desc = 'Two-wire, multi-master, serial bus.'
     license = 'gplv2+'
@@ -81,17 +79,23 @@ class Decoder(srd.Decoder):
         'address_format': ['Displayed slave address format', 'shifted'],
     }
     annotations = [
-        ['Start', 'Start condition'],
-        ['Repeat start', 'Repeat start condition'],
-        ['Stop', 'Stop condition'],
-        ['ACK', 'ACK'],
-        ['NACK', 'NACK'],
-        ['Address read', 'Address read'],
-        ['Address write', 'Address write'],
-        ['Data read', 'Data read'],
-        ['Data write', 'Data write'],
-        ['Warnings', 'Human-readable warnings'],
+        ['start', 'Start condition'],
+        ['repeat-start', 'Repeat start condition'],
+        ['stop', 'Stop condition'],
+        ['ack', 'ACK'],
+        ['nack', 'NACK'],
+        ['address-read', 'Address read'],
+        ['address-write', 'Address write'],
+        ['data-read', 'Data read'],
+        ['data-write', 'Data write'],
+        ['warnings', 'Human-readable warnings'],
     ]
+    binary = (
+        ('address-read', 'Address read'),
+        ('address-write', 'Address write'),
+        ('data-read', 'Data read'),
+        ('data-write', 'Data write'),
+    )
 
     def __init__(self, **kwargs):
         self.samplerate = None
@@ -113,8 +117,9 @@ class Decoder(srd.Decoder):
             self.samplerate = value
 
     def start(self):
-        self.out_proto = self.register(srd.OUTPUT_PYTHON)
+        self.out_python = self.register(srd.OUTPUT_PYTHON)
         self.out_ann = self.register(srd.OUTPUT_ANN)
+        self.out_binary = self.register(srd.OUTPUT_BINARY)
         self.out_bitrate = self.register(srd.OUTPUT_META,
                 meta=(int, 'Bitrate', 'Bitrate from Start bit to Stop bit'))
 
@@ -122,7 +127,10 @@ class Decoder(srd.Decoder):
         self.put(self.startsample, self.samplenum, self.out_ann, data)
 
     def putp(self, data):
-        self.put(self.startsample, self.samplenum, self.out_proto, data)
+        self.put(self.startsample, self.samplenum, self.out_python, data)
+
+    def putb(self, data):
+        self.put(self.startsample, self.samplenum, self.out_binary, data)
 
     def is_start_condition(self, scl, sda):
         # START condition (S): SDA = falling, SCL = high
@@ -178,18 +186,24 @@ class Decoder(srd.Decoder):
             if self.options['address_format'] == 'shifted':
                 d = d >> 1
 
+        bin_class = -1
         if self.state == 'FIND ADDRESS' and self.wr == 1:
             cmd = 'ADDRESS WRITE'
+            bin_class = 1
         elif self.state == 'FIND ADDRESS' and self.wr == 0:
             cmd = 'ADDRESS READ'
+            bin_class = 0
         elif self.state == 'FIND DATA' and self.wr == 1:
             cmd = 'DATA WRITE'
+            bin_class = 3
         elif self.state == 'FIND DATA' and self.wr == 0:
             cmd = 'DATA READ'
+            bin_class = 2
 
         self.putp([cmd, d])
         self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d),
                   '%s: %02X' % (proto[cmd][2], d), '%02X' % d]])
+        self.putb((bin_class, bytes([d])))
 
         # Done with this packet.
         self.startsample = -1