Compare commits
4 Commits
9b40b5dfdd
...
v0.2
Author | SHA1 | Date | |
---|---|---|---|
daf6effe59 | |||
09ae17fa31 | |||
2aaa85af71 | |||
711306f2c1 |
@ -1,4 +1,7 @@
|
|||||||
use std::sync::{Arc, Mutex, MutexGuard};
|
use std::{
|
||||||
|
sync::{Arc, Mutex, MutexGuard},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
use embedded_hal::digital::v2::{OutputPin, PinState};
|
use embedded_hal::digital::v2::{OutputPin, PinState};
|
||||||
use embedded_svc::event_bus::{EventBus, Postbox};
|
use embedded_svc::event_bus::{EventBus, Postbox};
|
||||||
@ -8,10 +11,13 @@ use esp_idf_svc::eventloop::{
|
|||||||
EspSubscription, EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource, User,
|
EspSubscription, EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource, User,
|
||||||
};
|
};
|
||||||
use esp_idf_sys::c_types;
|
use esp_idf_sys::c_types;
|
||||||
use log::{warn};
|
use log::warn;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
use crate::voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP};
|
use crate::{
|
||||||
|
time::Time,
|
||||||
|
voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP},
|
||||||
|
};
|
||||||
|
|
||||||
pub static mut CHARGE_STATE_EVENT_LOOP: Option<
|
pub static mut CHARGE_STATE_EVENT_LOOP: Option<
|
||||||
EspEventLoop<esp_idf_svc::eventloop::User<Background>>,
|
EspEventLoop<esp_idf_svc::eventloop::User<Background>>,
|
||||||
@ -27,6 +33,7 @@ pub enum ChargeStatus {
|
|||||||
pub struct ChargeControllerState {
|
pub struct ChargeControllerState {
|
||||||
pub status: ChargeStatus,
|
pub status: ChargeStatus,
|
||||||
pub pin_state: PinState,
|
pub pin_state: PinState,
|
||||||
|
pub charge_deadline_at: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChargeControllerState {
|
impl ChargeControllerState {
|
||||||
@ -34,6 +41,7 @@ impl ChargeControllerState {
|
|||||||
Self {
|
Self {
|
||||||
status: ChargeStatus::Charging,
|
status: ChargeStatus::Charging,
|
||||||
pin_state: PinState::Low,
|
pin_state: PinState::Low,
|
||||||
|
charge_deadline_at: Duration::ZERO,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,7 +54,13 @@ impl ChargeControllerState {
|
|||||||
PinState::Low => "Low",
|
PinState::Low => "Low",
|
||||||
PinState::High => "High",
|
PinState::High => "High",
|
||||||
};
|
};
|
||||||
json!({ "status": status, "pin_state": pin_state}).to_string()
|
let now = Time::new().get_time();
|
||||||
|
let charging_count_down = if now > self.charge_deadline_at {
|
||||||
|
-1i64
|
||||||
|
} else {
|
||||||
|
(self.charge_deadline_at - now).as_secs() as i64
|
||||||
|
};
|
||||||
|
json!({ "status": status, "pin_state": pin_state, "charging_count_down": charging_count_down}).to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +126,14 @@ impl ChargeController {
|
|||||||
if obj.battery_voltage < 12600 {
|
if obj.battery_voltage < 12600 {
|
||||||
state.status = ChargeStatus::Charging;
|
state.status = ChargeStatus::Charging;
|
||||||
} else {
|
} else {
|
||||||
|
let now = Time::new().get_time();
|
||||||
|
if state.charge_deadline_at == Duration::ZERO {
|
||||||
|
state.charge_deadline_at = now + Duration::from_secs(600);
|
||||||
|
} else if now > state.charge_deadline_at {
|
||||||
state.status = ChargeStatus::Charged;
|
state.status = ChargeStatus::Charged;
|
||||||
|
} else {
|
||||||
|
state.status = ChargeStatus::Charging;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ChargeStatus::Charged => {
|
ChargeStatus::Charged => {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
sync::{
|
sync::{Arc, Mutex, MutexGuard},
|
||||||
Arc, Mutex, MutexGuard,
|
time::Duration,
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use embedded_hal::digital::v2::{OutputPin, PinState};
|
use embedded_hal::digital::v2::{OutputPin, PinState};
|
||||||
@ -12,12 +11,16 @@ use esp_idf_svc::eventloop::{
|
|||||||
EspSubscription, EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource, User,
|
EspSubscription, EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource, User,
|
||||||
};
|
};
|
||||||
use esp_idf_sys::c_types;
|
use esp_idf_sys::c_types;
|
||||||
use log::{info, warn, debug};
|
use log::warn;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
use crate::voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP};
|
use crate::{
|
||||||
|
time::Time,
|
||||||
|
voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP},
|
||||||
|
};
|
||||||
|
|
||||||
const WAITING_OFF_SECONDS: u8 = 60;
|
const WAITING_OFF_DURATION: u64 = 60;
|
||||||
|
const WAITING_ON_DURATION: u64 = 60;
|
||||||
|
|
||||||
pub static mut DC_OUT_STATE_EVENT_LOOP: Option<
|
pub static mut DC_OUT_STATE_EVENT_LOOP: Option<
|
||||||
EspEventLoop<esp_idf_svc::eventloop::User<Background>>,
|
EspEventLoop<esp_idf_svc::eventloop::User<Background>>,
|
||||||
@ -25,11 +28,11 @@ pub static mut DC_OUT_STATE_EVENT_LOOP: Option<
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum DcOutStatus {
|
pub enum DcOutStatus {
|
||||||
WaitingOn(u8),
|
WaitingOn(Duration),
|
||||||
On,
|
On,
|
||||||
Off,
|
Off,
|
||||||
WaitingOff,
|
WaitingOff,
|
||||||
TurningOff(u8),
|
TurningOff(Duration),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@ -54,11 +57,9 @@ impl DcOutControllerState {
|
|||||||
DcOutStatus::WaitingOn(_) => {
|
DcOutStatus::WaitingOn(_) => {
|
||||||
self.status = DcOutStatus::Off;
|
self.status = DcOutStatus::Off;
|
||||||
}
|
}
|
||||||
DcOutStatus::TurningOff(seconds) => {
|
DcOutStatus::TurningOff(target) => {
|
||||||
let seconds = seconds - 1;
|
let now = Time::new().get_time();
|
||||||
if seconds > 0 {
|
if now > target {
|
||||||
self.status = DcOutStatus::TurningOff(seconds);
|
|
||||||
} else {
|
|
||||||
self.status = DcOutStatus::Off;
|
self.status = DcOutStatus::Off;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,21 +68,21 @@ impl DcOutControllerState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn turn_off(&mut self) {
|
fn turn_off(&mut self) {
|
||||||
|
let now = Time::new().get_time();
|
||||||
match self.status {
|
match self.status {
|
||||||
DcOutStatus::On => {
|
DcOutStatus::On => {
|
||||||
self.status = DcOutStatus::TurningOff(WAITING_OFF_SECONDS);
|
self.status =
|
||||||
|
DcOutStatus::TurningOff(now + Duration::from_secs(WAITING_OFF_DURATION));
|
||||||
}
|
}
|
||||||
DcOutStatus::WaitingOff => {
|
DcOutStatus::WaitingOff => {
|
||||||
self.status = DcOutStatus::TurningOff(WAITING_OFF_SECONDS);
|
self.status =
|
||||||
|
DcOutStatus::TurningOff(now + Duration::from_secs(WAITING_OFF_DURATION));
|
||||||
}
|
}
|
||||||
DcOutStatus::WaitingOn(_) => {
|
DcOutStatus::WaitingOn(_) => {
|
||||||
self.status = DcOutStatus::Off;
|
self.status = DcOutStatus::Off;
|
||||||
}
|
}
|
||||||
DcOutStatus::TurningOff(seconds) => {
|
DcOutStatus::TurningOff(target) => {
|
||||||
let seconds = seconds - 1;
|
if target < now {
|
||||||
if seconds > 0 {
|
|
||||||
self.status = DcOutStatus::TurningOff(seconds);
|
|
||||||
} else {
|
|
||||||
self.status = DcOutStatus::Off;
|
self.status = DcOutStatus::Off;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,26 +91,22 @@ impl DcOutControllerState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn turn_on(&mut self) {
|
fn turn_on(&mut self) {
|
||||||
|
let now = Time::new().get_time();
|
||||||
match self.status {
|
match self.status {
|
||||||
DcOutStatus::WaitingOff => {
|
DcOutStatus::WaitingOff => {
|
||||||
self.status = DcOutStatus::On;
|
self.status = DcOutStatus::On;
|
||||||
}
|
}
|
||||||
DcOutStatus::Off => {
|
DcOutStatus::Off => {
|
||||||
self.status = DcOutStatus::WaitingOn(WAITING_OFF_SECONDS);
|
self.status =
|
||||||
|
DcOutStatus::WaitingOn(now + Duration::from_secs(WAITING_ON_DURATION));
|
||||||
}
|
}
|
||||||
DcOutStatus::WaitingOn(seconds) => {
|
DcOutStatus::WaitingOn(target) => {
|
||||||
let seconds = seconds - 1;
|
if target <= now {
|
||||||
if seconds > 0 {
|
|
||||||
self.status = DcOutStatus::WaitingOn(seconds);
|
|
||||||
} else {
|
|
||||||
self.status = DcOutStatus::On;
|
self.status = DcOutStatus::On;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DcOutStatus::TurningOff(seconds) => {
|
DcOutStatus::TurningOff(target) => {
|
||||||
let seconds = seconds - 1;
|
if target <= now {
|
||||||
if seconds > 0 {
|
|
||||||
self.status = DcOutStatus::TurningOff(seconds);
|
|
||||||
} else {
|
|
||||||
self.status = DcOutStatus::On;
|
self.status = DcOutStatus::On;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,11 +126,18 @@ impl DcOutControllerState {
|
|||||||
PinState::Low => "Low",
|
PinState::Low => "Low",
|
||||||
PinState::High => "High",
|
PinState::High => "High",
|
||||||
};
|
};
|
||||||
let seconds: i16 = match self.status {
|
let now = Time::new().get_time();
|
||||||
DcOutStatus::WaitingOn(seconds) => seconds.into(),
|
let target = match self.status {
|
||||||
DcOutStatus::TurningOff(seconds) => seconds.into(),
|
DcOutStatus::WaitingOn(target) => target,
|
||||||
_ => -1,
|
DcOutStatus::TurningOff(target) => target,
|
||||||
|
_ => Duration::ZERO,
|
||||||
};
|
};
|
||||||
|
let seconds = if now < target {
|
||||||
|
target - now
|
||||||
|
} else {
|
||||||
|
Duration::ZERO
|
||||||
|
}
|
||||||
|
.as_secs();
|
||||||
json!({ "status": status, "pin_state": pin_state, "seconds": seconds }).to_string()
|
json!({ "status": status, "pin_state": pin_state, "seconds": seconds }).to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
for _ in 0..10 {
|
||||||
let voltage = adc.read(pin);
|
let voltage = adc.read(pin);
|
||||||
match voltage {
|
match voltage {
|
||||||
Ok(voltage) => anyhow::Ok(voltage),
|
Ok(voltage) => {
|
||||||
|
avg_voltage += voltage;
|
||||||
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
anyhow::bail!("Adapter Voltage read failed: {:?}", err)
|
anyhow::bail!("Adapter Voltage read failed: {:?}", err)
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
sleep(Duration::from_millis(100));
|
||||||
}
|
}
|
||||||
|
anyhow::Ok(avg_voltage / 10)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user