firmware for my Touchscreen E-Paper Input Module for Framework Laptop 16
1use core::convert::Infallible;
2use embedded_graphics::prelude::*;
3use embedded_graphics::geometry::Dimensions;
4use embedded_graphics::Pixel;
5use embedded_graphics::pixelcolor::BinaryColor;
6use embedded_graphics::primitives::Rectangle;
7use tp370pgh01::{DIM_X, DIM_Y, IMAGE_BYTES};
8use crate::ProgramFunctionTable;
9
10pub struct EpdDrawTarget {
11 functions: ProgramFunctionTable,
12 buf: [u8; IMAGE_BYTES],
13}
14
15impl Dimensions for EpdDrawTarget {
16 fn bounding_box(&self) -> Rectangle {
17 Rectangle::new(Point::new(0, 0), Size::new(DIM_X as u32, DIM_Y as u32))
18 }
19}
20
21impl DrawTarget for EpdDrawTarget {
22 type Color = BinaryColor;
23 type Error = Infallible;
24
25 fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
26 where
27 I: IntoIterator<Item = Pixel<Self::Color>>,
28 {
29 for Pixel(point @ Point { x, y }, colour) in pixels {
30 if !self.bounding_box().contains(point) {
31 continue;
32 }
33
34 let bit_index = (y as usize) * 240 + (x as usize);
35 let i = bit_index >> 3;
36 let j = 1 << (bit_index & 0x07);
37
38 match colour {
39 BinaryColor::Off => self.buf[i] &= !j,
40 BinaryColor::On => self.buf[i] |= j,
41 }
42 }
43
44 Ok(())
45 }
46
47 fn clear(&mut self, colour: Self::Color) -> Result<(), Self::Error> {
48 match colour {
49 BinaryColor::Off => self.buf.copy_from_slice(&[0; IMAGE_BYTES]),
50 BinaryColor::On => self.buf.copy_from_slice(&[u8::MAX; IMAGE_BYTES]),
51 }
52
53 Ok(())
54 }
55}
56
57impl EpdDrawTarget {
58 pub const fn new(functions: ProgramFunctionTable) -> Self {
59 Self { functions, buf: [0; IMAGE_BYTES] }
60 }
61
62 pub fn refresh(&self) {
63 (self.functions.write_image)(&self.buf);
64 (self.functions.refresh)();
65 }
66
67 pub fn refresh_fast(&self) {
68 (self.functions.write_image)(&self.buf);
69 (self.functions.refresh_fast)();
70 }
71}