firmware for my Touchscreen E-Paper Input Module for Framework Laptop 16
1#![no_std]
2
3use core::fmt::{Display, Formatter};
4pub use tp370pgh01::IMAGE_BYTES;
5
6/// Option type with stable ABI.
7#[repr(C)]
8pub enum SafeOption<T> {
9 None,
10 Some(T),
11}
12
13impl<T> From<Option<T>> for SafeOption<T> {
14 fn from(value: Option<T>) -> Self {
15 match value {
16 None => SafeOption::None,
17 Some(v) => SafeOption::Some(v),
18 }
19 }
20}
21
22impl<T> From<SafeOption<T>> for Option<T> {
23 fn from(value: SafeOption<T>) -> Self {
24 match value {
25 SafeOption::None => None,
26 SafeOption::Some(v) => Some(v),
27 }
28 }
29}
30
31#[repr(C)]
32#[derive(Copy, Clone)]
33pub struct ProgramFunctionTable {
34 pub write_image: extern "C" fn(&[u8; IMAGE_BYTES]),
35 pub refresh: extern "C" fn(bool, bool),
36 pub next_touch_event: extern "C" fn() -> SafeOption<TouchEvent>,
37 pub set_touch_enabled: unsafe extern "C" fn(bool),
38 pub serial_number: extern "C" fn() -> &'static [u8; 16]
39}
40
41#[repr(u8)]
42#[derive(Copy, Clone, Debug, defmt::Format)]
43pub enum TouchEventType {
44 Down,
45 Up,
46 Move,
47}
48
49#[repr(C)]
50#[derive(Copy, Clone, Debug, defmt::Format)]
51pub struct TouchEvent {
52 pub ev_type: TouchEventType,
53 pub x: u16,
54 pub y: u16,
55}
56
57impl TouchEvent {
58 pub const fn new() -> Self {
59 Self {
60 ev_type: TouchEventType::Down,
61 x: u16::MAX,
62 y: u16::MAX,
63 }
64 }
65
66 #[cfg(feature = "embedded-graphics")]
67 pub fn eg_point(&self) -> embedded_graphics::prelude::Point {
68 embedded_graphics::prelude::Point::new(self.x as i32, self.y as i32)
69 }
70}
71
72impl Display for TouchEvent {
73 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
74 let ty = match self.ev_type {
75 TouchEventType::Down => "Down",
76 TouchEventType::Up => "Up",
77 TouchEventType::Move => "Move",
78 };
79 write!(f, "{ty} @ ({}, {})", self.x, self.y)
80 }
81}