diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index ae59559..9d003f9 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -11,9 +11,11 @@ set(EXAMPLE_SOURCES read_with_formatted_timestamps.c print_data_descriptor_and_calculater_sample_rate.c create_and_read_sample_rate_buffers.c + simple_properties_display_handling.c + basic_conversions_example.c + advanced_conversions_example.c multi_reader_read_same_rates.c) - foreach(src ${EXAMPLE_SOURCES}) get_filename_component(exec_name ${src} NAME_WLE) add_executable(${exec_name} ${src}) diff --git a/examples/advanced_conversions_example.c b/examples/advanced_conversions_example.c new file mode 100644 index 0000000..7543747 --- /dev/null +++ b/examples/advanced_conversions_example.c @@ -0,0 +1,156 @@ +/* + * Example that shows how to create conversion functions for custom defined in openDAQ. + * Functions demonstrate conversions to and from openDAQ structure to its + * equivalent in native C. + */ +#include + +// Struct conversion +/* + * Conversion from and to daqStruct objects to C style structs. + * Warning: These types of conversions require prior knowledge of + * the struct structure and its definition in C. For conversion + * from C to openDAQ a pointer to the TypeManager is needed because + * of the way openDAQ structs are implemented. + */ +struct daqExample_Coordinates daqExample_fromDaqCoordinates(daqStruct* daq) +{ + struct daqExample_Coordinates native = { 0,0,0 }; + + daqBaseObject* value = NULL; + daqBaseObject* integerValue = NULL; + + daqString* fieldName = exdaq_toDaqString("x"); + daqStruct_get(daq, fieldName, &value); + daqQueryInterface(value, DAQ_INTEGER_INTF_ID, &integerValue); + native.x = exdaq_fromDaqInteger((daqInteger*) integerValue); + daqReleaseRef(integerValue); + daqReleaseRef(value); + daqReleaseRef(fieldName); + + fieldName = exdaq_toDaqString("y"); + daqStruct_get(daq, fieldName, &value); + daqQueryInterface(value, DAQ_INTEGER_INTF_ID, &integerValue); + native.y = exdaq_fromDaqInteger((daqInteger*) integerValue); + daqReleaseRef(integerValue); + daqReleaseRef(value); + daqReleaseRef(fieldName); + + fieldName = exdaq_toDaqString("z"); + daqStruct_get(daq, fieldName, &value); + daqQueryInterface(value, DAQ_INTEGER_INTF_ID, &integerValue); + native.z = exdaq_fromDaqInteger((daqInteger*) integerValue); + daqReleaseRef(integerValue); + daqReleaseRef(value); + daqReleaseRef(fieldName); + + return native; +} + +daqStruct* daqExample_toDaqCoordinates(struct daqExample_Coordinates native, daqTypeManager* typeManager) +{ + daqStructBuilder* builder = NULL; + daqStructBuilder_createStructBuilder(&builder, exdaq_toDaqString("DAQ_Coordinates"), typeManager); + + daqString* fieldName = NULL; + daqInteger* fieldValue = NULL; + + fieldName = exdaq_toDaqString("x"); + fieldValue = exdaq_toDaqInteger(native.x); + daqStructBuilder_set(builder, fieldName, (daqBaseObject*) fieldValue); + daqReleaseRef(fieldName); + daqReleaseRef(fieldValue); + + fieldName = exdaq_toDaqString("y"); + fieldValue = exdaq_toDaqInteger(native.y); + daqStructBuilder_set(builder, fieldName, (daqBaseObject*) fieldValue); + daqReleaseRef(fieldName); + daqReleaseRef(fieldValue); + + fieldName = exdaq_toDaqString("z"); + fieldValue = exdaq_toDaqInteger(native.z); + daqStructBuilder_set(builder, fieldName, (daqBaseObject*) fieldValue); + daqReleaseRef(fieldName); + daqReleaseRef(fieldValue); + + daqStruct* daq = NULL; + daqStructBuilder_build(builder, &daq); + daqReleaseRef(builder); + + return daq; +} + +// Enumeration conversion +/* + * Conversion from and to openDAQ String (daqEnumeration) core type + * from C language (enum ComponentStutesTypeEnum). + * Warning: These types of conversions require prior knowledge of + * the enumeration structure and its definition in C. For conversion + * from C to openDAQ a pointer to the TypeManager is needed because + * of the way openDAQ enumeration is implemented. + */ +enum daqExample_ComponentStatusTypeEnum daqExample_fromDaqCompStatusTypeEnum(daqEnumeration* daq) +{ + int64_t intValue = -1; + daqEnumeration_getIntValue(daq, &intValue); + + return intValue; +} + +daqEnumeration* daqExample_toCompStatusTypeEnum(enum daqExample_ComponentStatusTypeEnum native, daqTypeManager* typeManager) +{ + daqEnumeration* daq = NULL; + daqInteger* valueInt = exdaq_toDaqInteger(native); + daqString* valueName = exdaq_toDaqString("ComponentStatusType"); + + daqEnumeration_createEnumerationWithIntValue(&daq, valueName, valueInt, typeManager); + + daqReleaseRef(valueName); + daqReleaseRef(valueInt); + return daq; +} + +int main() +{ + daqInstance* simulatorInstance = NULL; + setupSimulator(&simulatorInstance); + daqExmaple_addCustomTypes(simulatorInstance); + + daqInstance* instance = NULL; + daqDevice* simulator = NULL; + addSimulator(&simulator, &instance); + + daqContext* context = NULL; + daqComponent_getContext((daqComponent*)simulator, &context); + daqTypeManager* typeManager = NULL; + daqContext_getTypeManager(context, &typeManager); + + enum daqExample_ComponentStatusTypeEnum enumStatusType = daqExample_ComponentStatusType_Error; + daqEnumeration* daqEnum = daqExample_toCompStatusTypeEnum(enumStatusType, typeManager); + enum daqExample_ComponentStatusTypeEnum enumStatusType2 = daqExample_fromDaqCompStatusTypeEnum(daqEnum); + + if (enumStatusType == enumStatusType2) + printf("Structs are the same after transforming them to and from openDAQ.\n"); + else + printf("Structs are different!\n"); + + struct daqExample_Coordinates nativeCoordinates = {4, 4, 4}; + daqStruct* tempStruct = daqExample_toDaqCoordinates(nativeCoordinates, typeManager); + struct daqExample_Coordinates nativeCoordinates2 = daqExample_fromDaqCoordinates(tempStruct); + + if (nativeCoordinates.x == nativeCoordinates2.x && + nativeCoordinates.y == nativeCoordinates2.y && + nativeCoordinates.z == nativeCoordinates2.z) + printf("Enums are the same after transforming them to and from openDAQ.\n"); + else + printf("Enums are different!\n"); + + daqReleaseRef(typeManager); + daqReleaseRef(tempStruct); + + daqReleaseRef(simulator); + daqReleaseRef(instance); + daqReleaseRef(simulatorInstance); + + return 0; +} \ No newline at end of file diff --git a/examples/basic_conversions_example.c b/examples/basic_conversions_example.c new file mode 100644 index 0000000..2f09588 --- /dev/null +++ b/examples/basic_conversions_example.c @@ -0,0 +1,50 @@ +/* + * Example that demonstrates conversions between native C types + * and their openDAQ equvalents. + */ +#include + +int main() +{ + // Integer example + int64_t i = 10; + daqInteger* daqIn = exdaq_toDaqInteger(i); + int64_t i2 = daqExample_fromDaqInteger(daqIn); + printf("Integer is equal: %s\n", i == i2 ? "True": "False"); + daqReleaseRef(daqIn); + + // Float example + double j = 1.0; + daqFloatObject* daqFl = exdaq_toDaqFloat(j); + double j2 = exdaq_fromDaqFloat(daqFl); + printf("Float is equal: %s\n", j == j2 ? "True" : "False"); + daqReleaseRef(daqFl); + + // String example + char* k = "check"; + daqString* daqStr = exdaq_toDaqString(k); + char* k2 = exdaq_fromDaqString(daqStr); + printf("String is equal: %s\n", strcmp(k, k2) == 0 ? "True" : "False"); + daqReleaseRef(daqStr); + + // Bool example + uint8_t l = True; + daqBoolean* daqBl = exdaq_toDaqBoolean(l); + uint8_t l2 = exdaq_fromDaqBoolean(daqBl); + printf("Boolean is equal: %s\n", l == l2 ? "True" : "False"); + daqReleaseRef(daqBl); + + // Complex Number example + struct exdaq_ComplexNumber m = { 1,5 }; + daqComplexNumber* daqComplex = exdaq_toDaqComplex(&m); + struct exdaq_ComplexNumber m2 = exdaq_fromDaqComplex(daqComplex); + printf("Complex Number is equal: %s\n", ((m.imaginary == m2.imaginary) && (m.real == m2.real)) ? "True" : "False"); + daqReleaseRef(daqComplex); + + // Range example + struct daqExample_Range n = { 1,10 }; + daqRange* daqRng = exdaq_toDaqRange(n); + struct daqExample_Range n2 = exdaq_fromDaqRange(daqRng); + printf("Range is equal: %s\n", ((n.max == n2.max) && (n.min == n2.min)) ? "True" : "False"); + daqReleaseRef(daqRng); +} \ No newline at end of file diff --git a/examples/simple_properties_display_handling.c b/examples/simple_properties_display_handling.c new file mode 100644 index 0000000..cc0cdfe --- /dev/null +++ b/examples/simple_properties_display_handling.c @@ -0,0 +1,70 @@ +/* + * Example that show how to properly display all relevant attributes and + * metadata in properties. + */ +#include + +void printPropertyTree(daqPropertyObject* propertyObject) +{ + daqList* visibleProperties = NULL; + daqPropertyObject_getVisibleProperties(propertyObject, &visibleProperties); + + daqSizeT count = 0; + daqList_getCount(visibleProperties, &count); + + for (daqSizeT i = 0; i < count; i++) + { + daqBaseObject* listItem = NULL; + daqProperty* property = NULL; + + daqList_getItemAt(visibleProperties, i, &listItem); + daqQueryInterface(listItem, DAQ_PROPERTY_INTF_ID, &property); + daqReleaseRef(listItem); + + daqCoreType valueType = daqCtUndefined; + daqProperty_getValueType(property, &valueType); + + daqBaseObject* propertyValue; + daqProperty_getValue(property, &propertyValue); + + printPropertyMetadata(property); + + if (valueType == daqCtObject) + { + printf("---\n"); + daqPropertyObject* childPropertyObject = NULL; + daqQueryInterface(propertyValue, DAQ_PROPERTY_OBJECT_INTF_ID, &childPropertyObject); + printPropertyTree(childPropertyObject); + printf("---\n\n"); + daqReleaseRef(childPropertyObject); + } + else + { + printf("- Value: "); + printPropertyValueOrDefault(property, False); + printf("\n"); + } + + daqReleaseRef(propertyValue); + daqReleaseRef(property); + } + + daqReleaseRef(visibleProperties); +} + +int main() +{ + daqInstance* simulatorInstance = NULL; + setupSimulator(&simulatorInstance); + + daqInstance* instance = NULL; + daqDevice* simulator = NULL; + addSimulator(&simulator, &instance); + + printPropertyTree((daqPropertyObject*) simulator); + + daqReleaseRef(simulator); + daqReleaseRef(instance); + daqReleaseRef(simulatorInstance); + return 0; +} \ No newline at end of file diff --git a/examples/util_headers/daq_c_conversions.h b/examples/util_headers/daq_c_conversions.h new file mode 100644 index 0000000..cbaa40f --- /dev/null +++ b/examples/util_headers/daq_c_conversions.h @@ -0,0 +1,207 @@ +#include + +// Core struct type +struct exdaq_ComplexNumber +{ + double real; + double imaginary; +}; + +// Core struct type +struct exdaq_Range +{ + double min; + double max; +}; + +// Core struct type +struct exdaq_Ratio +{ + int64_t denominator; + int64_t numerator; +}; + +// Int conversion +/* + * The following two funtions represent the conversion between the openDAQ native + * daqInt type and its coresponding C integer type. + */ +daqInt exdaq_fromDaqInteger(daqInteger* daq); +daqInteger* exdaq_toDaqInteger(daqInt native); + +// String conversion +/* + * Conversion from and to openDAQ String (daqString) core type + * from C language (const char*). The function that converts to native C return a non-owning pointer. + * Precautions should be taken, when using the received pointer. + */ +char* exdaq_fromDaqString(daqString* daq); +daqString* exdaq_toDaqString(const char* native); + +// Bool conversion +/* + * Conversion from and to openDAQ Boolean (daqString) core type from C language (uint8_t). + */ +uint8_t exdaq_fromDaqBoolean(daqBoolean* daq); +daqBoolean* exdaq_toDaqBoolean(uint8_t native); + +// Float conversion +/* + *Conversion from and to openDAQ String (daqFloat) core type from C language (double). + */ +daqFloat exdaq_fromDaqFloat(daqFloatObject* daq); +daqFloatObject* exdaq_toDaqFloat(daqFloat native); + +// Complex number conversion +/* + * Conversion from and to openDAQ Complex (daqComplex) core type + * from C language (struct ComplexNumber). + */ +struct exdaq_ComplexNumber exdaq_fromDaqComplex(daqComplexNumber* daq); +daqComplexNumber* exdaq_toDaqComplex(struct exdaq_ComplexNumber* native); + +// Range conversion +/* + * Conversion from and to openDAQ Struct (daqRange) core type + * from C language (struct Range). + */ +struct exdaq_Range exdaq_fromDaqRange(daqRange* daq); +daqRange* exdaq_toDaqRange(struct exdaq_Range native); + +// Ratio conversion +/* + * Conversion from and to openDAQ Ratio Struct (daqRatio) + * from C language (struct Ratio). + */ +struct exdaq_Ratio exdaq_fromDaqRatio(daqRatio* daq); +daqRatio* exdaq_toDaqRatio(struct exdaq_Ratio native); + +daqInt exdaq_fromDaqInteger(daqInteger* daq) +{ + daqInt native = 0; + daqInteger_getValue(daq, &native); + return native; +} + +daqInteger* exdaq_toDaqInteger(daqInt native) +{ + daqInteger* daq = NULL; + daqInteger_createInteger(&daq, native); + return daq; +} + +char* exdaq_fromDaqString(daqString* daq) +{ + char* native = NULL; + daqString_getCharPtr(daq, &native); + return native; +} + +daqString* exdaq_toDaqString(const char* native) +{ + daqString* daq = NULL; + daqString_createString(&daq, native); + return daq; +} + +uint8_t exdaq_fromDaqBoolean(daqBoolean* daq) +{ + uint8_t native = 0; + daqBoolean_getValue(daq, &native); + return native; +} + +daqBoolean* exdaq_toDaqBoolean(uint8_t native) +{ + daqBoolean* daq = NULL; + daqBoolean_createBoolean(&daq, native); + return daq; +} + +daqFloat exdaq_fromDaqFloat(daqFloatObject* daq) +{ + daqFloat native = 0; + daqFloatObject_getValue(daq, &native); + return native; +} + +daqFloatObject* exdaq_toDaqFloat(daqFloat native) +{ + daqFloatObject* daq = 0; + daqFloatObject_createFloatObject(&daq, native); + return daq; +} + +struct exdaq_ComplexNumber exdaq_fromDaqComplex(daqComplexNumber* daq) +{ + double real; + double complex; + daqComplexNumber_getReal(daq, &real); + daqComplexNumber_getImaginary(daq, &complex); + struct exdaq_ComplexNumber native = { real, complex }; + return native; +} + +daqComplexNumber* exdaq_toDaqComplex(struct exdaq_ComplexNumber* native) +{ + daqComplexNumber* daq = NULL; + daqComplexNumber_createComplexNumber(&daq, native->real, native->imaginary); + return daq; +} + +struct exdaq_Range exdaq_fromDaqRange(daqRange* daq) +{ + daqNumber* number = NULL; + double min = 0; + double max = 0; + + daqRange_getLowValue(daq, &number); + daqNumber_getFloatValue(number, &min); + daqReleaseRef(number); + + daqRange_getHighValue(daq, &number); + daqNumber_getFloatValue(number, &max); + struct exdaq_Range native = { min, max }; + daqReleaseRef(number); + return native; +} + +daqRange* exdaq_toDaqRange(struct exdaq_Range native) +{ + daqRange* daq = NULL; + daqNumber* lowValue = NULL; + daqNumber* highValue = NULL; + + daqFloatObject* lowFloat = exdaq_toDaqFloat(native.min); + daqFloatObject* highFloat = exdaq_toDaqFloat(native.max); + + daqQueryInterface(lowFloat, DAQ_NUMBER_INTF_ID, &lowValue); + daqQueryInterface(highFloat, DAQ_NUMBER_INTF_ID, &highValue); + + daqReleaseRef(lowFloat); + daqReleaseRef(highFloat); + + // Create and populate the daqRange object + daqRange_createRange(&daq, lowValue, highValue); + + daqReleaseRef(lowValue); + daqReleaseRef(highValue); + return daq; +} + +struct exdaq_Ratio exdaq_fromDaqRatio(daqRatio* daq) +{ + int64_t denominator = 0; + int64_t numerator = 0; + daqRatio_getDenominator(daq, &denominator); + daqRatio_getNumerator(daq, &numerator); + struct exdaq_Ratio native = { denominator, numerator }; + return native; +} + +daqRatio* exdaq_toDaqRatio(struct exdaq_Ratio native) +{ + daqRatio* daq = NULL; + daqRatio_createRatio(&daq, native.numerator, native.denominator); + return daq; +} diff --git a/examples/util_headers/daq_example_utils.h b/examples/util_headers/daq_example_utils.h new file mode 100644 index 0000000..5f677fb --- /dev/null +++ b/examples/util_headers/daq_example_utils.h @@ -0,0 +1,118 @@ +/* + * Disclamer: The following functions are + */ +#include + + // Custom struct type +struct daqExample_Coordinates +{ + int64_t x; + int64_t y; + int64_t z; +}; + +// Standard enum +enum daqExample_ComponentStatusTypeEnum +{ + daqExample_ComponentStatusType_Ok = 0, + daqExample_ComponentStatusType_Warning, + daqExample_ComponentStatusType_Error +}; + +// Adds Coodinates struct and ComponentStatusTypeEnum to types in Type Manager +void daqExample_addCoordinateStructToTypeManager(daqContext* context) +{ + daqTypeManager* typeManager = NULL; + daqContext_getTypeManager(context, &typeManager); + + daqList* names = NULL; + daqList_createList(&names); + + daqString* name = exdaq_toDaqString("x"); + daqList_pushBack(names, (daqBaseObject*) name); + daqReleaseRef(name); + + name = exdaq_toDaqString("y"); + daqList_pushBack(names, (daqBaseObject*) name); + daqReleaseRef(name); + + name = exdaq_toDaqString("z"); + daqList_pushBack(names, (daqBaseObject*) name); + daqReleaseRef(name); + + daqSimpleType* simpleType = NULL; + daqSimpleType_createSimpleType(&simpleType, daqCtInt); + + daqList* types = NULL; + daqList_createList(&types); + daqList_pushBack(types, simpleType); + daqList_pushBack(types, simpleType); + daqList_pushBack(types, simpleType); + daqReleaseRef(simpleType); + + daqStructType* newType = NULL; + daqString* typeName = exdaq_toDaqString("DAQ_Coordinates"); + daqStructType_createStructTypeNoDefaults(&newType, typeName, names, types); + daqReleaseRef(typeName); + + daqTypeManager_addType(typeManager, (daqType*) newType); + daqReleaseRef(newType); + daqReleaseRef(names); + daqReleaseRef(types); + daqReleaseRef(typeManager); +} + +void daqExample_addCustomStructAndEnumProp(daqDevice* device) +{ + daqTypeManager* typeManager = NULL; + daqContext* context = NULL; + daqComponent_getContext((daqComponent*)device, &context); + daqContext_getTypeManager(context, &typeManager); + + daqStructBuilder* builder = NULL; + daqString* name = exdaq_toDaqString("DAQ_Coordinates"); + daqStructBuilder_createStructBuilder(&builder, name, typeManager); + daqReleaseRef(name); + + daqInteger* value = NULL; + daqString* fieldName = NULL; + + fieldName = exdaq_toDaqString("x"); + value = exdaq_toDaqInteger(2); + daqStructBuilder_set(builder, fieldName, (daqBaseObject*) value); + daqReleaseRef(fieldName); + daqReleaseRef(value); + + fieldName = exdaq_toDaqString("y"); + value = exdaq_toDaqInteger(3); + daqStructBuilder_set(builder, fieldName, (daqBaseObject*) value); + daqReleaseRef(fieldName); + daqReleaseRef(value); + + fieldName = exdaq_toDaqString("z"); + value = exdaq_toDaqInteger(4); + daqStructBuilder_set(builder, fieldName, (daqBaseObject*) value); + daqReleaseRef(fieldName); + daqReleaseRef(value); + + daqStruct* coordinatesStruct = NULL; + daqStructBuilder_build(builder, &coordinatesStruct); + + name = exdaq_toDaqString("DAQ_CurrentPosition"); + daqPropertyBuilder* structBuilder = NULL; + daqPropertyBuilder_createStructPropertyBuilder(&structBuilder, name, coordinatesStruct); + daqProperty* structProp = NULL; + daqPropertyBuilder_build(structBuilder, &structProp); + daqPropertyObject_addProperty((daqPropertyObject*)device, structProp); + + daqReleaseRef(structProp); + daqReleaseRef(structBuilder); + daqReleaseRef(builder); +} + +void daqExmaple_addCustomTypes(daqInstance* instance) +{ + daqContext* context = NULL; + daqComponent_getContext((daqComponent*)instance, &context); + daqExample_addCoordinateStructToTypeManager(context); +} \ No newline at end of file diff --git a/examples/util_headers/daq_property_utils.h b/examples/util_headers/daq_property_utils.h new file mode 100644 index 0000000..886cc2b --- /dev/null +++ b/examples/util_headers/daq_property_utils.h @@ -0,0 +1,757 @@ +#include + +enum exdaq_PropertyType +{ + daqExample_propertyType_selection, + daqExample_propertyType_sparseSelection, + daqExample_propertyType_integer, + daqExample_propertyType_floatObject, + daqExample_propertyType_string, + daqExample_propertyType_boolean, + daqExample_propertyType_ratio, + daqExample_propertyType_enumeration, + daqExample_propertyType_structObject, + daqExample_propertyType_function, + daqExample_propertyType_list, + daqExample_propertyType_dictionary, + daqExample_propertyType_object, + daqExample_porpertyType_complexNumber, + daqExample_propertyType_unknown +}; + +void printSimpleCoreTypeValue(daqBaseObject* selectedValueObj, daqCoreType suggestedValuesItemType); +char* daqCoreTypeToString(daqCoreType type); +enum exdaq_PropertyType daq_getPropType(daqProperty* property); +void printPropertyValueOrDefault(daqProperty* property, daqBool defaultValue); +void printPropertyMetadata(daqProperty* property); +void printDaqNumber(daqNumber* number, enum exdaq_PropertyType type); +void printDaqDict(daqBaseObject* value, daqCoreType keyType, daqCoreType itemType); +void printDaqList(daqBaseObject* value, daqCoreType itemType); +void printDaqRatio(struct exdaq_Ratio native); +void printSelectedListValue(daqProperty* property, daqBool defaultValue); +void printSelectedDictValue(daqProperty* property, daqBool defaultValue); +void printDaqPropertyName(daqProperty* property); +void printDaqPropertyDescription(daqProperty* property); +void printDaqPropertyVisible(daqProperty* property); +void printDaqPropertyReadOnly(daqProperty* property); +void printDaqPropertyValueType(daqProperty* property); +void printDaqPropertyDefaultValue(daqProperty* property); +void printDaqPropertyKeyType(daqProperty* property); +void printDaqPropertyItemType(daqProperty* property); +void printDaqPropertyMinMaxValue(daqProperty* property, enum exdaq_PropertyType propertyType); +void printDaqPropertySelectionValues(daqProperty* property); +void printDaqPropertySparseSelectionValues(daqProperty* property); +void printDaqPropertySuggestedValues(daqProperty* property); +void printDaqPropertyUnit(daqProperty* property); +void printDaqPropertyCallableInfo(daqProperty* property); + +void printSimpleCoreTypeValue(daqBaseObject* selectedValueObj, daqCoreType suggestedValuesItemType) +{ + switch (suggestedValuesItemType) + { + case daqCtInt: + { + daqInteger* value = NULL; + daqQueryInterface(selectedValueObj, DAQ_INTEGER_INTF_ID, &value); + printf("%lld\n", daqExample_fromDaqInteger(value)); + daqReleaseRef(value); + return; + } + case daqCtFloat: + { + daqFloatObject* value = NULL; + daqQueryInterface(selectedValueObj, DAQ_FLOAT_OBJECT_INTF_ID, &value); + printf("%f\n", exdaq_fromDaqFloat(value)); + daqReleaseRef(value); + return; + } + case daqCtBool: + { + daqBoolean* value = NULL; + daqQueryInterface(selectedValueObj, DAQ_BOOLEAN_INTF_ID, &value); + printf("%s\n", exdaq_fromDaqBoolean(value) == True ? "True" : "False"); + daqReleaseRef(value); + return; + } + case daqCtString: + { + daqString* value = NULL; + daqQueryInterface(selectedValueObj, DAQ_STRING_INTF_ID, &value); + printDaqFormattedString("%s\n", value); + daqReleaseRef(value); + return; + } + case daqCtRatio: + { + daqRatio* value = NULL; + daqQueryInterface(selectedValueObj, DAQ_RATIO_INTF_ID, &value); + printDaqRatio(exdaq_fromDaqRatio(value)); + daqReleaseRef(value); + return; + } + case daqCtEnumeration: + printf("Enumeration\n"); + break; + case daqCtStruct: + printf("Struct\n"); + break; + case daqCtFunc: + printf("Function\n"); + break; + case daqCtProc: + printf("Procedure\n"); + break; + case daqCtObject: + case daqCtList: + case daqCtDict: + default: + { + daqConstCharPtr value = NULL; + daqBaseObject_toString(selectedValueObj, &value); + printf("%s\n", value); + return; + } + } +} + +char* daqCoreTypeToString(daqCoreType type) +{ + switch(type) + { + case daqCtBool: + return "Bool"; + case daqCtInt: + return "Integer"; + case daqCtFloat: + return "Float"; + case daqCtString: + return "String"; + case daqCtList: + return "List"; + case daqCtDict: + return "Dictionary"; + case daqCtRatio: + return "Ratio"; + case daqCtProc: + return "Procedure"; + case daqCtObject: + return "Object"; + case daqCtBinaryData: + return "Binary data"; + case daqCtFunc: + return "Function"; + case daqCtComplexNumber: + return "Complex number"; + case daqCtStruct: + return "Struct"; + case daqCtEnumeration: + return "Enumeration"; + case daqCtUndefined: + return "Undefined"; + } +} + +enum exdaq_PropertyType daq_getPropType(daqProperty* property) +{ + daqCoreType type = daqCtUndefined; + daqProperty_getValueType(property, &type); + + daqBaseObject* temp = NULL; + + switch (type) + { + case daqCtInt: + { + daqProperty_getSelectionValues(property, &temp); + if (temp != NULL) + { + if (DAQ_SUPPORTS_INTERFACE(temp, DAQ_DICT_INTF_ID)) + { + daqReleaseRef(temp); + return daqExample_propertyType_sparseSelection; + } + else if (DAQ_SUPPORTS_INTERFACE(temp, DAQ_LIST_INTF_ID)) + { + daqReleaseRef(temp); + return daqExample_propertyType_selection; + } + daqReleaseRef(temp); + } + return daqExample_propertyType_integer; + } + + case daqCtString: + return daqExample_propertyType_string; + + case daqCtBool: + return daqExample_propertyType_boolean; + + case daqCtFloat: + return daqExample_propertyType_floatObject; + + case daqCtProc: + case daqCtFunc: + return daqExample_propertyType_function; + + case daqCtObject: + return daqExample_propertyType_object; + + case daqCtStruct: + return daqExample_propertyType_structObject; + + case daqCtRatio: + return daqExample_propertyType_ratio; + + case daqCtComplexNumber: + return daqExample_porpertyType_complexNumber; + + case daqCtList: + return daqExample_propertyType_list; + + case daqCtDict: + return daqExample_propertyType_dictionary; + + default: + return daqExample_propertyType_unknown; + } +} + +void printPropertyValueOrDefault(daqProperty* property, daqBool defaultValue) +{ + daqBaseObject* value = NULL; + + if (defaultValue) + daqProperty_getDefaultValue(property, &value); + else + daqProperty_getValue(property, &value); + + enum exdaq_PropertyType propType = daq_getPropType(property); + + switch (propType) + { + case daqExample_propertyType_integer: + { + printSimpleCoreTypeValue(value, daqCtInt); + break; + } + case daqExample_propertyType_string: + { + printSimpleCoreTypeValue(value, daqCtString); + break; + } + case daqExample_propertyType_boolean: + { + printSimpleCoreTypeValue(value, daqCtBool); + break; + } + case daqExample_propertyType_floatObject: + { + printSimpleCoreTypeValue(value, daqCtFloat); + break; + } + case daqExample_propertyType_ratio: + { + printSimpleCoreTypeValue(value, daqCtRatio); + break; + } + case daqExample_propertyType_function: + { + printf("Function\n"); + if (value != NULL) + daqReleaseRef(value); + return; + } + case daqExample_propertyType_selection: + { + printSelectedListValue(property, defaultValue); + break; + } + case daqExample_propertyType_sparseSelection: + { + printSelectedDictValue(property, defaultValue); + break; + } + case daqExample_propertyType_dictionary: + { + daqCoreType itemType = daqCtUndefined; + daqCoreType keyType = daqCtUndefined; + daqProperty_getItemType(property, &itemType); + daqProperty_getKeyType(property, &keyType); + printDaqDict(value, keyType, itemType); + break; + } + case daqExample_propertyType_list: + { + daqCoreType itemType = daqCtUndefined; + daqProperty_getItemType(property, &itemType); + printDaqList(value, itemType); + break; + } + case daqExample_propertyType_object: + printf("Object\n"); + if (value != NULL) + daqReleaseRef(value); + return; + default: + { + printf("Property Value\n"); + if (value != NULL) + daqReleaseRef(value); + return; + } + } + daqReleaseRef(value); +} + +void printPropertyMetadata(daqProperty* property) +{ + enum exdaq_PropertyType propertyType = daq_getPropType(property); + + // Name + /* + * Mandatory field for all types of properties. + * Never empty. + */ + printDaqPropertyName(property); + + // Description + /* + * Optional field for all types of properties. + * Can be empty. + */ + printDaqPropertyDescription(property); + + // Visible + /* + * Mandatory field for all types of properties. + * When calling getAllVisibleProperties will always be True. + * Never empty. + */ + printDaqPropertyVisible(property); + + // Read-only + /* + * Mandatory field for all types of properties. + * Never empty. + */ + printDaqPropertyReadOnly(property); + + // Value Type + /* + * Mandatory field for all types of properties. + * Never empty. + */ + printDaqPropertyValueType(property); + + // Default Value + /* + * Mandatory field for all aside from + * Function and Procedure Properties. + * Will never be empty, aside from + * Function/Procedure Property where it is empty. + */ + printDaqPropertyDefaultValue(property); + + // Key type + /* + * Mandatory field for a Dictionary property. + * Will never be empty when required. + */ + if (propertyType == daqExample_propertyType_dictionary) + printDaqPropertyKeyType(property); + + // Item type + /* + * Mandatory field for Dictionary and List property. + * Will never be empty when required. + */ + if (propertyType == daqExample_propertyType_dictionary || propertyType == daqExample_propertyType_list) + printDaqPropertyItemType(property); + + // Min/Max value + /* + * Optional field for Integer and Float Property. + * May be empty even in Integer or Float Property. + */ + if (propertyType == daqExample_propertyType_integer || propertyType == daqExample_propertyType_floatObject) + printDaqPropertyMinMaxValue(property, propertyType); + + // Selection values (list) + /* + * Mandatory field for Selection Property. + * Will never be empty when it is required. + */ + if (propertyType == daqExample_propertyType_selection) + printDaqPropertySelectionValues(property); + + // Selection values (dictionary) + /* + * Mandatory field for Sparse Selection Property. + * Will never be empty when it is required. + */ + if (propertyType == daqExample_propertyType_sparseSelection) + printDaqPropertySparseSelectionValues(property); + + // Suggested values + /* + * Optional field for Integer, Float and String Property. + * Can be empty even in Integer, Float and String Property. + */ + if (propertyType == daqExample_propertyType_integer || propertyType == daqExample_propertyType_string || propertyType == daqExample_propertyType_floatObject) + printDaqPropertySuggestedValues(property); + + // Unit + /* + * Optional field for Integer and Float Property. + * Can be even in Integer and Float Property. + */ + if (propertyType == daqExample_propertyType_integer || propertyType == daqExample_propertyType_floatObject) + printDaqPropertyUnit(property); + + // Callable info + /* + * Mandatoty field for Function and Procedure Property. + * Will never be empty when required. + * The return field within Callable Info will be defined + * only when the Property type is Function. + */ + if (propertyType == daqExample_propertyType_function) + printDaqPropertyCallableInfo(property); +} + +void printDaqNumber(daqNumber* number, enum exdaq_PropertyType type) +{ + if (type == daqExample_propertyType_integer) + { + int64_t value = 0; + daqNumber_getIntValue(number, &value); + printf("%lld\n", value); + } + else if(type == daqExample_propertyType_floatObject) + { + double value = 0.0; + daqNumber_getFloatValue(number, &value); + printf("%f\n", value); + } +} + +void printDaqDict(daqBaseObject* value, daqCoreType keyType, daqCoreType itemType) +{ + daqDict* valueDict = NULL; + daqQueryInterface(value, DAQ_DICT_INTF_ID, &valueDict); + + daqBaseObject* key = NULL; + daqBaseObject* val = NULL; + + daqSizeT count = 0; + daqDict_getCount(valueDict, &count); + + daqList* keys = NULL; + daqDict_getKeyList(valueDict, &keys); + + printf("- Dictionary:\n"); + + for(daqSizeT i = 0; i -1) + { + printf(" -- Id: '%lld'\n", id); + } + + daqString* value = NULL; + daqUnit_getName(unit, &value); + if (value != NULL) + { + printDaqFormattedString(" -- Unit name: '%s'\n", value); + daqReleaseRef(value); + } + + daqUnit_getQuantity(unit, &value); + if (value != NULL) + { + printDaqFormattedString(" -- Quantity: '%s'\n", value); + daqReleaseRef(value); + } + + daqUnit_getSymbol(unit, &value); + if (value != NULL) + { + printDaqFormattedString(" -- Symbol: '%s'\n", value); + daqReleaseRef(value); + } + daqReleaseRef(unit); +} + +void printDaqPropertyCallableInfo(daqProperty* property) +{ + daqCallableInfo* callableInfo = NULL; + daqProperty_getCallableInfo(property, &callableInfo); + daqList* arguments = NULL; + daqCallableInfo_getArguments(callableInfo, &arguments); + + printf("- Callable info:\n"); + + daqSizeT count = 0; + daqList_getCount(arguments, &count); + + daqBaseObject* temp = NULL; + daqArgumentInfo* value = NULL; + daqString* name = NULL; + for (daqSizeT i = 0; i< count; i++) + { + // Arguments are a list of Argument Info Objects + daqList_getItemAt(arguments, i, &temp); + daqQueryInterface(temp, DAQ_ARGUMENT_INFO_INTF_ID, &value); + daqReleaseRef(temp); + + daqArgumentInfo_getName(value, &name); + printDaqFormattedString(" -- Argument: %s\n", name); + daqReleaseRef(name); + + daqCoreType argumentType = daqCtUndefined; + daqArgumentInfo_getType(value, &argumentType); + printf(" -- Type: %s\n", daqCoreTypeToString(argumentType)); + + daqReleaseRef(value); + } + + daqReleaseRef(arguments); + + daqCoreType returnType = daqCtUndefined; + daqCallableInfo_getReturnType(callableInfo, &returnType); + if (returnType != daqCtUndefined) + printf(" -- Return type: %s\n", daqCoreTypeToString(returnType)); + + daqReleaseRef(callableInfo); +} diff --git a/examples/util_headers/daq_utils.h b/examples/util_headers/daq_utils.h index a25d10f..c88b039 100644 --- a/examples/util_headers/daq_utils.h +++ b/examples/util_headers/daq_utils.h @@ -58,6 +58,11 @@ static inline daqErrCode calculateSampleRate(daqSizeT* sampleRate, daqRatio* tic */ static inline int checkIsLinearRule(daqDataRule* dataRule); +/* + * Function checks if the daqString object contains an empty string ("") + */ +static inline uint8_t isDaqStringEmpty(daqString* string); + void daqSleepMs(int milliseconds) { #ifdef _WIN32 @@ -176,7 +181,14 @@ static inline daqErrCode setupSimulator(daqInstance** instance) daqInstanceBuilder_setGlobalLogLevel(instanceBuilder, daqLogLevelOff); - daqInstance_createInstanceFromBuilder(instance, instanceBuilder); + daqErrCode err = 0; + err = daqInstanceBuilder_build(instanceBuilder, instance); + + if (err != 0) + { + printf("Error occured when creating simulator device."); + return DAQ_FAILED(err); + } daqReleaseRef(modulePath); daqReleaseRef(instanceBuilder); @@ -389,4 +401,11 @@ static inline int checkIsLinearRule(daqDataRule* dataRule) daqDataRule_getType(dataRule, &dataRuleType); return dataRuleType == daqDataRuleTypeLinear; -} \ No newline at end of file +} + +static inline uint8_t isDaqStringEmpty(daqString* string) +{ + daqConstCharPtr stringConstChar; + daqString_getCharPtr(string, &stringConstChar); + return (strcmp(stringConstChar, "") == 0); +}