- self.put(self.num_cycles, self.num_cycles, self.out_bin, data)
-
- def decode(self, ss, es, data):
- for (self.samplenum, pins) in data:
- # Ignore identical samples early on (for performance reasons).
- if self.oldpin == pins[0]:
- continue
-
- if self.oldpin == 0: # Rising edge.
- self.low = self.samplenum - self.lastedge
- else:
- self.high = self.samplenum - self.lastedge
-
- if self.oldpin == self.startedge:
- self.es = self.samplenum # This interval ends at this edge.
- if self.ss >= 0: # Have we completed a hi-lo sequence?
- self.putx([0, ["%d%%" % ((100 * self.high) // (self.high + self.low))]])
- self.putb((0, bytes([(256 * self.high) // (self.high + self.low)])))
- self.num_cycles += 1
- else:
- # Mid-interval.
- # This interval started at the previous edge.
- self.ss = self.lastedge
-
- self.lastedge = self.samplenum
- self.oldpin = pins[0]
+ self.put(self.ss_block, self.es_block, self.out_binary, data)
+
+ def decode(self):
+ if not self.samplerate:
+ raise SamplerateError('Cannot decode without samplerate.')
+
+ num_cycles = 0
+ average = 0
+
+ # Wait for an "active" edge (depends on config). This starts
+ # the first full period of the inspected signal waveform.
+ self.wait({0: 'f' if self.options['polarity'] == 'active-low' else 'r'})
+ self.first_samplenum = self.samplenum
+
+ # Keep getting samples for the period's middle and terminal edges.
+ # At the same time that last sample starts the next period.
+ while True:
+
+ # Get the next two edges. Setup some variables that get
+ # referenced in the calculation and in put() routines.
+ start_samplenum = self.samplenum
+ self.wait({0: 'e'})
+ end_samplenum = self.samplenum
+ self.wait({0: 'e'})
+ self.ss_block = start_samplenum
+ self.es_block = self.samplenum
+
+ # Calculate the period, the duty cycle, and its ratio.
+ period = self.samplenum - start_samplenum
+ duty = end_samplenum - start_samplenum
+ ratio = float(duty / period)
+
+ # Report the duty cycle in percent.
+ percent = float(ratio * 100)
+ self.putx([0, ['%f%%' % percent]])
+
+ # Report the duty cycle in the binary output.
+ self.putb([0, bytes([int(ratio * 256)])])
+
+ # Report the period in units of time.
+ period_t = float(period / self.samplerate)
+ self.putp(period_t)
+
+ # Update and report the new duty cycle average.
+ num_cycles += 1
+ average += percent
+ self.put(self.first_samplenum, self.es_block, self.out_average,
+ float(average / num_cycles))