GeneratorTriggeredBurst.cpp

/**
 * GeneratorTriggeredBurst.cpp
 *
 * This example generates a 100 kHz square waveform, 25% duty cycle, 0..5 V, 20 periods, this waveform is triggered by the external trigger (EXT 1).
 * Connect EXT 1 to GND to trigger the burst.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

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

#if defined(__linux) || defined(__unix)
#  include <cstdlib>
#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 triggered 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 triggered burst support:
      if(!((gen->modes() & TIEPIE_HW_GM_BURST_COUNT) && (gen->triggerInputs.count() > 0)))
        gen = nullptr;
    }
  }

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

      // Set frequency:
      gen->setFrequency(100e3); // 100 kHz

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

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

      // Set symmetry (duty cycle):
      gen->setSymmetry(0.25); // 25 %

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

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

      // Locate trigger input:
      const auto& triggerInput = [&gen]() -> const std::unique_ptr<TiePie::Hardware::TriggerInput>&
      {
        for(auto triggerInputId : {TIEPIE_HW_TIID_EXT1, TIEPIE_HW_TIID_EXT2, TIEPIE_HW_TIID_EXT3}) // EXT1, EXT2 or EXT3
          if(const auto& trInput = gen->triggerInputs.getById(triggerInputId))
            return trInput;

        throw std::runtime_error("Unknown trigger input!");
      }();

      // Enable trigger input:
      triggerInput->setEnabled(true);

      // Set trigger input kind:
      triggerInput->setKind(TIEPIE_HW_TK_FALLINGEDGE);

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

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

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

  return status;
}