GeneratorBurst.cpp

/**
 * GeneratorBurst.cpp - for LibTiePie 0.5+
 *
 * This example generates a 50 Hz sine waveform, 4 Vpp, 100 periods.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

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

#if defined(__linux) || defined(__unix)
  #include <cstdlib>
  #include <unistd.h>
#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 with burst support:
  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 burst support:
      if(!(gen->modes() & GM_BURST_COUNT))
      {
        delete gen;
        gen = 0;
      }
    }

    delete item;

    if(gen)
    {
      break;
    }
  }

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

      // Set frequency:
      gen->setFrequency(50); // 50 Hz

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

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

      // Set burst mode:
      gen->setMode(GM_BURST_COUNT);

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

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

      // Print Generator info:
      printDeviceInfo(gen);

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

      // Wait for burst to complete:
      while(gen->isBurstActive())
      {
#if defined(_WIN32) || defined(_WIN64)
        Sleep(10);
#elif defined(__linux) || defined(__unix)
        usleep(10000);
#endif
      }

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

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

  return status;
}