From de84c851905fcee5859cb99ead36d93fcb09268e Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Wed, 4 May 2022 10:43:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20ADC=20=E9=87=87?= =?UTF-8?q?=E6=A0=B7=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 17 ----------------- src/manager.rs | 30 +++++++++++++++--------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index dbccfc4..d2a74fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)); } } diff --git a/src/manager.rs b/src/manager.rs index cce5dbb..efc824c 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -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 { - 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 { - 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");