feat: 优化 ADC 采样逻辑。

This commit is contained in:
Ivan Li 2022-05-04 10:43:23 +08:00
parent 3eb680f1ce
commit de84c85190
2 changed files with 15 additions and 32 deletions

View File

@ -49,21 +49,6 @@ 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"),
// );
// loop {
// 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));
// }
// });
let mut dc_out_ctl = dc_out_controller::DcOutController::new(
dc_out_ctl_pin
.into_output()
@ -78,7 +63,5 @@ fn main() {
).expect("Failed to create manager");
loop {
manager.handling_once().expect("Failed to handle once");
println!("Hello, world!");
thread::sleep(Duration::from_millis(2000));
}
}

View File

@ -1,8 +1,10 @@
use std::{thread, time::Duration};
use embedded_hal::digital::blocking::OutputPin;
use embedded_hal_0_2::{adc::OneShot, digital::v2::InputPin};
use esp_idf_hal::{
adc::{Adc, Atten11dB, PoweredAdc, ADC1},
gpio::{Gpio1, Gpio2},
gpio::{Gpio1, Gpio2}, delay,
};
use esp_idf_sys::EspError;
@ -47,25 +49,23 @@ where
}
pub fn get_adapter_voltage(&mut self) -> Result<f32, EspError> {
let mut voltage = 0.0;
for _ in 0..10 {
voltage += self.adc.read(&mut self.adapter_pin).unwrap() as f32;
}
voltage /= 10.0;
return Ok(voltage);
return Ok(self.adc.read(&mut self.adapter_pin).unwrap() as f32);
}
pub fn get_battery_voltage(&mut self) -> Result<f32, EspError> {
let mut voltage = 0.0;
for _ in 0..10 {
voltage += self.adc.read(&mut self.battery_pin).unwrap() as f32;
}
voltage /= 10.0;
return Ok(voltage);
return Ok(self.adc.read(&mut self.battery_pin).unwrap() as f32);
}
pub fn handling_once(&mut self) -> Result<(), EspError> {
let adapter = self.get_adapter_voltage()?;
let battery = self.get_battery_voltage()?;
let mut adapter = 0.0_f32;
let mut battery = 0.0_f32;
for _ in 0..10 {
adapter += self.get_adapter_voltage()?;
battery += self.get_battery_voltage()?;
thread::sleep(Duration::from_millis(10));
}
adapter /= 10.0_f32;
battery /= 10.0_f32;
if adapter < 1000.0 {
self.dc_out_controller.off().expect("Can not turn off Out");