OscilloscopeCombineHS3HS4.cpp

/**
 * OscilloscopeCombineHS3HS4.cpp - for LibTiePie 0.6+
 *
 * This example demonstrates how to create and open a combined instrument of all found Handyscope HS3, Handyscope HS4 and/or Handyscope HS4 DIFF's.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#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 all HS3/HS4(D) oscilloscopes:
  const uint32_t allowedProductIDs[] = { PID_HS3 , PID_HS4 , PID_HS4D };
  const uint32_t* allowedProductIDsEnd = allowedProductIDs + (sizeof(allowedProductIDs) / sizeof(*allowedProductIDs)); // Pointer to end of allowedProductIDs.

  vector<Oscilloscope*> scps;

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

    if(find(allowedProductIDs , allowedProductIDsEnd , item->productId()) != allowedProductIDsEnd && item->canOpen(DEVICETYPE_OSCILLOSCOPE))
    {
      scps.push_back(item->openOscilloscope());
      cout << "Found:" << item->name() << ", s/n: " << item->serialNumber() << endl;
    }

    delete item;
  }

  if(scps.size() > 1)
  {
    try
    {
      // Create and open combined instrument:
      Oscilloscope* scp = (Oscilloscope*) DeviceList::createAndOpenCombinedDevice((Device**) scps.data() , scps.size());

      // Remove HS3/HS4(D) objects, not required anymore:
      for(vector<Oscilloscope*>::iterator it = scps.begin(); it != scps.end(); it++)
      {
        delete *it;
      }
      scps.clear();

      // Print combined oscilloscope info:
      printDeviceInfo(scp);

      // Get serial number, required for removing:
      uint32_t serialNumber = scp->serialNumber();

      // Close combined oscilloscope:
      delete scp;

      // Remove combined oscilloscope from the device list:
      DeviceList::removeDevice(serialNumber);
    }
    catch(const exception& e)
    {
      cerr << "Exception: " << e.what() << endl;
      status = EXIT_FAILURE;
    }
  }
  else
  {
    cerr << "Not enough HS3/HS4(D)\'s found, at least two required!" << endl;
    status = EXIT_FAILURE;
  }

  // Remove HS4(D) objects:
  for(vector<Oscilloscope*>::iterator it = scps.begin(); it != scps.end(); it++)
  {
    delete *it;
  }
  scps.clear();

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

  return status;
}