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