#!/usr/bin/python3 # -*- coding: UTF-8 -*- # vim:fileencoding=UTF-8:ts=4 import sys import array import time import gc class PulseGenerator: def __init__(self): self.timing = array.array('l', (0 for _ in range(64))) # int32_t @staticmethod def pulse_high(width=1_000_000): # 1ms t0 = time.time_ns() + width # # set GPIO high # while time.time_ns() < t0: pass t1 = time.time_ns() return t1 - t0 @staticmethod def pulse_low(width=1_000_000): # 1ms t0 = time.time_ns() + width # # set GPIO low # while time.time_ns() < t0: pass t1 = time.time_ns() return t1 - t0 def pulsetrain(self, value, length): for idx in range(length): if value & 1: delta = PulseGenerator.pulse_high() else: delta = PulseGenerator.pulse_low() value >>= 1 self.timing[idx] = delta def show_timing(self, length): min_µs = min(self.timing) / 1000.0 max_µs = max(self.timing) / 1000.0 avg_µs = sum(self.timing) / length / 1000.0 flag = 'X' if max_µs > 10 else '-' # mark if deviation is higher than 10µs print(f'Delta (64 × 1ms pulses): min:{min_µs:6.1f}µs avg:{avg_µs:6.1f}µs max:{max_µs:6.1f}µs {flag}') NUM_LOOPS = 1000 pulse = PulseGenerator() # define 64bits (pulses) pulsedef = 0 pulsedef <<= 16 pulsedef += 0b_1111_0000_1100_1111 pulsedef <<= 16 pulsedef += 0b_1111_0000_1100_0111 pulsedef <<= 16 pulsedef += 0b_1111_0000_1100_0011 pulsedef <<= 16 pulsedef += 0b_1111_0000_0000_0001 print(bin(pulsedef)) gc.collect() for _ in range(NUM_LOOPS): pulse.pulsetrain(pulsedef, 64) pulse.show_timing(64)