feat: 输出控制。

This commit is contained in:
Ivan Li 2022-05-01 13:43:34 +08:00
parent 70ca1b1241
commit 4fc93079c2
3 changed files with 44 additions and 3 deletions

View File

@ -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<P: OutputPin, T: ledc::HwTimer, C: ledc::HwChannel> = Channel<C, T, Timer<T>, P>;
type LedcChannel<P, T, C> = Channel<C, T, Timer<T>, P>;
pub struct Beep<P: OutputPin, T: ledc::HwTimer, C: ledc::HwChannel> {
state: bool,

24
src/dc_out_controller.rs Normal file
View File

@ -0,0 +1,24 @@
use embedded_hal::digital::blocking::OutputPin;
pub struct DcOutController<T: OutputPin> {
pub state: bool,
pin: T,
}
impl<T> DcOutController<T>
where
T: OutputPin,
{
pub fn new(pin: T) -> DcOutController<T> {
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();
}
}

View File

@ -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!");
}