diff --git a/src/beep.rs b/src/beep.rs index 1da33fc..0e87338 100644 --- a/src/beep.rs +++ b/src/beep.rs @@ -1,13 +1,12 @@ 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>; +type LedcChannel = Channel, P>; pub struct Beep { state: bool, diff --git a/src/dc_out_controller.rs b/src/dc_out_controller.rs new file mode 100644 index 0000000..7a52bb2 --- /dev/null +++ b/src/dc_out_controller.rs @@ -0,0 +1,24 @@ +use embedded_hal::digital::blocking::OutputPin; + +pub struct DcOutController { + pub state: bool, + pin: T, +} + +impl DcOutController +where + T: OutputPin, +{ + pub fn new(pin: T) -> DcOutController { + return DcOutController { state: false, pin }; + } + + pub fn on(&mut self) -> Result<(), T::Error> { + self.state = true; + return self.pin.set_low(); + } + pub fn off(&mut self) -> Result<(), T::Error> { + self.state = false; + return self.pin.set_high(); + } +} diff --git a/src/main.rs b/src/main.rs index 2e8a8d2..77f381f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,9 @@ use esp_idf_sys as _; -use std::thread; +use std::{thread, time::Duration}; mod beep; mod blink; +mod dc_out_controller; 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. @@ -14,6 +15,7 @@ fn main() { let gpio6 = peripherals.pins.gpio6; let ledc_timer0 = peripherals.ledc.timer0; let ledc_channel0 = peripherals.ledc.channel0; + let dc_out_ctl_pin = peripherals.pins.gpio2; thread::spawn(move || { let mut blink = blink::Blink::new(gpio5.into_output().expect("Failed to set GPIO5 as output")); @@ -30,5 +32,21 @@ fn main() { beep.play(); }); + thread::spawn(move || { + let mut dc_out_ctl = dc_out_controller::DcOutController::new( + dc_out_ctl_pin + .into_output() + .expect("Failed to set GPIO2 as output"), + ); + while true { + if dc_out_ctl.state { + dc_out_ctl.off().expect("Failed to turn DC_OUT_CTL off"); + } else { + dc_out_ctl.on().expect("Failed to turn DC_OUT_CTL on"); + } + thread::sleep(Duration::from_millis(500)); + } + }); + println!("Hello, world!"); }