#[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, 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)]
#[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, _timestamp: Self::Timestamp) {}
}