Files
Bewaesserung-Arduino/lib/Pump/pump.cpp
T
2023-07-03 17:46:44 +02:00

198 lines
4.3 KiB
C++

#include "pump.h"
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t ledPin) {
this->pumpPin = pumpPin;
this->pressurePin = pressurePin;
this->flowPin = flowPin;
this->ledPin = ledPin;
pinMode(this->pumpPin, OUTPUT);
pinMode(this->ledPin, OUTPUT);
pinMode(this->pressurePin, INPUT);
// Hall flow sensor?
digitalWrite(this->pumpPin, LOW);
digitalWrite(this->ledPin, LOW);
}
void Pump::loop() {
switch (this->state) {
case State::Off :
break;
case State::On :
this->timerStandby = false;
this->checkSensors();
break;
case State::Timer :
this->stateTimer();
break;
case State::Standby :
this->stateStandby();
break;
case State::Error :
this->stateError();
break;
default :
break;
}
}
void Pump::activate(uint32_t duration) {
if (duration) {
this->changeState(State::Timer);
this->duration = duration;
} else {
this->changeState(State::On);
}
}
void Pump::stop() {
this->changeState(State::Off);
}
void Pump::clearError() {
if (this->state == State::Error) {
this->state = State::Off;
this->changeState(State::Off);
}
}
uint16_t Pump::getPressure() {
return 0;
}
uint32_t Pump::getRunningTime() {
if (this->state == State::Off)
return 0;
return (millis() - this->startTime) / 1000;
}
uint16_t Pump::getFlow() {
return 0;
}
uint8_t Pump::getStatus() {
return (uint8_t) this->state;
}
bool Pump::checkSensors() {
if (millis() - this->lastMillisCheckSensor < Pump::delayCheckSensor)
return true;
this->lastMillisCheckSensor = millis();
//TODO: read pressure
//TODO: read flow
if (this->flow) {
return true;
}
if (!this->pressure) {
this->changeState(State::Error);
return false;
}
if (pressure > Pump::maxPressure) {
if (this->maxPressureStart == 0)
this->maxPressureStart = millis();
else if (millis() - this->maxPressureStart > Pump::maxMaxPressureTime)
this->changeState(State::Standby);
}
return true;
}
void Pump::handlePins(bool state) {
digitalWrite(this->pumpPin, state);
digitalWrite(this->ledPin, state);
if (state) {
this->timerStandby = false;
this->startTime = millis();
this->maxPressureStart = 0;
}
}
void Pump::changeState(State state) {
if (this->state == State::Error)
return;
if (this->state == state)
return;
switch (state) {
case State::Off :
this->handlePins(false);
this->pressure = 0;
this->flow = 0;
break;
case State::On :
this->handlePins(true);
break;
case State::Timer :
this->handlePins(true);
break;
case State::Standby :
if (this->state == State::Timer)
this->timerStandby = true;
this->handlePins(false);
break;
case State::Error :
this->handlePins(false);
break;
case State::OnStupid :
this->handlePins(true);
break;
default:
break;
}
this->state = state;
}
void Pump::stateTimer() {
if (!this->checkSensors())
return;
if (millis() - this->lastMillisTimer < Pump::delayTimer)
return;
this->lastMillisTimer = millis();
uint8_t minutesOn = (millis() - this->startTime) / 1000 / 60;
if (minutesOn >= this->duration)
this->changeState(State::Off);
}
void Pump::stateStandby() {
this->checkSensors();
if (this->pressure < Pump::maxPressure / 2) {
if (this->timerStandby)
this->changeState(State::Timer);
else
this->changeState(State::On);
}
}
void Pump::stateError() {
if (millis() - this->startTime > Pump::blinkTime) {
this->startTime = millis();
if (this->ledState) {
this->ledState = false;
digitalWrite(this->ledPin, LOW);
} else {
this->ledState = true;
digitalWrite(this->ledPin, HIGH);
}
}
}