OscilloscopeBlockSegmented.cpp

/**
 * OscilloscopeBlockSegmented.cpp
 *
 * This example performs a block mode measurement of 5 segments and writes the data to OscilloscopeBlockSegmented.csv.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

#include "PrintInfo.hpp"
#include <fstream>
#include <iostream>
#include <thread>

#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();

  // Enable network search:
  TiePie::Hardware::Network::setAutoDetectEnabled(true);

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

  // Try to open an oscilloscope with block measurement support:
  std::unique_ptr<TiePie::Hardware::Oscilloscope> scp;

  for(uint32_t i = 0; !scp && i < TiePie::Hardware::DeviceList::count(); i++)
  {
    const auto item = TiePie::Hardware::DeviceList::getItemByIndex(i);
    if(item->canOpen(TIEPIE_HW_DEVICETYPE_OSCILLOSCOPE) && (scp = item->openOscilloscope()) && (scp->segmentCountMax() > 1))
    {
      // Check for block support:
      if(!(scp->measureModes() & TIEPIE_HW_MM_BLOCK))
        scp = nullptr;
    }
  }

  if(scp)
  {
    uint16_t channelCount = 0;
    uint16_t segmentCount = 5;
    uint64_t samplesRead = 0;

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

      // Set measure mode:
      scp->setMeasureMode(TIEPIE_HW_MM_BLOCK);

      // Set sample rate:
      scp->setSampleRate(1e6); // 1 MHz

      // Set record length:
      scp->setRecordLength(1000);                        // 10 kS
      const uint64_t recordlength = scp->recordLength(); // Read actual record length.

      // Set pre sample ratio:
      scp->setPreSampleRatio(0); // 0 %

      // Set segment count:
      scp->setSegmentCount(segmentCount); // 5 segments
      segmentCount = scp->segmentCount(); // read actual segment count.

      // For all channels:
      for(const auto& channel : scp->channels)
      {
        // Disable channels:
        channel->setEnabled(false);

        // Set range:
        channel->setRange(8); // 8 V

        // Set coupling:
        channel->setCoupling(TIEPIE_HW_CK_DCV); // DC Volt
      }

      // Enable channel to measure it:
      scp->channels[0]->setEnabled(true);

      // Set trigger timeout:
      scp->trigger->setTimeout(100e-3); // 100 ms

      // Disable all channel trigger sources:
      for(const auto& channel : scp->channels)
        channel->trigger->setEnabled(false);

      // Setup channel trigger:
      const auto& chTr = scp->channels[0]->trigger; // Ch 1

      // Enable trigger source:
      chTr->setEnabled(true);

      // Kind:
      chTr->setKind(TIEPIE_HW_TK_RISINGEDGE); // Rising edge

      // Levels:
      *chTr->levels[0] = 0.5; // 50 %

      // Hysteresis:
      *chTr->hystereses[0] = 0.05; // 5 %

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

      // Start measurement:
      scp->start();

      // Wait for measurement to complete:
      while(!scp->isDataReady())
      {
        // 10 ms delay, to save CPU time:
        using namespace std::chrono_literals;
        std::this_thread::sleep_for(10ms);
      }

      // Create data buffer:
      std::vector<std::vector<std::vector<float>>> segmentData(segmentCount, std::vector<std::vector<float>>(channelCount, std::vector<float>(recordlength)));

      // Get all data from the scope:
      uint16_t seg = 0;
      while(scp->isDataReady())
      {
        std::vector<float*> channelDataPointers(channelCount);
        for(size_t i = 0; i < channelCount; i++)
          channelDataPointers[i] = segmentData[seg][i].data();

        samplesRead = scp->getData(channelDataPointers.data(), channelCount, 0, recordlength);
        seg++;
      }

      // Open file with write/update permissions:
      const std::string filename("OscilloscopeBlockSegmented.csv");
      std::ofstream csv(filename.c_str(), std::ofstream::out);

      if(csv.is_open())
      {
        // Write csv header:
        csv << "Sample";
        for(uint16_t seg = 0; seg < segmentCount; seg++)
          csv << ";Segment " << (seg + 1);
        csv << '\n';

        // Write the Ch1 data to csv:
        for(uint64_t i = 0; i < samplesRead; i++)
        {
          csv << i;
          for(uint16_t seg = 0; seg < segmentCount; seg++)
            csv << ";" << (float)segmentData[seg][0][i];
          csv << '\n';
        }

        std::cout << "Data written to: " << filename << '\n';

        // Close file:
        csv.close();
      }
      else
      {
        std::cerr << "Couldn't open file: " << filename << '\n';
        status = EXIT_FAILURE;
      }
    }
    catch(const std::exception& e)
    {
      std::cerr << "Exception: " << e.what() << '\n';
      status = EXIT_FAILURE;
    }

    // Close oscilloscope:
    scp = nullptr;
  }
  else
  {
    std::cerr << "No oscilloscope available with block measurement and segmented trigger support!\n";
    status = EXIT_FAILURE;
  }

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

  return status;
}