From bd699cd238d5e010fba030c07c77d322b745ec50 Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Wed, 15 Feb 2023 18:25:59 +0100 Subject: [PATCH] added class Pump --- lib/Pump/pump.cpp | 60 ++++++++++++++++++++++++++++++++++++----------- lib/Pump/pump.h | 16 +++++++++++-- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/lib/Pump/pump.cpp b/lib/Pump/pump.cpp index b7fc60d..4c19bbf 100644 --- a/lib/Pump/pump.cpp +++ b/lib/Pump/pump.cpp @@ -19,47 +19,79 @@ void Pump::loop() { this->calculatePressure(); this->calculateFlow(); this->checkErrors(); + this->controlPump(); } bool Pump::switchPump(bool state) { - if (this->error != Error::None) + if (this->currentStatus == Status::Failure) + return false; + + if (state && this->currentStatus == Status::Standby) return false; if (state) - this->pumpRunningStart = millis(); - - digitalWrite(this->pumpPin, state); + this->currentStatus = Status::On; + else + this->currentStatus = Status::Off; return true; } uint32_t Pump::getPumpRunningTime() { - if (pumpState) + if (this->currentStatus == Status::On) return millis() - this->pumpRunningStart; return 0; } void Pump::calculatePressure() { - uint16_t pressure; uint16_t rawValue = analogRead(this->pressurePin); if (rawValue < 104) - pressure = 0; + this->pressure = 0; else if (rawValue > 922) - pressure = 550; + this->pressure = 550; else - pressure = map(rawValue, 104, 922, 0, 550); + this->pressure = map(rawValue, 104, 922, 0, 550); - //TODO: wenn pumpe lange an und druck hoch dann aus + //TODO: Error checking } void Pump::calculateFlow() { - + //TODO: Error checking } void Pump::checkErrors() { if (this->error == Error::None) return; - digitalWrite(this->pumpPin, LOW); - if (this->errorFunction) - this->errorFunction(this->error); + if (this->error ) + +} + +void Pump::controlPump() { + if (this->lastStatus == this->currentStatus) + return; + this->lastStatus = this->currentStatus; + + switch (this->currentStatus) { + case Status::Off : + digitalWrite(this->pumpPin, LOW); + break; + + case Status::On : + digitalWrite(this->pumpPin, HIGH); + this->pumpRunningStart = millis(); + break; + + case Status::Standby : + ; + break; + + case Status::Failure : + digitalWrite(this->pumpPin, LOW); + if (this->errorFunction) + this->errorFunction(this->error); + break; + + default: + break; + } } diff --git a/lib/Pump/pump.h b/lib/Pump/pump.h index 7ae9368..087003c 100644 --- a/lib/Pump/pump.h +++ b/lib/Pump/pump.h @@ -11,6 +11,14 @@ class Pump { LowFlow, Both }; + + enum Status : uint8_t { + Off, + On, + Standby, + Failure + }; + typedef void (*ErrorFunction)(Error); Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin); @@ -29,18 +37,22 @@ class Pump { void calculatePressure(); void calculateFlow(); void checkErrors(); + void controlPump(); + void setStatus(Status status); + + Status lastStatus = Status::Off; + Status currentStatus = Status::Off; Error error = Error::None; ErrorFunction errorFunction = nullptr; - bool pumpState = false; - uint8_t pumpPin; uint8_t pressurePin; uint8_t flowPin; uint8_t delayLoopMs = 20; uint16_t flowCounter = 0; + uint16_t pressure = 0; uint32_t lastMillisLoop = 0; uint32_t lastPressureCalculation = 0;