v0.2:重写项目。 #6
@ -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::{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},
|
||||||
|
};
|
||||||
|
|
||||||
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user