diff --git a/Cargo.lock b/Cargo.lock index eb9e760..85855fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,49 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "gpio-cdev" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09831ec59b80be69e75d29cf36e16afbbe5fd1af9c1bf4689ad91c77db5aa6a6" +dependencies = [ + "bitflags", + "libc", + "nix", +] + +[[package]] +name = "libc" +version = "0.2.178" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" + +[[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags", + "cfg-if", + "libc", +] + [[package]] name = "snooze-pal" version = "0.1.0" +dependencies = [ + "gpio-cdev", +] diff --git a/Cargo.toml b/Cargo.toml index 76b7d75..5642777 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +gpio-cdev = "0.6.0" diff --git a/src/main.rs b/src/main.rs index ec1fd02..1c7319b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,34 @@ // This project is licensed under MIT Licese. // See the LICENSE folder for details. -fn main() { - println!("Hello, world!!"); +use std::thread; + +use gpio_cdev::{Chip, LineRequestFlags}; + +fn main() -> Result<(), Box> { + let mut chip = Chip::new("/dev/gpiochip0")?; + alarm(&mut chip)?; + Ok(()) +} + +fn alarm(chip: &mut Chip) -> Result<(), Box> { + let beeper = chip + .get_line(17)? + .request(LineRequestFlags::OUTPUT, 0, "my-gpio")?; + + for _ in 0..5 { + beeper.set_value(1)?; + thread::sleep(std::time::Duration::from_secs(1)); + beeper.set_value(0)?; + thread::sleep(std::time::Duration::from_secs(1)); + } + + Ok(()) +} + +fn turn_off_led(chip: &mut Chip) -> Result<(), Box> { + chip.get_line(17)? + .request(LineRequestFlags::OUTPUT, 0, "my-gpio")? + .set_value(0)?; + Ok(()) }