#include "UsbIO.h"
#ifndef _CINT_
#include "cycfx2dev.hh"
#include "fx2-iolib.cc"
#include "cycfx2dev.cc"
#endif
#include <iostream>
using namespace std;
ClassImp(UsbIO)
UsbIO::UsbIO(char* name,char* title):PelIO(name,title)
{
mIsUsb = false;
}
UsbIO::~UsbIO()
{
for(int i=0;i<MAXOUTPUT;i++) closeOutput(i);
closeInput();
}
int UsbIO::openInput(char *file)
{
if(mInput) { delete mInput; mInput = NULL;}
TString tmp = file;
if(tmp.BeginsWith("/dev/usb"))
{
int s;
s = initfx2_();
if(s!=0) return kWarn;
mIsUsb = true;
return kOk;
}
else
{
if(isDebug()) cout <<"Opening file instead of Usb"<<endl;
return PelIO::openInput(file);
}
return kOk;
}
int UsbIO::closeInput()
{
if(!mIsUsb) return kWarn;
else
{
stop();
mIsUsb = false;
}
return kWarn;
}
int UsbIO::readBlock()
{
if(!mIsUsb)
{
PelIO::readBlock();
}
else
{
int arg = 0;
int ierr = 0;
int nbytes = 0;
int ndat = 256;
nbytes = get_fx2buff_(ndat,(unsigned char*)mBlock);
if(isDebug()) cout <<"ierr = "<<ierr<<" nbytes = "<<nbytes<<endl;
if(nbytes<2) return kEOF;
BLOCKSIZE = nbytes/sizeof(unsigned short int);
if(BLOCKSIZE<1) return kEOF;
if(mDebug)
{
for(int i=0;i<BLOCKSIZE/8;i++)
{
for (int j=0;j<8;j++) cout <<hex<<(unsigned int)mBlock[i*8+j]<<" ";
cout <<dec<<endl;
}
}
}
return decodeBlock();
}
int UsbIO::getNEventsLost()
{
if(!mIsUsb) return 0;
else
{
int arg = 0;
}
return 0;
}
int UsbIO::getNUsedBuffers()
{
if(!mIsUsb) return 0;
else
{
int arg = 0;
}
return 0;
}
int UsbIO::getNBuffers()
{
if(!mIsUsb) return 0;
else
{
int arg = 0;
}
return 0;
}
int UsbIO::loadEVO(char* file)
{
if(mIsUsb)
{
TString T = file;
if(!T.EndsWith(".ihx")) return kWarn;
TString F = "prg:";
F+=file;
resetfx2_();
int s = initfx2_();
if(s!=0) return kWarn;
prgfx2_((char*)F.Data());
runfx2_();
return kOk;
}
return kOk;
}
int UsbIO::start()
{
if(mIsUsb)
{
char *FX2CMD;
FX2CMD="sbulk:1,R";
int rv=cmd_proc_(1,FX2CMD);
if(isDebug()) cout <<"Usb has started\n";
}
else
{
if(isDebug()) cout <<"Usb Connection is not open \n";
return kWarn;
}
return kOk;
}
int UsbIO::setBufferSize(int size)
{
if(mIsUsb)
{
int arg=size;
int size1 = 0;
if(isDebug()) cout <<"Usb buffer size set to "<<size1<<". Requested value was = "<<size<<"\n";
return size1;
}
else
{
if(isDebug()) cout <<"Usb Connection is not open \n";
return 0;
}
return 0;
}
int UsbIO::stop()
{
if(mIsUsb)
{
char * FX2CMD;
FX2CMD="sbulk:1,S";
int rv=cmd_proc_(1,FX2CMD);
if(isDebug()) cout <<"Usb has stopped\n";
}
else
{
if(isDebug()) cout <<"Usb Connection is not open \n";
return kWarn;
}
return kOk;
}
int UsbIO::readScalers(int slot,int* value,int crate)
{
if(mIsUsb)
{
if(isDebug()) cout <<"Scalers are not implemented in USB \n";
for(int i=0;i<12;i++) value[i] = 0;
return kWarn;
}
else
{
if(isDebug()) cout <<"Usb Connection is not open \n";
for(int i=0;i<12;i++) value[i] = 0;
return kWarn;
}
return kOk;
}
int UsbIO::clearScalers(int slot,int crate)
{
if(mIsUsb)
{
if(isDebug()) cout <<"Scalers are not implemented in USB \n";
return kWarn;
}
else
{
if(isDebug()) cout <<"Usb Connection is not open \n";
return kWarn;
}
return kOk;
}