From 64f88adf4b627952238b711ba6c08def07c2e8ce Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Wed, 20 Apr 2022 21:23:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(blink):=20=E9=97=AA..=E9=97=AA..=E5=8F=91.?= =?UTF-8?q?.=E4=BA=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 ++ src/blink.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 24 +++++++++++++++++++++++- 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/blink.rs create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index ddd35e5..b985235 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,8 @@ pio = ["esp-idf-sys/pio"] [dependencies] esp-idf-sys = { version = "0.31", features = ["binstart"] } +esp-idf-hal = "0.36.0" +embedded-hal = "1.0.0-alpha.8" [build-dependencies] diff --git a/src/blink.rs b/src/blink.rs new file mode 100644 index 0000000..cc51890 --- /dev/null +++ b/src/blink.rs @@ -0,0 +1,47 @@ +use embedded_hal::digital::blocking::OutputPin; +use std::thread; +use std::time::Duration; + +pub struct Blink +where + T: OutputPin, +{ + state: bool, + pin: T, + stopped: bool, +} + +impl Blink +where + T: OutputPin, +{ + pub fn new(pin: T) -> Blink { + return Blink { + state: false, + pin, + stopped: false, + }; + } + + pub fn toggle(&mut self) -> Result<(), T::Error> { + self.state = !self.state; + if self.state { + self.pin.set_high() + } else { + self.pin.set_low() + } + } + + pub fn play(&mut self) { + loop { + if self.stopped { + break; + } + self.toggle().unwrap(); + thread::sleep(Duration::from_millis(50)); + + self.toggle().unwrap(); + thread::sleep(Duration::from_millis(950)); + } + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..06e5129 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod blink; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2d3bfaf..71c9a7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,31 @@ -use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported +use esp_idf_sys as _; +use std::thread; +use ups_esp32c3_rust::beep; +use ups_esp32c3_rust::blink; fn main() { // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, // or else some patches to the runtime implemented by esp-idf-sys might not link properly. esp_idf_sys::link_patches(); + let peripherals = esp_idf_hal::peripherals::Peripherals::take().unwrap(); + + thread::spawn(move || { + let mut blink = blink::Blink::new( + peripherals + .pins + .gpio5 + .into_output() + .expect("Failed to set GPIO5 as output"), + ); + blink.play(); + }); + + beep::Beep::new(peripherals + .pins + .gpio5 + .into_output() + .expect("Failed to set GPIO6 as output"),); + println!("Hello, world!"); }