#include "demo_timer_trigger_function.h"
#include <easy/profiler.h>
ADTF_PLUGIN(
"Demo Time Trigger Plugin", cSimpleDataGenerator);
cSimpleDataGenerator::cSimpleDataGenerator():
m_oDistribution(0.0, 100.0)
{
m_pWriterSimple = CreateOutputPin("out", stream_type_plain<float>());
SetDescription("out", "Provides the generated data based on number_gen_type");
m_pWriterSimpleArray = CreateOutputPin("out_array", stream_type_plain<std::array<int64_t, 10>>());
SetDescription("out_array", "Provides the generated data array based on number_gen_type");
auto oSimpleEnumDefinition = enumeration<eSimpleEnum>("eSimpleEnum")
.Add("eEnum_value_zero", eSimpleEnum::eEnum_value_zero)
.Add("eEnum_value_one", eSimpleEnum::eEnum_value_one)
.Add("eEnum_value_two", eSimpleEnum::eEnum_value_two)
.Add("eEnum_value_three", eSimpleEnum::eEnum_value_three)
.Add("eEnum_value_four", eSimpleEnum::eEnum_value_four)
.Add("eEnum_value_five", eSimpleEnum::eEnum_value_five)
.Add("eEnum_value_six", eSimpleEnum::eEnum_value_six)
.Add("eEnum_value_seven", eSimpleEnum::eEnum_value_seven)
.Add("eEnum_value_eight", eSimpleEnum::eEnum_value_eight)
.Add("eEnum_value_nine", eSimpleEnum::eEnum_value_nine);
auto oSimpleStructDefinition = structure<tSimpleStruct>("tSimpleStruct")
.Add("ui8Val", &tSimpleStruct::ui8Val)
.Add("ui16Val", &tSimpleStruct::ui16Val)
.Add("ui32Val", &tSimpleStruct::ui32Val)
.Add("i32Val", &tSimpleStruct::i32Val)
.Add("i64Val", &tSimpleStruct::i64Val)
.Add("f64Val", &tSimpleStruct::f64Val)
.Add("f32Val", &tSimpleStruct::f32Val)
.Add("ei8EnumVal", &tSimpleStruct::ei8EnumVal, oSimpleEnumDefinition);
auto oHeaderStructDefinition = structure<tHeaderStruct>("tHeaderStruct")
.Add("ui32HeaderVal", &tHeaderStruct::ui32HeaderVal)
.Add("f64HeaderVal", &tHeaderStruct::f64HeaderVal);
auto oNestedDefinition = structure<tNestedStruct>("tNestedStruct")
.Add("sHeaderStruct", &tNestedStruct::sHeaderStruct, oHeaderStructDefinition)
.Add("sSimpleStruct", &tNestedStruct::sSimpleStruct, oSimpleStructDefinition);
m_pWriterNested = CreateOutputPin("nested_struct", oNestedDefinition);
SetDescription("nested_struct", "Provides the generated data based on number_gen_type");
auto oNestedArrayDefinition = structure<tNestedArrayStruct>("tNestedArrayStruct")
.Add("sHeaderStructArray", &tNestedArrayStruct::sHeaderStructArray, oHeaderStructDefinition)
.Add("sSimpleStructArray", &tNestedArrayStruct::sSimpleStructArray, oSimpleStructDefinition);
m_pWriterNestedArray = CreateOutputPin("nested_struct_array", oNestedArrayDefinition);
SetDescription("nested_struct_array", "Provides the generated data based on number_gen_type in a structure with arrays");
SetDescription("data_generator_function", "Runner to periodically trigger the function which generates the output data for all pins");
m_eGeneratorType.SetValueList({{eCounter, "Counter"},
{eRandom, "Random"}});
m_eGeneratorType.SetDescription("Chooses the generator used for calculating output data.");
RegisterPropertyVariable("data_generator_function/number_gen_type", m_eGeneratorType);
SetDescription("Use this filter to generate data whenever a defined Timer Runner triggers.");
SetHelpLink("$(ADTF_DIR)/doc/adtf_html/page_demo_time_triggered_filter.html");
}
tResult cSimpleDataGenerator::Init(tInitStage eStage)
{
if (eStage == StageNormal)
{
switch (m_eGeneratorType)
{
case eCounter:
{
m_fnGenerator = [&]
{
return static_cast<double>(++m_nCounter);
};
LOG_INFO(
"Current generation type is Counter");
break;
}
case eRandom:
{
m_fnGenerator = [&]
{
return m_oDistribution(m_oRandomGenerator);
};
LOG_INFO(
"Current generation type is Random");
break;
}
default:
{
RETURN_ERROR_DESC(ERR_INVALID_ARG,
"Invalid property value '%d' for 'number_gen_type', use Counter", *m_eGeneratorType);
}
}
}
}
tResult cSimpleDataGenerator::Process(tNanoSeconds tmTimeOfTrigger,
IRunner* )
{
double fOutValue = m_fnGenerator();
GenerateSimpleData(tmTimeOfTrigger, fOutValue);
GenerateSimpleArrayData(tmTimeOfTrigger, fOutValue);
GenerateNestedData(tmTimeOfTrigger, fOutValue);
GenerateNestedArrayData(tmTimeOfTrigger, fOutValue);
}
void cSimpleDataGenerator::GenerateSimpleData(tNanoSeconds tmTimeOfTrigger, double fOutValue)
{
EASY_FUNCTION();
output_sample_data<float> oOutputData(tmTimeOfTrigger);
oOutputData = static_cast<float>(fOutValue);
m_pWriterSimple->Write(oOutputData.Release());
}
void cSimpleDataGenerator::GenerateSimpleArrayData(tNanoSeconds tmTimeOfTrigger, double fOutValue)
{
EASY_FUNCTION();
output_sample_data<std::array<int64_t, 10>> oOutputData(tmTimeOfTrigger);
for (auto& oCurrentArrayValue : *oOutputData)
{
oCurrentArrayValue = static_cast<int64_t>(fOutValue);
}
m_pWriterSimpleArray->Write(oOutputData.Release());
}
void cSimpleDataGenerator::GenerateNestedData(tNanoSeconds tmTimeOfTrigger, double fOutValue)
{
EASY_FUNCTION();
output_sample_data<tNestedStruct> oOutputData(tmTimeOfTrigger);
uint8_t ui8Val = static_cast<uint8_t>(fOutValue);
oOutputData->sHeaderStruct.ui32HeaderVal = static_cast<uint32_t>(fOutValue);
oOutputData->sHeaderStruct.f64HeaderVal = fOutValue;
oOutputData->sSimpleStruct.ui8Val = ui8Val;
oOutputData->sSimpleStruct.ui16Val = static_cast<uint16_t>(fOutValue);
oOutputData->sSimpleStruct.ui32Val = static_cast<uint32_t>(fOutValue);
oOutputData->sSimpleStruct.i32Val = static_cast<int32_t>(fOutValue);
oOutputData->sSimpleStruct.i64Val = static_cast<int64_t>(fOutValue);
oOutputData->sSimpleStruct.f64Val = fOutValue;
oOutputData->sSimpleStruct.f32Val = static_cast<float>(fOutValue);
oOutputData->sSimpleStruct.ei8EnumVal = static_cast<eSimpleEnum>(ui8Val % 10);
m_pWriterNested->Write(oOutputData.Release());
}
void cSimpleDataGenerator::GenerateNestedArrayData(tNanoSeconds tmTimeOfTrigger, double fOutValue)
{
EASY_FUNCTION();
output_sample_data<tNestedArrayStruct> oOutputData(tmTimeOfTrigger);
for (size_t nCurrentPos = 0; nCurrentPos < 100; ++nCurrentPos)
{
oOutputData->sHeaderStructArray[nCurrentPos].ui32HeaderVal = static_cast<uint32_t>(fOutValue);
oOutputData->sHeaderStructArray[nCurrentPos].f64HeaderVal = fOutValue;
}
for (size_t nCurrentPos = 0; nCurrentPos < 2000; ++nCurrentPos)
{
uint8_t ui8Val = static_cast<uint8_t>(fOutValue);
oOutputData->sSimpleStructArray[nCurrentPos].ui8Val = ui8Val;
oOutputData->sSimpleStructArray[nCurrentPos].ui16Val = static_cast<uint16_t>(fOutValue);
oOutputData->sSimpleStructArray[nCurrentPos].ui32Val = static_cast<uint32_t>(fOutValue);
oOutputData->sSimpleStructArray[nCurrentPos].i32Val = static_cast<int32_t>(fOutValue);
oOutputData->sSimpleStructArray[nCurrentPos].i64Val = static_cast<int64_t>(fOutValue);
oOutputData->sSimpleStructArray[nCurrentPos].f64Val = fOutValue;
oOutputData->sSimpleStructArray[nCurrentPos].f32Val = static_cast<float>(fOutValue);
oOutputData->sSimpleStructArray[nCurrentPos].ei8EnumVal = static_cast<eSimpleEnum>(ui8Val % 10);
}
m_pWriterNestedArray->Write(oOutputData.Release());
}
#define ADTF_PLUGIN(__plugin_identifier,...)
The ADTF Plugin Macro will add the code of a adtf::ucom::ant::IPlugin implementation.
#define LOG_INFO(...)
Logs an info message.
#define RETURN_ERROR_DESC(_code,...)
Same as RETURN_ERROR(_error) using a printf like parameter list for detailed error description.
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
std::chrono::seconds seconds
Compatibility to C++11 std::chrono::seconds