firmware for my Touchscreen E-Paper Input Module for Framework Laptop 16
1use core::fmt::{Display, Formatter};
2use core::mem::MaybeUninit;
3use crate::{syscall, SafeOption};
4use crate::syscall::SyscallNumber;
5
6#[repr(usize)]
7#[derive(Copy, Clone, Debug, Eq, PartialEq)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9pub enum InputSyscall {
10 NextEvent = 0,
11 SetTouchEnabled = 1,
12 HasEvent = 2,
13}
14
15impl TryFrom<usize> for InputSyscall {
16 type Error = ();
17
18 fn try_from(value: usize) -> Result<Self, Self::Error> {
19 match value {
20 x if x == InputSyscall::NextEvent as usize => Ok(InputSyscall::NextEvent),
21 x if x == InputSyscall::SetTouchEnabled as usize => Ok(InputSyscall::SetTouchEnabled),
22 x if x == InputSyscall::HasEvent as usize => Ok(InputSyscall::HasEvent),
23 _ => Err(()),
24 }
25 }
26}
27
28#[repr(C)]
29#[cfg_attr(feature = "defmt", derive(defmt::Format))]
30#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
31pub enum Event {
32 Touch(TouchEvent),
33 RefreshFinished,
34}
35
36#[repr(u8)]
37#[cfg_attr(feature = "defmt", derive(defmt::Format))]
38#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
39pub enum TouchEventType {
40 Down,
41 Up,
42 Move,
43}
44
45#[repr(C)]
46#[cfg_attr(feature = "defmt", derive(defmt::Format))]
47#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
48pub struct TouchEvent {
49 pub ev_type: TouchEventType,
50 pub x: u16,
51 pub y: u16,
52}
53
54impl TouchEvent {
55 pub const fn new() -> Self {
56 Self {
57 ev_type: TouchEventType::Down,
58 x: u16::MAX,
59 y: u16::MAX,
60 }
61 }
62
63 #[cfg(feature = "embedded-graphics")]
64 pub fn eg_point(&self) -> embedded_graphics::prelude::Point {
65 embedded_graphics::prelude::Point::new(self.x as i32, self.y as i32)
66 }
67}
68
69impl Display for TouchEvent {
70 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
71 let ty = match self.ev_type {
72 TouchEventType::Down => "Down",
73 TouchEventType::Up => "Up",
74 TouchEventType::Move => "Move",
75 };
76 write!(f, "{ty} @ ({}, {})", self.x, self.y)
77 }
78}
79
80pub fn next_event() -> Option<Event> {
81 let mut event: MaybeUninit<SafeOption<Event>> = MaybeUninit::uninit();
82
83 unsafe {
84 syscall!(
85 SyscallNumber::Input,
86 in InputSyscall::NextEvent,
87 in event.as_mut_ptr(),
88 );
89
90 event.assume_init().into()
91 }
92}
93
94pub fn set_touch_enabled(enabled: bool) {
95 unsafe {
96 syscall!(
97 SyscallNumber::Input,
98 in InputSyscall::SetTouchEnabled,
99 in enabled,
100 );
101 }
102}
103
104pub fn has_event() -> bool {
105 let mut has_event: usize;
106
107 unsafe {
108 syscall!(
109 SyscallNumber::Input,
110 out has_event in InputSyscall::HasEvent,
111 );
112 }
113
114 has_event != 0
115}