feat: 输出控制。
This commit is contained in:
parent
70ca1b1241
commit
4fc93079c2
@ -1,13 +1,12 @@
|
|||||||
use esp_idf_hal::gpio::OutputPin;
|
use esp_idf_hal::gpio::OutputPin;
|
||||||
use esp_idf_hal::ledc;
|
use esp_idf_hal::ledc;
|
||||||
use esp_idf_hal::ledc::{config::TimerConfig, Channel, Timer};
|
use esp_idf_hal::ledc::{config::TimerConfig, Channel, Timer};
|
||||||
use esp_idf_hal::peripherals::Peripherals;
|
|
||||||
use esp_idf_hal::prelude::*;
|
use esp_idf_hal::prelude::*;
|
||||||
use esp_idf_sys::EspError;
|
use esp_idf_sys::EspError;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
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> {
|
pub struct Beep<P: OutputPin, T: ledc::HwTimer, C: ledc::HwChannel> {
|
||||||
state: bool,
|
state: bool,
|
||||||
|
24
src/dc_out_controller.rs
Normal file
24
src/dc_out_controller.rs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
20
src/main.rs
20
src/main.rs
@ -1,8 +1,9 @@
|
|||||||
use esp_idf_sys as _;
|
use esp_idf_sys as _;
|
||||||
use std::thread;
|
use std::{thread, time::Duration};
|
||||||
|
|
||||||
mod beep;
|
mod beep;
|
||||||
mod blink;
|
mod blink;
|
||||||
|
mod dc_out_controller;
|
||||||
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.
|
||||||
@ -14,6 +15,7 @@ fn main() {
|
|||||||
let gpio6 = peripherals.pins.gpio6;
|
let gpio6 = peripherals.pins.gpio6;
|
||||||
let ledc_timer0 = peripherals.ledc.timer0;
|
let ledc_timer0 = peripherals.ledc.timer0;
|
||||||
let ledc_channel0 = peripherals.ledc.channel0;
|
let ledc_channel0 = peripherals.ledc.channel0;
|
||||||
|
let dc_out_ctl_pin = peripherals.pins.gpio2;
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let mut blink =
|
let mut blink =
|
||||||
blink::Blink::new(gpio5.into_output().expect("Failed to set GPIO5 as output"));
|
blink::Blink::new(gpio5.into_output().expect("Failed to set GPIO5 as output"));
|
||||||
@ -30,5 +32,21 @@ fn main() {
|
|||||||
beep.play();
|
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!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user