00001 #ifndef DIGITALETHERCATOUTPUTDEVICE_HPP
00002 #define DIGITALETHERCATOUTPUTDEVICE_HPP
00003
00004 #include "rtt/dev/DigitalOutInterface.hpp"
00005 #include <vector>
00006
00007 namespace RTT
00008 {
00013 class DigitalEtherCATOutputDevice
00014 : public DigitalOutInterface
00015 {
00016 public:
00017 std::vector<bool> mchannels;
00018 unsigned char* mstartaddress;
00019 unsigned int startb;
00020 unsigned int nbofchannels;
00021
00022 DigitalEtherCATOutputDevice(unsigned char* startaddress, unsigned int start_bit,unsigned int channels=32)
00023 : DigitalOutInterface("DigitalEtherCATOutputDevice"),
00024 mchannels(channels,false),
00025 mstartaddress(startaddress),
00026 startb(start_bit),
00027 nbofchannels(channels)
00028 {}
00029
00030 virtual void switchOn( unsigned int n )
00031 {
00032 if ( n < nbofchannels ) {
00033 unsigned char tmp = 1 << n+startb;
00034 *mstartaddress = *mstartaddress | tmp;
00035 }
00036 }
00037
00038 virtual void switchOff( unsigned int n )
00039 {
00040 if ( n < nbofchannels ) {
00041 unsigned char tmp = (1 << n+startb) ^ 0xff;
00042 *mstartaddress = *mstartaddress & tmp;
00043 }
00044 }
00045
00046 virtual void setBit( unsigned int bit, bool value )
00047 {
00048 if(value)
00049 switchOn(bit);
00050 else
00051 switchOff(bit);
00052 }
00053
00054 virtual void setSequence(unsigned int start_bit, unsigned int stop_bit, unsigned int value)
00055 {
00056 if ( start_bit < nbofchannels && stop_bit < nbofchannels ) {
00057
00058 for (unsigned int i = start_bit; i <= stop_bit; i++)
00059 mchannels[i] = value & ( 1<<( i - start_bit ) );
00060 }
00061
00062 }
00063
00064 virtual bool checkBit(unsigned int n) const
00065 {
00066 if ( n < nbofchannels )
00067 return mchannels[n];
00068 return false;
00069 }
00070
00071
00072 virtual unsigned int checkSequence( unsigned int start_bit, unsigned int stop_bit ) const
00073 {
00074 unsigned int result = 0;
00075 if ( start_bit < nbofchannels && stop_bit < nbofchannels )
00076 for (unsigned int i = start_bit; i <= stop_bit; ++i)
00077 result += (mchannels[i] & 1)<<i;
00078 return result;
00079 }
00080
00081 virtual unsigned int nbOfOutputs() const
00082 {
00083 return nbofchannels;
00084 }
00085
00086 };
00087
00088
00089 }
00090
00091
00092 #endif