98 lines
2.1 KiB
C++
98 lines
2.1 KiB
C++
#include "pump.h"
|
|
|
|
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin) {
|
|
this->pumpPin = pumpPin;
|
|
this->pressurePin = pressurePin;
|
|
this->flowPin = flowPin;
|
|
|
|
pinMode(this->pumpPin, OUTPUT);
|
|
pinMode(this->pressurePin, INPUT);
|
|
|
|
digitalWrite(this->pumpPin, LOW);
|
|
}
|
|
|
|
void Pump::loop() {
|
|
if (millis() - lastMillisLoop < this->delayLoopMs)
|
|
return;
|
|
this->lastMillisLoop = millis();
|
|
|
|
this->calculatePressure();
|
|
this->calculateFlow();
|
|
this->checkErrors();
|
|
this->controlPump();
|
|
}
|
|
|
|
bool Pump::switchPump(bool state) {
|
|
if (this->currentStatus == Status::Failure)
|
|
return false;
|
|
|
|
if (state && this->currentStatus == Status::Standby)
|
|
return false;
|
|
|
|
if (state)
|
|
this->currentStatus = Status::On;
|
|
else
|
|
this->currentStatus = Status::Off;
|
|
return true;
|
|
}
|
|
|
|
uint32_t Pump::getPumpRunningTime() {
|
|
if (this->currentStatus == Status::On)
|
|
return millis() - this->pumpRunningStart;
|
|
return 0;
|
|
}
|
|
|
|
void Pump::calculatePressure() {
|
|
uint16_t rawValue = analogRead(this->pressurePin);
|
|
if (rawValue < 104)
|
|
this->pressure = 0;
|
|
else if (rawValue > 922)
|
|
this->pressure = 550;
|
|
else
|
|
this->pressure = map(rawValue, 104, 922, 0, 550);
|
|
|
|
//TODO: Error checking
|
|
}
|
|
|
|
void Pump::calculateFlow() {
|
|
//TODO: Error checking
|
|
}
|
|
|
|
void Pump::checkErrors() {
|
|
if (this->error == Error::None)
|
|
return;
|
|
|
|
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;
|
|
}
|
|
}
|