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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Backends for a framebuffer.
//!
//! One could use a simple array of [`PixelColor`], or some more elaborate proxy
//! backends.
//!
//! Example:
//! ```rust
//! use embedded_graphics::{pixelcolor::Rgb565, prelude::RgbColor};
//! use embedded_graphics_framebuf::{
//!     backends::{EndianCorrectedBuffer, EndianCorrection},
//!     FrameBuf,
//! };
//! let mut data = [Rgb565::BLACK; 12 * 11]; // A potential backend
//! let mut fbuf = FrameBuf::new(&mut data, 12, 11);
//!
//! let mut fbuf = FrameBuf::new(
//!     EndianCorrectedBuffer::new(&mut data, EndianCorrection::ToBigEndian),
//!     12,
//!     11,
//! );
//! ```

use embedded_graphics::pixelcolor::{raw::RawU16, IntoStorage, PixelColor};

/// This trait marks the requirements for backends for a
/// [`FrameBuf`](crate::FrameBuf).
///
/// In a basic scenario this is just some memory.
/// But one could implement more elaborate backends which allow manipulation of
/// the data on the fly.
pub trait FrameBufferBackend {
    type Color: PixelColor;
    /// Sets a pixel to the respective color
    fn set(&mut self, index: usize, color: Self::Color);

    /// Returns a pixels color
    fn get(&self, index: usize) -> Self::Color;

    /// Nr of elements in the backend
    fn nr_elements(&self) -> usize;
}

/// Backends implementing this Trait can be used for DMA.
///
/// # Safety
///
/// The same restrictions as for [`embedded_dma::ReadBuffer`] apply.
pub unsafe trait DMACapableFrameBufferBackend: FrameBufferBackend {
    fn data_ptr(&self) -> *const Self::Color;
}
impl<'a, C: PixelColor, const N: usize> FrameBufferBackend for &'a mut [C; N] {
    type Color = C;
    fn set(&mut self, index: usize, color: C) {
        self[index] = color
    }

    fn get(&self, index: usize) -> C {
        self[index]
    }

    fn nr_elements(&self) -> usize {
        self.len()
    }
}

/// # Safety:
///
/// The implementation of the trait for all lifetimes `'a` is safe. However,
/// this doesn't mean that the use of it is safe for all lifetimes. The
/// requirements specified in [`embedded_dma::ReadBuffer::read_buffer`] remain.
unsafe impl<'a, C: PixelColor, const N: usize> DMACapableFrameBufferBackend for &'a mut [C; N] {
    fn data_ptr(&self) -> *const C {
        self.as_ptr()
    }
}

/// Enum indicating how the bytes should be converted in the host's memory.
#[derive(PartialEq, Eq)]
pub enum EndianCorrection {
    ToLittleEndian,
    ToBigEndian,
}

/// A backend for [`FrameBuf`](crate::FrameBuf) which changes the underlying
/// byte order. This can be useful when using the buffer for DMA with
/// peripherals that have a different endianness than the host.
pub struct EndianCorrectedBuffer<'a, C: PixelColor> {
    data: &'a mut [C],
    endian: EndianCorrection,
}
impl<'a, C: PixelColor> EndianCorrectedBuffer<'a, C> {
    pub fn new(data: &'a mut [C], endian: EndianCorrection) -> Self {
        Self { data, endian }
    }
}
impl<'a, C> FrameBufferBackend for EndianCorrectedBuffer<'a, C>
where
    // TODO: Make this generic over other
    // types than u16 with associated
    // type bounds once they are stable
    C: IntoStorage<Storage = u16> + PixelColor,
    RawU16: From<C>,
    C: core::convert::From<RawU16>,
{
    type Color = C;
    fn set(&mut self, index: usize, color: C) {
        self.data[index] = match self.endian {
            EndianCorrection::ToBigEndian => RawU16::new(color.into_storage().to_be()).into(),
            EndianCorrection::ToLittleEndian => RawU16::new(color.into_storage().to_le()).into(),
        }
    }

    fn get(&self, index: usize) -> C {
        match self.endian {
            EndianCorrection::ToBigEndian => {
                C::from(RawU16::new(u16::from_be(self.data[index].into_storage())))
            }
            EndianCorrection::ToLittleEndian => {
                C::from(RawU16::new(u16::from_le(self.data[index].into_storage())))
            }
        }
    }

    fn nr_elements(&self) -> usize {
        self.data.len()
    }
}
unsafe impl<'a, C> DMACapableFrameBufferBackend for EndianCorrectedBuffer<'a, C>
where
    C: IntoStorage<Storage = u16> + PixelColor,
    RawU16: From<C>,
    C: core::convert::From<RawU16>,
{
    fn data_ptr(&self) -> *const C {
        self.data.as_ptr()
    }
}

#[cfg(test)]
mod tests {
    extern crate std;

    use super::*;
    use crate::FrameBuf;
    use embedded_graphics::pixelcolor::{raw::RawU16, Rgb565};
    use embedded_graphics::prelude::{Point, RawData, RgbColor};

    #[test]
    fn test_no_endian_correction() {
        let mut data = [Rgb565::BLUE; 2 * 3];
        let mut fbuf = FrameBuf::new(&mut data, 2, 3);
        fbuf.set_color_at(Point::new(1, 0), Rgb565::RED);
        fbuf.set_color_at(Point::new(2, 0), Rgb565::BLUE);
        // Blue in native endian
        assert_eq!(RawU16::from(fbuf.data[0]).into_inner(), 0b00000000_00011111);
        // Red in native endian
        assert_eq!(RawU16::from(fbuf.data[1]).into_inner(), 0b11111000_00000000);
        // Blue in native endian
        assert_eq!(RawU16::from(fbuf.data[2]).into_inner(), 0b00000000_00011111);
    }

    #[test]
    fn test_big_endian_correction() {
        let mut data = [Rgb565::BLUE; 2 * 3];
        let mut fbuf = FrameBuf::new(
            EndianCorrectedBuffer::new(&mut data, EndianCorrection::ToBigEndian),
            2,
            3,
        );
        fbuf.set_color_at(Point::new(1, 0), Rgb565::RED);
        fbuf.set_color_at(Point::new(2, 0), Rgb565::BLUE);

        // Access functions work as expected
        assert_eq!(fbuf.get_color_at(Point::new(1, 0)), Rgb565::RED);
        assert_eq!(fbuf.get_color_at(Point::new(2, 0)), Rgb565::BLUE);

        // Red in big endian
        assert_eq!(
            RawU16::from(fbuf.data.data[1]).into_inner(),
            0b00000000_11111000
        );
        // Blue in big endian
        assert_eq!(
            RawU16::from(fbuf.data.data[2]).into_inner(),
            0b00011111_00000000
        );
    }

    #[test]
    fn test_little_endian_correction() {
        let mut data = [Rgb565::BLUE; 2 * 3];
        let mut fbuf = FrameBuf::new(
            EndianCorrectedBuffer::new(&mut data, EndianCorrection::ToLittleEndian),
            2,
            3,
        );
        fbuf.set_color_at(Point::new(1, 0), Rgb565::RED);
        fbuf.set_color_at(Point::new(2, 0), Rgb565::BLUE);

        // Access functions work as expected
        assert_eq!(fbuf.get_color_at(Point::new(1, 0)), Rgb565::RED);
        assert_eq!(fbuf.get_color_at(Point::new(2, 0)), Rgb565::BLUE);

        // Red in little endian
        assert_eq!(
            RawU16::from(fbuf.data.data[1]).into_inner(),
            0b11111000_00000000
        );
        // Blue in little endian
        assert_eq!(
            RawU16::from(fbuf.data.data[2]).into_inner(),
            0b00000000_00011111
        );
    }
}