From 70ca1b124176dbe4464df131ae111cfbd59faa8f Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Thu, 21 Apr 2022 21:39:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=9C=82=E9=B8=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/beep.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 - src/main.rs | 31 ++++++++++++++++------------- 3 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 src/beep.rs delete mode 100644 src/lib.rs diff --git a/src/beep.rs b/src/beep.rs new file mode 100644 index 0000000..1da33fc --- /dev/null +++ b/src/beep.rs @@ -0,0 +1,57 @@ +use esp_idf_hal::gpio::OutputPin; +use esp_idf_hal::ledc; +use esp_idf_hal::ledc::{config::TimerConfig, Channel, Timer}; +use esp_idf_hal::peripherals::Peripherals; +use esp_idf_hal::prelude::*; +use esp_idf_sys::EspError; +use std::thread; +use std::time::Duration; + +type LedcChannel = Channel, P>; + +pub struct Beep { + state: bool, + stopped: bool, + channel: LedcChannel, +} + +impl Beep { + pub fn new(pin: P, timer: T, channel: C) -> Result { + return Ok(Beep { + state: false, + stopped: false, + channel: Self::init_channel(pin, timer, channel)?, + }); + } + + fn init_channel(pin: P, timer: T, channel: C) -> Result, EspError> { + let config = TimerConfig::default().frequency(1.kHz().into()); + let timer = Timer::new(timer, &config)?; + let mut channel = Channel::new(channel, timer, pin)?; + let max_duty = channel.get_max_duty(); + channel.set_duty(max_duty * 3 / 4)?; + return Ok(channel); + } + + pub fn toggle(&mut self) -> Result<(), EspError> { + self.state = !self.state; + if self.state { + self.channel.set_duty(50) + } else { + self.channel.set_duty(0) + } + } + + pub fn play(&mut self) { + loop { + if self.stopped { + break; + } + self.toggle().unwrap(); + thread::sleep(Duration::from_millis(500)); + + self.toggle().unwrap(); + thread::sleep(Duration::from_millis(1500)); + } + } +} diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 06e5129..0000000 --- a/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod blink; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 71c9a7a..2e8a8d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ use esp_idf_sys as _; use std::thread; -use ups_esp32c3_rust::beep; -use ups_esp32c3_rust::blink; +mod beep; +mod 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. @@ -10,22 +10,25 @@ fn main() { let peripherals = esp_idf_hal::peripherals::Peripherals::take().unwrap(); + let gpio5 = peripherals.pins.gpio5; + let gpio6 = peripherals.pins.gpio6; + let ledc_timer0 = peripherals.ledc.timer0; + let ledc_channel0 = peripherals.ledc.channel0; thread::spawn(move || { - let mut blink = blink::Blink::new( - peripherals - .pins - .gpio5 - .into_output() - .expect("Failed to set GPIO5 as output"), - ); + let mut blink = + blink::Blink::new(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"),); + thread::spawn(move || { + let mut beep = beep::Beep::new( + gpio6.into_output().expect("Failed to set GPIO6 as output"), + ledc_timer0, + ledc_channel0, + ) + .expect("Failed to create beep"); + beep.play(); + }); println!("Hello, world!"); }