00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "ComediSubDeviceDIn.hpp"
00031 #include <rtt/os/fosi.h>
00032 #include "comedi_internal.h"
00033
00034 namespace OCL
00035 {
00036
00037
00038
00039
00040 ComediSubDeviceDIn::ComediSubDeviceDIn( ComediDevice* cd, const std::string& name, unsigned int subdevice, bool all_bits)
00041 : DigitalInInterface( name ),
00042 myCard( cd ), _subDevice( subdevice )
00043 {
00044 init(all_bits);
00045 }
00046
00047 ComediSubDeviceDIn::ComediSubDeviceDIn( ComediDevice* cd, unsigned int subdevice, bool all_bits )
00048 : myCard( cd ), _subDevice( subdevice )
00049 {
00050 init(all_bits);
00051 }
00052
00053 void ComediSubDeviceDIn::init(bool all_bits)
00054 {
00055 Logger::In in( nameserver.getName( this ).c_str() );
00056 if (!myCard) {
00057 log(Error) << "Error creating ComediSubDeviceDIn: null ComediDevice given." <<endlog();
00058 return;
00059 }
00060 if ( ( myCard->getSubDeviceType( _subDevice ) != COMEDI_SUBD_DI ) &&
00061 ( myCard->getSubDeviceType( _subDevice ) != COMEDI_SUBD_DIO) )
00062 {
00063 Logger::In in("ComediSubDeviceDIn");
00064 log(Error) << "SubDevice "<< _subDevice <<" is not a digital input: ";
00065 log() << "type = " << myCard->getSubDeviceType( _subDevice ) << endlog();
00066 myCard = 0;
00067 return;
00068 }
00069
00070 unsigned int channels = this->nbOfInputs();
00071 if ( ( myCard->getSubDeviceType( _subDevice ) == COMEDI_SUBD_DIO) && all_bits ) {
00072 log(Info) << "Configuring first "<<channels<<" dio channels on subdevice "<<_subDevice<<" as input type." << endlog();
00073
00074 for (unsigned int i=0; i<channels; ++i)
00075 comedi_dio_config(myCard->getDevice()->it, _subDevice, i, COMEDI_INPUT);
00076 } else {
00077 log(Info) << "Subdevice "<<_subDevice<<" is digital input with "<<channels << " channels." << endlog();
00078 }
00079 }
00080
00081 bool ComediSubDeviceDIn::useBit( unsigned int bit )
00082 {
00083 return comedi_dio_config(myCard->getDevice()->it, _subDevice, bit, COMEDI_INPUT) == 0;
00084 }
00085
00086 bool ComediSubDeviceDIn::isOn( unsigned int bit ) const
00087 {
00088 unsigned int tmp;
00089 if (!myCard) return false;
00090 comedi_dio_read( myCard->getDevice()->it,_subDevice,bit, &tmp );
00091 return (tmp == 1);
00092 }
00093
00094 bool ComediSubDeviceDIn::isOff( unsigned int bit ) const
00095 {
00096 return !isOn(bit);
00097 }
00098
00099 bool ComediSubDeviceDIn::readBit( unsigned int bit ) const
00100 {
00101 return isOn(bit);
00102 }
00103
00104 unsigned int ComediSubDeviceDIn::nbOfInputs() const
00105 {
00106 if (!myCard) return 0;
00107 return comedi_get_n_channels(myCard->getDevice()->it, _subDevice);
00108 }
00109
00110 unsigned int ComediSubDeviceDIn::readSequence(unsigned int start_bit, unsigned int stop_bit) const
00111 {
00112 unsigned int value = 0;
00113 if (!myCard) return 0;
00114 if ((start_bit > stop_bit) || (stop_bit >= this->nbOfInputs()))
00115 {
00116 Logger::In in("ComediSubDeviceDIn");
00117 log(Error) << "start_bit should be less than stop_bit" << endlog();
00118 }
00119 else
00120 {
00121
00122 comedi_dio_bitfield(myCard->getDevice()->it, _subDevice, 0x0, &value);
00123
00124 unsigned int write_mask = 0;
00125
00126 for (unsigned int i = start_bit; i <= stop_bit ; i++)
00127 {
00128 write_mask = write_mask | (0x1 << i);
00129 }
00130
00131 value = value & write_mask ;
00132
00133 value = value >> start_bit;
00134
00135
00136 }
00137
00138 return value;
00139
00140 }
00141
00142 }