libFAUDES

Sections

Index

iop_comedi.cpp

Go to the documentation of this file.
00001  
00002 /** @file iop_comedi.cpp Low-level access to physical device over comedi-driver*/
00003 
00004 /*
00005    FAU Discrete Event Systems Library (libfaudes)
00006 
00007    Copyright (C) 2008, Thomas Moor
00008    Exclusive copyright is granted to Klaus Schmidt
00009 
00010 */
00011 
00012 
00013 // include header
00014 #include "iop_comedi.h"
00015 
00016 namespace faudes {
00017 
00018 // only compile for comedi support
00019 #ifdef FAUDES_IODEVICE_COMEDI
00020 
00021 // std faudes, incl dummy
00022 FAUDES_TYPE_IMPLEMENTATION(ComediDevice,cDevice,sDevice)
00023 
00024 // autoregister
00025 AutoRegisterType<cDevice> gRtiRegisterComediDevice("ComediDevice");
00026 
00027 //constructor
00028 cDevice::cDevice(void) : sDevice(), mDev(0) {
00029   FD_DHV("cDevice(" << mName << ")::cDevice()");
00030   // have appropriate default label for token io
00031   mDefaultLabel = "ComediDevice";
00032 }
00033 
00034 //deconstructor
00035 cDevice::~cDevice(void) {
00036   FD_DHV("cDevice(" << mName << ")::~cDevice()");
00037   Stop();
00038 }
00039 
00040 // Start(void)
00041 void cDevice::Start(void) {
00042   //open comedi-device
00043   
00044   if(mState!=Down) return;
00045   FD_DH("cDevice(" << mName << ")::Start(): open devices");
00046   mDev=comedi_open(mSystemFile.c_str());
00047   // throw exception if opening device failed
00048   if(!mDev) {
00049     std::stringstream errstr;
00050     errstr << "cannot open device /dev/comedi0 (inputs)";
00051     throw Exception("cDevice()::Start()", errstr.str(), 552);
00052   }
00053 
00054   // check some properties on input card
00055   /*
00056   int sub, chan, dir;
00057   for(sub=0; sub<2; sub++) {
00058     for(chan=0; chan<32; chan++) {
00059       if(comedi_dio_get_config(DevIn, sub, chan, &dir) !=0) {
00060         FD_DH("dio::DioInitCards: error: on subdevice " << sub << " channel " << chan
00061           << "  /dev/comedi0 (inputs)");
00062         return 1;
00063       }
00064       if(dir != COMEDI_INPUT) {
00065         FD_DH("dio::DioInitCards: error: on subdevice " << sub << " channel " << chan
00066           << "  /dev/comedi0 (inputs)");
00067         return 1;
00068       }
00069     }
00070   }
00071   */
00072 
00073   // check some properties on output card
00074   /*
00075   for(sub=0; sub<2; sub++) {
00076     for(chan=0; chan<32; chan++) {
00077       if(comedi_dio_get_config(DevIn, sub, chan, &dir) !=0) {
00078         FD_DH("dio::DioInitCards: error: on subdevice " << sub << " channel " << chan
00079           << "  /dev/comedi1 (outputs)");
00080         return 1;
00081       }
00082       if(dir != COMEDI_INPUT) {
00083         FD_DH("dio::DioInitCards: error: on subdevice " << sub << " channel " << chan
00084           << "  /dev/comedi1 (outputs)");
00085         return 1;
00086       }
00087     }
00088   }
00089   */
00090   // call base
00091   sDevice::Start();
00092 }//end Start()
00093 
00094 // Stop()
00095 void cDevice::Stop(void) {
00096   //close comedi-device
00097 
00098   if(mState != Up && mState != StartUp) return;
00099   FD_DHV("cDevice(" << mName << ")::Stop()");
00100   // call base
00101   sDevice::Stop();
00102   // close device
00103   comedi_close(mDev);
00104   mDev=0;
00105 }
00106 
00107 //DoWrite(rTr,rLabel,pContext)
00108 void cDevice::DoWritePreface(TokenWriter& rTw, const std::string& rLabel,  const Type* pContext) const {
00109   FD_DHV("cDevice("<<mName<<")::DoWritePreface()");
00110   //call base
00111   sDevice::DoWritePreface(rTw,"",pContext);
00112   // write mSystemFile
00113   rTw.WriteString(mSystemFile);
00114 }
00115 
00116 
00117 //DoReadPreface(rTr,rLabel,pContext)
00118 void cDevice::DoReadPreface(TokenReader& rTr,const std::string& rLabel, const Type* pContext){
00119   FD_DHV("cDevice("<<mName<<")::DoReadPreface()");
00120   //call base
00121   sDevice::DoReadPreface(rTr,"",pContext);
00122 
00123 
00124   // sense and digest pre 2.16 format
00125   Token token;
00126   rTr.Peek(token);
00127   if(token.IsString()) {
00128     mSystemFile = rTr.ReadString();
00129     return;
00130   }
00131   
00132   // loop my members
00133   while(true) {
00134     Token token;
00135     rTr.Peek(token);
00136     // system file
00137     if(token.IsBegin()) 
00138     if(token.StringValue()=="DeviceFile") {
00139       rTr.ReadBegin("DeviceFile", token);
00140       mSystemFile=token.AttributeStringValue("value");
00141       rTr.ReadEnd("DeviceFile");
00142       continue;
00143     }
00144     // break on unknown
00145     break;
00146   }
00147 
00148 }
00149 
00150 //ReadSignal(int)
00151 bool cDevice::DoReadSignal(int bit){
00152   // read one input value, addressed by bit number (0 to 63);
00153 
00154   // todo: report errors, check range
00155   lsampl_t input=0;
00156   comedi_data_read(mDev,bit/32,bit%32,0,AREF_GROUND,&input);
00157 
00158   return (input!=0);
00159 }
00160 
00161 
00162 //WriteSignal(int,int)
00163 void cDevice::DoWriteSignal(int bit, bool value){
00164   // write one actor value, adressed by bit number (0 to 63);
00165 
00166   // todo: report errors, check range
00167   lsampl_t output= (value ? 1 : 0);
00168   //std::cout << "dio::write " << bit << ": " << output << std::endl;
00169   comedi_data_write(mDev,bit/32,bit%32,0,AREF_GROUND,output);
00170 }
00171 
00172 
00173 #endif // if comedi
00174 
00175 } // namespace
00176 
00177 

libFAUDES 2.22s --- 2013.10.07 --- c++ source docu by doxygen