#include "Pump.h" Pump::Pump( int iOutputPin, int iOffValue, int iMaxValue ) { Pump(iOutputPin, iOffValue, iMaxValue, DEFAULT_PUMP_MAX_OPETATION_TEMPERARTURE, DEFAULT_PUMP_PRIMING_TIME_IN_SECONDS); } Pump::Pump( int iOutputPin, int iOffValue, int iMaxValue, int iMaxTemperature, int iPrimingTimeInSeconds ) { _iOutputPin = iOutputPin; _iOffValue = iOffValue; _iMaxValue = iMaxValue; _iMaxTemperature = iMaxTemperature; _iPrimingTimeInSeconds = iPrimingTimeInSeconds; _iActualPumpSpeed = _iOffValue; // Time frame to operate in _iTargetPumpSpeed = _iOffValue; _temperature = NULL; _selfPrimingMode = false; _hasBeenPrimedSinceStartup = false; pinMode(_iOutputPin, OUTPUT); // sets the pin as output analogWrite(_iOutputPin, _iOffValue); // analogWrite values from 0 to 255 } void Pump::shutDown() { _iActualPumpSpeed = _iOffValue; // Time frame to operate in _iTargetPumpSpeed = _iOffValue; _selfPrimingMode = false; // Turn off gracefully process(); } bool Pump::process() { if(_temperature != NULL) { // Operate pump if its operating temperature is bellow the maximum operating temperature float currentTemperature = _temperature->getCurrentTemperature(); if (currentTemperature <= _iMaxTemperature) { // Self prime on first use if(!_hasBeenPrimedSinceStartup && _iTargetPumpSpeed != _iOffValue) { _hasBeenPrimedSinceStartup = true; _selfPrimingMode = true; _millisAtPrimingStart = millis(); } // Process end of self priming mode if(millis() - _millisAtPrimingStart > _iPrimingTimeInSeconds * 1000) { _selfPrimingMode = false; } // Control speed for self priming mode of operation if(_selfPrimingMode) { // Alternate speed if ( ((millis()/1000) % 2) == 0 ) { _iActualPumpSpeed = _iMaxValue; } else { _iActualPumpSpeed = _iOffValue; } } // Set target speed in normal mode of operation else { _iActualPumpSpeed = _iTargetPumpSpeed; } } // Turn pump off if its operating temperature exceeds the maximum operating temperature else { _iActualPumpSpeed = _iOffValue; } } else { // The pump will remain off until temperature feedback is provided _iActualPumpSpeed = _iOffValue; } // Set pump speed analogWrite(_iOutputPin, _iActualPumpSpeed); // analogWrite values from 0 to 255 return _iActualPumpSpeed; } bool Pump::process( int iTargetPumpSpeed ) { setTargetPumpSpeed(iTargetPumpSpeed); return process(); } void Pump::forcePumpSelfPrime() { _selfPrimingMode = true; _millisAtPrimingStart = millis(); } // Getter and setters bool Pump::isPumpOn() { return _iActualPumpSpeed != _iOffValue; } void Pump::setCheckTemperatureFunction( Temperature *temperature ) { _temperature = temperature; } int Pump::getTargetPumpSpeed() { return _iTargetPumpSpeed; } void Pump::setTargetPumpSpeed( int iTargetPumpSpeed ) { _iTargetPumpSpeed = iTargetPumpSpeed; } // Power Increments int Pump::getNullSpeed() { return _iOffValue; } int Pump::getOneSixthSpeed() { return calculateSpeedFraction(1.0/6.0); } int Pump::getOneThirdSpeed() { return calculateSpeedFraction(1.0/3.0); } int Pump::getHalfSpeed() { return calculateSpeedFraction(1.0/2.0); } int Pump::getTwoThirdSpeed() { return calculateSpeedFraction(2.0/3.0); } int Pump::getMaxSpeed() { return _iMaxValue; } // Private functions int Pump::calculateSpeedFraction(double fraction) { int ret; if(_iMaxValue >= _iOffValue) { ret = _iMaxValue * fraction; } else { ret = _iOffValue * ((1.0 - fraction) * -1.0); } return ret; }