GeneratorArbitrary.cpp

/**
 * GeneratorArbitrary.cpp - for LibTiePie 0.5+
 *
 * This example generates an arbitrary waveform.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

#include <iostream>
#include <math.h>
#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 info:
  printLibraryInfo();

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

  // Try to open a generator with arbitrary suppport:
  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()))
    {
      // Check for arbitrary support:
      if(!(gen->signalTypes() & ST_ARBITRARY))
      {
        delete gen;
        gen = 0;
      }
    }

    delete item;

    if(gen)
    {
      break;
    }
  }

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

      // Select frequency mode:
      gen->setFrequencyMode(FM_SAMPLEFREQUENCY);

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

      // Create signal array:
      float data[8192];
      for(unsigned int i = 0; i < 8192; i++)
      {
        data[i] = sin((float)(i / 100)) * (1 - ((float)(i / 8192)));
      }

      // Load the signal array into the generator:
      gen->setData(data , 8192);

      // Print generator info:
      printDeviceInfo(gen);

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

      // Wait for keystroke:
      cout << "Press Enter to stop signal generation...";
      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 with arbitrary support!" << endl;
    status = EXIT_FAILURE;
  }

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

  return status;
}