/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2024 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "./u_highpass.h"

#include <gwenhywfar/debug.h>



/* ------------------------------------------------------------------------------------------------
 * defines
 * ------------------------------------------------------------------------------------------------
 */

#define AQHOMEREACT_UNIT_HIGHPASS_INSLOT_INPUT   0
#define AQHOMEREACT_UNIT_HIGHPASS_OUTSLOT_OUTPUT 0



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void _cbInputData(AQHREACT_UNIT *unit, AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject);



/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

AQHREACT_UNIT *AqHomeReact_UnitHighPass_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;
  AQHREACT_PORT *port;
  AQHREACT_PARAM *param;

  unit=AQHREACT_Unit_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "highpass");
  AQHREACT_Unit_SetDescription(unit, "Highpass filter for data");
  AQHREACT_Unit_SetInputDataFn(unit, _cbInputData);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "output");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_HIGHPASS_OUTSLOT_OUTPUT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddOutputPort(unit, port);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "input");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_HIGHPASS_INSLOT_INPUT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddInputPort(unit, port);

  param=AQHREACT_Param_new();
  AQHREACT_Param_SetName(param, AQHOMEREACT_UNIT_HIGHPASS_PARAM_LIMIT);
  AQHREACT_Param_SetDataType(param, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddParam(unit, param);

  param=AQHREACT_Param_new();
  AQHREACT_Param_SetName(param, AQHOMEREACT_UNIT_HIGHPASS_PARAM_NEWVALUE);
  AQHREACT_Param_SetDataType(param, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddParam(unit, param);

  return unit;
}



void _cbInputData(AQHREACT_UNIT *unit, AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject)
{
  if (unit && port && dataObject && AQHREACT_Port_GetIdForUnit(port)==AQHOMEREACT_UNIT_HIGHPASS_INSLOT_INPUT) {
    AQHREACT_PORT *outputPort;

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_HIGHPASS_OUTSLOT_OUTPUT);
    if (outputPort) {
      double data;
      double limit;
      double newValue;

      data=AQHREACT_DataObject_GetDoubleData(dataObject);
      limit=AQHREACT_Unit_GetParamValueDouble(unit, AQHOMEREACT_UNIT_HIGHPASS_PARAM_LIMIT, data);
      newValue=AQHREACT_Unit_GetParamValueDouble(unit, AQHOMEREACT_UNIT_HIGHPASS_PARAM_NEWVALUE, data);
      AQHREACT_Unit_OutputDoubleData(unit, outputPort, (data>=limit)?data:newValue);
    }
  }
}




