feat(blink): 闪..闪..发..亮。

This commit is contained in:
Ivan Li 2022-04-20 21:23:23 +08:00
parent eb8d30afa1
commit 64f88adf4b
4 changed files with 73 additions and 1 deletions

View File

@ -17,6 +17,8 @@ pio = ["esp-idf-sys/pio"]
[dependencies] [dependencies]
esp-idf-sys = { version = "0.31", features = ["binstart"] } esp-idf-sys = { version = "0.31", features = ["binstart"] }
esp-idf-hal = "0.36.0"
embedded-hal = "1.0.0-alpha.8"
[build-dependencies] [build-dependencies]

47
src/blink.rs Normal file
View File

@ -0,0 +1,47 @@
use embedded_hal::digital::blocking::OutputPin;
use std::thread;
use std::time::Duration;
pub struct Blink<T>
where
T: OutputPin,
{
state: bool,
pin: T,
stopped: bool,
}
impl<T> Blink<T>
where
T: OutputPin,
{
pub fn new(pin: T) -> Blink<T> {
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));
}
}
}

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod blink;

View File

@ -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() { fn main() {
// Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, // 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. // or else some patches to the runtime implemented by esp-idf-sys might not link properly.
esp_idf_sys::link_patches(); 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!"); println!("Hello, world!");
} }