aqhome-react: more work on modules and networks.
- tested AND network and new suntime units. - add unit XML property "invert" (inverts output for logical units)
This commit is contained in:
@@ -21,8 +21,8 @@
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_INPUT 0
|
||||
#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_AUTOINPUT 100
|
||||
#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_INPUT 100
|
||||
#define AQHOMEREACT_UNIT_LOGICAL_INSLOT_AUTOINPUT 101
|
||||
|
||||
#define AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT 0
|
||||
|
||||
@@ -34,9 +34,11 @@
|
||||
*/
|
||||
|
||||
static AQHREACT_UNIT *_unitLogical_new(AQHOME_REACT *aqh);
|
||||
static int _outputResult(AQHREACT_UNIT *unit, AQHREACT_PORT *port, int result);
|
||||
static int _cbProcessOr(AQHREACT_UNIT *unit);
|
||||
static int _cbProcessAnd(AQHREACT_UNIT *unit);
|
||||
static int _cbProcessXor(AQHREACT_UNIT *unit);
|
||||
static int _cbProcessInvert(AQHREACT_UNIT *unit);
|
||||
|
||||
|
||||
|
||||
@@ -59,6 +61,20 @@ AQHREACT_UNIT *AqHomeReact_UnitOr_new(AQHOME_REACT *aqh)
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitNor_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitLogical_new(aqh);
|
||||
AQHREACT_Unit_SetTypeName(unit, "or");
|
||||
AQHREACT_Unit_SetDescription(unit, "Logical NOR inputs");
|
||||
AQHREACT_Unit_AddFlags(unit, AQHREACT_UNIT_FLAGS_INVERT);
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessOr);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitAnd_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
@@ -72,6 +88,20 @@ AQHREACT_UNIT *AqHomeReact_UnitAnd_new(AQHOME_REACT *aqh)
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitNand_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitLogical_new(aqh);
|
||||
AQHREACT_Unit_SetTypeName(unit, "and");
|
||||
AQHREACT_Unit_SetDescription(unit, "Logical NAND inputs");
|
||||
AQHREACT_Unit_AddFlags(unit, AQHREACT_UNIT_FLAGS_INVERT);
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessAnd);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitXor_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
@@ -85,6 +115,19 @@ AQHREACT_UNIT *AqHomeReact_UnitXor_new(AQHOME_REACT *aqh)
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitInvert_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitLogical_new(aqh);
|
||||
AQHREACT_Unit_SetTypeName(unit, "invert");
|
||||
AQHREACT_Unit_SetDescription(unit, "Logical invert input");
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessInvert);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *_unitLogical_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
@@ -92,9 +135,8 @@ AQHREACT_UNIT *_unitLogical_new(AQHOME_REACT *aqh)
|
||||
AQHREACT_PORT *port;
|
||||
|
||||
unit=AQHREACT_Unit_new(aqh);
|
||||
AQHREACT_Unit_SetTypeName(unit, "or");
|
||||
AQHREACT_Unit_SetDescription(unit, "Logical OR inputs");
|
||||
|
||||
AQHREACT_Unit_SetGpInt(unit, -1);
|
||||
AQHREACT_Unit_SetNextInputPortId(unit, AQHOMEREACT_UNIT_LOGICAL_INSLOT_AUTOINPUT); /* for auto-gen multi-slots */
|
||||
|
||||
port=AQHREACT_Port_new();
|
||||
@@ -115,6 +157,24 @@ AQHREACT_UNIT *_unitLogical_new(AQHOME_REACT *aqh)
|
||||
|
||||
|
||||
|
||||
int _outputResult(AQHREACT_UNIT *unit, AQHREACT_PORT *port, int result)
|
||||
{
|
||||
int currentState;
|
||||
|
||||
currentState=AQHREACT_Unit_GetGpInt(unit);
|
||||
if (AQHREACT_Unit_GetFlags(unit) & AQHREACT_UNIT_FLAGS_INVERT)
|
||||
result^=1;
|
||||
if (currentState!=result) {
|
||||
DBG_INFO(NULL, "%s: Changing result to %d", AQHREACT_Unit_GetId(unit), result);
|
||||
AQHREACT_Unit_OutputDoubleData(unit, port, result?1.0:0.0);
|
||||
AQHREACT_Unit_SetGpInt(unit, result);
|
||||
return 1; /* we changed something */
|
||||
}
|
||||
return 0; /* nothing changed */
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessOr(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
@@ -122,42 +182,39 @@ int _cbProcessOr(AQHREACT_UNIT *unit)
|
||||
|
||||
outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT);
|
||||
if (outputPort) {
|
||||
AQHREACT_PORT_LIST *portList;
|
||||
const AQHREACT_PORT *port;
|
||||
int result=0;
|
||||
int numHandledInputs=0;
|
||||
|
||||
portList=AQHREACT_Unit_GetInputPortList(unit);
|
||||
if (portList) {
|
||||
const AQHREACT_PORT *port;
|
||||
int result=0;
|
||||
port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
port=AQHREACT_Port_List_First(portList);
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
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", portName?portName:"<unnamed>");
|
||||
}
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
if (d>0.0)
|
||||
result|=1;
|
||||
numHandledInputs++;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", portName?portName:"<unnamed>");
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
AQHREACT_Unit_OutputDoubleData(unit, outputPort, result?1.0:0.0);
|
||||
return 1;
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
if (numHandledInputs)
|
||||
return _outputResult(unit, outputPort, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,42 +230,41 @@ int _cbProcessAnd(AQHREACT_UNIT *unit)
|
||||
|
||||
outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT);
|
||||
if (outputPort) {
|
||||
AQHREACT_PORT_LIST *portList;
|
||||
AQHREACT_PORT *port;
|
||||
int result=1;
|
||||
int numHandledInputs=0;
|
||||
|
||||
portList=AQHREACT_Unit_GetInputPortList(unit);
|
||||
if (portList) {
|
||||
AQHREACT_PORT *port;
|
||||
int result=1;
|
||||
port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
port=AQHREACT_Port_List_First(portList);
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
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", portName?portName:"<unnamed>");
|
||||
}
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
if (!(d>0.0))
|
||||
result=0;
|
||||
numHandledInputs++;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", portName?portName:"<unnamed>");
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
AQHREACT_Unit_OutputDoubleData(unit, port, result?1.0:0.0);
|
||||
return 1;
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" (%d) has no current data, assuming 0",
|
||||
portName?portName:"<unnamed>", AQHREACT_Port_GetIdForUnit(port));
|
||||
result=0;
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
if (numHandledInputs)
|
||||
return _outputResult(unit, outputPort, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,41 +280,81 @@ int _cbProcessXor(AQHREACT_UNIT *unit)
|
||||
|
||||
outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT);
|
||||
if (outputPort) {
|
||||
AQHREACT_PORT_LIST *portList;
|
||||
AQHREACT_PORT *port;
|
||||
int result=0;
|
||||
int numHandledInputs=0;
|
||||
|
||||
portList=AQHREACT_Unit_GetInputPortList(unit);
|
||||
if (portList) {
|
||||
AQHREACT_PORT *port;
|
||||
int result=0;
|
||||
port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
port=AQHREACT_Port_List_First(portList);
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
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", portName?portName:"<unnamed>");
|
||||
}
|
||||
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) */
|
||||
numHandledInputs++;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", portName?portName:"<unnamed>");
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
AQHREACT_Unit_OutputDoubleData(unit, port, result?1.0:0.0);
|
||||
return 1;
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
if (numHandledInputs)
|
||||
return _outputResult(unit, outputPort, result);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessInvert(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
AQHREACT_PORT *outputPort;
|
||||
|
||||
outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_LOGICAL_OUTSLOT_RESULT);
|
||||
if (outputPort) {
|
||||
AQHREACT_PORT *port;
|
||||
int result=1;
|
||||
|
||||
port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
|
||||
if(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
if (d>0.0)
|
||||
result=0; /* invert */
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
return _outputResult(unit, outputPort, result);
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has not current data, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user