ROOT logo
#include "UsbIO.h"

#ifndef _CINT_
#include "cycfx2dev.hh" 
#include "fx2-iolib.cc" 
#include "cycfx2dev.cc" 
#endif

#include <iostream>
using namespace std;

ClassImp(UsbIO)

/*!
Default contructor. Set object name and title
\param name is the object name
\param title is the object tile
*/
UsbIO::UsbIO(char* name,char* title):PelIO(name,title)
{
  mIsUsb = false;
}
/*!
*/
UsbIO::~UsbIO()
{
  for(int i=0;i<MAXOUTPUT;i++) closeOutput(i);
  closeInput();
}
/*!
Open input file from stream
\param file is the file name. If NULL Usb connection is open
*/
int UsbIO::openInput(char *file)
{
  if(mInput) { delete mInput; mInput = NULL;}
	
	TString tmp = file;
	
	if(tmp.BeginsWith("/dev/usb"))
	{
	   int s;
     //resetfx2_();
     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;
}
/*!
Close input file 
*/
int UsbIO::closeInput()
{
  if(!mIsUsb) return kWarn; 
  else
  {
    stop();
    mIsUsb = false;
  }
  return kWarn;
}
/*!
Read data block from Usb
*/
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 ioctl(cc_handle, CCGETLOST, (unsigned long) arg);
  }
	return 0;
}
/*!
*/
int UsbIO::getNUsedBuffers()
{  
  if(!mIsUsb) return 0;
  else
  {
    int arg = 0;
    //return ioctl(cc_handle, CCGETUSED, (unsigned long) arg);
  }
	return 0;
}
/*!
*/
int UsbIO::getNBuffers()
{  
  if(!mIsUsb) return 0;
  else
  {
    int arg = 0;
    //return ioctl(cc_handle, CCGETBUF, (unsigned long) arg);
  }
	return 0;
}
/*!
\param file is the .evo filename
*/
int UsbIO::loadEVO(char* file)
{
  if(mIsUsb)
  {  
	   TString T = file;
		 if(!T.EndsWith(".ihx")) return kWarn;
		 
		 //int s;
     //char *p="prg:fx2prog.ihx";
		 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";
		//cout <<FX2CMD<<endl;
    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;
}
/*!
\param slot is a slot in the Usb where the scaler module is located
\param value is an array with the scaler values
*/
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;
}
/*!
\param slot is a slot in the Usb where the scaler module is located
*/
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;
}
 UsbIO.cxx:1
 UsbIO.cxx:2
 UsbIO.cxx:3
 UsbIO.cxx:4
 UsbIO.cxx:5
 UsbIO.cxx:6
 UsbIO.cxx:7
 UsbIO.cxx:8
 UsbIO.cxx:9
 UsbIO.cxx:10
 UsbIO.cxx:11
 UsbIO.cxx:12
 UsbIO.cxx:13
 UsbIO.cxx:14
 UsbIO.cxx:15
 UsbIO.cxx:16
 UsbIO.cxx:17
 UsbIO.cxx:18
 UsbIO.cxx:19
 UsbIO.cxx:20
 UsbIO.cxx:21
 UsbIO.cxx:22
 UsbIO.cxx:23
 UsbIO.cxx:24
 UsbIO.cxx:25
 UsbIO.cxx:26
 UsbIO.cxx:27
 UsbIO.cxx:28
 UsbIO.cxx:29
 UsbIO.cxx:30
 UsbIO.cxx:31
 UsbIO.cxx:32
 UsbIO.cxx:33
 UsbIO.cxx:34
 UsbIO.cxx:35
 UsbIO.cxx:36
 UsbIO.cxx:37
 UsbIO.cxx:38
 UsbIO.cxx:39
 UsbIO.cxx:40
 UsbIO.cxx:41
 UsbIO.cxx:42
 UsbIO.cxx:43
 UsbIO.cxx:44
 UsbIO.cxx:45
 UsbIO.cxx:46
 UsbIO.cxx:47
 UsbIO.cxx:48
 UsbIO.cxx:49
 UsbIO.cxx:50
 UsbIO.cxx:51
 UsbIO.cxx:52
 UsbIO.cxx:53
 UsbIO.cxx:54
 UsbIO.cxx:55
 UsbIO.cxx:56
 UsbIO.cxx:57
 UsbIO.cxx:58
 UsbIO.cxx:59
 UsbIO.cxx:60
 UsbIO.cxx:61
 UsbIO.cxx:62
 UsbIO.cxx:63
 UsbIO.cxx:64
 UsbIO.cxx:65
 UsbIO.cxx:66
 UsbIO.cxx:67
 UsbIO.cxx:68
 UsbIO.cxx:69
 UsbIO.cxx:70
 UsbIO.cxx:71
 UsbIO.cxx:72
 UsbIO.cxx:73
 UsbIO.cxx:74
 UsbIO.cxx:75
 UsbIO.cxx:76
 UsbIO.cxx:77
 UsbIO.cxx:78
 UsbIO.cxx:79
 UsbIO.cxx:80
 UsbIO.cxx:81
 UsbIO.cxx:82
 UsbIO.cxx:83
 UsbIO.cxx:84
 UsbIO.cxx:85
 UsbIO.cxx:86
 UsbIO.cxx:87
 UsbIO.cxx:88
 UsbIO.cxx:89
 UsbIO.cxx:90
 UsbIO.cxx:91
 UsbIO.cxx:92
 UsbIO.cxx:93
 UsbIO.cxx:94
 UsbIO.cxx:95
 UsbIO.cxx:96
 UsbIO.cxx:97
 UsbIO.cxx:98
 UsbIO.cxx:99
 UsbIO.cxx:100
 UsbIO.cxx:101
 UsbIO.cxx:102
 UsbIO.cxx:103
 UsbIO.cxx:104
 UsbIO.cxx:105
 UsbIO.cxx:106
 UsbIO.cxx:107
 UsbIO.cxx:108
 UsbIO.cxx:109
 UsbIO.cxx:110
 UsbIO.cxx:111
 UsbIO.cxx:112
 UsbIO.cxx:113
 UsbIO.cxx:114
 UsbIO.cxx:115
 UsbIO.cxx:116
 UsbIO.cxx:117
 UsbIO.cxx:118
 UsbIO.cxx:119
 UsbIO.cxx:120
 UsbIO.cxx:121
 UsbIO.cxx:122
 UsbIO.cxx:123
 UsbIO.cxx:124
 UsbIO.cxx:125
 UsbIO.cxx:126
 UsbIO.cxx:127
 UsbIO.cxx:128
 UsbIO.cxx:129
 UsbIO.cxx:130
 UsbIO.cxx:131
 UsbIO.cxx:132
 UsbIO.cxx:133
 UsbIO.cxx:134
 UsbIO.cxx:135
 UsbIO.cxx:136
 UsbIO.cxx:137
 UsbIO.cxx:138
 UsbIO.cxx:139
 UsbIO.cxx:140
 UsbIO.cxx:141
 UsbIO.cxx:142
 UsbIO.cxx:143
 UsbIO.cxx:144
 UsbIO.cxx:145
 UsbIO.cxx:146
 UsbIO.cxx:147
 UsbIO.cxx:148
 UsbIO.cxx:149
 UsbIO.cxx:150
 UsbIO.cxx:151
 UsbIO.cxx:152
 UsbIO.cxx:153
 UsbIO.cxx:154
 UsbIO.cxx:155
 UsbIO.cxx:156
 UsbIO.cxx:157
 UsbIO.cxx:158
 UsbIO.cxx:159
 UsbIO.cxx:160
 UsbIO.cxx:161
 UsbIO.cxx:162
 UsbIO.cxx:163
 UsbIO.cxx:164
 UsbIO.cxx:165
 UsbIO.cxx:166
 UsbIO.cxx:167
 UsbIO.cxx:168
 UsbIO.cxx:169
 UsbIO.cxx:170
 UsbIO.cxx:171
 UsbIO.cxx:172
 UsbIO.cxx:173
 UsbIO.cxx:174
 UsbIO.cxx:175
 UsbIO.cxx:176
 UsbIO.cxx:177
 UsbIO.cxx:178
 UsbIO.cxx:179
 UsbIO.cxx:180
 UsbIO.cxx:181
 UsbIO.cxx:182
 UsbIO.cxx:183
 UsbIO.cxx:184
 UsbIO.cxx:185
 UsbIO.cxx:186
 UsbIO.cxx:187
 UsbIO.cxx:188
 UsbIO.cxx:189
 UsbIO.cxx:190
 UsbIO.cxx:191
 UsbIO.cxx:192
 UsbIO.cxx:193
 UsbIO.cxx:194
 UsbIO.cxx:195
 UsbIO.cxx:196
 UsbIO.cxx:197
 UsbIO.cxx:198
 UsbIO.cxx:199
 UsbIO.cxx:200
 UsbIO.cxx:201
 UsbIO.cxx:202
 UsbIO.cxx:203
 UsbIO.cxx:204
 UsbIO.cxx:205
 UsbIO.cxx:206
 UsbIO.cxx:207
 UsbIO.cxx:208
 UsbIO.cxx:209
 UsbIO.cxx:210
 UsbIO.cxx:211
 UsbIO.cxx:212
 UsbIO.cxx:213
 UsbIO.cxx:214
 UsbIO.cxx:215
 UsbIO.cxx:216
 UsbIO.cxx:217
 UsbIO.cxx:218
 UsbIO.cxx:219
 UsbIO.cxx:220
 UsbIO.cxx:221
 UsbIO.cxx:222
 UsbIO.cxx:223
 UsbIO.cxx:224
 UsbIO.cxx:225
 UsbIO.cxx:226
 UsbIO.cxx:227
 UsbIO.cxx:228
 UsbIO.cxx:229
 UsbIO.cxx:230
 UsbIO.cxx:231
 UsbIO.cxx:232
 UsbIO.cxx:233
 UsbIO.cxx:234
 UsbIO.cxx:235
 UsbIO.cxx:236
 UsbIO.cxx:237
 UsbIO.cxx:238
 UsbIO.cxx:239
 UsbIO.cxx:240
 UsbIO.cxx:241
 UsbIO.cxx:242
 UsbIO.cxx:243
 UsbIO.cxx:244
 UsbIO.cxx:245
 UsbIO.cxx:246
 UsbIO.cxx:247
 UsbIO.cxx:248
 UsbIO.cxx:249
 UsbIO.cxx:250
 UsbIO.cxx:251
 UsbIO.cxx:252
 UsbIO.cxx:253
 UsbIO.cxx:254