Use struct for passing module arguments: gpio
This commit is contained in:
parent
bace7ef138
commit
104d8a862d
3 changed files with 27 additions and 13 deletions
|
@ -1,13 +1,20 @@
|
|||
use gpio_cdev::{Chip, LineRequestFlags};
|
||||
|
||||
pub fn gpio(device: String, pin: u8, active_low: bool, active_time: u32) {
|
||||
let mut chip = Chip::new(device).unwrap();
|
||||
pub struct GPIOArgs {
|
||||
pub device: String,
|
||||
pub pin: u8,
|
||||
pub active_low: bool,
|
||||
pub active_time: u32,
|
||||
}
|
||||
|
||||
pub fn gpio(args: GPIOArgs) {
|
||||
let mut chip = Chip::new(args.device).unwrap();
|
||||
let handle = chip
|
||||
.get_line(pin as u32)
|
||||
.get_line(args.pin as u32)
|
||||
.unwrap()
|
||||
.request(LineRequestFlags::OUTPUT, 1, "labadoor-gpio")
|
||||
.unwrap();
|
||||
handle.set_value(!active_low as u8).unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_millis(active_time as u64));
|
||||
handle.set_value(active_low as u8).unwrap();
|
||||
handle.set_value(!args.active_low as u8).unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_millis(args.active_time as u64));
|
||||
handle.set_value(args.active_low as u8).unwrap();
|
||||
}
|
||||
|
|
|
@ -31,13 +31,8 @@ fn main() {
|
|||
}
|
||||
#[cfg(feature = "gpio")]
|
||||
cli::Command::GPIO(_) => {
|
||||
let gpio = config.get::<cli::GPIO>("gpio").unwrap();
|
||||
labadoor_gpio::gpio(
|
||||
gpio.device.unwrap(),
|
||||
gpio.pin.unwrap(),
|
||||
gpio.active_low.unwrap(),
|
||||
gpio.active_time.unwrap(),
|
||||
);
|
||||
let gpio = config.get::<cli::GPIO>("gpio").unwrap().to_config();
|
||||
labadoor_gpio::gpio(gpio);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,3 +23,15 @@ impl ToConfig<labadoor_matrix::MatrixArgs> for Matrix {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "gpio")]
|
||||
impl ToConfig<labadoor_gpio::GPIOArgs> for GPIO {
|
||||
fn to_config(&self) -> labadoor_gpio::GPIOArgs {
|
||||
labadoor_gpio::GPIOArgs {
|
||||
device: self.device.clone().unwrap(),
|
||||
pin: self.pin.unwrap(),
|
||||
active_low: self.active_low.unwrap(),
|
||||
active_time: self.active_time.unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue