"""Logical time in microseconds, with receive interrupts represented by events"""
from dataclasses import dataclass


@dataclass(frozen=True)
class Run:
    release: int
    start: int
    finish: int
    sample: int


def main():
    releases = [0, 2000, 4000, 6000]
    starts = [100, 2250, 4520, 6050]
    durations = [410, 420, 460, 420]
    # Actual observation time (one frame per 4 ms), with the last frame held by the consumer
    samples = [0, 0, 4000, 4000]
    runs = [Run(r, s, s + d, a) for r, s, d, a in zip(releases, starts, durations, samples)]
    delay = [v.start - v.release for v in runs]
    response = [v.finish - v.release for v in runs]
    age = [v.start - v.sample for v in runs]
    print("dispatch delay (us):", delay)
    print("response (us):", response)
    print("sample age (us):", age)
    print("start periods (us):", [b.start - a.start for a, b in zip(runs, runs[1:])])
    assert delay == [100, 250, 520, 50]
    assert max(response) == 980 and max(age) == 2250
    assert ((1 - 0xFFFFFFFE) & 0xFFFFFFFF) == 3


if __name__ == "__main__":
    main()
