QuadroCopter  0.1.4
quadro::i2c::i2cDevice Class Referenceabstract

#include <i2cDevice.h>

Inheritance diagram for quadro::i2c::i2cDevice:
quadro::IDevice quadro::i2c::L3GD20H quadro::i2c::LSM303DLHC quadro::i2c::LSM303Accelerometer quadro::i2c::LSM303Magnetometer

Public Member Functions

 ~i2cDevice ()
 
 i2cDevice ()
 
void initDevice () throw ( i2cSetupException& )
 
short getValueFromRegister (unsigned char _registerAddress)
 
void setRegisterValue (unsigned char _registerValue)
 
void setRegisterAddress (unsigned char _registerAddress)
 
int writeToDevice (size_t _bufferSize) throw ( i2cSetupException& )
 
int stop (pthread_t threadHandle)
 
- Public Member Functions inherited from quadro::IDevice
virtual ~IDevice ()
 

Protected Types

enum  deviceStatus { On = 1, Off = 0, Error = 2 }
 

Protected Member Functions

int getStatus ()
 
void setStatus (deviceStatus _status)
 
int getDeviceFileHandle ()
 
const char * getFilePath ()
 
void setBusPaths ()
 
int validateBusId () throw ( i2cSetupException& )
 
char * validateBusPath (char *_busProposedPath) throw ( i2cSetupException& )
 
void selectABusPath ()
 
virtual void setDeviceAddress (unsigned char _deviceAddress)=0
 
virtual void setBusId (int _busId)=0
 
int connectToDevice ()
 
int openDevice () throw ( i2cSetupException& )
 
short readDevice (size_t _bufferSize) throw ( i2cSetupException& )
 

Protected Attributes

const char * deviceBusPath
 
unsigned char deviceAddress
 
unsigned char registerValue
 
unsigned char registerAddress
 
char readAndWriteBuffer [TWO_BYTES]
 
char writeBufferOnly [ONE_BYTE]
 
char errMessage [MAX_BUFF]
 
int fileHandle
 
int busId
 
bool deviceInitialised
 

Detailed Description

A base class for I2C devices, handles a lot of the lower level aspects like setting up the slave device, reading and writing to the device etc.

Member Enumeration Documentation

Stores the current deviceStatus for easy reference fro other objects

Enumerator
On 

Status On (thread is active)

Off 

Status Off (thread is disabled)

Error 

Status Error (thread is disabled)

110  {
111  On = 1,
112  Off = 0,
113  Error = 2
114  };
Status On (thread is active)
Definition: i2cDevice.h:111
Status Off (thread is disabled)
Definition: i2cDevice.h:112
Status Error (thread is disabled)
Definition: i2cDevice.h:113

Constructor & Destructor Documentation

i2cDevice::~i2cDevice ( )

Public Destructor

29  {
30  close( fileHandle );
31  }
int fileHandle
Definition: i2cDevice.h:231
i2cDevice::i2cDevice ( )

Public Constructor

23  {
24  deviceAddress = 0x00;
25  busId = 0;
26  deviceInitialised = false;
27  }
int busId
Definition: i2cDevice.h:232
unsigned char deviceAddress
Definition: i2cDevice.h:223
bool deviceInitialised
Definition: i2cDevice.h:234

Member Function Documentation

int quadro::i2c::i2cDevice::connectToDevice ( )
inlineprotectedvirtual

Protected connectToDevice - Sets up an IOCTL connection to the I2C device as a Slave for Linux.

Parameters
none
Returns
int

Implements quadro::IDevice.

201 { return ioctl( this->fileHandle, I2C_SLAVE, this->deviceAddress ); };
int fileHandle
Definition: i2cDevice.h:231
unsigned char deviceAddress
Definition: i2cDevice.h:223
int i2cDevice::getDeviceFileHandle ( )
protected

Protected getDeviceHandle - Returns the current fileHandle for reading and writing to the I2C device.

Parameters
none
Returns
int : The current fileHandle for reading and writing to the I2C device.
90 { return fileHandle; }
int fileHandle
Definition: i2cDevice.h:231
const char * i2cDevice::getFilePath ( )
protected

Protected getFilePath - Returns then filePath for accessing the I2C device.

Parameters
none
Returns
char
78 { return deviceBusPath; }
const char * deviceBusPath
Definition: i2cDevice.h:221
int i2cDevice::getStatus ( )
protected

getStatus returns the current status of the sensor

Returns
int
86  {
87  return currentStatus;
88  }
short i2cDevice::getValueFromRegister ( unsigned char  _registerAddress)

Public getValueFromRegister - Writes the register that you want to read, then performs a read on that register.

Parameters
unsignedchar _registerValue
Returns
short
115  {
116  if ( !deviceInitialised )
117  throw i2cSetupException( "i2c Device Not Initialised ( try : 'obj->initDevice()' )" );
118  setRegisterAddress( _RegisterAddress );
120  if ( write( getDeviceFileHandle(), writeBufferOnly, 1 ) == 1 ) {
121  return readDevice( ONE_BYTE );
122  }
123  else {
124  snprintf( errMessage, sizeof( errMessage ), "Fatal i2c Error - Unable to write to file : %s",
125  getFilePath());
126  throw i2cSetupException( errMessage );
127  }
128  }
const char * getFilePath()
Definition: i2cDevice.cpp:78
unsigned char registerAddress
Definition: i2cDevice.h:225
Definition: i2cSetupException.h:24
int getDeviceFileHandle()
Definition: i2cDevice.cpp:90
char errMessage[MAX_BUFF]
Definition: i2cDevice.h:230
char writeBufferOnly[ONE_BYTE]
Definition: i2cDevice.h:228
bool deviceInitialised
Definition: i2cDevice.h:234
#define ONE_BYTE
Used for specifying how many bytes to read.
Definition: i2cDevice.h:23
short readDevice(size_t _bufferSize)
Definition: i2cDevice.cpp:130
void setRegisterAddress(unsigned char _registerAddress)
Definition: i2cDevice.cpp:74
void i2cDevice::initDevice ( )
throw (i2cSetupException &
)
virtual

Public initDevice - Requires the device address and bus id to be configured.

Implements quadro::IDevice.

34  {
35  if ( !deviceAddress )
36  throw i2cSetupException( "i2c Device Not Configured ( try : 'obj->setDeviceAddress([hex address])' )" );
37  if ( !busId )
38  throw i2cSetupException( "i2c Device Not Configured ( try : 'obj->setBusId([bus number])' )" );
39  /*
40  * ** ## -- Setup Stage -- ## ** *
41  * setBusPaths : Saves the file paths to the available buses for ease of access.
42  */
43  setBusPaths();
44 
45  /*
46  * ** ## -- Assignment Stage ( based on args ) -- ## ** *
47  * validateBusId : Make sure we have a valid bus ID before proceeding.
48  * selectABusPath : Used to specify which bus your i2c device is on.
49  * setDeviceAddress: Hex value for your specific i2c Device.
50  */
51  validateBusId();
53 
54  /*
55  * ** ## -- init Stage -- ## ** *
56  * openDevice : Creates a file handle for the device, should it be closed? Probably... :)
57  * connectToDevice : Assigns the device as an i2c Slave and checks availability using IOCTL
58  *
59  * More info on IOCTL : http://man7.org/linux/man-pages/man2/ioctl.2.html
60  */
61  openDevice();
63 
64  deviceInitialised = true;
65  }
void selectABusPath()
Definition: i2cDevice.cpp:72
Definition: i2cSetupException.h:24
int openDevice()
Definition: i2cDevice.cpp:144
int validateBusId()
Definition: i2cDevice.cpp:92
int busId
Definition: i2cDevice.h:232
unsigned char deviceAddress
Definition: i2cDevice.h:223
bool deviceInitialised
Definition: i2cDevice.h:234
int connectToDevice()
Definition: i2cDevice.h:201
void setBusPaths()
Definition: i2cDevice.cpp:67
int i2cDevice::openDevice ( )
throw (i2cSetupException &
)
protectedvirtual

Protected openDevice - Attempts to open the fileHandle.

Parameters
none
Exceptions
i2cSetupException
Returns
int

Implements quadro::IDevice.

145  {
146  fileHandle = open( getFilePath(), O_RDWR );
147  if ( fileHandle == 0 ) {
148  snprintf( errMessage, sizeof( errMessage ), "Fatal i2c Error - Unable to open file : %s",
149  getFilePath());
150  throw i2cSetupException( errMessage );
151  }
152  return fileHandle;
153  }
const char * getFilePath()
Definition: i2cDevice.cpp:78
int fileHandle
Definition: i2cDevice.h:231
Definition: i2cSetupException.h:24
char errMessage[MAX_BUFF]
Definition: i2cDevice.h:230
short i2cDevice::readDevice ( size_t  _bufferSize)
throw (i2cSetupException &
)
protectedvirtual

Protected readDevice - Reads the current buffer from the I2C device; first writes the register address that will be read.

Parameters
size_t_bufferSize
Exceptions
i2cSetupException
Returns
short

Implements quadro::IDevice.

131  {
132  if ( !deviceInitialised )
133  throw i2cSetupException( "i2c Device Not Initialised ( try : 'obj->initDevice()' )" );
134  unsigned char buff[_BufferSize];
135  if ( read( getDeviceFileHandle(), buff, _BufferSize ) != _BufferSize ) {
136  snprintf( errMessage, sizeof( errMessage ), "Fatal i2c Error - Unable to read from file : %s",
137  getFilePath());
138  throw i2cSetupException( errMessage );
139  }
140  else
141  return buff[ 0 ];
142  }
const char * getFilePath()
Definition: i2cDevice.cpp:78
Definition: i2cSetupException.h:24
int getDeviceFileHandle()
Definition: i2cDevice.cpp:90
char errMessage[MAX_BUFF]
Definition: i2cDevice.h:230
bool deviceInitialised
Definition: i2cDevice.h:234
void i2cDevice::selectABusPath ( )
protected

Protected selectABusPath - Select which Bus Path we can find your I2C device at.

Parameters
none
72 { deviceBusPath = _bus[ busId ].busPath; }
int busId
Definition: i2cDevice.h:232
const char * deviceBusPath
Definition: i2cDevice.h:221
virtual void quadro::i2c::i2cDevice::setBusId ( int  _busId)
protectedpure virtual

Protected setBusId - Used to store the bus id (int)

Parameters
int_busId

Implemented in quadro::i2c::LSM303DLHC, and quadro::i2c::L3GD20H.

void i2cDevice::setBusPaths ( )
protected

Protected setBusPaths - Set Path to all the available buses. As set with I2CBus (struct) and Defines.

Parameters
none
68  {
69  _bus[ 1 ].busPath = validateBusPath(( char* ) I2C_1 );
70  }
char * validateBusPath(char *_busProposedPath)
Definition: i2cDevice.cpp:103
#define I2C_1
File Path for the i2c-1 bus.
Definition: i2cDevice.h:21
virtual void quadro::i2c::i2cDevice::setDeviceAddress ( unsigned char  _deviceAddress)
protectedpure virtual

Protected setDeviceAddress - Used to store the device address (Hex)

Parameters
int_deviceAddress

Implemented in quadro::i2c::LSM303DLHC, and quadro::i2c::L3GD20H.

void i2cDevice::setRegisterAddress ( unsigned char  _registerAddress)

Public setRegisterAddress - Set the Register address that the _RegisterValue will be written to.

Parameters
unsignedchar _registerAddress
74 { registerAddress = _registerAddress; }
unsigned char registerAddress
Definition: i2cDevice.h:225
void i2cDevice::setRegisterValue ( unsigned char  _registerValue)

Public setRegisterValue - Set the value that will next be written to the I2C device.

Parameters
unsignedchar _registerValue
76 { registerValue = _RegisterValue; }
unsigned char registerValue
Definition: i2cDevice.h:224
void i2cDevice::setStatus ( deviceStatus  _status)
protected

setStatus allows the status of the sensor to be changed to one of the values set in deviceStatus

Parameters
_status
81  {
82  currentStatus = _status;
83  }
int i2cDevice::stop ( pthread_t  threadHandle)

stop() - Allows the independent control to stop the specified thread.

Parameters
pthread_tthreadHandle
Exceptions
i2cRuntimeException
Returns
enum deviceStatus : 1 = on, 0 = off or 2 = error
180  {
181  //pthread_cancel doesn't throw an exception, only returns error codes - these are handled below.
182  int threadRet = pthread_cancel( threadHandle );
183 
184  if ( threadRet != 0 ) {
185  //The sonic sensor thread cancellation failed, set the status to "Error" and throw an appropriate exception.
187  if ( threadRet == ESRCH ) {
188  //Unable to locate process to cancel.
190  }
191  else {
192  //An unknown error occurred - unknown error code.
194  }
195  }
196 
197  //The thread was cancelled successfully - set the device status to "Off".
199 
200  //return the current device status, should always be Off here.
201  return getStatus();
202 
203  }
Off
Status Off (thread is disabled)
Definition: LVMaxSonarEZ.h:91
void setStatus(deviceStatus _status)
Definition: i2cDevice.cpp:80
Error
Status Error (thread is disabled)
Definition: LVMaxSonarEZ.h:92
const string THREAD_CANCELLATION_UNKNOWN
Definition: i2cRuntimeException.h:17
int getStatus()
Definition: i2cDevice.cpp:85
const string THREAD_CANCELLATION_FAILURE
Definition: i2cRuntimeException.h:16
Definition: i2cRuntimeException.h:21
int i2cDevice::validateBusId ( )
throw (i2cSetupException &
)
protected

Protected validateBusId - Make sure the BusId being used is valid.

Parameters
int_busId
Exceptions
i2cSetupException
Returns
int
93  {
94  if ( busId > I2C_BUS_COUNT || busId < 1 ) {
95  snprintf( errMessage, sizeof( errMessage ), "Bus ID : %d is not a valid BUS for this device.",
96  busId );
97  throw ( i2cSetupException( errMessage ));
98  }
99  else
100  return EXIT_SUCCESS;
101  }
Definition: i2cSetupException.h:24
char errMessage[MAX_BUFF]
Definition: i2cDevice.h:230
int busId
Definition: i2cDevice.h:232
#define I2C_BUS_COUNT
How many I2C buses are available. Add paths below as required.
Definition: i2cDevice.h:20
char * i2cDevice::validateBusPath ( char *  _busProposedPath)
throw (i2cSetupException &
)
protected

Protected validateBusPath - Make sure the busPath exists.

Parameters
I2CBus_busId
Exceptions
i2cSetupException
Returns
char
104  {
105  if ( stat( _busProposedPath, &buffer ) == 0 )
106  return _busProposedPath;
107  else {
108  snprintf( errMessage, sizeof( errMessage ),
109  "Fatal i2c Error - Unable to locate the i2c Bus file : %s", _busProposedPath );
110  throw i2cSetupException( errMessage );
111  }
112  }
Definition: i2cSetupException.h:24
char errMessage[MAX_BUFF]
Definition: i2cDevice.h:230
int i2cDevice::writeToDevice ( size_t  _bufferSize)
throw (i2cSetupException &
)
virtual

Public writeToDevice - Perform the write request. The _bufferSize is used to differentiate between a read or write request.

Parameters
size_t_bufferSize
Exceptions
i2cSetupException
Returns
int

Implements quadro::IDevice.

156  {
157  if ( !deviceInitialised )
158  throw i2cSetupException( "i2c Device Not Initialised ( try : 'obj->initDevice()' )" );
159  try {
160  if ( _bufferSize > ONE_BYTE ) {
163  write( getDeviceFileHandle(), readAndWriteBuffer, _bufferSize );
164  }
165  else {
167  write( getDeviceFileHandle(), writeBufferOnly, _bufferSize );
168  }
169  }
170  catch ( exception& e ) {
171  snprintf( errMessage, sizeof( errMessage ), "Fatal i2c Error - Unable to write to file : %s",
172  getFilePath());
173  throw i2cSetupException( errMessage );
174  }
175 
176  return EXIT_SUCCESS;
177  }
const char * getFilePath()
Definition: i2cDevice.cpp:78
unsigned char registerAddress
Definition: i2cDevice.h:225
Definition: i2cSetupException.h:24
int getDeviceFileHandle()
Definition: i2cDevice.cpp:90
char errMessage[MAX_BUFF]
Definition: i2cDevice.h:230
char writeBufferOnly[ONE_BYTE]
Definition: i2cDevice.h:228
bool deviceInitialised
Definition: i2cDevice.h:234
unsigned char registerValue
Definition: i2cDevice.h:224
#define ONE_BYTE
Used for specifying how many bytes to read.
Definition: i2cDevice.h:23
char readAndWriteBuffer[TWO_BYTES]
Definition: i2cDevice.h:227

Member Data Documentation

int quadro::i2c::i2cDevice::busId
protected
unsigned char quadro::i2c::i2cDevice::deviceAddress
protected
const char* quadro::i2c::i2cDevice::deviceBusPath
protected
bool quadro::i2c::i2cDevice::deviceInitialised
protected
char quadro::i2c::i2cDevice::errMessage[MAX_BUFF]
protected
int quadro::i2c::i2cDevice::fileHandle
protected
char quadro::i2c::i2cDevice::readAndWriteBuffer[TWO_BYTES]
protected
unsigned char quadro::i2c::i2cDevice::registerAddress
protected
unsigned char quadro::i2c::i2cDevice::registerValue
protected
char quadro::i2c::i2cDevice::writeBufferOnly[ONE_BYTE]
protected

The documentation for this class was generated from the following files:
  • /Users/michaelbrookes/ClionProjects/Quadro/lib/i2c/i2cDevice.h
  • /Users/michaelbrookes/ClionProjects/Quadro/lib/i2c/i2cDevice.cpp