1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#[cfg(feature = "snapshot")]
use serde::{Serialize, Deserialize};
mod keypad;
mod rs232;
pub use rs232::*;
pub use keypad::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum DataState {
Space = 0,
Mark = 1
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum ControlState {
Active = 0,
Inactive = 1
}
pub trait SerialPortDevice {
type Timestamp: Sized;
fn write_data(&mut self, rxd: DataState, timestamp: Self::Timestamp) -> ControlState;
fn poll_ready(&mut self, timestamp: Self::Timestamp) -> ControlState;
fn update_cts(&mut self, cts: ControlState, timestamp: Self::Timestamp);
fn read_data(&mut self, timestamp: Self::Timestamp) -> DataState;
fn next_frame(&mut self, eof_timestamp: Self::Timestamp);
}
impl DataState {
#[inline]
pub fn is_space(self) -> bool {
self == DataState::Space
}
#[inline]
pub fn is_mark(self) -> bool {
self == DataState::Mark
}
}
impl ControlState {
#[inline]
pub fn is_active(self) -> bool {
self == ControlState::Active
}
#[inline]
pub fn is_inactive(self) -> bool {
self == ControlState::Inactive
}
}
impl From<DataState> for bool {
#[inline]
fn from(ds: DataState) -> bool {
match ds {
DataState::Space => false,
DataState::Mark => true,
}
}
}
impl From<DataState> for u8 {
#[inline]
fn from(ds: DataState) -> u8 {
ds as u8
}
}
impl From<bool> for DataState {
#[inline]
fn from(flag: bool) -> DataState {
if flag {
DataState::Mark
}
else {
DataState::Space
}
}
}
impl From<ControlState> for bool {
#[inline]
fn from(cs: ControlState) -> bool {
match cs {
ControlState::Active => false,
ControlState::Inactive => true,
}
}
}
impl From<bool> for ControlState {
#[inline]
fn from(flag: bool) -> ControlState {
if flag {
ControlState::Inactive
}
else {
ControlState::Active
}
}
}
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
pub struct NullSerialPort<T>(core::marker::PhantomData<T>);
impl<T> SerialPortDevice for NullSerialPort<T> {
type Timestamp = T;
#[inline(always)]
fn write_data(&mut self, _rxd: DataState, _timestamp: Self::Timestamp) -> ControlState {
ControlState::Inactive
}
#[inline(always)]
fn poll_ready(&mut self, _timestamp: Self::Timestamp) -> ControlState {
ControlState::Inactive
}
#[inline(always)]
fn update_cts(&mut self, _cts: ControlState, _timestamp: Self::Timestamp) {}
#[inline(always)]
fn read_data(&mut self, _timestamp: Self::Timestamp) -> DataState {
DataState::Mark
}
#[inline(always)]
fn next_frame(&mut self, _eof_timestamp: Self::Timestamp) {}
}