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
use core::time::Duration;
use embedded_hal::blocking::delay::DelayMs;
use crate::{Transmit, Receive, Power, State};
pub struct BlockingOptions {
pub power: Option<i8>,
pub poll_interval: Duration,
pub timeout: Duration,
}
impl Default for BlockingOptions {
fn default() -> Self {
Self {
power: None,
poll_interval: Duration::from_millis(1),
timeout: Duration::from_millis(100),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum BlockingError<E> {
Inner(E),
Timeout,
}
impl <E> From<E> for BlockingError<E> {
fn from(e: E) -> Self {
BlockingError::Inner(e)
}
}
pub trait BlockingTransmit<E> {
fn do_transmit(&mut self, data: &[u8], tx_options: BlockingOptions) -> Result<(), BlockingError<E>>;
}
impl <T, E> BlockingTransmit<E> for T
where
T: Transmit<Error = E> + Power<Error = E> + DelayMs<u32>,
E: core::fmt::Debug,
{
fn do_transmit(&mut self, data: &[u8], tx_options: BlockingOptions) -> Result<(), BlockingError<E>> {
if let Some(p) = tx_options.power {
self.set_power(p)?;
}
self.start_transmit(data)?;
let t = tx_options.timeout.as_millis();
let mut c = 0;
loop {
if self.check_transmit()? {
debug!("Blocking send complete");
break;
}
c += tx_options.poll_interval.as_millis();
if c > t {
debug!("Blocking send timeout");
return Err(BlockingError::Timeout)
}
self.delay_ms(tx_options.poll_interval.as_millis() as u32);
}
Ok(())
}
}
pub trait BlockingReceive<I, E> {
fn do_receive(&mut self, buff: &mut [u8], info: &mut I, rx_options: BlockingOptions) -> Result<usize, BlockingError<E>>;
}
impl <T, I, E> BlockingReceive<I, E> for T
where
T: Receive<Info=I, Error=E> + DelayMs<u32>,
I: core::fmt::Debug,
E: core::fmt::Debug,
{
fn do_receive(&mut self, buff: &mut [u8], info: &mut I, rx_options: BlockingOptions) -> Result<usize, BlockingError<E>> {
self.start_receive()?;
let t = rx_options.timeout.as_millis();
let mut c = 0;
loop {
if self.check_receive(true)? {
let n = self.get_received(info, buff)?;
return Ok(n)
}
c += rx_options.poll_interval.as_millis();
if c > t {
debug!("Blocking receive timeout");
return Err(BlockingError::Timeout)
}
self.delay_ms(rx_options.poll_interval.as_millis() as u32);
}
}
}
pub trait BlockingSetState<S, E> {
fn set_state_checked(&mut self, state: S, options: BlockingOptions) -> Result<(), BlockingError<E>>;
}
impl <T, S, E>BlockingSetState<S, E> for T
where
T: State<State=S, Error=E> + DelayMs<u32>,
S: core::fmt::Debug + core::cmp::PartialEq + Copy,
E: core::fmt::Debug,
{
fn set_state_checked(&mut self, state: S, options: BlockingOptions) -> Result<(), BlockingError<E>> {
self.set_state(state)?;
let t = options.timeout.as_millis();
let mut c = 0;
loop {
let s = self.get_state()?;
if state == s {
return Ok(())
}
c += options.poll_interval.as_millis();
if c > t {
debug!("Blocking receive timeout");
return Err(BlockingError::Timeout)
}
self.delay_ms(options.poll_interval.as_millis() as u32);
}
}
}