2
2
3
3
use core:: { convert:: From , ops:: Not } ;
4
4
5
+ /// GPIO error type trait
6
+ ///
7
+ /// This just defines the error type, to be used by the other traits.
8
+ pub trait ErrorType {
9
+ /// Error type
10
+ type Error : core:: fmt:: Debug ;
11
+ }
12
+
13
+ impl < T : ErrorType > ErrorType for & T {
14
+ type Error = T :: Error ;
15
+ }
16
+ impl < T : ErrorType > ErrorType for & mut T {
17
+ type Error = T :: Error ;
18
+ }
19
+
5
20
/// Digital output pin state
6
21
///
7
22
/// Conversion from `bool` and logical negation are also implemented
@@ -45,10 +60,7 @@ pub mod blocking {
45
60
use super :: PinState ;
46
61
47
62
/// Single digital push-pull output pin
48
- pub trait OutputPin {
49
- /// Error type
50
- type Error : core:: fmt:: Debug ;
51
-
63
+ pub trait OutputPin : super :: ErrorType {
52
64
/// Drives the pin low
53
65
///
54
66
/// *NOTE* the actual electrical state of the pin may not actually be low, e.g. due to external
@@ -74,8 +86,6 @@ pub mod blocking {
74
86
}
75
87
76
88
impl < T : OutputPin > OutputPin for & mut T {
77
- type Error = T :: Error ;
78
-
79
89
fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
80
90
T :: set_low ( self )
81
91
}
@@ -113,27 +123,19 @@ pub mod blocking {
113
123
}
114
124
115
125
/// Output pin that can be toggled
116
- pub trait ToggleableOutputPin {
117
- /// Error type
118
- type Error : core:: fmt:: Debug ;
119
-
126
+ pub trait ToggleableOutputPin : super :: ErrorType {
120
127
/// Toggle pin output.
121
128
fn toggle ( & mut self ) -> Result < ( ) , Self :: Error > ;
122
129
}
123
130
124
131
impl < T : ToggleableOutputPin > ToggleableOutputPin for & mut T {
125
- type Error = T :: Error ;
126
-
127
132
fn toggle ( & mut self ) -> Result < ( ) , Self :: Error > {
128
133
T :: toggle ( self )
129
134
}
130
135
}
131
136
132
137
/// Single digital input pin
133
- pub trait InputPin {
134
- /// Error type
135
- type Error : core:: fmt:: Debug ;
136
-
138
+ pub trait InputPin : super :: ErrorType {
137
139
/// Is the input pin high?
138
140
fn is_high ( & self ) -> Result < bool , Self :: Error > ;
139
141
@@ -142,8 +144,6 @@ pub mod blocking {
142
144
}
143
145
144
146
impl < T : InputPin > InputPin for & T {
145
- type Error = T :: Error ;
146
-
147
147
fn is_high ( & self ) -> Result < bool , Self :: Error > {
148
148
T :: is_high ( self )
149
149
}
@@ -179,14 +179,11 @@ pub mod blocking {
179
179
/// pin.is_high()
180
180
/// }
181
181
/// ```
182
- pub trait IoPin < TInput , TOutput >
182
+ pub trait IoPin < TInput , TOutput > : super :: ErrorType
183
183
where
184
- TInput : InputPin + IoPin < TInput , TOutput > ,
185
- TOutput : OutputPin + IoPin < TInput , TOutput > ,
184
+ TInput : super :: ErrorType < Error = Self :: Error > + InputPin + IoPin < TInput , TOutput > ,
185
+ TOutput : super :: ErrorType < Error = Self :: Error > + OutputPin + IoPin < TInput , TOutput > ,
186
186
{
187
- /// Error type.
188
- type Error : core:: fmt:: Debug ;
189
-
190
187
/// Tries to convert this pin to input mode.
191
188
///
192
189
/// If the pin is already in input mode, this method should succeed.
0 commit comments