init
This commit is contained in:
commit
b0f8b37428
2 changed files with 65 additions and 0 deletions
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "relay_control"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
gpio-cdev = "0.6"
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
57
src/main.rs
Normal file
57
src/main.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
use gpio_cdev::{Chip, LineRequestFlags};
|
||||
use std::{thread, time::Duration};
|
||||
|
||||
/// Banana Pi M5 relay controller (gpiochip0 line 68 = CON1-P11).
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
cmd: Command,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
enum Command {
|
||||
/// Turn relay ON (active-low → set line 0)
|
||||
On,
|
||||
/// Turn relay OFF (set line 1)
|
||||
Off,
|
||||
/// Pulse relay ON for N ms, then OFF
|
||||
Pulse {
|
||||
#[arg(long, default_value_t = 500)]
|
||||
ms: u64,
|
||||
},
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
// Open gpiochip0
|
||||
let mut chip = Chip::new("/dev/gpiochip0")?;
|
||||
|
||||
// Request line 68 (CON1-P11) as output, default HIGH (relay OFF)
|
||||
let handle = chip
|
||||
.get_line(68)?
|
||||
.request(LineRequestFlags::OUTPUT, 1, "relay_control")?;
|
||||
|
||||
match cli.cmd {
|
||||
Command::On => {
|
||||
println!("Relay ON");
|
||||
handle.set_value(0)?;
|
||||
}
|
||||
Command::Off => {
|
||||
println!("Relay OFF");
|
||||
handle.set_value(1)?;
|
||||
}
|
||||
Command::Pulse { ms } => {
|
||||
println!("Relay ON for {} ms", ms);
|
||||
handle.set_value(0)?;
|
||||
thread::sleep(Duration::from_millis(ms));
|
||||
println!("Relay OFF");
|
||||
handle.set_value(1)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue