v0.2:重写项目。 #6
@ -5,7 +5,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use embedded_hal::{adc::Channel, prelude::_embedded_hal_adc_OneShot};
|
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::{
|
use esp_idf_hal::{
|
||||||
adc::{
|
adc::{
|
||||||
config::{self},
|
config::{self},
|
||||||
@ -13,11 +13,9 @@ use esp_idf_hal::{
|
|||||||
},
|
},
|
||||||
gpio::{Gpio1, Gpio2, Gpio3, Input},
|
gpio::{Gpio1, Gpio2, Gpio3, Input},
|
||||||
};
|
};
|
||||||
use esp_idf_svc::{
|
use esp_idf_svc::eventloop::{
|
||||||
eventloop::{
|
Background, EspBackgroundEventLoop, EspEventFetchData, EspEventLoop, EspEventPostData,
|
||||||
Background, EspBackgroundEventLoop, EspEventFetchData, EspEventLoop, EspEventPostData,
|
EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource,
|
||||||
EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
use esp_idf_sys::c_types;
|
use esp_idf_sys::c_types;
|
||||||
use log::{debug, warn};
|
use log::{debug, warn};
|
||||||
@ -75,6 +73,8 @@ impl VoltageDetection {
|
|||||||
anyhow::anyhow!("Failed to set GPIO 3 as analog input. {}", err)
|
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| {
|
let mut worker = worker.lock().map_err(|err| {
|
||||||
anyhow::anyhow!("Lock VoltageDetection Worker Failed. {}", err)
|
anyhow::anyhow!("Lock VoltageDetection Worker Failed. {}", err)
|
||||||
})?;
|
})?;
|
||||||
@ -83,7 +83,12 @@ impl VoltageDetection {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
last_runing_at = Time::new().get_time();
|
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!(
|
Ok(_) => debug!(
|
||||||
"Adapter: {},\tBattery: {},\t Output: {}",
|
"Adapter: {},\tBattery: {},\t Output: {}",
|
||||||
worker.adapter_voltage, worker.battery_voltage, worker.output_voltage
|
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;
|
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
|
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(
|
pub fn read_once(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
adc: &mut PoweredAdc<ADC1>,
|
||||||
adapter_pin: &mut Gpio1<Atten6dB<ADC1>>,
|
adapter_pin: &mut Gpio1<Atten6dB<ADC1>>,
|
||||||
battery_pin: &mut Gpio2<Atten6dB<ADC1>>,
|
battery_pin: &mut Gpio2<Atten6dB<ADC1>>,
|
||||||
output_pin: &mut Gpio3<Atten6dB<ADC1>>,
|
output_pin: &mut Gpio3<Atten6dB<ADC1>>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
match self.read_pin_once(adapter_pin) {
|
match self.read_pin_once(adc, adapter_pin) {
|
||||||
Ok(voltage) => {
|
Ok(voltage) => {
|
||||||
self.adapter_voltage = ((voltage as f32) * ADAPTER_OFFSET) as u16;
|
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) => {
|
Ok(voltage) => {
|
||||||
self.battery_voltage = ((voltage as f32) * BATTERY_OFFSET) as u16;
|
self.battery_voltage = ((voltage as f32) * BATTERY_OFFSET) as u16;
|
||||||
}
|
}
|
||||||
@ -155,7 +161,7 @@ impl VoltageDetectionWorker {
|
|||||||
warn!("Adapter Voltage read failed: {:?}", err);
|
warn!("Adapter Voltage read failed: {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match self.read_pin_once(output_pin) {
|
match self.read_pin_once(adc, output_pin) {
|
||||||
Ok(voltage) => {
|
Ok(voltage) => {
|
||||||
self.output_voltage = ((voltage as f32) * OUTPUT_OFFSET) as u16;
|
self.output_voltage = ((voltage as f32) * OUTPUT_OFFSET) as u16;
|
||||||
}
|
}
|
||||||
@ -168,16 +174,23 @@ impl VoltageDetectionWorker {
|
|||||||
}
|
}
|
||||||
pub fn read_pin_once<AN: Analog<ADC1>, PIN: Channel<AN, ID = u8>>(
|
pub fn read_pin_once<AN: Analog<ADC1>, PIN: Channel<AN, ID = u8>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
adc: &mut PoweredAdc<ADC1>,
|
||||||
pin: &mut PIN,
|
pin: &mut PIN,
|
||||||
) -> anyhow::Result<u16> {
|
) -> anyhow::Result<u16> {
|
||||||
let mut adc = PoweredAdc::new(unsafe { ADC1::new() }, config::Config::new())?;
|
let mut avg_voltage: u16 = 0;
|
||||||
let voltage = adc.read(pin);
|
for _ in 0..10 {
|
||||||
match voltage {
|
let voltage = adc.read(pin);
|
||||||
Ok(voltage) => anyhow::Ok(voltage),
|
match voltage {
|
||||||
Err(err) => {
|
Ok(voltage) => {
|
||||||
anyhow::bail!("Adapter Voltage read failed: {:?}", err)
|
avg_voltage += voltage;
|
||||||
}
|
}
|
||||||
|
Err(err) => {
|
||||||
|
anyhow::bail!("Adapter Voltage read failed: {:?}", err)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
sleep(Duration::from_millis(100));
|
||||||
}
|
}
|
||||||
|
anyhow::Ok(avg_voltage / 10)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user