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
use core::pin::Pin;
use core::task::Poll;
use futures::{future, ready, Future};
use crate::incoming_package::{IncomingPackage, IncomingStateMachine};
use crate::outgoing_package::{bytes_from_package, OutgoingPackage};
use crate::traits::AsyncReadWrite;
use crate::{Error, Result};
pub struct PulseOximeter<T: AsyncReadWrite + Unpin> {
port: T,
incoming: IncomingStateMachine,
outgoing: OutgoingStatus,
}
enum OutgoingStatus {
None,
Some {
buffer: [u8; 9],
already_sent: usize,
},
}
impl<T: AsyncReadWrite + Unpin> PulseOximeter<T> {
pub fn new(port: T) -> Self {
Self {
port,
incoming: IncomingStateMachine::None,
outgoing: OutgoingStatus::None,
}
}
#[allow(clippy::bool_assert_comparison)]
pub fn send_package<P>(&mut self, package: P) -> impl Future<Output = Result<(), T::Error>> + '_
where
P: OutgoingPackage,
{
let buffer = bytes_from_package(package);
let mut unfinished_send = matches!(self.outgoing, OutgoingStatus::Some { .. });
future::poll_fn(move |cx| loop {
match self.outgoing {
OutgoingStatus::None => {
self.outgoing = OutgoingStatus::Some {
buffer,
already_sent: 0,
}
}
OutgoingStatus::Some {
buffer,
ref mut already_sent,
} => {
let slice = &buffer[*already_sent..9];
let bytes_written = ready!(Pin::new(&mut self.port).poll_write(cx, slice))?;
if bytes_written > slice.len() {
return Err(Error::DeviceWriteTooMuch {
requested: slice.len(),
reported: bytes_written,
})
.into();
}
*already_sent += bytes_written;
if *already_sent == 9 {
self.outgoing = OutgoingStatus::None;
if unfinished_send {
unfinished_send = false;
} else {
return Poll::Ready(Ok(()));
}
}
}
}
})
}
pub fn receive_package(
&mut self,
) -> impl Future<Output = Result<IncomingPackage, T::Error>> + '_ {
future::poll_fn(move |cx| {
self.incoming.resume(|buf| Pin::new(&mut self.port).poll_read(cx, buf))
})
}
}