22//!
33//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5+ use std:: fmt;
6+
57/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
68///
79/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.5.0/gpio_cdev/struct.LineHandle.html
@@ -33,7 +35,7 @@ impl CdevPin {
3335 } else if self . 1 . is_open_source ( ) {
3436 flags. insert ( gpio_cdev:: LineRequestFlags :: OPEN_SOURCE ) ;
3537 }
36- return flags;
38+ flags
3739 }
3840
3941 /// Set this pin to input mode
@@ -113,6 +115,18 @@ impl From<gpio_cdev::errors::Error> for CdevPinError {
113115 }
114116}
115117
118+ impl fmt:: Display for CdevPinError {
119+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
120+ write ! ( f, "{}" , self . err)
121+ }
122+ }
123+
124+ impl std:: error:: Error for CdevPinError {
125+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
126+ Some ( & self . err )
127+ }
128+ }
129+
116130impl embedded_hal:: digital:: Error for CdevPinError {
117131 fn kind ( & self ) -> embedded_hal:: digital:: ErrorKind {
118132 use embedded_hal:: digital:: ErrorKind ;
@@ -125,7 +139,7 @@ impl embedded_hal::digital::ErrorType for CdevPin {
125139}
126140
127141impl embedded_hal:: digital:: OutputPin for CdevPin {
128- fn set_low ( & mut self ) -> Result < ( ) , CdevPinError > {
142+ fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
129143 self . 0
130144 . set_value ( state_to_value (
131145 embedded_hal:: digital:: PinState :: Low ,
@@ -134,7 +148,7 @@ impl embedded_hal::digital::OutputPin for CdevPin {
134148 . map_err ( CdevPinError :: from)
135149 }
136150
137- fn set_high ( & mut self ) -> Result < ( ) , CdevPinError > {
151+ fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
138152 self . 0
139153 . set_value ( state_to_value (
140154 embedded_hal:: digital:: PinState :: High ,
@@ -145,7 +159,7 @@ impl embedded_hal::digital::OutputPin for CdevPin {
145159}
146160
147161impl embedded_hal:: digital:: InputPin for CdevPin {
148- fn is_high ( & self ) -> Result < bool , CdevPinError > {
162+ fn is_high ( & self ) -> Result < bool , Self :: Error > {
149163 self . 0
150164 . get_value ( )
151165 . map ( |val| {
@@ -157,7 +171,7 @@ impl embedded_hal::digital::InputPin for CdevPin {
157171 . map_err ( CdevPinError :: from)
158172 }
159173
160- fn is_low ( & self ) -> Result < bool , CdevPinError > {
174+ fn is_low ( & self ) -> Result < bool , Self :: Error > {
161175 self . is_high ( ) . map ( |val| !val)
162176 }
163177}
0 commit comments