onewire: combined reset and presence detect events, avoid unneeded overdrive exit...
[libsigrokdecode.git] / decoders / onewire_link / onewire_link.py
index 3236f4d9e866ec3d549fc2db09c17630644f3ea9..d111882f8e81a1d5fc91fbec0d0a2f9cc61a3151 100644 (file)
@@ -57,8 +57,9 @@ class Decoder(srd.Decoder):
         self.state   = 'WAIT FOR FALLING EDGE'
         self.present = 0
         self.bit     = 0
+        self.bit_cnt = 0
+        self.command = 0
         self.overdrive = 0
-        self.cmd_cnt = 0
         # Event timing variables
         self.fall    = 0
         self.rise    = 0
@@ -171,11 +172,11 @@ class Decoder(srd.Decoder):
                     if (self.bit):  self.state = 'WAIT FOR FALLING EDGE'
                     else         :  self.state = 'WAIT FOR RISING EDGE'
                     self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['BIT: %01x' % self.bit]])
-                    self.put(self.out_proto, ['BIT', self.bit])
+                    self.put(self.fall, self.cnt_bit[self.overdrive], self.out_proto, ['BIT', self.bit])
                     # Checking the first command to see if overdrive mode should be entered
-                    if   (self.cmd_cnt <= 8):
-                        self.command = self.command | (self.bit << self.cmd_cnt)
-                    elif (self.cmd_cnt == 8):
+                    if   (self.bit_cnt <= 8):
+                        self.command = self.command | (self.bit << self.bit_cnt)
+                    elif (self.bit_cnt == 8):
                         if (self.command in [0x3c, 0x69]):
                             self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['ENTER OVERDRIVE MODE']])
                     # incrementing the bit counter
@@ -188,21 +189,19 @@ class Decoder(srd.Decoder):
                         # Save the sample number for the falling edge.
                         self.rise = self.samplenum
                         self.state = "WAIT FOR PRESENCE DETECT"
-                        self.put(self.fall, self.rise, self.out_ann, [0, ['RESET']])
-                        self.put(self.fall, self.rise, self.out_proto, ['RESET', 0])
                         # Reset the timer.
                         self.fall = self.samplenum
                         # Exit overdrive mode
-                        self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['EXIT OVERDRIVE MODE']])
-                        self.overdrive = 0
-                        self.cmd_cnt = 0
+                        if (self.overdrive):
+                            self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['EXIT OVERDRIVE MODE']])
+                            self.overdrive = 0
+                        # Clear command bit counter and data register
+                        self.bit_cnt = 0
                         self.command = 0
                     elif ((self.samplenum - self.fall > self.cnt_overdrive_reset) and (self.overdrive)):
                         # Save the sample number for the falling edge.
                         self.rise = self.samplenum
                         self.state = "WAIT FOR PRESENCE DETECT"
-                        self.put(self.fall, self.rise, self.out_ann, [0, ['RESET']])
-                        self.put(self.fall, self.rise, self.out_proto, ['RESET', 0])
                         # Reset the timer.
                         self.fall = self.samplenum
                     # Otherwise this is assumed to be a data bit.
@@ -217,7 +216,7 @@ class Decoder(srd.Decoder):
                     # create presence detect event
                     if (self.present) :  self.state = 'WAIT FOR FALLING EDGE'
                     else              :  self.state = 'WAIT FOR RISING EDGE'
-                    self.put(self.samplenum, 0, self.out_ann, [0, ['PRESENCE: ' + "False" if self.present else "True"]])
-                    self.put(self.samplenum, 0, self.out_proto, ['PRESENCE', self.present])
+                    self.put(self.samplenum, 0, self.out_ann, [0, ['RESET/PRESENCE: %s' % ('False' if self.present else 'True')]])
+                    self.put(self.samplenum, 0, self.out_proto, ['RESET/PRESENCE', not self.present])
             else:
                 raise Exception('Invalid state: %d' % self.state)