GeneratorGatedBurst.cpp

/**
 * GeneratorGatedBurst.cpp
 *
 * This example generates a 10 kHz square waveform, 10 Vpp when the external trigger input is active.
 *
 * 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 gated 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 gated burst support:
      if(!((gen->modes() & TIEPIE_HW_GM_GATED_PERIODS) && (gen->triggerInputs.count() > 0)))
        gen = nullptr;
    }
  }

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

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

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

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

      // Set mode:
      gen->setMode(TIEPIE_HW_GM_GATED_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);

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

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

  return status;
}