/**
* OscilloscopeBlock.cpp
*
* This example performs a block mode measurement and writes the data to OscilloscopeBlock.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()))
{
// Check for block support:
if(!(scp->measureModes() & TIEPIE_HW_MM_BLOCK))
scp = nullptr;
}
}
if(scp)
{
uint16_t channelCount = 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(10000); // 10 kS
const uint64_t recordlength = scp->recordLength(); // Read actual record length.
// Set pre sample ratio:
scp->setPreSampleRatio(0); // 0 %
// For all channels:
for(const auto& channel : scp->channels)
{
// Enable channel to measure it:
channel->setEnabled(true);
// Set range:
channel->setRange(8); // 8 V
// Set coupling:
channel->setCoupling(TIEPIE_HW_CK_DCV); // DC Volt
}
// 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<float>> channelData(channelCount, std::vector<float>(recordlength));
std::vector<float*> channelDataPointers(channelCount);
for(size_t i = 0; i < channelCount; i++)
channelDataPointers[i] = channelData[i].data();
// Get the data from the scope:
uint64_t samplesRead = scp->getData(channelDataPointers.data(), channelCount, 0, recordlength);
// Open file with write/update permissions:
const std::string filename("OscilloscopeBlock.csv");
std::ofstream csv(filename.c_str(), std::ofstream::out);
if(csv.is_open())
{
// Write csv header:
csv << "Sample";
for(uint16_t ch = 0; ch < channelCount; ch++)
csv << ";Ch" << (ch + 1);
csv << '\n';
// Write the data to csv:
for(uint64_t i = 0; i < samplesRead; i++)
{
csv << i;
for(uint16_t ch = 0; ch < channelCount; ch++)
csv << ";" << channelData[ch][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 support!\n";
status = EXIT_FAILURE;
}
// Finalize library:
TiePie::Hardware::Library::fini();
return status;
}