OscilloscopeCombineHS3HS4.cpp

/**
 * OscilloscopeCombineHS3HS4.cpp
 *
 * 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 "PrintInfo.hpp"
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <vector>

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

int main()
{
  int status = EXIT_SUCCESS;

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

  // Print library information:
  printLibraryInfo();

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

  // Try to open all HS3/HS4(D) oscilloscopes:
  const std::array<tiepie_hw_productid, 3> allowedProductIDs{TIEPIE_HW_PRODUCTID_HS3, TIEPIE_HW_PRODUCTID_HS4, TIEPIE_HW_PRODUCTID_HS4D};

  std::vector<std::unique_ptr<TiePie::Hardware::Oscilloscope>> scps;

  for(uint32_t i = 0; i < TiePie::Hardware::DeviceList::count(); i++)
  {
    const auto item = TiePie::Hardware::DeviceList::getItemByIndex(i);
    if(std::any_of(allowedProductIDs.begin(), allowedProductIDs.end(), [&item](auto pid)
                   { return pid == item->productId(); }) &&
       item->canOpen(TIEPIE_HW_DEVICETYPE_OSCILLOSCOPE))
    {
      scps.push_back(item->openOscilloscope());
      std::cout << "Found:" << item->name() << ", s/n: " << item->serialNumber() << '\n';
    }
  }

  if(scps.size() > 1)
  {
    try
    {
      // Create and open combined instrument:
      std::unique_ptr<TiePie::Hardware::Oscilloscope> scp = TiePie::Hardware::DeviceList::createAndOpenCombinedOscilloscope(scps);

      // Remove HS3/HS4(D) objects, not required anymore:
      scps.clear();

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

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

      // Close combined oscilloscope:
      scp = nullptr;

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

  // Remove HS4(D) objects:
  scps.clear();

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

  return status;
}