- if pins[0] not in (0, 1):
- self.handle_bits(pins[1:])
- else:
- self.find_clk_edge(pins[0], pins[1:])
+ def decode(self):
+ # Determine which (optional) channels have input data. Insist in
+ # a non-empty input data set. Cope with sparse connection maps.
+ # Store enough state to later "compress" sampled input data.
+ max_possible = len(self.optional_channels)
+ idx_channels = [
+ idx if self.has_channel(idx) else None
+ for idx in range(max_possible)
+ ]
+ has_channels = [idx for idx in idx_channels if idx is not None]
+ if not has_channels:
+ raise ChannelError('At least one channel has to be supplied.')
+ max_connected = max(has_channels)
+ idx_strip = max_connected + 1
+
+ # Determine .wait() conditions, depending on the presence of a
+ # clock signal. Either inspect samples on the configured edge of
+ # the clock, or inspect samples upon ANY edge of ANY of the pins
+ # which provide input data.
+ if self.has_channel(0):
+ edge = self.options['clock_edge'][0]
+ conds = {0: edge}
+ else:
+ conds = [{idx: 'e'} for idx in has_channels]
+
+ # Keep processing the input stream. Assume "always zero" for
+ # not-connected input lines. Pass data bits (all inputs except
+ # clock) to the handle_bits() method.
+ while True:
+ pins = self.wait(conds)
+ bits = [0 if idx is None else pins[idx] for idx in idx_channels]
+ bits = bits[1:idx_strip]
+ self.handle_bits(bitpack(bits), len(bits))