-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay_ctrl.rs
68 lines (62 loc) · 2.1 KB
/
relay_ctrl.rs
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
use crate::error::AtmosError;
use log::{debug, error, info};
use rppal::gpio::{Gpio, Level};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum RelayStatus {
On,
Off,
}
impl fmt::Display for RelayStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RelayStatus::On => write!(f, "On"),
RelayStatus::Off => write!(f, "Off"),
}
}
}
pub fn change_relay_status(pin: u8, status: RelayStatus) -> Result<(), AtmosError> {
info!(
"Attempting to change relay status for pin {} to {:?}",
pin, status
);
let gpio = Gpio::new()?;
let mut pin_obj = gpio.get(pin)?.into_output();
// Set this so the pin stays put after leaving this function - and having
// pin fall out of scope. Default behavior is to reset the pin back to low (false)
// when pin falls out of scope. This is simpler than trying to keep pin as a long-running
// (static) value.
pin_obj.set_reset_on_drop(false);
match status {
RelayStatus::On => {
pin_obj.set_low(); // Set low to turn on (active-low)
debug!("Set pin {} to low (RelayStatus::On)", pin);
}
RelayStatus::Off => {
pin_obj.set_high(); // Set high to turn off (active-low)
debug!("Set pin {} to high (RelayStatus::Off)", pin);
}
}
info!(
"Successfully changed relay status for pin {} to {:?}",
pin, status
);
Ok(())
}
pub fn check_relay_status(pin: u8) -> Result<RelayStatus, AtmosError> {
info!("Checking relay status for pin {}", pin);
let gpio = Gpio::new()?;
let pin_obj = gpio.get(pin)?.into_input();
let status = match pin_obj.read() {
Level::Low => {
debug!("Pin {} is Low, interpreting as RelayStatus::On", pin);
Ok(RelayStatus::On)
}
Level::High => {
debug!("Pin {} is High, interpreting as RelayStatus::Off", pin);
Ok(RelayStatus::Off)
}
};
info!("Relay status for pin {} is {:?}", pin, status);
status
}