#include <automation.h>
#include "actuator_mgr.h"

extern FLASH_STRUCT Flash;
extern bool actuatorRunning[];


// Start actuator when low limit is reached and stop it when low limit - hysteresis is reached
void actuator_AAR_OVER( uint16_t id, uint16_t reference, uint16_t hysteresis )
{
	WATERING_ACTUATOR* wa = &Flash.settings.actuators[id];
	bool* stateFlag = &actuatorRunning[id];

	if( wa->autoRunFeature != 0 )	//AAR_DISABLE
	{
		if( wa->autoRunEnabled )
		{
			// Start actuator
			if( reference > wa->setpoints[0] && !*stateFlag )
			{
				*stateFlag = true;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_START, UNDEF_FLOWRATE );
			}
			// Stop actuator
			if( (reference <= (wa->setpoints[0] - hysteresis) ) && *stateFlag )
			{
				*stateFlag = false;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_STOP, UNDEF_FLOWRATE );
			}
		}
		else
		{
			// Stop actuator
			if( *stateFlag )
			{
				*stateFlag = false;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_STOP, UNDEF_FLOWRATE );
			}
		}
	}
}


// Start actuator when low limit is reached and stop it when high limit is reached
void actuator_AAR_OUTOFRANGE( uint16_t id, uint16_t reference, uint16_t hysteresis )
{
	WATERING_ACTUATOR* wa = &Flash.settings.actuators[id];
	bool* stateFlag = &actuatorRunning[id];

	if( wa->autoRunFeature != 0 )	// AAR_DISABLE
	{
		if( wa->autoRunEnabled )
		{
			// Start actuator
			if( (reference < wa->setpoints[0]) && !*stateFlag )
			{
				*stateFlag = true;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_START, UNDEF_FLOWRATE );
			}
			// Stop actuator
			if( (reference > wa->setpoints[1]) && *stateFlag )
			{
				*stateFlag = false;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_STOP, UNDEF_FLOWRATE );
			}
		}
		else
		{
			// Stop actuator
			if( *stateFlag )
			{
				*stateFlag = false;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_STOP, UNDEF_FLOWRATE );
			}
		}
	}
}

// Stop actuator when reference is reached and start it when reference + hysteresis is reached
void actuator_AAR_ABOVE( uint16_t id, uint16_t reference, uint16_t hysteresis )
{
	WATERING_ACTUATOR* wa = &Flash.settings.actuators[id];
	bool* stateFlag = &actuatorRunning[id];

	if( wa->autoRunFeature != 0 )	//AAR_DISABLE
	{
		if( wa->autoRunEnabled )
		{
			// Start actuator
			if( ( reference <= wa->setpoints[0] ) && !*stateFlag )
			{
				*stateFlag = true;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_START, UNDEF_FLOWRATE );
			}
			// Stop actuator
			if( ( reference > (wa->setpoints[0] + hysteresis) ) && *stateFlag )
			{
				*stateFlag = false;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_STOP, UNDEF_FLOWRATE );
			}
		}
		else
		{
			// Stop actuator
			if( *stateFlag )
			{
				*stateFlag = false;
				Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_STOP, UNDEF_FLOWRATE );
			}
		}
	}
}

// Start actuator
void actuator_AAR_ON( uint16_t id )
{
	WATERING_ACTUATOR* wa = &Flash.settings.actuators[id];
	Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_START, UNDEF_FLOWRATE );
}

// Stop actuator
void actuator_AAR_OFF( uint16_t id )
{
	WATERING_ACTUATOR* wa = &Flash.settings.actuators[id];
	Automation_sendCommandToActuators( BITSHIFT_OUTPUT(wa->outputNum), Z_STOP, UNDEF_FLOWRATE );
}

