Compare commits
No commits in common. "daf6effe59800ff40fda3da399472f0ab47b64d7" and "2aaa85af71cc9485d690c0b3021b45c83fcb4e69" have entirely different histories.
daf6effe59
...
2aaa85af71
@ -1,7 +1,4 @@
|
|||||||
use std::{
|
use std::sync::{Arc, Mutex, MutexGuard};
|
||||||
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};
|
||||||
@ -11,13 +8,10 @@ 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::{
|
use crate::voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP};
|
||||||
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>>,
|
||||||
@ -33,7 +27,6 @@ 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 {
|
||||||
@ -41,7 +34,6 @@ impl ChargeControllerState {
|
|||||||
Self {
|
Self {
|
||||||
status: ChargeStatus::Charging,
|
status: ChargeStatus::Charging,
|
||||||
pin_state: PinState::Low,
|
pin_state: PinState::Low,
|
||||||
charge_deadline_at: Duration::ZERO,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,13 +46,7 @@ impl ChargeControllerState {
|
|||||||
PinState::Low => "Low",
|
PinState::Low => "Low",
|
||||||
PinState::High => "High",
|
PinState::High => "High",
|
||||||
};
|
};
|
||||||
let now = Time::new().get_time();
|
json!({ "status": status, "pin_state": pin_state}).to_string()
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,14 +112,7 @@ 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();
|
state.status = ChargeStatus::Charged;
|
||||||
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;
|
|
||||||
} else {
|
|
||||||
state.status = ChargeStatus::Charging;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ChargeStatus::Charged => {
|
ChargeStatus::Charged => {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::{
|
use std::{
|
||||||
sync::{Arc, Mutex, MutexGuard},
|
sync::{
|
||||||
time::Duration,
|
Arc, Mutex, MutexGuard,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use embedded_hal::digital::v2::{OutputPin, PinState};
|
use embedded_hal::digital::v2::{OutputPin, PinState};
|
||||||
@ -11,16 +12,12 @@ 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::{
|
use crate::voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP};
|
||||||
time::Time,
|
|
||||||
voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP},
|
|
||||||
};
|
|
||||||
|
|
||||||
const WAITING_OFF_DURATION: u64 = 60;
|
const WAITING_OFF_SECONDS: u8 = 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>>,
|
||||||
@ -28,11 +25,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(Duration),
|
WaitingOn(u8),
|
||||||
On,
|
On,
|
||||||
Off,
|
Off,
|
||||||
WaitingOff,
|
WaitingOff,
|
||||||
TurningOff(Duration),
|
TurningOff(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@ -57,9 +54,11 @@ impl DcOutControllerState {
|
|||||||
DcOutStatus::WaitingOn(_) => {
|
DcOutStatus::WaitingOn(_) => {
|
||||||
self.status = DcOutStatus::Off;
|
self.status = DcOutStatus::Off;
|
||||||
}
|
}
|
||||||
DcOutStatus::TurningOff(target) => {
|
DcOutStatus::TurningOff(seconds) => {
|
||||||
let now = Time::new().get_time();
|
let seconds = seconds - 1;
|
||||||
if now > target {
|
if seconds > 0 {
|
||||||
|
self.status = DcOutStatus::TurningOff(seconds);
|
||||||
|
} else {
|
||||||
self.status = DcOutStatus::Off;
|
self.status = DcOutStatus::Off;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,21 +67,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 =
|
self.status = DcOutStatus::TurningOff(WAITING_OFF_SECONDS);
|
||||||
DcOutStatus::TurningOff(now + Duration::from_secs(WAITING_OFF_DURATION));
|
|
||||||
}
|
}
|
||||||
DcOutStatus::WaitingOff => {
|
DcOutStatus::WaitingOff => {
|
||||||
self.status =
|
self.status = DcOutStatus::TurningOff(WAITING_OFF_SECONDS);
|
||||||
DcOutStatus::TurningOff(now + Duration::from_secs(WAITING_OFF_DURATION));
|
|
||||||
}
|
}
|
||||||
DcOutStatus::WaitingOn(_) => {
|
DcOutStatus::WaitingOn(_) => {
|
||||||
self.status = DcOutStatus::Off;
|
self.status = DcOutStatus::Off;
|
||||||
}
|
}
|
||||||
DcOutStatus::TurningOff(target) => {
|
DcOutStatus::TurningOff(seconds) => {
|
||||||
if target < now {
|
let seconds = seconds - 1;
|
||||||
|
if seconds > 0 {
|
||||||
|
self.status = DcOutStatus::TurningOff(seconds);
|
||||||
|
} else {
|
||||||
self.status = DcOutStatus::Off;
|
self.status = DcOutStatus::Off;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -91,22 +90,26 @@ 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 =
|
self.status = DcOutStatus::WaitingOn(WAITING_OFF_SECONDS);
|
||||||
DcOutStatus::WaitingOn(now + Duration::from_secs(WAITING_ON_DURATION));
|
|
||||||
}
|
}
|
||||||
DcOutStatus::WaitingOn(target) => {
|
DcOutStatus::WaitingOn(seconds) => {
|
||||||
if target <= now {
|
let seconds = seconds - 1;
|
||||||
|
if seconds > 0 {
|
||||||
|
self.status = DcOutStatus::WaitingOn(seconds);
|
||||||
|
} else {
|
||||||
self.status = DcOutStatus::On;
|
self.status = DcOutStatus::On;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DcOutStatus::TurningOff(target) => {
|
DcOutStatus::TurningOff(seconds) => {
|
||||||
if target <= now {
|
let seconds = seconds - 1;
|
||||||
|
if seconds > 0 {
|
||||||
|
self.status = DcOutStatus::TurningOff(seconds);
|
||||||
|
} else {
|
||||||
self.status = DcOutStatus::On;
|
self.status = DcOutStatus::On;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,18 +129,11 @@ impl DcOutControllerState {
|
|||||||
PinState::Low => "Low",
|
PinState::Low => "Low",
|
||||||
PinState::High => "High",
|
PinState::High => "High",
|
||||||
};
|
};
|
||||||
let now = Time::new().get_time();
|
let seconds: i16 = match self.status {
|
||||||
let target = match self.status {
|
DcOutStatus::WaitingOn(seconds) => seconds.into(),
|
||||||
DcOutStatus::WaitingOn(target) => target,
|
DcOutStatus::TurningOff(seconds) => seconds.into(),
|
||||||
DcOutStatus::TurningOff(target) => target,
|
_ => -1,
|
||||||
_ => 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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user