|
libFAUDES
Sections
Index
|
iop_comedi.cppGo 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 // only compile for comedi support 00017 #ifdef FAUDES_IODEVICE_COMEDI 00018 00019 namespace faudes { 00020 00021 //cDevice() 00022 cDevice::cDevice(void) : sDevice(), mDev(0) { 00023 //constructor 00024 00025 FD_DHV("cDevice(" << mName << ")::cDevice()"); 00026 // have appropriate default label for token io 00027 mDefaultLabel = "ComediDevice"; 00028 } 00029 00030 //~cDevice() 00031 cDevice::~cDevice(void) { 00032 //deconstructor 00033 00034 FD_DHV("cDevice(" << mName << ")::~cDevice()"); 00035 Stop(); 00036 } 00037 00038 // Start(void) 00039 void cDevice::Start(void) { 00040 //open comedi-device 00041 00042 if(mState!=Down) return; 00043 FD_DHV("cDevice(" << mName << ")::Start()"); 00044 FD_DH("cDevice(" << mName << ")::Start(): open devices"); 00045 mDev=comedi_open(mSystemFile.c_str()); 00046 // throw exception if opening device failed 00047 if(!mDev) { 00048 std::stringstream errstr; 00049 errstr << "cannot open device /dev/comedi0 (inputs)"; 00050 throw Exception("cDevice()::Start()", errstr.str(), 552); 00051 } 00052 00053 // check some properties on input card 00054 /* 00055 int sub, chan, dir; 00056 for(sub=0; sub<2; sub++) { 00057 for(chan=0; chan<32; chan++) { 00058 if(comedi_dio_get_config(DevIn, sub, chan, &dir) !=0) { 00059 FD_DH("dio::DioInitCards: error: on subdevice " << sub << " channel " << chan 00060 << " /dev/comedi0 (inputs)"); 00061 return 1; 00062 } 00063 if(dir != COMEDI_INPUT) { 00064 FD_DH("dio::DioInitCards: error: on subdevice " << sub << " channel " << chan 00065 << " /dev/comedi0 (inputs)"); 00066 return 1; 00067 } 00068 } 00069 } 00070 */ 00071 00072 // check some properties on output card 00073 /* 00074 for(sub=0; sub<2; sub++) { 00075 for(chan=0; chan<32; chan++) { 00076 if(comedi_dio_get_config(DevIn, sub, chan, &dir) !=0) { 00077 FD_DH("dio::DioInitCards: error: on subdevice " << sub << " channel " << chan 00078 << " /dev/comedi1 (outputs)"); 00079 return 1; 00080 } 00081 if(dir != COMEDI_INPUT) { 00082 FD_DH("dio::DioInitCards: error: on subdevice " << sub << " channel " << chan 00083 << " /dev/comedi1 (outputs)"); 00084 return 1; 00085 } 00086 } 00087 } 00088 */ 00089 // call base 00090 sDevice::Start(); 00091 }//end Start() 00092 00093 // Stop() 00094 void cDevice::Stop(void) { 00095 //close comedi-device 00096 00097 if(mState != Up && mState != StartUp) return; 00098 FD_DHV("cDevice(" << mName << ")::Stop()"); 00099 // call base 00100 sDevice::Stop(); 00101 // close device 00102 comedi_close(mDev); 00103 mDev=0; 00104 } 00105 00106 //DoWrite(rTr,rLabel,pContext) 00107 void cDevice::DoWritePreface(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const { 00108 //dummy for token-output 00109 00110 FD_DHV("cDevice("<<mName<<")::DoWritePreface()"); 00111 //call base 00112 sDevice::DoWritePreface(rTw,"",pContext); 00113 // write mSystemFile 00114 rTw.WriteString(mSystemFile); 00115 } 00116 00117 00118 //DoReadPreface(rTr,rLabel,pContext) 00119 void cDevice::DoReadPreface(TokenReader& rTr,const std::string& rLabel, const Type* pContext){ 00120 //dummy for token-input 00121 00122 FD_DHV("cDevice("<<mName<<")::DoReadPreface()"); 00123 //call base 00124 sDevice::DoReadPreface(rTr,"",pContext); 00125 //read my data 00126 mSystemFile = rTr.ReadString(); 00127 } 00128 00129 //ReadSignal(int) 00130 bool cDevice::DoReadSignal(int bit){ 00131 // read one sensor value, addressed by bit number (0 to 63); 00132 00133 // todo: report errors, check range 00134 lsampl_t input=0; 00135 comedi_data_read(mDev,bit/32,bit%32,0,AREF_GROUND,&input); 00136 00137 return (input!=0); 00138 } 00139 00140 00141 //WriteSignal(int,int) 00142 void cDevice::DoWriteSignal(int bit, bool value){ 00143 // write one actor value, adressed by bit number (0 to 63); 00144 00145 // todo: report errors, check range 00146 lsampl_t output= (value ? 1 : 0); 00147 //std::cout << "dio::write " << bit << ": " << output << std::endl; 00148 comedi_data_write(mDev,bit/32,bit%32,0,AREF_GROUND,output); 00149 } 00150 00151 00152 } // namespace 00153 00154 00155 #endif // if comedi |
libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3