/**
* GeneratorArbitrary.cpp
*
* This example generates an arbitrary waveform.
*
* Find more information on http://www.tiepie.com/LibTiePie .
*/
#include "PrintInfo.hpp"
#include <array>
#include <iostream>
#include <math.h>
#if defined(__linux) || defined(__unix)
# include <cstdlib>
#endif
int main()
{
int status = EXIT_SUCCESS;
// Initialize library:
TiePie::Hardware::Library::init();
// Print library info:
printLibraryInfo();
// Enable network search:
TiePie::Hardware::Network::setAutoDetectEnabled(true);
// Update device list:
TiePie::Hardware::DeviceList::update();
// Try to open a generator with arbitrary suppport:
std::unique_ptr<TiePie::Hardware::Generator> gen;
for(uint32_t index = 0; !gen && index < TiePie::Hardware::DeviceList::count(); index++)
{
const auto item = TiePie::Hardware::DeviceList::getItemByIndex(index);
if(item->canOpen(TIEPIE_HW_DEVICETYPE_GENERATOR) && (gen = item->openGenerator()))
{
// Check for arbitrary support:
if(!(gen->signalTypes() & TIEPIE_HW_ST_ARBITRARY))
gen = nullptr;
}
}
if(gen)
{
try
{
// Set signal type:
gen->setSignalType(TIEPIE_HW_ST_ARBITRARY);
// Select frequency mode:
gen->setFrequencyMode(TIEPIE_HW_FM_SAMPLERATE);
// Set frequency:
gen->setFrequency(100e3); // 100 kHz
// Set amplitude:
gen->setAmplitude(2); // 2 V
// Set offset:
gen->setOffset(0); // 0 V
// Enable output:
gen->setOutputEnable(true);
// Create signal array:
std::array<float, 8192> data;
for(size_t i = 0; i < data.size(); i++)
data[i] = sin((float)(i / 100)) * (1 - ((float)(i / data.size())));
// Load the signal array into the generator:
gen->setData(data.data(), data.size());
// Print generator info:
printDeviceInfo(*gen);
// Start signal generation:
gen->start();
// Wait for keystroke:
std::cout << "Press Enter to stop signal generation...\n";
while(std::cin.get() != '\n')
;
// Stop generator:
gen->stop();
// Disable output:
gen->setOutputEnable(false);
}
catch(const std::exception& e)
{
std::cerr << "Exception: " << e.what() << '\n';
status = EXIT_FAILURE;
}
// Close generator:
gen = nullptr;
}
else
{
std::cerr << "No generator available with arbitrary support!\n";
status = EXIT_FAILURE;
}
// Finalize library:
TiePie::Hardware::Library::fini();
return status;
}