/**
* OscilloscopeStream.cpp - for LibTiePie 0.5+
*
* This example performs a stream mode measurement and writes the data to OscilloscopeStream.csv.
*
* Find more information on http://www.tiepie.com/LibTiePie .
*/
#include <iostream>
#include <fstream>
#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 with stream measurement 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 stream measurement support:
if(!(scp->measureModes() & MM_STREAM))
{
delete scp;
scp = 0;
}
}
delete item;
if(scp)
{
break;
}
}
if(scp)
{
float** channelData = 0;
uint16_t channelCount = 0;
try
{
// Get the number of channels:
channelCount = scp->channels.count();
// Set measure mode:
scp->setMeasureMode(MM_STREAM);
// Set sample frequency:
scp->setSampleFrequency(1e3); // 1 kHz
// Set record length:
scp->setRecordLength(1000); // 1 kS
const uint64_t recordlength = scp->recordLength(); // Read actual record length
// For all channels:
for(uint16_t ch = 0; ch < channelCount; ch++)
{
OscilloscopeChannel& channel = scp->channels[ch];
// Enable channel to measure it:
channel.setEnabled(true);
// Set range:
channel.setRange(8); // 8 V
// Set coupling:
channel.setCoupling(CK_DCV); // DC Volt
}
// Print oscilloscope info:
printDeviceInfo(scp);
// Create data buffers:
channelData = new float*[channelCount];
for(uint16_t ch = 0; ch < channelCount; ch++)
{
channelData[ch] = new float[recordlength];
}
// Open file with write/update permissions:
const std::string filename("OscilloscopeStream.csv");
ofstream csv(filename.c_str() , std::ofstream::out);
if(csv.is_open())
{
// Start measurement:
scp->start();
// Write csv header:
csv << "Sample";
for(uint16_t ch = 0; ch < channelCount; ch++)
{
csv << ";Ch" << (ch + 1);
}
csv << endl;
uint64_t currentSample = 0;
for(uint8_t chunk = 0; chunk < 10; chunk++) // Measure 10 chunks
{
// Print a message, to inform the user that we still do something:
cout << "Data chunk " << (chunk + 1) << endl;
// Wait for measurement to complete:
while(!(scp->isDataReady() || scp->isDataOverflow()))
{
// 10 ms delay, to save CPU time:
#if defined(_WIN32) || defined(_WIN64)
Sleep(10);
#elif defined(__linux) || defined(__unix)
usleep(10000);
#endif
}
// Throw error on data overflow:
if(scp->isDataOverflow())
{
throw std::runtime_error("Data overflow!");
}
// Get data:
uint64_t samplesRead = scp->getData(channelData , channelCount , 0 , recordlength);
// Write the data to csv:
for(uint64_t i = 0; i < samplesRead; i++)
{
csv << currentSample + i;
for(uint16_t ch = 0; ch < channelCount; ch++)
{
csv << ";" << (float)channelData[ch][i];
}
csv << endl;
}
currentSample += samplesRead;
}
cout << "Data written to: " << filename << endl;
// Close file:
csv.close();
// Stop measurement:
scp->stop();
}
else
{
cerr << "Couldn't open file: " << filename;
status = EXIT_FAILURE;
}
}
catch(const exception& e)
{
cerr << "Exception: " << e.what() << endl;
status = EXIT_FAILURE;
}
// Delete data buffer:
if(channelData)
for(uint16_t ch = 0; ch < channelCount; ch++)
delete [] channelData[ch];
delete [] channelData;
// Close oscilloscope:
delete scp;
}
else
{
cerr << "No oscilloscope available with stream measurement support!" << endl;
status = EXIT_FAILURE;
}
// Exit library:
Library::exit();
return status;
}