14
14
15
15
extern crate cast;
16
16
extern crate embedded_hal as hal;
17
+ #[ cfg( feature = "gpio_cdev" ) ]
18
+ pub extern crate gpio_cdev;
17
19
pub extern crate i2cdev;
18
20
pub extern crate nb;
19
21
pub extern crate serial_core;
20
22
pub extern crate serial_unix;
21
23
pub extern crate spidev;
24
+ #[ cfg( not( feature = "gpio_cdev" ) ) ]
22
25
pub extern crate sysfs_gpio;
23
26
24
27
use std:: io:: { self , Write } ;
@@ -92,11 +95,30 @@ impl hal::blocking::delay::DelayMs<u64> for Delay {
92
95
}
93
96
}
94
97
98
+ /// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
99
+ ///
100
+ /// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.LineHandle.html
101
+ #[ cfg( feature = "gpio_cdev" ) ]
102
+ pub struct Pin ( pub gpio_cdev:: LineHandle , pub bool ) ;
103
+
104
+ #[ cfg( feature = "gpio_cdev" ) ]
105
+ impl Pin {
106
+ /// See [`gpio_cdev::Line::request`][0] for details.
107
+ ///
108
+ /// [0]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.Line.html#method.request
109
+ pub fn new ( handle : gpio_cdev:: LineHandle ) -> Result < Pin , gpio_cdev:: errors:: Error > {
110
+ let info = handle. line ( ) . info ( ) ?;
111
+ Ok ( Pin ( handle, info. is_active_low ( ) ) )
112
+ }
113
+ }
114
+
95
115
/// Newtype around [`sysfs_gpio::Pin`] that implements the `embedded-hal` traits
96
116
///
97
117
/// [`sysfs_gpio::Pin`]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html
118
+ #[ cfg( not( feature = "gpio_cdev" ) ) ]
98
119
pub struct Pin ( pub sysfs_gpio:: Pin ) ;
99
120
121
+ #[ cfg( not( feature = "gpio_cdev" ) ) ]
100
122
impl Pin {
101
123
/// See [`sysfs_gpio::Pin::new`][0] for details.
102
124
///
@@ -117,6 +139,9 @@ impl Pin {
117
139
}
118
140
119
141
impl hal:: digital:: v2:: OutputPin for Pin {
142
+ #[ cfg( feature = "gpio_cdev" ) ]
143
+ type Error = gpio_cdev:: errors:: Error ;
144
+ #[ cfg( not( feature = "gpio_cdev" ) ) ]
120
145
type Error = sysfs_gpio:: Error ;
121
146
122
147
fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
@@ -129,8 +154,20 @@ impl hal::digital::v2::OutputPin for Pin {
129
154
}
130
155
131
156
impl hal:: digital:: v2:: InputPin for Pin {
157
+ #[ cfg( feature = "gpio_cdev" ) ]
158
+ type Error = gpio_cdev:: errors:: Error ;
159
+ #[ cfg( not( feature = "gpio_cdev" ) ) ]
132
160
type Error = sysfs_gpio:: Error ;
133
161
162
+ #[ cfg( feature = "gpio_cdev" ) ]
163
+ fn is_high ( & self ) -> Result < bool , Self :: Error > {
164
+ if !self . 1 {
165
+ self . 0 . get_value ( ) . map ( |val| val != 0 )
166
+ } else {
167
+ self . 0 . get_value ( ) . map ( |val| val == 0 )
168
+ }
169
+ }
170
+ #[ cfg( not( feature = "gpio_cdev" ) ) ]
134
171
fn is_high ( & self ) -> Result < bool , Self :: Error > {
135
172
if !self . 0 . get_active_low ( ) ? {
136
173
self . 0 . get_value ( ) . map ( |val| val != 0 )
@@ -145,6 +182,9 @@ impl hal::digital::v2::InputPin for Pin {
145
182
}
146
183
147
184
impl ops:: Deref for Pin {
185
+ #[ cfg( feature = "gpio_cdev" ) ]
186
+ type Target = gpio_cdev:: LineHandle ;
187
+ #[ cfg( not( feature = "gpio_cdev" ) ) ]
148
188
type Target = sysfs_gpio:: Pin ;
149
189
150
190
fn deref ( & self ) -> & Self :: Target {
0 commit comments