aqhome-react: added more logical units (or, and, xor). started "module" unit.
module units now are units created from previous networks of units thus introducing nested units.
This commit is contained in:
256
apps/aqhome-react/units/u_logical.c
Normal file
256
apps/aqhome-react/units/u_logical.c
Normal file
@@ -0,0 +1,256 @@
|
||||
/****************************************************************************
|
||||
* 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_logical.h"
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* defines
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_INPUT 0
|
||||
#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_AUTOINPUT 100
|
||||
|
||||
#define AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT 0
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static AQHREACT_UNIT *_unitLogical_new(AQHOME_REACT *aqh);
|
||||
static int _cbProcessOr(AQHREACT_UNIT *unit);
|
||||
static int _cbProcessAnd(AQHREACT_UNIT *unit);
|
||||
static int _cbProcessXor(AQHREACT_UNIT *unit);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitOr_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitLogical_new(aqh);
|
||||
AQHREACT_Unit_SetName(unit, "or");
|
||||
AQHREACT_Unit_SetDescription(unit, "Logical OR inputs");
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessOr);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitAnd_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitLogical_new(aqh);
|
||||
AQHREACT_Unit_SetName(unit, "and");
|
||||
AQHREACT_Unit_SetDescription(unit, "Logical AND inputs");
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessAnd);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitXor_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitLogical_new(aqh);
|
||||
AQHREACT_Unit_SetName(unit, "xor");
|
||||
AQHREACT_Unit_SetDescription(unit, "Logical XOR inputs");
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessXor);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *_unitLogical_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
AQHREACT_OUTPUT_SLOT *outputSlot;
|
||||
AQHREACT_INPUT_SLOT *inputSlot;
|
||||
|
||||
unit=AQHREACT_Unit_new(aqh);
|
||||
AQHREACT_Unit_SetName(unit, "or");
|
||||
AQHREACT_Unit_SetDescription(unit, "Logical OR inputs");
|
||||
|
||||
AQHREACT_Unit_SetNextInputSlotId(unit, AQHOMEREACT_UNIT_LOGICAL_INSLOT_AUTOINPUT); /* for auto-gen multi-slots */
|
||||
|
||||
outputSlot=AQHREACT_OutputSlot_new();
|
||||
AQHREACT_OutputSlot_SetName(outputSlot, "output");
|
||||
AQHREACT_OutputSlot_SetIdForUnit(outputSlot, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT);
|
||||
AQHREACT_OutputSlot_SetEmittedDataType(outputSlot, AQHREACT_DATAOBJECTTYPE_DOUBLE);
|
||||
AQHREACT_Unit_AddOutputSlot(unit, outputSlot);
|
||||
|
||||
inputSlot=AQHREACT_InputSlot_new();
|
||||
AQHREACT_InputSlot_SetName(inputSlot, "input");
|
||||
AQHREACT_InputSlot_SetIdForUnit(inputSlot, AQHOMEREACT_UNIT_LOGICAL_INSLOT_INPUT);
|
||||
AQHREACT_InputSlot_SetAcceptedDataType(inputSlot, AQHREACT_DATAOBJECTTYPE_DOUBLE);
|
||||
AQHREACT_InputSlot_AddFlags(inputSlot, AQHREACT_UNIT_FLAGS_MULTI);
|
||||
AQHREACT_Unit_AddInputSlot(unit, inputSlot);
|
||||
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessOr(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
AQHREACT_INPUT_SLOT_LIST *inputSlotList;
|
||||
|
||||
inputSlotList=AQHREACT_Unit_GetInputSlots(unit);
|
||||
if (inputSlotList) {
|
||||
const AQHREACT_INPUT_SLOT *inputSlot;
|
||||
int result=0;
|
||||
|
||||
inputSlot=AQHREACT_InputSlot_List_First(inputSlotList);
|
||||
while(inputSlot) {
|
||||
const char *slotName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
slotName=AQHREACT_InputSlot_GetName(inputSlot);
|
||||
dataObject=AQHREACT_InputSlot_GetCurrentDataObject(inputSlot);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
if (d>0.0)
|
||||
result|=1;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", slotName?slotName:"<unnamed>");
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", slotName?slotName:"<unnamed>");
|
||||
}
|
||||
inputSlot=AQHREACT_InputSlot_List_Next(inputSlot);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputSlots(unit);
|
||||
AQHREACT_Unit_OutputDoubleData(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT, result?1.0:0.0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessAnd(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
AQHREACT_INPUT_SLOT_LIST *inputSlotList;
|
||||
|
||||
inputSlotList=AQHREACT_Unit_GetInputSlots(unit);
|
||||
if (inputSlotList) {
|
||||
const AQHREACT_INPUT_SLOT *inputSlot;
|
||||
int result=1;
|
||||
|
||||
inputSlot=AQHREACT_InputSlot_List_First(inputSlotList);
|
||||
while(inputSlot) {
|
||||
const char *slotName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
slotName=AQHREACT_InputSlot_GetName(inputSlot);
|
||||
dataObject=AQHREACT_InputSlot_GetCurrentDataObject(inputSlot);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
if (!(d>0.0))
|
||||
result=0;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", slotName?slotName:"<unnamed>");
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", slotName?slotName:"<unnamed>");
|
||||
}
|
||||
inputSlot=AQHREACT_InputSlot_List_Next(inputSlot);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputSlots(unit);
|
||||
AQHREACT_Unit_OutputDoubleData(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT, result?1.0:0.0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessXor(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
AQHREACT_INPUT_SLOT_LIST *inputSlotList;
|
||||
|
||||
inputSlotList=AQHREACT_Unit_GetInputSlots(unit);
|
||||
if (inputSlotList) {
|
||||
const AQHREACT_INPUT_SLOT *inputSlot;
|
||||
int result=0;
|
||||
|
||||
inputSlot=AQHREACT_InputSlot_List_First(inputSlotList);
|
||||
while(inputSlot) {
|
||||
const char *slotName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
slotName=AQHREACT_InputSlot_GetName(inputSlot);
|
||||
dataObject=AQHREACT_InputSlot_GetCurrentDataObject(inputSlot);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
if (d>0.0)
|
||||
result^=1; /*only xor when >0.0, otherwise no change (x XOR 0 is still x) */
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", slotName?slotName:"<unnamed>");
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", slotName?slotName:"<unnamed>");
|
||||
}
|
||||
inputSlot=AQHREACT_InputSlot_List_Next(inputSlot);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputSlots(unit);
|
||||
AQHREACT_Unit_OutputDoubleData(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT, result?1.0:0.0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user