CMI2c Class Reference

Comunication through I2C busses. More...

Public Member Functions

 CMI2c (const char *dev, __u8 address)
 ~CMI2c ()
int sendBlock (__u8 *data)
int recvWord (char *data, __u8 command)

Private Member Functions

__u16 swap_bytes (__u16 data)
int strlen__u8 (__u8 *data)

Private Attributes

char * dev
__u8 address

Detailed Description

Header for comunication through an I2C bus.

Author:
Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>

Codigo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Codigo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Examples:

TestI2c.cpp.


Constructor & Destructor Documentation

CMI2c::CMI2c ( const char *  dev,
__u8  address 
)

Constructor.

Parameters:
[in] dev The device for communication through I2C.
[in] address The address of the I2C device.

00034 {
00035   this->dev=(char*)malloc((strlen(dev)+1)*sizeof(char));
00036   strcpy(this->dev,dev);
00037   this->address=address;
00038 }

CMI2c::~CMI2c (  ) 

Destructor.

00041 {}


Member Function Documentation

int CMI2c::recvWord ( char *  data,
__u8  command 
)

Connect to the DBUS bus and recive a word.

Parameters:
[out] data The data received.
[in] command The command sended to device before receive data.
Returns:
Returns 0 if succesfull, -1 if not.
Examples:
TestI2c.cpp.

00077 {
00078   int file;
00079   __u16 buf;
00080   int res;
00081 
00082 //  this->debug.debug("Trying to open the device...");
00083   if ((file = open(this->dev,O_RDWR)) < 0)
00084   {
00085 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00086     return -1;
00087   }
00088 
00089 //  this->debug.debug("Trying to open the address of the device...");
00090   res = ioctl(file,I2C_SLAVE,this->address);
00091   if (res < 0)
00092   {
00093 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00094     return -1;
00095   }
00096   
00097   buf=i2c_smbus_read_word_data(file,command);
00098   sprintf(data,"%u",swap_bytes(buf));
00099 
00100   close(file);
00101 
00102   return 0;
00103 
00104 }

int CMI2c::sendBlock ( __u8 *  data  ) 

Connect to the I2C bus and send a block of characters.

Parameters:
[in] data The data to send.
Returns:
Returns 0 if succesfull, -1 if not.
Examples:
TestI2c.cpp.

00044 {
00045   int file=0,size=0;
00046   size=this->strlen__u8(data);
00047   int res;
00048   
00049 //  this->debug.debug("Trying to open the device...");
00050   if ((file = open(this->dev,O_RDWR)) < 0)
00051   {
00052 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00053     return -1;
00054   }
00055 
00056 //  this->debug.debug("Trying to open the address of the device...");
00057   res = ioctl(file,I2C_SLAVE,this->address);
00058   if (res < 0)
00059   {
00060 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00061     return -1;
00062   }
00063 
00064 //  this->debug.debug("Trying to send the data to the device...");
00065   if(i2c_smbus_write_block_data(file,0,size,data) == -1 )
00066   {
00067 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00068     return -1;
00069   }
00070   
00071   close(file);
00072   return 0;
00073     
00074 }

int CMI2c::strlen__u8 ( __u8 *  data  )  [private]

A strlen function for an array of bytes.

Parameters:
[in] data The data to compute.
Returns:
Returns the size of the array.

00107 {
00108   int a=0;
00109   while(data[a]!='\0')
00110   { a++; }
00111   return a;
00112 }

__u16 CMI2c::swap_bytes ( __u16  data  )  [private]

Swap bytes on a word(2 bytes).

Parameters:
[in] data The data to swap.
Returns:
Returns the data swaped.

00115 {
00116   return (data >> 8) | (data << 8);
00117 }


Field Documentation

__u8 CMI2c::address [private]

The address of the i2c device

char* CMI2c::dev [private]

The sys where is the i2c device

 All Data Structures Functions Variables
Generated on Mon Sep 12 07:59:09 2011 for API MokArdu by  doxygen 1.6.3