firmware for my Touchscreen E-Paper Input Module for Framework Laptop 16
1#![no_std]
2
3pub use eepy_sys::input_common::Event;
4
5use core::fmt::{Display, Formatter};
6
7#[repr(u8)]
8#[derive(Copy, Clone, Debug, Eq, PartialEq)]
9pub enum SerialCommand {
10 /// Refresh the screen. Must be followed by exactly 12480 bytes of image data.
11 RefreshNormal = 0,
12 /// Fast refresh the screen. Must be followed by exactly 12480 bytes of image data.
13 RefreshFast = 1,
14
15 /// Enter Host App mode. In this mode, events will not be processed by the builtin UI and can
16 /// instead be retrieved by host programs using the [SerialCommand::NextEvent] command. This
17 /// mode should be used by all host programs writing images to the display.
18 EnterHostApp = 2,
19 /// Exit Host App mode (see [SerialCommand::EnterHostApp]).
20 ExitHostApp = 3,
21
22 /// Get the next event. Only works in Host App mode.
23 NextEvent = 4,
24
25 /// Disable touch. Touch events will no longer be added to the event queue. Only works in Host
26 /// App mode.
27 DisableTouch = 5,
28 /// Enable touch.
29 EnableTouch = 6,
30
31 /// Get the program slot that will be used to store the next uploaded program. This command is
32 /// only available when not in Host App mode.
33 GetProgramSlot = 7,
34 /// Upload a program. The program will be stored in the slot returned by the previous
35 /// GetProgramSlot call. The program uploaded must be linked correctly for the
36 /// slot. Only available when not in Host App mode.
37 UploadProgram = 8,
38}
39
40impl TryFrom<u8> for SerialCommand {
41 type Error = ();
42
43 fn try_from(value: u8) -> Result<Self, Self::Error> {
44 match value {
45 x if x == SerialCommand::RefreshNormal as u8 => Ok(SerialCommand::RefreshNormal),
46 x if x == SerialCommand::RefreshFast as u8 => Ok(SerialCommand::RefreshFast),
47 x if x == SerialCommand::EnterHostApp as u8 => Ok(SerialCommand::EnterHostApp),
48 x if x == SerialCommand::ExitHostApp as u8 => Ok(SerialCommand::ExitHostApp),
49 x if x == SerialCommand::NextEvent as u8 => Ok(SerialCommand::NextEvent),
50 x if x == SerialCommand::DisableTouch as u8 => Ok(SerialCommand::DisableTouch),
51 x if x == SerialCommand::EnableTouch as u8 => Ok(SerialCommand::EnableTouch),
52 x if x == SerialCommand::GetProgramSlot as u8 => Ok(SerialCommand::GetProgramSlot),
53 x if x == SerialCommand::UploadProgram as u8 => Ok(SerialCommand::UploadProgram),
54
55 _ => Err(()),
56 }
57 }
58}
59
60#[repr(u8)]
61#[derive(Copy, Clone, Debug, Eq, PartialEq)]
62pub enum Response {
63 UnknownCommand = 0x00,
64 IncorrectMode = 0x01,
65 ProgramSlotsFull = 0x02,
66 NoProgramSlot = 0x03,
67 Ack = 0xff,
68}
69
70impl From<Response> for Result<(), SerialError> {
71 fn from(value: Response) -> Self {
72 match value {
73 Response::UnknownCommand => Err(SerialError::UnknownCommand),
74 Response::IncorrectMode => Err(SerialError::IncorrectMode),
75 Response::ProgramSlotsFull => Err(SerialError::ProgramSlotsFull),
76 Response::NoProgramSlot => Err(SerialError::NoProgramSlot),
77 Response::Ack => Ok(()),
78 }
79 }
80}
81
82impl Display for Response {
83 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
84 match self {
85 Response::UnknownCommand => write!(f, "Invalid command"),
86 Response::IncorrectMode => write!(f, "Incorrect mode"),
87 Response::ProgramSlotsFull => write!(f, "No program slot is available"),
88 Response::NoProgramSlot => write!(f, "Call GetProgramSlot before UploadProgram"),
89 Response::Ack => write!(f, "Success"),
90 }
91 }
92}
93
94impl TryFrom<u8> for Response {
95 type Error = ();
96
97 fn try_from(value: u8) -> Result<Self, Self::Error> {
98 match value {
99 x if x == Response::UnknownCommand as u8 => Ok(Response::UnknownCommand),
100 x if x == Response::IncorrectMode as u8 => Ok(Response::IncorrectMode),
101 x if x == Response::ProgramSlotsFull as u8 => Ok(Response::ProgramSlotsFull),
102 x if x == Response::NoProgramSlot as u8 => Ok(Response::NoProgramSlot),
103 x if x == Response::Ack as u8 => Ok(Response::Ack),
104
105 _ => Err(()),
106 }
107 }
108}
109
110#[derive(Copy, Clone, Debug, Eq, PartialEq)]
111pub enum SerialError {
112 UnknownCommand,
113 IncorrectMode,
114 ProgramSlotsFull,
115 NoProgramSlot,
116}
117
118impl Display for SerialError {
119 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
120 match self {
121 SerialError::UnknownCommand => write!(f, "Invalid command"),
122 SerialError::IncorrectMode => write!(f, "Incorrect mode"),
123 SerialError::ProgramSlotsFull => write!(f, "No program slot is available"),
124 SerialError::NoProgramSlot => write!(f, "Call GetProgramSlot before UploadProgram"),
125 }
126 }
127}
128
129impl core::error::Error for SerialError {}