]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/rc_encode/pd.py
lin: Simplify state handling a bit.
[libsigrokdecode.git] / decoders / rc_encode / pd.py
index 902594c80da09d2cabe9e2aaed26cda4c022ba1c..56b60caed3c1067fa8bdbca294620a5709e2c92f 100644 (file)
@@ -19,6 +19,8 @@
 
 import sigrokdecode as srd
 
+bitvals = ('0', '1', 'f', 'U')
+
 def decode_bit(edges):
     # Datasheet says long pulse is 3 times short pulse.
     lmin = 2 # long min multiplier
@@ -53,20 +55,19 @@ def decode_model(model, bits):
     if model == 'maplin_l95ar':
         address = 'Addr' # Address pins A0 to A5
         for i in range(0, 6):
-            address = address + ' %i:' % (i + 1) + \
-                      ('on' if bits[i][0] == '0' else 'off')
+            address += ' %i:' % (i + 1) + ('on' if bits[i][0] == '0' else 'off')
         button = 'Button'
         # Button pins A6/D5 to A11/D0
         if bits[6][0] == '0' and bits[11][0] == '0':
-            button = button + ' A ON/OFF'
+            button += ' A ON/OFF'
         elif bits[7][0] == '0' and bits[11][0] == '0':
-            button = button + ' B ON/OFF'
+            button += ' B ON/OFF'
         elif bits[9][0] == '0' and bits[11][0] == '0':
-            button = button + ' C ON/OFF'
+            button += ' C ON/OFF'
         elif bits[8][0] == '0' and bits[11][0] == '0':
-            button = button + ' D ON/OFF'
+            button += ' D ON/OFF'
         else:
-            button = button + ' Unknown'
+            button += ' Unknown'
         return ['%s' % address, bits[0][1], bits[5][2], \
                 '%s' % button, bits[6][1], bits[11][2]]
 
@@ -83,14 +84,19 @@ class Decoder(srd.Decoder):
         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
     )
     annotations = (
-        ('bits', 'Bits'),
-        ('pins', 'Pins'),
-        ('remote', 'Remote'),
+        ('bit-0', 'Bit 0'),
+        ('bit-1', 'Bit 1'),
+        ('bit-f', 'Bit f'),
+        ('bit-U', 'Bit U'),
+        ('bit-sync', 'Bit sync'),
+        ('pin', 'Pin'),
+        ('code-word-addr', 'Code word address'),
+        ('code-word-data', 'Code word data'),
     )
     annotation_rows = (
-        ('bits', 'Bits', (0,)),
-        ('pins', 'Pins', (1,)),
-        ('remote', 'Remote', (2,)),
+        ('bits', 'Bits', (0, 1, 2, 3, 4)),
+        ('pins', 'Pins', (5,)),
+        ('code-words', 'Code words', (6, 7)),
     )
     options = (
         {'id': 'remote', 'desc': 'Remote', 'default': 'none', 
@@ -106,14 +112,17 @@ class Decoder(srd.Decoder):
         self.bits = []
         self.labels = []
         self.bit_count = 0
-        self.bit_first = None
-        self.bit_last = None
+        self.ss = None
+        self.es = None
         self.state = 'IDLE'
 
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
         self.model = self.options['remote']
 
+    def putx(self, data):
+        self.put(self.ss, self.es, self.out_ann, data)
+
     def decode(self):
         while True:
             pin = self.wait({0: 'e'})
@@ -121,7 +130,7 @@ class Decoder(srd.Decoder):
 
             if not self.samplenumber_last: # Set counters to start of signal.
                 self.samplenumber_last = self.samplenum
-                self.bit_first = self.samplenum
+                self.ss = self.samplenum
                 continue
 
             if self.bit_count < 12: # Decode A0 to A11.
@@ -132,27 +141,25 @@ class Decoder(srd.Decoder):
                     samples = self.samplenum - self.samplenumber_last
                     self.pulses.append(samples) # Save the pulse width.
                     self.samplenumber_last = self.samplenum
-                self.bit_last = self.samplenum
-                self.bits.append([decode_bit(self.pulses), self.bit_first,
-                                  self.bit_last]) # Save states and times.
-                self.put(self.bit_first, self.bit_last, self.out_ann,
-                         [0, [decode_bit(self.pulses)]]) # Write decoded bit.
-                self.put(self.bit_first, self.bit_last, self.out_ann,
-                         [1, [pinlabels(self.bit_count)]]) # Write pin labels.
+                self.es = self.samplenum
+                self.bits.append([decode_bit(self.pulses), self.ss,
+                                  self.es]) # Save states and times.
+                idx = bitvals.index(decode_bit(self.pulses))
+                self.putx([idx, [decode_bit(self.pulses)]]) # Write decoded bit.
+                self.putx([5, [pinlabels(self.bit_count)]]) # Write pin labels.
                 self.pulses = []
-                self.bit_first = self.samplenum
+                self.ss = self.samplenum
             else:
                 if self.model != 'none':
                     self.labels = decode_model(self.model, self.bits)
                     self.put(self.labels[1], self.labels[2], self.out_ann,
-                             [2, [self.labels[0]]]) # Write model decode.
+                             [6, [self.labels[0]]]) # Write model decode.
                     self.put(self.labels[4], self.labels[5], self.out_ann,
-                             [2, [self.labels[3]]]) # Write model decode.
+                             [7, [self.labels[3]]]) # Write model decode.
                 samples = self.samplenum - self.samplenumber_last
                 pin = self.wait({'skip': 8 * samples}) # Wait for end of sync bit.
-                self.bit_last = self.samplenum
-                self.put(self.bit_first, self.bit_last, self.out_ann,
-                         [0, ['Sync']]) # Write sync label.
+                self.es = self.samplenum
+                self.putx([4, ['Sync']]) # Write sync label.
                 self.reset() # Reset and wait for next set of pulses.
                 self.state = 'DECODE_TIMEOUT'
             if not self.state == 'DECODE_TIMEOUT':