Follow this link to skip to the main content

claraty::DIO Class Reference

#include <digital_io.h>

Inheritance diagram for claraty::DIO:

Inheritance graph
[legend]
Collaboration diagram for claraty::DIO:

Collaboration graph
[legend]
List of all members.

Public Types

enum  IO_MODE { INPUT, OUTPUT, INPUT_OUTPUT }

Public Member Functions

 DIO (size_t n_bits, IO_MODE io_mode, DIO_impl &dio_impl, size_t start_bit_impl=0, bool invert_logic=false)
DIOoperator= (const DIO &rhs)
 operator Bits () const
 operator unsigned long () const
DIOset ()
DIOset (const Bits &data)
DIOset (unsigned long data)
DIOclr ()
DIOtoggle ()
DIOset_bit (size_t pos, bool val=true)
DIOclr_bit (size_t pos)
DIOtoggle_bit (size_t pos)
DIOinput ()
DIOoutput ()
DIOoutput (const Bits &data)
DIOoutput (unsigned long data)
bool get_bit (size_t pos)
void pulse (size_t pos)
bool test (size_t pos) const
size_t bit_size () const
unsigned long get_mask ()
DIO_implimpl ()
std::string to_str () const
void print (const char *msg="") const

Private Member Functions

int _verify_io_mode () const

Private Attributes

unsigned long _n_bits
IO_MODE _io_mode
DIO_impl_dio_impl
size_t _start_bit_impl
bool _invert
Bits _mask_bits

Detailed Description

Definition at line 51 of file digital_io.h.


Member Enumeration Documentation

Enumerator:
INPUT 
OUTPUT 
INPUT_OUTPUT 

Definition at line 55 of file digital_io.h.


Constructor & Destructor Documentation

claraty::DIO::DIO ( size_t  n_bits,
IO_MODE  io_mode,
DIO_impl dio_impl,
size_t  start_bit_impl = 0,
bool  invert_logic = false 
)

Definition at line 38 of file digital_io.cc.

References _dio_impl, _invert, claraty::DIO_impl::_invert_signal, _mask_bits, _start_bit_impl, _verify_io_mode(), clr(), OK, and claraty::Bits::set_bits().

00044   : _n_bits (n_bits),
00045     _io_mode (io_mode),
00046     _dio_impl (dio_impl),
00047     _start_bit_impl (start_bit_impl),
00048     _mask_bits (0, MAX_BITS)    // 1 for no-mask; shifted for dio_impl as below
00049 {
00050 
00051   // 
00052   // Constructor
00053   //
00054   // default values:
00055   //   start_bit_impl = 0
00056   //   invert_logic = false
00057   //
00058 
00059   if (_verify_io_mode () != OK)
00060     cerr << "DIO ERROR: _verify_io_mode() output_bits error" << endl;
00061 
00062 
00063   _mask_bits.set_bits (0, n_bits); // set bits from 0 to n_bits-1 before shift
00064   _mask_bits <<= _start_bit_impl;  // shift the pattern for the DIO_impl mask
00065 
00066   // If invert_signal = true, the _data_bits are inverted compared to
00067   // actual hardware output.
00068   //
00069   // If invert_logic = true, the logic operation is always inverted relative to
00070   // actual hardware ouput, not to the internal _data_bits; namely,
00071   // set() sets _data_bits so that all the output bits are cleared, 
00072   // while clr() sets _data_bits so that all the output bits are set
00073   // regardless of the actual hardware signal inversion (invert_signal = true).
00074   //
00075   if (_dio_impl._invert_signal == invert_logic) {
00076     _invert = false;    // DIO data all 0's (corresponding impl data all 0's)
00077   }
00078   else {
00079     _invert = true;     // DIO data all i's (corresponding impl data all 0's)
00080   }
00081 
00082   clr();        // set to high if _invert
00083 }

Here is the call graph for this function:


Member Function Documentation

DIO& claraty::DIO::operator= ( const DIO rhs  ) 

claraty::DIO::operator Bits (  )  const

Definition at line 125 of file digital_io.cc.

References _n_bits.

00126 {
00127   return Bits( (unsigned long) *this, _n_bits); 
00128 }

claraty::DIO::operator unsigned long (  )  const

Definition at line 102 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, claraty::DIO_impl::_invert_signal, _mask_bits, _start_bit_impl, and claraty::Bits::to_ulong().

00103 {
00104   // conversion to unsigned long
00105   //
00106   // unsigned long x = d1.to_ulong();
00107   // unsigned short y = d1.to_ulong();
00108   // unsigned char z = d1.to_ulong();
00109   // BYTE z = d1.to_ulong();                    // BYTE = unsigned char
00110   //
00111 
00112   // return _data_bits.to_ulong();
00113   // It returns actual hardware signal output, not internal _data_bits
00114   //
00115   unsigned long data = _dio_impl._data_bits.to_ulong();
00116   if (_dio_impl._invert_signal)
00117         data = ~data;
00118   data &= _mask_bits.to_ulong();
00119   data >>= _start_bit_impl;
00120 
00121   return data;
00122 }

Here is the call graph for this function:

DIO & claraty::DIO::set (  ) 

Reimplemented in claraty::CDIO.

Definition at line 135 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, _invert, _n_bits, _start_bit_impl, claraty::Bits::clr_bits(), claraty::Bits::set_bit(), and claraty::Bits::set_bits().

Referenced by output(), and set().

00136 {
00137   // _data_bits.set();
00138 
00139   if (_n_bits == 1)
00140     _dio_impl._data_bits.set_bit(_start_bit_impl, !_invert);
00141   else if (!_invert)
00142     _dio_impl._data_bits.set_bits(_start_bit_impl, _n_bits);
00143   else
00144     _dio_impl._data_bits.clr_bits(_start_bit_impl, _n_bits);
00145 
00146   return *this;
00147 }

Here is the call graph for this function:

DIO & claraty::DIO::set ( const Bits data  ) 

Reimplemented in claraty::CDIO.

Definition at line 149 of file digital_io.cc.

References set(), and claraty::Bits::to_ulong().

00150 {
00151   // _data_bits = data;
00152   return set(data.to_ulong());
00153 }

Here is the call graph for this function:

DIO & claraty::DIO::set ( unsigned long  data  ) 

Reimplemented in claraty::CDIO.

Definition at line 155 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, _invert, _mask_bits, _start_bit_impl, claraty::Bits::to_ulong(), and claraty::Bits::unchecked_set().

00156 {
00157   // _data_bits = data;
00158 
00159   // _dio_impl._data_bits &= ~_mask_bits; // clear _n_bits only; keep the rest
00160   // To avoid _sanitize() requiring 5 arithmetic ops, do the following instead.
00161   _dio_impl._data_bits.unchecked_set ( _dio_impl._data_bits.to_ulong() & 
00162     ~(_mask_bits.to_ulong()) );
00163 
00164   // _dio_impl._data_bits |= ((data << _start_bit_impl) & _mask_bits);
00165   // To avoid _sanitize() requiring 5 arithmetic ops, do the following instead.
00166   _dio_impl._data_bits.unchecked_set ( _dio_impl._data_bits.to_ulong() | 
00167     (((_invert ? ~data : data) << _start_bit_impl) & _mask_bits.to_ulong()) );
00168 
00169   //  if (_data_bits != data)
00170   //    cerr << "DIO::set(unsigned long) WARNING: data truncated" << endl;
00171 
00172   return *this;
00173 }

Here is the call graph for this function:

DIO & claraty::DIO::clr (  ) 

Reimplemented in claraty::CDIO.

Definition at line 175 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, _invert, _n_bits, _start_bit_impl, claraty::Bits::clr_bits(), claraty::Bits::set_bit(), and claraty::Bits::set_bits().

Referenced by DIO().

00176 {
00177   // _data_bits.clr();
00178 
00179   if (_n_bits == 1) 
00180     _dio_impl._data_bits.set_bit(_start_bit_impl, _invert);
00181   else if (!_invert)
00182     _dio_impl._data_bits.clr_bits(_start_bit_impl, _n_bits);
00183   else
00184     _dio_impl._data_bits.set_bits(_start_bit_impl, _n_bits);
00185 
00186   return *this;
00187 }

Here is the call graph for this function:

DIO & claraty::DIO::toggle (  ) 

Reimplemented in claraty::CDIO.

Definition at line 189 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, _mask_bits, _n_bits, _start_bit_impl, and claraty::Bits::toggle_bit().

00190 {
00191   // _data_bits.toggle();
00192   if (_n_bits == 1)
00193     _dio_impl._data_bits.toggle_bit(_start_bit_impl);
00194   else {
00195     Bits data (_dio_impl._data_bits);
00196     _dio_impl._data_bits &= ~_mask_bits;// clear _n_bits only; keep the rest
00197     _dio_impl._data_bits |= (~data & _mask_bits);
00198                                         // bitwise OR with shifted data 
00199   }
00200 
00201   return *this;
00202 }

Here is the call graph for this function:

DIO & claraty::DIO::set_bit ( size_t  pos,
bool  val = true 
)

Reimplemented in claraty::CDIO.

Definition at line 204 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, _invert, _n_bits, _start_bit_impl, and claraty::Bits::set_bit().

Referenced by pulse(), claraty::CDIO::set_bit(), and claraty::CDIO::set_bit_output().

00205 {
00206   if (pos >= _n_bits)
00207     // throw out_of_range("Bits::set_bit");
00208     cerr << "DIO WARNING: out_of_range - DIO::set_bit" << endl;
00209 
00210   // _data_bits.set_bit(pos, val);
00211   _dio_impl._data_bits.set_bit(pos + _start_bit_impl, (_invert ? !val : val) );
00212 
00213   return *this;
00214 }

Here is the call graph for this function:

DIO & claraty::DIO::clr_bit ( size_t  pos  ) 

Reimplemented in claraty::CDIO.

Definition at line 216 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, _invert, _n_bits, _start_bit_impl, and claraty::Bits::set_bit().

Referenced by claraty::CDIO::clr_bit(), claraty::CDIO::clr_bit_output(), and pulse().

00217 {
00218   if (pos >= _n_bits)
00219     // throw out_of_range("Bits::clr_bit");
00220     cerr << "DIO WARNING: out_of_range - DIO::clr_bit" << endl;
00221  
00222   // _data_bits.clr_bit(pos);
00223   _dio_impl._data_bits.set_bit(pos + _start_bit_impl, _invert);
00224 
00225   return *this;
00226 }

Here is the call graph for this function:

DIO & claraty::DIO::toggle_bit ( size_t  pos  ) 

Reimplemented in claraty::CDIO.

Definition at line 228 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, _n_bits, _start_bit_impl, and claraty::Bits::toggle_bit().

Referenced by claraty::CDIO::toggle_bit(), and claraty::CDIO::toggle_bit_output().

00229 {
00230   if (pos >= _n_bits)
00231     // throw out_of_range("Bits::toggle_bit");
00232     cerr << "DIO WARNING: out_of_range - DIO::toggle_bit" << endl;
00233 
00234   // _data_bits.toggle_bit(pos);
00235   _dio_impl._data_bits.toggle_bit(pos + _start_bit_impl);
00236 
00237   return *this;
00238 }

Here is the call graph for this function:

DIO & claraty::DIO::input (  ) 

Reimplemented in claraty::CDIO.

Definition at line 241 of file digital_io.cc.

References _dio_impl, _io_mode, _mask_bits, claraty::DIO_impl::_mask_bits, claraty::DIO_impl::input(), and OUTPUT.

Referenced by get_bit().

00242 {
00243   if (_io_mode == DIO::OUTPUT) {
00244     cerr << "DIO output() ERROR: io_mode is OUTPUT" << endl;
00245     return *this;
00246   }
00247 
00248   // Copy mask bits from virtual DIO port to actual hardware port
00249   //
00250   _dio_impl._mask_bits = _mask_bits;
00251 
00252   _dio_impl.input();
00253 
00254   // Copy bits from the actual DIO h/w port to the logical DIO
00255   // _data_bits = (_dio_impl._data_bits >> _start_bit_impl);
00256 
00257   // if (_invert)
00258   //   _data_bits.toggle();     // toggle
00259 
00260   return *this;
00261 }

Here is the call graph for this function:

DIO & claraty::DIO::output (  ) 

Reimplemented in claraty::CDIO.

Definition at line 274 of file digital_io.cc.

References _dio_impl, _io_mode, _mask_bits, claraty::DIO_impl::_mask_bits, INPUT, and claraty::DIO_impl::output().

Referenced by claraty::CDIO::clr_bit_output(), output(), claraty::CDIO::set_bit_output(), and claraty::CDIO::toggle_bit_output().

00275 {
00276   if (_io_mode == INPUT) {
00277     cerr << "DIO output() ERROR: io_mode is INPUT" << endl;
00278     return *this;
00279   }
00280 
00281   // Copy mask bits from virtual DIO port to actual hardware port
00282   //
00283   _dio_impl._mask_bits = _mask_bits;
00284 
00285   // do actual hardware output operation
00286   _dio_impl.output();
00287 
00288   return *this;
00289 }

Here is the call graph for this function:

DIO & claraty::DIO::output ( const Bits data  ) 

Reimplemented in claraty::CDIO.

Definition at line 292 of file digital_io.cc.

References output(), and claraty::Bits::to_ulong().

00293 {
00294   return output(data.to_ulong());
00295 }

Here is the call graph for this function:

DIO & claraty::DIO::output ( unsigned long  data  ) 

Reimplemented in claraty::CDIO.

Definition at line 298 of file digital_io.cc.

References output(), and set().

00299 {
00300   set(data);
00301   return output();
00302 }

Here is the call graph for this function:

bool claraty::DIO::get_bit ( size_t  pos  ) 

Reimplemented in claraty::CDIO.

Definition at line 264 of file digital_io.cc.

References input(), and test().

Referenced by claraty::CDIO::get_bit().

00265 {
00266   // return does not indicate input() error
00267   //
00268   input();
00269 
00270   return test(pos);
00271 }

Here is the call graph for this function:

void claraty::DIO::pulse ( size_t  pos  ) 

Definition at line 305 of file digital_io.cc.

References clr_bit(), and set_bit().

00306 {
00307   // Clear a bit, sets it and then clear it again. This function
00308   // can be very useful in hardware I/O control.
00309   // do we want to specify the pulse duration??
00310   //
00311   clr_bit(pos);
00312   // add delay ??
00313   set_bit(pos);
00314   // add delay ??
00315   clr_bit(pos);
00316  }

Here is the call graph for this function:

bool claraty::DIO::test ( size_t  pos  )  const

Reimplemented in claraty::CDIO.

Definition at line 319 of file digital_io.cc.

References claraty::DIO_impl::_data_bits, _dio_impl, _n_bits, _start_bit_impl, and claraty::Bits::test().

Referenced by get_bit(), and claraty::CDIO::test().

00320 {
00321   // no input() unlike get_bit()
00322   //
00323 
00324   if (pos >= _n_bits)
00325     // throw out_of_range("Bits::test");
00326     cerr << "DIO WARNING: out_of_range - DIO::test" << endl;
00327 
00328   return _dio_impl._data_bits.test(pos + _start_bit_impl);
00329 }

Here is the call graph for this function:

unsigned long claraty::DIO::get_mask (  ) 

Definition at line 344 of file digital_io.cc.

References _mask_bits, and claraty::Bits::to_ulong().

00345 {
00346   return _mask_bits.to_ulong();
00347 }

Here is the call graph for this function:

DIO_impl& claraty::DIO::impl (  )  [inline]

Definition at line 133 of file digital_io.h.

References _dio_impl.

00133 { return _dio_impl;}

string claraty::DIO::to_str (  )  const

Reimplemented in claraty::CDIO.

Definition at line 351 of file digital_io.cc.

References _n_bits.

00352 {
00353   return Bits((unsigned long) *this, _n_bits).to_str();
00354 }

void claraty::DIO::print ( const char *  msg = ""  )  const

Reimplemented in claraty::CDIO.

Definition at line 357 of file digital_io.cc.

References _n_bits.

00358 {
00359   Bits((unsigned long) *this, _n_bits).print(msg);
00360 }

int claraty::DIO::_verify_io_mode (  )  const [private]

Definition at line 363 of file digital_io.cc.

References _dio_impl, claraty::DIO_impl::_input_bits, _io_mode, _n_bits, claraty::DIO_impl::_output_bits, _start_bit_impl, ERROR, INPUT, INPUT_OUTPUT, OK, OUTPUT, and claraty::Bits::set().

Referenced by DIO().

00364 {
00365   Bits input_bits(0, _n_bits);
00366   Bits output_bits(0, _n_bits);
00367 
00368   if (_io_mode==INPUT || _io_mode==INPUT_OUTPUT)
00369     input_bits.set();
00370 
00371   if (_io_mode==OUTPUT || _io_mode==INPUT_OUTPUT)
00372     output_bits.set();
00373 
00374   input_bits <<= _start_bit_impl;
00375   output_bits <<= _start_bit_impl;
00376 
00377   if ( (input_bits & _dio_impl._input_bits) != input_bits )
00378     return ERROR;
00379 
00380   if ( (output_bits & _dio_impl._output_bits) != output_bits)
00381     return ERROR;
00382 
00383   return OK;
00384 }

Here is the call graph for this function:


Member Data Documentation

unsigned long claraty::DIO::_n_bits [private]

Definition at line 148 of file digital_io.h.

Referenced by _verify_io_mode(), input(), and output().

bool claraty::DIO::_invert [private]

Definition at line 151 of file digital_io.h.

Referenced by clr(), clr_bit(), DIO(), set(), and set_bit().

Definition at line 152 of file digital_io.h.

Referenced by DIO(), get_mask(), input(), operator unsigned long(), output(), set(), and toggle().


The documentation for this class was generated from the following files: