#include <string>
#include <windows.h>
#include <fstream>
#include <map>
#include "wavebps.h"
#include "CMatlabBlock.h"
using namespace std;
/*
* WaveBPS plugin example.
* This will load .MAT files generated by PicoScope 6
* (c) 2008 Intrepid Control Systems, Inc.
* www.wavebps.com
*/
string ToString(const wstring & sStr)
{
string::size_type size = sStr.length() + 1;
char * temp = new char[size];
WideCharToMultiByte(CP_UTF8, 0, sStr.c_str(), size, temp, size, NULL, NULL);
string ret(temp);
delete [] temp;
return ret;
}
WAVEBPSEXPORT WaveBPSImportGetInfo(wchar_t* sDescription, wchar_t* sDialogFileSpec, int* piAPIVersion, int* piDLLVersion )
{
wsprintfW(sDescription,L"Matlab 4");
wsprintfW(sDialogFileSpec,L"Matlab 4 Files (.mat)|*.mat|");
*piAPIVersion = 1;
*piDLLVersion = 1;
return 1;
}
WAVEBPSEXPORT WaveBPSImportData(const wchar_t* sFilePath, SETBUFFSIZEPROC SetBufferSize, UPDATEPROGRESSBARPROC UpdateProgressBar,SETCHANNEL_BUFFERINFO SetChannelBufferInfo)
{
ifstream matfile( ToString( wstring(sFilePath) ).c_str(), ios_base::in | ios_base::binary );
if( matfile.fail() )
{
MessageBox(NULL,"Error reading in file","matimport",MB_OK);
return 0;
}
map< string, CMatlabBlock > matlabblocks;
UpdateProgressBar(1,0);
matfile.seekg(ios::end);
int filesize = matfile.tellg()*2; //double scale it so reading the file only goes up to 50%
matfile.seekg(ios::beg);
do
{
try
{
CMatlabBlock mblock( matfile );
matlabblocks.insert( pair<string,CMatlabBlock>( mblock.GetName(), mblock ) );
}
catch( CMatlabBlock::ReadFileError& rfe )
{
MessageBox(NULL,"Error reading in file","matimport",MB_OK);
matfile.close();
return 0;
}
catch( CMatlabBlock::GotEOF& eof )
{
continue;
}
catch(...)
{
MessageBox(NULL,"Error reading in file","matimport",MB_OK);
matfile.close();
return 0;
}
UpdateProgressBar(1, ((float)matfile.tellg()/filesize)*100);
} while( !matfile.eof() );
matfile.close();
//find the "A" channel
map< string, CMatlabBlock >::iterator it = matlabblocks.find(string("A"));
if( it == matlabblocks.end() )
{
MessageBox(NULL,"No data channel","matimport",MB_OK);
return 0;
}
CMatlabBlock& datachannel = it->second;
//what kind of timing is in the file? an array that matches up to the data or an offset and interval?
CMatlabBlock* timechannel = NULL;
double timestart, timeinterval;
it = matlabblocks.find(string("T"));
if( it == matlabblocks.end() )
{
//not an array, it's offset type
it = matlabblocks.find(string("Tstart"));
if( it == matlabblocks.end() )
{
MessageBox(NULL,"No timing data!","matimport",MB_OK);
return 0;
}
switch( it->second.GetHeader().DataFormat )
{
case Double:
timestart = *(double*)it->second.GetData();
break;
case Single:
timestart = *(float*)it->second.GetData();
break;
case Integer:
timestart = *(int*)it->second.GetData();
break;
}
it = matlabblocks.find(string("Tinterval"));
if( it == matlabblocks.end() )
{
MessageBox(NULL,"No timing data!","matimport",MB_OK);
return 0;
}
switch( it->second.GetHeader().DataFormat )
{
case Double:
timeinterval = *(double*)it->second.GetData();
break;
case Single:
timeinterval = *(float*)it->second.GetData();
break;
case Integer:
timeinterval = *(int*)it->second.GetData();
break;
}
}
else
timechannel = &it->second;
SetChannelBufferInfo(1,0, 0.0, 0.0);
stWaveformPoint* pPoints = (stWaveformPoint*) SetBufferSize(0, datachannel.GetHeader().NumberOfValues, false );
for( int i = 0 ; i < datachannel.GetHeader().NumberOfValues ; i++ )
{
double time;
float volt;
switch( datachannel.GetHeader().DataFormat )
{
case Double:
volt = ((double*)datachannel.GetData())[i];
break;
case Single:
volt = ((float*)datachannel.GetData())[i];
break;
case Integer:
volt = ((int*)datachannel.GetData())[i];
break;
}
if( timechannel )
{
switch( timechannel->GetHeader().DataFormat )
{
case Double:
volt = ((double*)timechannel->GetData())[i];
break;
case Single:
volt = ((float*)timechannel->GetData())[i];
break;
case Integer:
volt = ((int*)timechannel->GetData())[i];
break;
}
}
else
{
time = timestart + (timeinterval*i);
}
pPoints[i].dMicroseconds = time * 1000000;
pPoints[i].dVoltage = volt;
UpdateProgressBar(1, (((float)i/datachannel.GetHeader().NumberOfValues*2)*100) + 50 );
}
UpdateProgressBar(0,100);
return 1;
}
|