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
use std::path::Path;
pub struct SysfsPin(pub sysfs_gpio::Pin);
impl SysfsPin {
pub fn new(pin_num: u64) -> Self {
SysfsPin(sysfs_gpio::Pin::new(pin_num))
}
pub fn from_path<P>(path: P) -> sysfs_gpio::Result<Self>
where
P: AsRef<Path>,
{
sysfs_gpio::Pin::from_path(path).map(SysfsPin)
}
}
impl embedded_hal::digital::blocking::OutputPin for SysfsPin {
type Error = sysfs_gpio::Error;
fn set_low(&mut self) -> Result<(), Self::Error> {
if self.0.get_active_low()? {
self.0.set_value(1)
} else {
self.0.set_value(0)
}
}
fn set_high(&mut self) -> Result<(), Self::Error> {
if self.0.get_active_low()? {
self.0.set_value(0)
} else {
self.0.set_value(1)
}
}
}
impl embedded_hal::digital::blocking::InputPin for SysfsPin {
type Error = sysfs_gpio::Error;
fn is_high(&self) -> Result<bool, Self::Error> {
if !self.0.get_active_low()? {
self.0.get_value().map(|val| val != 0)
} else {
self.0.get_value().map(|val| val == 0)
}
}
fn is_low(&self) -> Result<bool, Self::Error> {
self.is_high().map(|val| !val)
}
}
impl embedded_hal::digital::blocking::IoPin<SysfsPin, SysfsPin> for SysfsPin {
type Error = sysfs_gpio::Error;
fn into_input_pin(self) -> Result<SysfsPin, Self::Error> {
self.set_direction(sysfs_gpio::Direction::In)?;
Ok(self)
}
fn into_output_pin(
self,
state: embedded_hal::digital::PinState,
) -> Result<SysfsPin, Self::Error> {
self.set_direction(match state {
embedded_hal::digital::PinState::High => sysfs_gpio::Direction::High,
embedded_hal::digital::PinState::Low => sysfs_gpio::Direction::Low,
})?;
Ok(self)
}
}
impl core::ops::Deref for SysfsPin {
type Target = sysfs_gpio::Pin;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl core::ops::DerefMut for SysfsPin {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}