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
41impl ProgramFunctionTable {
42 pub fn serial_number_str(&self) -> &'static str {
43 unsafe { core::str::from_utf8_unchecked((self.serial_number)()) }
44 }
45}
46
47#[repr(u8)]
48#[derive(Copy, Clone, Debug, defmt::Format)]
49pub enum TouchEventType {
50 Down,
51 Up,
52 Move,
53}
54
55#[repr(C)]
56#[derive(Copy, Clone, Debug, defmt::Format)]
57pub struct TouchEvent {
58 pub ev_type: TouchEventType,
59 pub x: u16,
60 pub y: u16,
61}
62
63impl TouchEvent {
64 pub const fn new() -> Self {
65 Self {
66 ev_type: TouchEventType::Down,
67 x: u16::MAX,
68 y: u16::MAX,
69 }
70 }
71
72 #[cfg(feature = "embedded-graphics")]
73 pub fn eg_point(&self) -> embedded_graphics::prelude::Point {
74 embedded_graphics::prelude::Point::new(self.x as i32, self.y as i32)
75 }
76}
77
78impl Display for TouchEvent {
79 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
80 let ty = match self.ev_type {
81 TouchEventType::Down => "Down",
82 TouchEventType::Up => "Up",
83 TouchEventType::Move => "Move",
84 };
85 write!(f, "{ty} @ ({}, {})", self.x, self.y)
86 }
87}