new code base
This commit is contained in:
+64
-73
@@ -1,97 +1,88 @@
|
||||
#include "pump.h"
|
||||
|
||||
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin) {
|
||||
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t errorLedPin) {
|
||||
this->pumpPin = pumpPin;
|
||||
this->pressurePin = pressurePin;
|
||||
this->flowPin = flowPin;
|
||||
this->errorLedPin = errorLedPin;
|
||||
|
||||
pinMode(this->pumpPin, OUTPUT);
|
||||
pinMode(this->errorLedPin, OUTPUT);
|
||||
pinMode(this->pressurePin, INPUT);
|
||||
// Hall flow sensor?
|
||||
|
||||
digitalWrite(this->pumpPin, LOW);
|
||||
digitalWrite(this->errorLedPin, 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);
|
||||
switch (this->state) {
|
||||
case State::Off :
|
||||
|
||||
break;
|
||||
|
||||
case Status::On :
|
||||
digitalWrite(this->pumpPin, HIGH);
|
||||
this->pumpRunningStart = millis();
|
||||
case State::On :
|
||||
|
||||
break;
|
||||
|
||||
case Status::Standby :
|
||||
;
|
||||
case State::Timer :
|
||||
|
||||
break;
|
||||
|
||||
case Status::Failure :
|
||||
digitalWrite(this->pumpPin, LOW);
|
||||
if (this->errorFunction)
|
||||
this->errorFunction(this->error);
|
||||
case State::Standby :
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case State::Error :
|
||||
if (millis() - this->startTime > Pump::blinkTime) {
|
||||
this->startTime = millis();
|
||||
if (this->ledState) {
|
||||
this->ledState = false;
|
||||
digitalWrite(this->errorLedPin, LOW);
|
||||
} else {
|
||||
this->ledState = true;
|
||||
digitalWrite(this->errorLedPin, HIGH);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Pump::activate(uint32_t duration = 0) {
|
||||
this->startTime = millis();
|
||||
if (duration) {
|
||||
this->state = State::Timer;
|
||||
this->duration = duration;
|
||||
} else {
|
||||
this->state = State::On;
|
||||
}
|
||||
}
|
||||
|
||||
void Pump::stop() {
|
||||
if (this->state != State::Error)
|
||||
this->state = State::Off;
|
||||
}
|
||||
|
||||
void Pump::clearError() {
|
||||
if (this->state == State::Error)
|
||||
this->state = State::Off;
|
||||
}
|
||||
|
||||
uint16_t Pump::getPressure() {
|
||||
|
||||
}
|
||||
|
||||
uint16_t Pump::getRunningTime() {
|
||||
|
||||
}
|
||||
|
||||
uint16_t Pump::getFlow() {
|
||||
|
||||
}
|
||||
|
||||
void Pump::checkSensors() {
|
||||
|
||||
}
|
||||
+19
-37
@@ -2,60 +2,42 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
class Pump {
|
||||
public:
|
||||
enum Error : uint8_t {
|
||||
None,
|
||||
HighPressure,
|
||||
LowFlow,
|
||||
Both
|
||||
};
|
||||
|
||||
enum Status : uint8_t {
|
||||
static const uint16_t blinkTime = 1000;
|
||||
enum State {
|
||||
Off,
|
||||
On,
|
||||
Timer,
|
||||
Standby,
|
||||
Failure
|
||||
Error
|
||||
};
|
||||
|
||||
typedef void (*ErrorFunction)(Error);
|
||||
|
||||
Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin);
|
||||
Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t errorLedPin);
|
||||
|
||||
void loop();
|
||||
void clearError() { this->error = Error::None; }
|
||||
bool switchPump(bool state);
|
||||
void incrementFlowCounter() { this->flowCounter++; };
|
||||
|
||||
uint8_t getFlowPin() { return this->flowPin; }
|
||||
uint32_t getPumpRunningTime();
|
||||
void activate(uint32_t duration = 0);
|
||||
void stop();
|
||||
void clearError();
|
||||
|
||||
void setErrorFunction(ErrorFunction ptr) { this->errorFunction = ptr; }
|
||||
uint16_t getPressure();
|
||||
uint16_t getRunningTime();
|
||||
uint16_t getFlow();
|
||||
|
||||
State getState() { return this->state; }
|
||||
|
||||
private:
|
||||
void calculatePressure();
|
||||
void calculateFlow();
|
||||
void checkErrors();
|
||||
void controlPump();
|
||||
void checkSensors();
|
||||
State state = State::Off;
|
||||
|
||||
void setStatus(Status status);
|
||||
|
||||
Status lastStatus = Status::Off;
|
||||
Status currentStatus = Status::Off;
|
||||
Error error = Error::None;
|
||||
ErrorFunction errorFunction = nullptr;
|
||||
bool ledState = false;
|
||||
|
||||
uint8_t pumpPin;
|
||||
uint8_t pressurePin;
|
||||
uint8_t flowPin;
|
||||
uint8_t delayLoopMs = 20;
|
||||
uint8_t errorLedPin;
|
||||
|
||||
uint16_t flowCounter = 0;
|
||||
uint16_t pressure = 0;
|
||||
|
||||
uint32_t lastMillisLoop = 0;
|
||||
uint32_t lastPressureCalculation = 0;
|
||||
uint32_t lastFlowCalculation = 0;
|
||||
uint32_t pumpRunningStart = 0;
|
||||
uint32_t startTime;
|
||||
uint32_t duration;
|
||||
};
|
||||
Reference in New Issue
Block a user