weact-studio-epd 0.1.2

Unofficial driver for WeAct Studio E-paper modules
Documentation
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use core::convert::Infallible;

use embedded_graphics::{
    draw_target::DrawTarget,
    geometry::{OriginDimensions, Point, Size},
    pixelcolor::PixelColor,
    Pixel,
};

use crate::color::{Color, ColorType, TriColor};

/// Rotation of the display.
#[derive(Debug, Clone, Copy, Default)]
pub enum DisplayRotation {
    /// No rotation.
    #[default]
    Rotate0,
    /// Rotate by 90 degrees clockwise.
    Rotate90,
    /// Rotate by 180 degrees clockwise.
    Rotate180,
    /// Rotate 270 degrees clockwise.
    Rotate270,
}

/// Computes the needed buffer length. Takes care of rounding up in case `width`
/// is not divisible by 8.
pub const fn buffer_len<C>(width: usize, height: usize) -> usize
where
    C: ColorType,
{
    (width + 7) / 8 * height * C::BUFFER_COUNT
}

/// In-memory display buffer to render to using `embedded-graphics`.
///
/// `BUFFER_SIZE` can be calculated using [`buffer_len`].
pub struct Display<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize, C> {
    buffer: [u8; BUFFER_SIZE],
    rotation: DisplayRotation,
    _color: core::marker::PhantomData<C>,
}

/// Display buffer for the WeAct Studio 2.9 inch B/W display.
pub type Display290BlackWhite = Display<128, 296, { buffer_len::<Color>(128, 296) }, Color>;
/// Display buffer for the WeAct Studio 2.9 inch tri-color display.
pub type Display290TriColor = Display<128, 296, { buffer_len::<TriColor>(128, 296) }, TriColor>;
/// Display buffer for the WeAct Studio 2.13 inch B/W display.
///
/// The screen uses a 128 pixel wide buffer but only 122 pixels are visible.
pub type Display213BlackWhite = Display<128, 250, { buffer_len::<Color>(128, 250) }, Color>;
/// Display buffer for the WeAct Studio 2.13 inch tri-color display.
///
/// The screen uses a 128 pixel wide buffer but only 122 pixels are visible.
pub type Display213TriColor = Display<128, 250, { buffer_len::<TriColor>(128, 250) }, TriColor>;

/// Generically-sized B/W display buffer.
///
/// `WIDTH` must be a multiple of 8. `BUFFER_SIZE` can be calculated using [`buffer_len`].
pub type DisplayBlackWhite<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize> =
    Display<WIDTH, HEIGHT, BUFFER_SIZE, Color>;

/// Generically-sized tri-color display buffer.
///
/// `WIDTH` must be a multiple of 8. `BUFFER_SIZE` can be calculated using [`buffer_len`].
pub type DisplayTriColor<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize> =
    Display<WIDTH, HEIGHT, BUFFER_SIZE, TriColor>;

impl<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize>
    Display<WIDTH, HEIGHT, BUFFER_SIZE, Color>
{
    /// Creates a new display buffer filled with the default color.
    pub fn new() -> Self {
        Self {
            buffer: [Color::default().byte_value().0; BUFFER_SIZE],
            rotation: Default::default(),
            _color: core::marker::PhantomData,
        }
    }

    /// Get the internal buffer.
    pub fn buffer(&self) -> &[u8] {
        &self.buffer
    }

    /// Clear the display buffer with the given color.
    pub fn clear(&mut self, color: Color) {
        self.buffer.fill(color.byte_value().0);
    }
}

impl<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize> Default
    for Display<WIDTH, HEIGHT, BUFFER_SIZE, Color>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize>
    Display<WIDTH, HEIGHT, BUFFER_SIZE, TriColor>
{
    /// Creates a new display buffer filled with the default color.
    pub fn new() -> Self {
        let background_color = TriColor::default();

        let mut buffer = [0; BUFFER_SIZE];
        buffer[..(BUFFER_SIZE / 2)].fill(background_color.byte_value().0);
        buffer[(BUFFER_SIZE / 2)..].fill(background_color.byte_value().1);

        Self {
            buffer,
            rotation: Default::default(),
            _color: core::marker::PhantomData,
        }
    }

    /// Get the internal B/W buffer.
    pub fn bw_buffer(&self) -> &[u8] {
        &self.buffer[..(BUFFER_SIZE / 2)]
    }

    /// Get the internal red buffer.
    pub fn red_buffer(&self) -> &[u8] {
        &self.buffer[(BUFFER_SIZE / 2)..]
    }

    /// Clear the display buffer with the given color.
    pub fn clear(&mut self, color: TriColor) {
        self.buffer[..(BUFFER_SIZE / 2)].fill(color.byte_value().0);
        self.buffer[(BUFFER_SIZE / 2)..].fill(color.byte_value().1);
    }
}

impl<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize> Default
    for Display<WIDTH, HEIGHT, BUFFER_SIZE, TriColor>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize, C>
    Display<WIDTH, HEIGHT, BUFFER_SIZE, C>
where
    C: ColorType + PixelColor,
{
    /// Get the current rotation of the display.
    pub fn rotation(&self) -> DisplayRotation {
        self.rotation
    }

    /// Sets the rotation of the display.
    pub fn set_rotation(&mut self, rotation: DisplayRotation) {
        self.rotation = rotation;
    }

    fn set_pixel(&mut self, pixel: Pixel<C>) {
        // let rotation = self.rotation;
        let Pixel(point, color) = pixel;
        let Point { x, y } = point;

        if outside_display(point, WIDTH, HEIGHT, self.rotation) {
            return;
        }

        let (index, bit) =
            pixel_position_in_buffer(x as u32, y as u32, WIDTH, HEIGHT, self.rotation);
        let index = index as usize;
        let (bw_bit, red_bit) = color.bit_value();

        #[allow(clippy::collapsible_else_if)]
        if C::BUFFER_COUNT == 2 {
            if red_bit == 1 {
                // Red buffer takes precendence over B/W buffer so no need to update B/W buffer.
                self.buffer[index + BUFFER_SIZE / 2] |= bit;
            } else {
                if bw_bit == 1 {
                    self.buffer[index] |= bit;
                } else {
                    self.buffer[index] &= !bit;
                }
                self.buffer[index + BUFFER_SIZE / 2] &= !bit;
            }
        } else {
            if bw_bit == 1 {
                self.buffer[index] |= bit;
            } else {
                self.buffer[index] &= !bit;
            }
        }
    }
}

impl<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize> DrawTarget
    for Display<WIDTH, HEIGHT, BUFFER_SIZE, Color>
{
    type Color = Color;
    type Error = Infallible;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        for p in pixels.into_iter() {
            self.set_pixel(p);
        }
        Ok(())
    }

    fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
        self.clear(color);
        Ok(())
    }
}

impl<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize> DrawTarget
    for Display<WIDTH, HEIGHT, BUFFER_SIZE, TriColor>
{
    type Color = TriColor;
    type Error = Infallible;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        for p in pixels.into_iter() {
            self.set_pixel(p);
        }
        Ok(())
    }

    fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
        self.clear(color);
        Ok(())
    }
}

impl PixelColor for Color {
    type Raw = ();
}

impl PixelColor for TriColor {
    type Raw = ();
}

impl<const WIDTH: u32, const HEIGHT: u32, const BUFFER_SIZE: usize, C> OriginDimensions
    for Display<WIDTH, HEIGHT, BUFFER_SIZE, C>
where
    C: PixelColor + ColorType,
{
    fn size(&self) -> Size {
        //if display is rotated 90 deg or 270 then swap height and width
        match self.rotation() {
            DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => Size::new(WIDTH, HEIGHT),
            DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => Size::new(HEIGHT, WIDTH),
        }
    }
}

fn outside_display(p: Point, width: u32, height: u32, rotation: DisplayRotation) -> bool {
    if p.x < 0 || p.y < 0 {
        return true;
    }
    let (x, y) = (p.x as u32, p.y as u32);
    match rotation {
        DisplayRotation::Rotate0 | DisplayRotation::Rotate180 if x >= width || y >= height => true,
        DisplayRotation::Rotate90 | DisplayRotation::Rotate270 if y >= width || x >= height => true,
        _ => false,
    }
}

/// Returns the position of the pixel in the (single color) buffer.
///
/// Return type is (byte index, bit)
fn pixel_position_in_buffer(
    x: u32,
    y: u32,
    width: u32,
    height: u32,
    rotation: DisplayRotation,
) -> (u32, u8) {
    let (nx, ny) = find_rotation(x, y, width, height, rotation);
    (nx / 8 + bytes_per_line(width) * ny, 0x80 >> (nx % 8))
}

fn find_rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> (u32, u32) {
    match rotation {
        DisplayRotation::Rotate0 => (x, y),
        DisplayRotation::Rotate90 => (width - 1 - y, x),
        DisplayRotation::Rotate180 => (width - 1 - x, height - 1 - y),
        DisplayRotation::Rotate270 => (y, height - 1 - x),
    }
}

const fn bytes_per_line(width: u32) -> u32 {
    (width + 7) / 8
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pixels_are_set_correctly_in_both_buffers_when_creating_new_tri_color_display() {
        let display = Display::<8, 1, 2, TriColor>::new();
        assert_eq!(display.buffer.len(), 2);

        assert_eq!(
            display.buffer[0], 0b1111_1111,
            "B/W buffer has incorrect value"
        );
        assert_eq!(
            display.buffer[1], 0b0000_0000,
            "Red buffer has incorrect value"
        );
    }

    #[test]
    fn pixel_is_set_in_bw_buffer_when_drawing_black() {
        let mut display = Display::<8, 1, 2, TriColor>::new();

        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Black));

        assert_eq!(
            display.buffer[0], 0b0111_1111,
            "B/W buffer has incorrect value"
        );
        assert_eq!(
            display.buffer[1], 0b0000_0000,
            "Red buffer has incorrect value"
        );
    }

    #[test]
    fn pixel_is_set_in_both_buffers_when_drawing_red() {
        let mut display = Display::<8, 1, 2, TriColor>::new();

        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Red));

        assert_eq!(
            display.buffer[0], 0b1111_1111,
            "B/W buffer has incorrect value"
        );
        assert_eq!(
            display.buffer[1], 0b1000_0000,
            "Red buffer has incorrect value"
        );
    }

    #[test]
    fn pixel_is_set_in_both_buffers_when_drawing_red_then_black() {
        let mut display = Display::<8, 1, 2, TriColor>::new();

        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Red));
        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Black));

        assert_eq!(
            display.buffer[0], 0b0111_1111,
            "B/W buffer has incorrect value"
        );
        assert_eq!(
            display.buffer[1], 0b0000_0000,
            "Red buffer has incorrect value"
        );
    }

    #[test]
    fn pixel_is_set_in_both_buffers_when_drawing_red_black_red() {
        let mut display = Display::<8, 1, 2, TriColor>::new();

        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Red));
        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Black));
        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Red));

        assert_eq!(
            display.buffer[0], 0b0111_1111,
            "B/W buffer has incorrect value"
        );
        assert_eq!(
            display.buffer[1], 0b1000_0000,
            "Red buffer has incorrect value"
        );
    }

    #[test]
    fn clear_sets_both_buffers() {
        let mut display = Display::<8, 1, 2, TriColor>::new();

        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Black));
        display.set_pixel(Pixel(Point::new(0, 0), TriColor::Red));

        display.clear(TriColor::White);
        assert_eq!(
            display.buffer[0], 0b1111_1111,
            "B/W buffer has incorrect value"
        );
        assert_eq!(
            display.buffer[1], 0b0000_0000,
            "Red buffer has incorrect value"
        );

        display.clear(TriColor::Red);
        assert_eq!(
            display.buffer[0], 0b0000_0000,
            "B/W buffer has incorrect value"
        );
        assert_eq!(
            display.buffer[1], 0b1111_1111,
            "Red buffer has incorrect value"
        );
    }
}