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
use crate::pixel::Pixel;

pub trait PixelStreamRef {
    fn next(&mut self) -> Option<u8>;
}

pub struct PixelStream<P, I>
where
    P: Pixel,
    I: Iterator<Item = P>,
{
    pixel_stream: I,
    bytes_iter: Option<P::BytesIter>,
    finished: bool,
}

impl<I, P> PixelStream<P, I>
where
    P: Pixel,
    I: Iterator<Item = P>,
{
    fn new(pixel_stream: I) -> Self {
        Self {
            pixel_stream,
            bytes_iter: None,
            finished: false,
        }
    }
}

impl<I, P> PixelStreamRef for PixelStream<P, I>
where
    P: Pixel,
    I: Iterator<Item = P>,
{
    fn next(&mut self) -> Option<u8> {
        loop {
            if self.finished {
                return None;
            }

            if self.bytes_iter.is_none() {
                self.bytes_iter = self.pixel_stream.next().map(|p| p.into_ws2812_bytes());
            }

            if let Some(bytes_iter) = self.bytes_iter.as_mut() {
                if let Some(byte) = bytes_iter.next() {
                    return Some(byte);
                } else {
                    self.bytes_iter = None;
                }
            } else {
                self.finished = true;
            }
        }
    }
}

/// Converts an iterator of pixels into a pixel stream, usable by the driver's `write` function.
pub trait IntoPixelStream {
    /// The pixel type.
    type Pixel: Pixel;
    /// The pixel iterator type.
    type PixelIter: Iterator<Item = Self::Pixel>;

    /// Converts the current object into a pixel stream.
    fn into_pixel_stream(self) -> PixelStream<Self::Pixel, Self::PixelIter>;
}

impl<T> IntoPixelStream for T
where
    T: IntoIterator,
    <T as IntoIterator>::Item: Pixel,
{
    type Pixel = <T as IntoIterator>::Item;
    type PixelIter = <T as IntoIterator>::IntoIter;

    fn into_pixel_stream(self) -> PixelStream<Self::Pixel, Self::PixelIter> {
        PixelStream::new(self.into_iter())
    }
}