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 #ifndef TEMPLATEDIGITALOUT_HPP
00031 #define TEMPLATEDIGITALOUT_HPP
00032
00033 #include <rtt/os/CAS.hpp>
00034
00035 namespace RTT
00036 {
00037
00048 class TemplateDigitalOut
00049 {
00054 volatile unsigned int bit_status;
00055
00056 public:
00060 TemplateDigitalOut()
00061 : bit_status(0)
00062 {
00063 }
00064
00070 unsigned int getBitStatus() const { return bit_status; }
00071
00072 void switchOn( unsigned int bit )
00073 {
00074 unsigned int orig;
00075 unsigned int bcopy;
00076 do {
00077 orig = bit_status;
00078 bcopy = orig | (0x1 << bit);
00079 } while (!OS::CAS(&bit_status, orig, bcopy));
00080 }
00081
00082 void switchOff( unsigned int bit )
00083 {
00084 unsigned int orig;
00085 unsigned int bcopy;
00086 do {
00087 orig = bit_status;
00088 bcopy = orig & ~(1 << bit);
00089 } while (!OS::CAS(&bit_status, orig, bcopy));
00090 }
00091
00092 void setBit( unsigned int bit, bool value )
00093 {
00094 unsigned int orig;
00095 unsigned int bcopy;
00096 do {
00097 orig = bit_status;
00098 bcopy = orig & ~(!value << bit);
00099 bcopy = bcopy & (value << bit);
00100 } while (!OS::CAS(&bit_status, orig, bcopy));
00101 }
00102
00103 void setSequence(unsigned int start_bit, unsigned int stop_bit, unsigned int value)
00104 {
00105 unsigned int orig;
00106 unsigned int bcopy;
00107 unsigned int orcopy = value << start_bit;
00108
00109
00110 unsigned int andcopy = ~(( ((( 1<< (stop_bit-start_bit+1))-1)) ^ value )<<start_bit);
00111 do {
00112 orig = bit_status;
00113 bcopy = orig | orcopy;
00114 bcopy = bcopy & andcopy;
00115 } while (!OS::CAS(&bit_status, orig, bcopy));
00116
00117
00118
00119 }
00120
00121 bool checkBit(unsigned int n) const
00122 {
00123 return (bit_status >> n) & 0x1;
00124 }
00125
00126 unsigned int checkSequence( unsigned int start_bit, unsigned int stop_bit ) const
00127 {
00128 return (bit_status >> start_bit) & ( (0x1 << (stop_bit - start_bit + 1)) - 1);
00129 }
00130 };
00131
00132 }
00133
00134
00135 #endif
00136
00137