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
use core::convert::TryFrom;
use core::future::Future;
mod sealed {
pub trait I2cAddressType {}
}
use sealed::I2cAddressType;
#[derive(Debug)]
pub struct AddressRangeError;
pub struct I2cAddress7Bit(u8);
impl TryFrom<u8> for I2cAddress7Bit {
type Error = AddressRangeError;
#[inline]
fn try_from(address: u8) -> Result<Self, Self::Error> {
if address < 0x80 {
Ok(I2cAddress7Bit(address))
} else {
Err(AddressRangeError)
}
}
}
impl From<I2cAddress7Bit> for u8 {
#[inline(always)]
fn from(address: I2cAddress7Bit) -> Self {
address.0
}
}
impl I2cAddressType for I2cAddress7Bit {}
pub struct I2cAddress10Bit(u16);
impl TryFrom<u16> for I2cAddress10Bit {
type Error = AddressRangeError;
#[inline]
fn try_from(address: u16) -> Result<Self, Self::Error> {
if address < 0x400 {
Ok(I2cAddress10Bit(address))
} else {
Err(AddressRangeError)
}
}
}
impl From<I2cAddress10Bit> for u16 {
#[inline(always)]
fn from(address: I2cAddress10Bit) -> Self {
address.0
}
}
impl I2cAddressType for I2cAddress10Bit {}
pub trait AsyncI2cTransfer<A: I2cAddressType> {
type Error;
type TransferFuture<'f>: Future<Output = Result<(), Self::Error>>;
fn async_transfer<'a>(
&'a mut self,
address: A,
tx_data: &'a [u8],
rx_data: &'a mut [u8],
) -> Self::TransferFuture<'a>;
}
pub trait AsyncI2cWrite<A: I2cAddressType> {
type Error;
type WriteFuture<'f>: Future<Output = Result<(), Self::Error>>;
fn async_write<'a>(&'a mut self, address: A, data: &'a [u8]) -> Self::WriteFuture<'_>;
}
pub trait AsyncI2cRead<A: I2cAddressType> {
type Error;
type ReadFuture<'f>: Future<Output = Result<(), Self::Error>>;
fn async_read<'a>(&'a mut self, address: A, data: &'a mut [u8]) -> Self::ReadFuture<'_>;
}