Use struct for passing module arguments: gpio

This commit is contained in:
George Kaklamanos 2023-11-21 21:39:48 +02:00
parent bace7ef138
commit 104d8a862d
Signed by: gkaklas
GPG key ID: C0CAB8A6BDC9399D
3 changed files with 27 additions and 13 deletions

View file

@ -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();
}