5
5
//! Operations on 10-bit slave addresses are not supported by the API yet (but applications might
6
6
//! be able to emulate some operations).
7
7
8
+ use crate :: private;
9
+
10
+ /// Address mode (7-bit / 10-bit)
11
+ ///
12
+ /// Note: This trait is sealed and should not be implemented outside of this crate.
13
+ pub trait AddressMode : private:: Sealed {
14
+ /// Address value type
15
+ type Address ;
16
+ }
17
+
18
+ /// 7-bit address mode type
19
+ pub struct SevenBitAddress ( ( ) ) ;
20
+
21
+ /// 10-bit address mode type
22
+ pub struct TenBitAddress ( ( ) ) ;
23
+
24
+ impl AddressMode for SevenBitAddress {
25
+ type Address = u8 ;
26
+ }
27
+
28
+ impl AddressMode for TenBitAddress {
29
+ type Address = u16 ;
30
+ }
31
+
8
32
/// Blocking read
9
- pub trait Read {
33
+ pub trait Read < A : AddressMode > {
10
34
/// Error type
11
35
type Error ;
12
36
@@ -28,11 +52,11 @@ pub trait Read {
28
52
/// - `MAK` = master acknowledge
29
53
/// - `NMAK` = master no acknowledge
30
54
/// - `SP` = stop condition
31
- fn try_read ( & mut self , address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > ;
55
+ fn try_read ( & mut self , address : A :: Address , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > ;
32
56
}
33
57
34
58
/// Blocking write
35
- pub trait Write {
59
+ pub trait Write < A : AddressMode > {
36
60
/// Error type
37
61
type Error ;
38
62
@@ -52,11 +76,11 @@ pub trait Write {
52
76
/// - `SAK` = slave acknowledge
53
77
/// - `Bi` = ith byte of data
54
78
/// - `SP` = stop condition
55
- fn try_write ( & mut self , address : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > ;
79
+ fn try_write ( & mut self , address : A :: Address , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > ;
56
80
}
57
81
58
82
/// Blocking write (iterator version)
59
- pub trait WriteIter {
83
+ pub trait WriteIter < A : AddressMode > {
60
84
/// Error type
61
85
type Error ;
62
86
@@ -65,13 +89,13 @@ pub trait WriteIter {
65
89
/// # I2C Events (contract)
66
90
///
67
91
/// Same as `Write`
68
- fn try_write < B > ( & mut self , address : u8 , bytes : B ) -> Result < ( ) , Self :: Error >
92
+ fn try_write < B > ( & mut self , address : A :: Address , bytes : B ) -> Result < ( ) , Self :: Error >
69
93
where
70
94
B : IntoIterator < Item = u8 > ;
71
95
}
72
96
73
97
/// Blocking write + read
74
- pub trait WriteRead {
98
+ pub trait WriteRead < A : AddressMode > {
75
99
/// Error type
76
100
type Error ;
77
101
@@ -99,14 +123,14 @@ pub trait WriteRead {
99
123
/// - `SP` = stop condition
100
124
fn try_write_read (
101
125
& mut self ,
102
- address : u8 ,
126
+ address : A :: Address ,
103
127
bytes : & [ u8 ] ,
104
128
buffer : & mut [ u8 ] ,
105
129
) -> Result < ( ) , Self :: Error > ;
106
130
}
107
131
108
132
/// Blocking write (iterator version) + read
109
- pub trait WriteIterRead {
133
+ pub trait WriteIterRead < A : AddressMode > {
110
134
/// Error type
111
135
type Error ;
112
136
@@ -118,7 +142,7 @@ pub trait WriteIterRead {
118
142
/// Same as the `WriteRead` trait
119
143
fn try_write_iter_read < B > (
120
144
& mut self ,
121
- address : u8 ,
145
+ address : A :: Address ,
122
146
bytes : B ,
123
147
buffer : & mut [ u8 ] ,
124
148
) -> Result < ( ) , Self :: Error >
0 commit comments