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};
8
9pub struct EpdDrawTarget {
10 write_image: extern "C" fn(&[u8; IMAGE_BYTES]),
11 refresh: extern "C" fn(bool, bool),
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(write_image: extern "C" fn(&[u8; IMAGE_BYTES]), refresh: extern "C" fn(bool, bool)) -> Self {
59 Self {
60 write_image,
61 refresh,
62 buf: [0; IMAGE_BYTES]
63 }
64 }
65
66 pub fn refresh(&self, fast_refresh: bool, block_ack: bool) {
67 (self.write_image)(&self.buf);
68 (self.refresh)(fast_refresh, block_ack);
69 }
70}