1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use core::ops::{Add, Sub};

#[cfg(feature = "fugit")]
mod fgt;
mod primitives;

pub trait InfraMonotonic: Sized {
    type Instant: Ord
        + Copy
        + Add<Self::Duration, Output = Self::Instant>
        + Sub<Self::Duration, Output = Self::Instant>
        + Sub<Self::Instant, Output = Self::Duration>;
    type Duration: PartialOrd
        + Copy
        + Add<Self::Duration, Output = Self::Duration>
        + core::fmt::Debug;

    const ZERO_INSTANT: Self::Instant;
    const ZERO_DURATION: Self::Duration;

    fn checked_sub(a: Self::Instant, b: Self::Instant) -> Option<Self::Duration>;

    fn create_span(freq: u32, p: u32, t: u32) -> Span<Self::Duration>;
}

#[derive(Debug)]
pub struct Span<Dur> {
    low: Dur,
    high: Dur,
}

#[derive(Debug)]
pub struct PulseSpans<Mono: InfraMonotonic> {
    pub(crate) spans: [Span<Mono::Duration>; 8],
}

impl<Mono> PulseSpans<Mono>
where
    Mono: InfraMonotonic,
{
    pub fn new(freq: u32, pulse: &[u32; 8], tolerance: &[u32; 8]) -> Self {
        PulseSpans {
            spans: [
                Mono::create_span(freq, pulse[0], tolerance[0]),
                Mono::create_span(freq, pulse[1], tolerance[1]),
                Mono::create_span(freq, pulse[2], tolerance[2]),
                Mono::create_span(freq, pulse[3], tolerance[3]),
                Mono::create_span(freq, pulse[4], tolerance[4]),
                Mono::create_span(freq, pulse[5], tolerance[5]),
                Mono::create_span(freq, pulse[6], tolerance[6]),
                Mono::create_span(freq, pulse[7], tolerance[7]),
            ],
        }
    }

    pub fn get<P: From<usize>>(&self, pl: Mono::Duration) -> Option<P> {
        self.spans
            .iter()
            .position(|v| v.contains(pl))
            .map(Into::into)
    }

    pub fn check_overlaps(&self) -> bool {
        for i in 0..self.spans.len() {
            for j in 0..self.spans.len() {
                if i == j {
                    continue;
                }

                if self.spans[i].overlaps(&self.spans[j]) {
                    return true;
                }
            }
        }

        false
    }
}

impl<Dur> Span<Dur>
where
    Dur: PartialOrd + Copy,
{
    fn contains(&self, other: Dur) -> bool {
        self.low <= other && other <= self.high
    }

    fn overlaps(&self, other: &Span<Dur>) -> bool {
        self.contains(other.low) || self.contains(other.high)
    }
}