From b2939ee04f0060c87c624d2456e1129bbcf62912 Mon Sep 17 00:00:00 2001 From: "Jack Gabriel C. Acodili" <112671361+acodili-jg@users.noreply.github.com> Date: Tue, 14 May 2024 18:59:38 +0800 Subject: [PATCH] Attempt at organizing stuff --- src/duration.rs | 2 +- src/sketch.rs | 6 ++-- src/state.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/src/duration.rs b/src/duration.rs index e31717b..6b4477a 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -20,5 +20,5 @@ constants! { RINSING = DEFAULT_LONG; SEPARATOR_HOLDING = DEFAULT_LONG; SEPARATOR_TRANSITION = DEFAULT; - SOAK_WATER_PUMPING = DEFAULT_LONG; + WATER_PUMPING = DEFAULT_LONG; } diff --git a/src/sketch.rs b/src/sketch.rs index 656b669..aa3e2b4 100644 --- a/src/sketch.rs +++ b/src/sketch.rs @@ -189,7 +189,7 @@ impl Sketch { self.water_pump.set_high(); } - State::InitialSetupWaterPumping if delta_ms < duration::SOAK_WATER_PUMPING => {} + State::InitialSetupWaterPumping if delta_ms < duration::WATER_PUMPING => {} State::InitialSetupWaterPumping => { transition_to!(InitialSetupSeparatorClosing); self.water_pump.set_low(); @@ -209,7 +209,7 @@ impl Sketch { self.upper_drain_pump.set_high(); self.water_pump.set_low(); } - State::SoakWaterPumping if delta_ms < duration::SOAK_WATER_PUMPING => {} + State::SoakWaterPumping if delta_ms < duration::WATER_PUMPING => {} State::SoakWaterPumping => { transition_to!(SoakWaterHeating); self.water_pump.set_low(); @@ -318,7 +318,7 @@ impl Sketch { self.water_pump.set_high(); } - State::SetupWaterPumping if delta_ms < duration::SOAK_WATER_PUMPING => {} + State::SetupWaterPumping if delta_ms < duration::WATER_PUMPING => {} State::SetupWaterPumping => { transition_to!(SetupSeparatorClosing); self.water_pump.set_low(); diff --git a/src/state.rs b/src/state.rs index ea28ad9..9a903fc 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,3 +1,5 @@ +use crate::{duration, millis::Millis}; + #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub enum State { #[default] @@ -27,3 +29,95 @@ pub enum State { Locking, Unlocking, } + +#[allow(clippy::cognitive_complexity)] +#[must_use] +pub fn override_for(curr: State, delta: Millis, starting: bool, stopping: bool) -> Option { + Some(match curr { + State::InitialDraining if delta >= duration::DRAINING => State::InitialIdling, + + State::InitialIdling if starting => State::InitialLocking, + + State::InitialLocking if !starting => State::InitialUnlocking, + State::InitialLocking if delta >= duration::LOCKING => State::InitialSetupSeparatorOpening, + + State::InitialUnlocking if delta >= duration::LOCKING => State::InitialIdling, + + State::InitialSetupSeparatorOpening if delta >= duration::SEPARATOR_TRANSITION => { + State::InitialSetupWaterPumping + } + + State::InitialSetupWaterPumping if delta >= duration::WATER_PUMPING => { + State::InitialSetupSeparatorClosing + } + + State::InitialSetupSeparatorClosing if delta >= duration::SEPARATOR_TRANSITION => { + State::SoakWaterPumping + } + + State::SoakWaterPumping + | State::SoakWaterHeating + | State::SoakWaterHeatedMixing + | State::SoakWaterMixing + if stopping => + { + State::SoakWaterDraining + } + + State::SoakWaterPumping if delta >= duration::WATER_PUMPING => State::SoakWaterDraining, + State::SoakWaterHeating if delta >= duration::HEATING => State::SoakWaterHeatedMixing, + State::SoakWaterHeatedMixing if delta >= duration::HEATED_MIXING => State::SoakWaterMixing, + State::SoakWaterMixing if delta >= duration::MIXING => State::SoakWaterDraining, + + State::SoakWaterDraining if delta >= duration::DRAINING => { + if stopping { + State::Idling + } else { + State::RinseWaterPumping + } + } + + State::RinseWaterPumping if stopping || delta >= duration::RINSING => { + State::RinseWaterDraining + } + + State::RinseWaterDraining if delta >= duration::DRAINING => { + if stopping { + State::Idling + } else { + State::SeparatorOpening + } + } + + State::SeparatorOpening if delta >= duration::SEPARATOR_TRANSITION => { + State::SeparatorHolding + } + + State::SeparatorHolding if delta >= duration::SEPARATOR_HOLDING => State::SeparatorClosing, + + State::SeparatorClosing if delta >= duration::SEPARATOR_TRANSITION => State::Blending, + + State::Blending if delta >= duration::BLENDING => State::PulpDraining, + + State::PulpDraining if delta >= duration::DRAINING => State::SetupSeparatorOpening, + + State::SetupSeparatorOpening if delta >= duration::SEPARATOR_TRANSITION => { + State::SetupWaterPumping + } + + State::SetupWaterPumping if delta >= duration::WATER_PUMPING => { + State::SetupSeparatorClosing + } + + State::SetupSeparatorClosing if delta >= duration::SEPARATOR_TRANSITION => State::Idling, + + State::Idling if starting => State::Locking, + + State::Locking if !starting => State::Unlocking, + State::Locking if delta >= duration::LOCKING => State::SoakWaterPumping, + + State::Unlocking if delta >= duration::LOCKING => State::Idling, + + _ => return None, + }) +}