OscilloscopeConnectionTest.cpp

/**
 * OscilloscopeConnectionTest.cpp - for LibTiePie 0.5+
 *
 * This example performs a connection test.
 *
 * 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 an oscilloscope with connection test support:
  Oscilloscope* scp = 0;

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

    if(item->canOpen(DEVICETYPE_OSCILLOSCOPE) && (scp = item->openOscilloscope()))
    {
      // Check for connection test support:
      if(!scp->hasConnectionTest())
      {
        delete scp;
        scp = 0;
      }
    }

    delete item;

    if(scp)
    {
      break;
    }
  }

  if(scp)
  {
    LibTiePieTriState_t* data = 0;

    try
    {
      // Get the number of channels:
      const uint16_t channelCount = scp->channels.count();

      // Enable all channels that support connection testing:
      for(uint16_t ch = 0; ch < channelCount; ch++)
      {
        scp->channels[ch].setEnabled(scp->channels[ch].hasConnectionTest());
      }

      // Start connection test on current active channels:
      scp->startConnectionTest();

      // Wait for connection test to complete:
      while(!(scp->isConnectionTestCompleted()))
      {
        // 10 ms delay, to save CPU time:
#if defined(_WIN32) || defined(_WIN64)
        Sleep(10);
#elif defined(__linux) || defined(__unix)
        usleep(10000);
#endif
      }

      // Create data buffer:
      data = new LibTiePieTriState_t[channelCount];

      // Get data:
      scp->connectionTestData(data , channelCount);

      // Print results:
      cout << endl << "Connection test result: " << endl;
      for(uint16_t ch = 0; ch < channelCount; ch++)
      {
        cout << "Ch" << (unsigned int)(ch + 1) << " = ";

        switch(data[ch])
        {
          case LIBTIEPIE_TRISTATE_UNDEFINED:
            cout << "undefined" << endl;
            break;

          case LIBTIEPIE_TRISTATE_FALSE:
            cout << "false" << endl;
            break;

          case LIBTIEPIE_TRISTATE_TRUE:
            cout << "true" << endl;
            break;

          default:
            cout << "unknown state" << endl;
        }
      }
    }
    catch(const exception& e)
    {
      cerr << "Exception: " << e.what() << endl;
      status = EXIT_FAILURE;
    }

    // Delete data buffers:
    delete [] data;

    // Close oscilloscope:
    delete scp;
  }
  else
  {
    cerr << "No oscilloscope available with connection test support!" << endl;
    status = EXIT_FAILURE;
  }

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

  return status;
}