From 2aaa85af71cc9485d690c0b3021b45c83fcb4e69 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Mon, 12 Sep 2022 10:13:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=94=B5=E5=8E=8B=E9=87=87=E6=A0=B7?= =?UTF-8?q?=E6=AF=8F=E6=AC=A1=E9=87=87=E6=A0=B710=E6=AC=A1=E5=8F=96?= =?UTF-8?q?=E5=B9=B3=E5=9D=87=E5=80=BC=E3=80=82Close=20#5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/voltage_detection.rs | 51 +++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/src/voltage_detection.rs b/src/voltage_detection.rs index fdcd0ea..f122cad 100644 --- a/src/voltage_detection.rs +++ b/src/voltage_detection.rs @@ -5,7 +5,7 @@ use std::{ }; use embedded_hal::{adc::Channel, prelude::_embedded_hal_adc_OneShot}; -use embedded_svc::{event_bus::Postbox}; +use embedded_svc::event_bus::Postbox; use esp_idf_hal::{ adc::{ config::{self}, @@ -13,11 +13,9 @@ use esp_idf_hal::{ }, gpio::{Gpio1, Gpio2, Gpio3, Input}, }; -use esp_idf_svc::{ - eventloop::{ - Background, EspBackgroundEventLoop, EspEventFetchData, EspEventLoop, EspEventPostData, - EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource, - }, +use esp_idf_svc::eventloop::{ + Background, EspBackgroundEventLoop, EspEventFetchData, EspEventLoop, EspEventPostData, + EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource, }; use esp_idf_sys::c_types; use log::{debug, warn}; @@ -75,6 +73,8 @@ impl VoltageDetection { anyhow::anyhow!("Failed to set GPIO 3 as analog input. {}", err) })?; + let mut adc = PoweredAdc::new(unsafe { ADC1::new() }, config::Config::new())?; + let mut worker = worker.lock().map_err(|err| { anyhow::anyhow!("Lock VoltageDetection Worker Failed. {}", err) })?; @@ -83,7 +83,12 @@ impl VoltageDetection { loop { last_runing_at = Time::new().get_time(); - match worker.read_once(&mut adapter_pin, &mut battery_pin, &mut output_pin) { + match worker.read_once( + &mut adc, + &mut adapter_pin, + &mut battery_pin, + &mut output_pin, + ) { Ok(_) => debug!( "Adapter: {},\tBattery: {},\t Output: {}", worker.adapter_voltage, worker.battery_voltage, worker.output_voltage @@ -107,11 +112,11 @@ impl VoltageDetection { } let mut delta = Time::new().get_time() - last_runing_at; - if delta >= Duration::from_millis(1000) { + if delta >= Duration::from_millis(5000) { delta = Duration::ZERO } - sleep(Duration::from_millis(1000) - delta); + sleep(Duration::from_millis(5000) - delta); } }; @@ -134,11 +139,12 @@ impl VoltageDetectionWorker { pub fn read_once( &mut self, + adc: &mut PoweredAdc, adapter_pin: &mut Gpio1>, battery_pin: &mut Gpio2>, output_pin: &mut Gpio3>, ) -> anyhow::Result<()> { - match self.read_pin_once(adapter_pin) { + match self.read_pin_once(adc, adapter_pin) { Ok(voltage) => { self.adapter_voltage = ((voltage as f32) * ADAPTER_OFFSET) as u16; } @@ -147,7 +153,7 @@ impl VoltageDetectionWorker { } } - match self.read_pin_once(battery_pin) { + match self.read_pin_once(adc, battery_pin) { Ok(voltage) => { self.battery_voltage = ((voltage as f32) * BATTERY_OFFSET) as u16; } @@ -155,7 +161,7 @@ impl VoltageDetectionWorker { warn!("Adapter Voltage read failed: {:?}", err); } } - match self.read_pin_once(output_pin) { + match self.read_pin_once(adc, output_pin) { Ok(voltage) => { self.output_voltage = ((voltage as f32) * OUTPUT_OFFSET) as u16; } @@ -168,16 +174,23 @@ impl VoltageDetectionWorker { } pub fn read_pin_once, PIN: Channel>( &mut self, + adc: &mut PoweredAdc, pin: &mut PIN, ) -> anyhow::Result { - let mut adc = PoweredAdc::new(unsafe { ADC1::new() }, config::Config::new())?; - let voltage = adc.read(pin); - match voltage { - Ok(voltage) => anyhow::Ok(voltage), - Err(err) => { - anyhow::bail!("Adapter Voltage read failed: {:?}", err) - } + let mut avg_voltage: u16 = 0; + for _ in 0..10 { + let voltage = adc.read(pin); + match voltage { + Ok(voltage) => { + avg_voltage += voltage; + } + Err(err) => { + anyhow::bail!("Adapter Voltage read failed: {:?}", err) + } + }; + sleep(Duration::from_millis(100)); } + anyhow::Ok(avg_voltage / 10) } }