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 eepy_sys::image::{maybe_refresh, refresh};
8use tp370pgh01::{DIM_X, DIM_Y, IMAGE_BYTES};
9
10pub struct EpdDrawTarget {
11 pub framebuffer: [u8; IMAGE_BYTES],
12}
13
14impl Dimensions for EpdDrawTarget {
15 fn bounding_box(&self) -> Rectangle {
16 Rectangle::new(Point::new(0, 0), Size::new(DIM_X as u32, DIM_Y as u32))
17 }
18}
19
20impl DrawTarget for EpdDrawTarget {
21 type Color = BinaryColor;
22 type Error = Infallible;
23
24 fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
25 where
26 I: IntoIterator<Item = Pixel<Self::Color>>,
27 {
28 for Pixel(point @ Point { x, y }, colour) in pixels {
29 if !self.bounding_box().contains(point) {
30 continue;
31 }
32
33 let bit_index = (y as usize) * 240 + (x as usize);
34 let i = bit_index >> 3;
35 let j = 1 << (bit_index & 0x07);
36
37 match colour {
38 BinaryColor::Off => self.framebuffer[i] &= !j,
39 BinaryColor::On => self.framebuffer[i] |= j,
40 }
41 }
42
43 Ok(())
44 }
45
46 fn clear(&mut self, colour: Self::Color) -> Result<(), Self::Error> {
47 match colour {
48 BinaryColor::Off => self.framebuffer.copy_from_slice(&[0; IMAGE_BYTES]),
49 BinaryColor::On => self.framebuffer.copy_from_slice(&[u8::MAX; IMAGE_BYTES]),
50 }
51
52 Ok(())
53 }
54}
55
56impl Default for EpdDrawTarget {
57 fn default() -> Self {
58 Self {
59 framebuffer: [0u8; IMAGE_BYTES],
60 }
61 }
62}
63
64impl EpdDrawTarget {
65 pub fn refresh(&self, fast_refresh: bool) {
66 refresh(&self.framebuffer, fast_refresh);
67 }
68
69 pub fn maybe_refresh(&self, fast_refresh: bool) {
70 maybe_refresh(&self.framebuffer, fast_refresh);
71 }
72}