- for (self.samplenum, pins) in data:
- data = pins[0]
-
- # Initialize self.olddata with the first sample value.
- if self.olddata == None:
- self.olddata = data
- continue
-
- # First we need to recover the clock.
- if self.olddata == data:
- self.pulse_width += 1
- continue
-
- # Found rising or falling edge.
- if self.first_edge:
- # Throw away first detected edge as it might be mangled data.
- self.first_edge = False
- self.pulse_width = 0
- else:
- if self.state == 0:
- self.find_first_pulse_width()
- elif self.state == 1:
- self.find_second_pulse_width()
- elif self.state == 2:
- self.find_third_pulse_width()
- elif self.state == 3:
- self.decode_stream()
- elif self.state == 4:
- self.handle_preamble()
-
- self.pulse_width = 0
-
- self.olddata = data
+ # Throw away first detected edge as it might be mangled data.
+ self.wait({0: 'e'})
+
+ while True:
+ # Wait for any edge (rising or falling).
+ (data,) = self.wait({0: 'e'})
+ self.pulse_width = self.samplenum - self.samplenum_prev_edge - 1
+ self.samplenum_prev_edge = self.samplenum
+
+ if self.state == 'GET FIRST PULSE WIDTH':
+ self.find_first_pulse_width()
+ elif self.state == 'GET SECOND PULSE WIDTH':
+ self.find_second_pulse_width()
+ elif self.state == 'GET THIRD PULSE WIDTH':
+ self.find_third_pulse_width()
+ elif self.state == 'DECODE STREAM':
+ self.decode_stream()
+ elif self.state == 'DECODE PREAMBLE':
+ self.decode_preamble()