feat: 电压检测与控制。
This commit is contained in:
81
src/manager.rs
Normal file
81
src/manager.rs
Normal file
@ -0,0 +1,81 @@
|
||||
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},
|
||||
};
|
||||
use esp_idf_sys::EspError;
|
||||
|
||||
use crate::{dc_out_controller::DcOutController, screen::Screen};
|
||||
|
||||
type AdapterGpio = Gpio1<Atten11dB<ADC1>>;
|
||||
type BatteryGpio = Gpio2<Atten11dB<ADC1>>;
|
||||
|
||||
pub struct Manager<C>
|
||||
where
|
||||
C: OutputPin,
|
||||
{
|
||||
dc_out_controller: DcOutController<C>,
|
||||
screen: Screen,
|
||||
adc: PoweredAdc<ADC1>,
|
||||
adapter_pin: AdapterGpio,
|
||||
battery_pin: BatteryGpio,
|
||||
}
|
||||
|
||||
impl<C> Manager<C>
|
||||
where
|
||||
C: OutputPin,
|
||||
{
|
||||
pub fn new(
|
||||
dc_out_controller: DcOutController<C>,
|
||||
screen: Screen,
|
||||
adc1: ADC1,
|
||||
adapter_pin: AdapterGpio,
|
||||
battery_pin: BatteryGpio,
|
||||
) -> Result<Self, EspError> {
|
||||
let adc = PoweredAdc::new(
|
||||
adc1,
|
||||
esp_idf_hal::adc::config::Config::new().calibration(true),
|
||||
)?;
|
||||
return Ok(Manager {
|
||||
dc_out_controller,
|
||||
screen,
|
||||
adc,
|
||||
adapter_pin,
|
||||
battery_pin,
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
pub fn handling_once(&mut self) -> Result<(), EspError> {
|
||||
let adapter = self.get_adapter_voltage()?;
|
||||
let battery = self.get_battery_voltage()?;
|
||||
|
||||
if adapter < 1000.0 {
|
||||
self.dc_out_controller.off().expect("Can not turn off Out");
|
||||
} else {
|
||||
self.dc_out_controller.on().expect("Can not turn on Out");
|
||||
}
|
||||
self.screen
|
||||
.draw_voltage(adapter, battery)
|
||||
.expect("Failed to draw voltage");
|
||||
|
||||
Ok({})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user