Generator.cpp

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

#include <iostream>
#include <libtiepie++.h>
#include <PrintInfo.h>

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

using namespace std;
using namespace LibTiePie;

int main()
{
  int status = EXIT_SUCCESS;

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

  // Print library information:
  printLibraryInfo();

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

  // Try to open a generator:
  Generator* gen = 0;

  for(uint32_t index = 0; index < DeviceList::count(); index++)
  {
    DeviceListItem* item = DeviceList::getItemByIndex(index);

    if(item->canOpen(DEVICETYPE_GENERATOR))
    {
      gen = item->openGenerator();
    }

    delete item;

    if(gen)
    {
      break;
    }
  }

  if(gen)
  {
    try
    {
      // Set signal type:
      gen->setSignalType(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->setOutputOn(true);

      // Print generator info
      printDeviceInfo(gen);

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

      // Wait for keystroke:
      cout << "Press Enter to stop signal generation..." << endl;
      while(cin.get() != '\n');

      // Stop generator:
      gen->stop();

      // Disable output:
      gen->setOutputOn(false);
    }
    catch(const exception& e)
    {
      cerr << "Exception: " << e.what() << endl;
      status = EXIT_FAILURE;
    }

    // Close generator:
    delete gen;
  }
  else
  {
    cerr << "No generator available!" << endl;
    status = EXIT_FAILURE;
  }

  // Exit library:
  Library::exit();

  return status;
}