Generator.cpp

/**
 * Generator.cpp
 *
 * This example generates a 100 kHz triangle waveform, 4 Vpp.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

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

#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:
  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();
  }

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

      // 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);

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

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

  return status;
}