GeneratorBurst.cpp

/**
 * GeneratorBurst.cpp
 *
 * This example generates a 50 Hz sine waveform, 4 Vpp, 100 periods.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

#include "PrintInfo.hpp"
#include <iostream>
#include <thread>

#if defined(__linux) || defined(__unix)
#  include <cstdlib>
#  include <unistd.h>
#endif

int main()
{
  int status = EXIT_SUCCESS;

  // Initialize library:
  TiePie::Hardware::Library::init();

  // Print library information:
  printLibraryInfo();

  // Enable network search:
  TiePie::Hardware::Network::setAutoDetectEnabled(true);

  // Update device list:
  TiePie::Hardware::DeviceList::update();

  // Try to open a generator with burst support:
  std::unique_ptr<TiePie::Hardware::Generator> gen;

  for(uint32_t i = 0; !gen && i < TiePie::Hardware::DeviceList::count(); i++)
  {
    const auto item = TiePie::Hardware::DeviceList::getItemByIndex(i);
    if(item->canOpen(TIEPIE_HW_DEVICETYPE_GENERATOR) && (gen = item->openGenerator()))
    {
      // Check for burst support:
      if(!(gen->modes() & TIEPIE_HW_GM_BURST_COUNT))
        gen = nullptr;
    }
  }

  if(gen)
  {
    try
    {
      // Set signal type:
      gen->setSignalType(TIEPIE_HW_ST_SINE);

      // Set frequency:
      gen->setFrequency(50); // 50 Hz

      // Set amplitude:
      gen->setAmplitude(2); // 2 V

      // Set offset:
      gen->setOffset(0); // 0 V

      // Set burst mode:
      gen->setMode(TIEPIE_HW_GM_BURST_COUNT);

      // Set burst count:
      gen->setBurstCount(100); // 100 periods

      // Enable output:
      gen->setOutputEnable(true);

      // Print Generator info:
      printDeviceInfo(*gen);

      // Start signal generation:
      gen->start();

      // Wait for burst to complete:
      while(gen->isBurstActive())
      {
        using namespace std::chrono_literals;
        std::this_thread::sleep_for(10ms);
      }

      // 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 burst support!\n";
    status = EXIT_FAILURE;
  }

  // Finalize library:
  TiePie::Hardware::Library::fini();

  return status;
}