PIDUINO
Loading...
Searching...
No Matches
Piduino::I2cDev

I2C device interface for communicating with I2C peripheral devices. More...

#include <i2cdev.h>

Inheritance diagram for Piduino::I2cDev:
Collaboration diagram for Piduino::I2cDev:

Classes

class  Info
 Information class for I2C bus configuration and management. More...
 

Public Types

enum  OpenModeFlag {
  NotOpen = 0x0000 , Append = std::ios_base::app , AtEnd = std::ios_base::ate , Binary = std::ios_base::binary ,
  ReadOnly = std::ios_base::in , WriteOnly = std::ios_base::out , ReadWrite = ReadOnly | WriteOnly , Truncate = std::ios_base::trunc ,
  IosModes = Append | AtEnd | Binary | ReadOnly | WriteOnly | Truncate , Unbuffered = (Truncate << 1)
}
 Flags that specify how the device is to be opened. More...
 
typedef Flags< OpenModeFlagOpenMode
 Type representing a combination of OpenModeFlag values.
 

Public Member Functions

 I2cDev ()
 Default constructor that creates an I2cDev object without specifying a bus. The bus must be set later using setBus() before opening the device.
 
 I2cDev (const Info &bus)
 Constructs an I2cDev object for the specified I2C bus.
 
 I2cDev (const char *path)
 Constructs an I2cDev object using a device path.
 
 I2cDev (const std::string &path)
 Constructs an I2cDev object using a device path.
 
 I2cDev (int idBus)
 Constructs an I2cDev object for the specified bus ID.
 
virtual ~I2cDev ()
 Virtual destructor that properly cleans up the I2C device.
 
virtual bool open (OpenMode mode=IoDevice::ReadWrite)
 Opens the I2C device for communication.
 
virtual void close ()
 Closes the I2C device and releases system resources.
 
void setBus (const Info &bus)
 Sets the I2C bus using an Info object.
 
void setBus (int idBus)
 Sets the I2C bus using a bus ID.
 
const Infobus () const
 Gets the current I2C bus information.
 
void setBusPath (const char *path)
 Sets the I2C bus using a device path.
 
void setBusPath (const std::string &path)
 Sets the I2C bus using a device path.
 
void beginTransmission (uint16_t slave)
 Begins an I2C transmission to the specified slave device. This must be called before writing data to a slave device. This function sets the slave address for the transaction and initializes the internal buffer but it does not send any data yet.
 
bool endTransmission (bool stop=true)
 Ends the current I2C transmission and sends the data if stop is true.
 
virtual int write (uint8_t data)
 Writes a single byte to the current I2C transaction buffer.
 
virtual int write (const uint8_t *buffer, uint16_t len)
 Writes multiple bytes to the current I2C transaction buffer.
 
int write (std::string str)
 Writes a string to the current I2C transaction buffer.
 
int write (const char *str)
 Writes a C-style string to the current I2C transaction buffer.
 
int write (int data)
 Writes an integer value (cast to uint8_t) to the current I2C transaction buffer.
 
int write (unsigned int data)
 Writes an unsigned integer value (cast to uint8_t) to the current I2C transaction buffer.
 
int write (long data)
 Writes a long value (cast to uint8_t) to the current I2C transaction buffer.
 
int write (unsigned long data)
 Writes an unsigned long value (cast to uint8_t) to the current I2C transaction buffer.
 
int requestFrom (uint16_t slave, uint16_t max, bool stop=true)
 Requests data from an I2C slave device and stores it in the read buffer.
 
int requestFrom (int slave, int max, int stop=1)
 Requests data from an I2C slave device (convenience overload with int parameters).
 
uint16_t available () const
 Gets the number of bytes available for reading from the internal buffer.
 
virtual int read (uint8_t *buffer, uint16_t max)
 Reads data from the internal buffer into the provided buffer.
 
virtual int read ()
 Reads a single byte from the internal buffer.
 
virtual int peek () const
 Peeks at the next byte in the buffer without removing it.
 
virtual void flush ()
 Flushes any pending data in the internal buffers. This ensures all data is sent before returning.
 
OpenMode openMode () const
 Returns the current open mode of the device.
 
virtual bool isOpen () const
 Returns true if the device is currently open.
 
bool isReadable () const
 Returns true if the device is readable.
 
bool isWritable () const
 Returns true if the device is writable.
 
bool isBuffered () const
 Returns true if the device is buffered.
 
virtual bool isSequential () const
 Returns true if this device is sequential; otherwise returns false.
 
void setTextModeEnabled (bool enabled)
 Enables or disables text mode for the device.
 
bool isTextModeEnabled () const
 Returns true if text mode is enabled.
 
void setDebug (bool enabled)
 Enables or disables debug mode for the device.
 
bool isDebug () const
 Returns true if debug mode is enabled.
 
virtual std::string errorString () const
 Returns a human-readable description of the last error.
 
virtual int error () const
 Returns the code of the last error.
 

Static Public Member Functions

static std::shared_ptr< I2cDevfactory (int busId)
 Factory method that creates a shared pointer to an I2cDev instance.
 

Protected Member Functions

 I2cDev (Private &dd)
 Protected constructor for derived classes using the PIMPL pattern.
 

Protected Attributes

std::unique_ptr< Privated_ptr
 Pointer to the private implementation.
 

Detailed Description

I2C device interface for communicating with I2C peripheral devices.

This class provides a high-level interface for I2C communication on Linux systems. It supports master mode operations including reading from and writing to I2C slave devices. The class handles I2C bus management, transaction control, and data buffering.

Definition at line 35 of file i2cdev.h.

Member Typedef Documentation

◆ OpenMode

Type representing a combination of OpenModeFlag values.

Definition at line 70 of file iodevice.h.

Member Enumeration Documentation

◆ OpenModeFlag

Flags that specify how the device is to be opened.

These flags control the behavior of the device when it is opened. They can be combined using bitwise OR.

Enumerator
NotOpen 

Device is not open.

Append 

Set the stream's position indicator to the end of the stream before each output operation.

AtEnd 

Set the stream's position indicator to the end of the stream on opening.

Binary 

Open the device in binary mode (no text translation).

ReadOnly 

Open the device for input operations only.

Text mode: When reading, end-of-line terminators are translated to '
'. When writing, end-of-line terminators are translated to the local encoding (e.g., '\r
' for Win32).

WriteOnly 

Open the device for output operations only.

ReadWrite 

Open the device for both input and output operations.

Truncate 

Discard any existing content when opening the device.

IosModes 

Combination of all standard I/O modes.

Unbuffered 

Open the device in unbuffered mode (implementation-defined).

Definition at line 48 of file iodevice.h.

Constructor & Destructor Documentation

◆ I2cDev() [1/6]

Piduino::I2cDev::I2cDev ( )

Default constructor that creates an I2cDev object without specifying a bus. The bus must be set later using setBus() before opening the device.

◆ I2cDev() [2/6]

Piduino::I2cDev::I2cDev ( const Info bus)

Constructs an I2cDev object for the specified I2C bus.

Parameters
busThe I2C bus information

◆ I2cDev() [3/6]

Piduino::I2cDev::I2cDev ( const char *  path)
explicit

Constructs an I2cDev object using a device path.

Parameters
pathThe device path (e.g., "/dev/i2c-0")

◆ I2cDev() [4/6]

Piduino::I2cDev::I2cDev ( const std::string &  path)
explicit

Constructs an I2cDev object using a device path.

Parameters
pathThe device path as a string

◆ I2cDev() [5/6]

Piduino::I2cDev::I2cDev ( int  idBus)
explicit

Constructs an I2cDev object for the specified bus ID.

Parameters
idBusThe I2C bus ID

◆ ~I2cDev()

virtual Piduino::I2cDev::~I2cDev ( )
virtual

Virtual destructor that properly cleans up the I2C device.

◆ I2cDev() [6/6]

Piduino::I2cDev::I2cDev ( Private dd)
protected

Protected constructor for derived classes using the PIMPL pattern.

Parameters
ddReference to the private implementation

Member Function Documentation

◆ available()

uint16_t Piduino::I2cDev::available ( ) const

Gets the number of bytes available for reading from the internal buffer.

Returns
The number of bytes available

◆ beginTransmission()

void Piduino::I2cDev::beginTransmission ( uint16_t  slave)

Begins an I2C transmission to the specified slave device. This must be called before writing data to a slave device. This function sets the slave address for the transaction and initializes the internal buffer but it does not send any data yet.

Parameters
slaveThe 7-bit I2C slave address

◆ bus()

const Info & Piduino::I2cDev::bus ( ) const

Gets the current I2C bus information.

Returns
A reference to the current bus Info object

◆ close()

virtual void Piduino::I2cDev::close ( )
virtual

Closes the I2C device and releases system resources.

Reimplemented from Piduino::IoDevice.

◆ endTransmission()

bool Piduino::I2cDev::endTransmission ( bool  stop = true)

Ends the current I2C transmission and sends the data if stop is true.

This function sends the data accumulated with write() in the internal buffer only if stop is true. If stop is false, the transmission is not finalized, allowing for further data to be added (e.g. for reading).

Parameters
stopWhether to send a stop condition (default: true)
Returns
true if the transmission was successful, false otherwise

◆ error()

virtual int Piduino::IoDevice::error ( ) const
virtualinherited

Returns the code of the last error.

Returns
The error code.

◆ errorString()

virtual std::string Piduino::IoDevice::errorString ( ) const
virtualinherited

Returns a human-readable description of the last error.

Returns
The error string.

◆ factory()

static std::shared_ptr< I2cDev > Piduino::I2cDev::factory ( int  busId)
static

Factory method that creates a shared pointer to an I2cDev instance.

This method is used to create an I2cDev object for a specific I2C bus ID and shares the instance across the application. It ensures that only one instance per bus ID exists.

Parameters
busIdThe I2C bus ID
Returns
A shared pointer to the created I2cDev object

◆ flush()

virtual void Piduino::I2cDev::flush ( )
virtual

Flushes any pending data in the internal buffers. This ensures all data is sent before returning.

◆ isBuffered()

bool Piduino::IoDevice::isBuffered ( ) const
inherited

Returns true if the device is buffered.

Returns
True if buffered, false otherwise.

◆ isDebug()

bool Piduino::IoDevice::isDebug ( ) const
inherited

Returns true if debug mode is enabled.

Returns
True if debug mode is enabled, false otherwise.

◆ isOpen()

virtual bool Piduino::IoDevice::isOpen ( ) const
virtualinherited

Returns true if the device is currently open.

Returns
True if open, false otherwise.

◆ isReadable()

bool Piduino::IoDevice::isReadable ( ) const
inherited

Returns true if the device is readable.

Returns
True if readable, false otherwise.

◆ isSequential()

virtual bool Piduino::IoDevice::isSequential ( ) const
virtualinherited

Returns true if this device is sequential; otherwise returns false.

Sequential devices, as opposed to random-access devices, have no concept of a start, an end, a size, or a current position, and do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential. Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential.

The IoDevice implementation returns false.

Returns
True if the device is sequential, false otherwise.

◆ isTextModeEnabled()

bool Piduino::IoDevice::isTextModeEnabled ( ) const
inherited

Returns true if text mode is enabled.

Returns
True if text mode is enabled, false otherwise.

◆ isWritable()

bool Piduino::IoDevice::isWritable ( ) const
inherited

Returns true if the device is writable.

Returns
True if writable, false otherwise.

◆ open()

virtual bool Piduino::I2cDev::open ( OpenMode  mode = IoDevice::ReadWrite)
virtual

Opens the I2C device for communication.

Parameters
modeThe opening mode (default: ReadWrite)
Returns
true if the device was successfully opened, false otherwise

Reimplemented from Piduino::IoDevice.

◆ openMode()

OpenMode Piduino::IoDevice::openMode ( ) const
inherited

Returns the current open mode of the device.

Returns
The open mode flags.

◆ peek()

virtual int Piduino::I2cDev::peek ( ) const
virtual

Peeks at the next byte in the buffer without removing it.

Returns
The next byte, or -1 if no data is available

◆ read() [1/2]

virtual int Piduino::I2cDev::read ( )
virtual

Reads a single byte from the internal buffer.

Returns
The byte read, or -1 if no data is available

◆ read() [2/2]

virtual int Piduino::I2cDev::read ( uint8_t *  buffer,
uint16_t  max 
)
virtual

Reads data from the internal buffer into the provided buffer.

Parameters
bufferPointer to the destination buffer
maxMaximum number of bytes to read
Returns
The number of bytes actually read

◆ requestFrom() [1/2]

int Piduino::I2cDev::requestFrom ( int  slave,
int  max,
int  stop = 1 
)
inline

Requests data from an I2C slave device (convenience overload with int parameters).

This function initiates a read transaction from the specified I2C slave device, but does not transfer any data yet, even if the stop parameter is true.

Parameters
slaveThe I2C slave address
maxThe maximum number of bytes to request
stopWhether to send a stop condition (1 = true, 0 = false, default: 1)
Returns
The number of bytes actually received, or -1 on error

◆ requestFrom() [2/2]

int Piduino::I2cDev::requestFrom ( uint16_t  slave,
uint16_t  max,
bool  stop = true 
)

Requests data from an I2C slave device and stores it in the read buffer.

This function initiates a read transaction from the specified I2C slave device, but does not transfer any data yet, even if the stop parameter is true.

Parameters
slaveThe 7-bit I2C slave address
maxThe maximum number of bytes to request
stopWhether to send a stop condition after the transaction (default: true)
Returns
The number of bytes actually received, or -1 on error

◆ setBus() [1/2]

void Piduino::I2cDev::setBus ( const Info bus)

Sets the I2C bus using an Info object.

Parameters
busThe I2C bus information

◆ setBus() [2/2]

void Piduino::I2cDev::setBus ( int  idBus)

Sets the I2C bus using a bus ID.

Parameters
idBusThe I2C bus ID

◆ setBusPath() [1/2]

void Piduino::I2cDev::setBusPath ( const char *  path)

Sets the I2C bus using a device path.

Parameters
pathThe device path (e.g., "/dev/i2c-0")

◆ setBusPath() [2/2]

void Piduino::I2cDev::setBusPath ( const std::string &  path)

Sets the I2C bus using a device path.

Parameters
pathThe device path as a string

◆ setDebug()

void Piduino::IoDevice::setDebug ( bool  enabled)
inherited

Enables or disables debug mode for the device.

Parameters
enabledTrue to enable debug mode, false to disable.

◆ setTextModeEnabled()

void Piduino::IoDevice::setTextModeEnabled ( bool  enabled)
inherited

Enables or disables text mode for the device.

Parameters
enabledTrue to enable text mode, false to disable.

◆ write() [1/8]

int Piduino::I2cDev::write ( const char *  str)
inline

Writes a C-style string to the current I2C transaction buffer.

Parameters
strThe null-terminated string to write
Returns
The number of bytes successfully written

◆ write() [2/8]

virtual int Piduino::I2cDev::write ( const uint8_t *  buffer,
uint16_t  len 
)
virtual

Writes multiple bytes to the current I2C transaction buffer.

Parameters
bufferPointer to the data buffer
lenNumber of bytes to write
Returns
The number of bytes successfully written

◆ write() [3/8]

int Piduino::I2cDev::write ( int  data)
inline

Writes an integer value (cast to uint8_t) to the current I2C transaction buffer.

Parameters
dataThe integer value to write
Returns
1 if successful, 0 if the buffer is full

◆ write() [4/8]

int Piduino::I2cDev::write ( long  data)
inline

Writes a long value (cast to uint8_t) to the current I2C transaction buffer.

Parameters
dataThe long value to write
Returns
1 if successful, 0 if the buffer is full

◆ write() [5/8]

int Piduino::I2cDev::write ( std::string  str)
inline

Writes a string to the current I2C transaction buffer.

Parameters
strThe string to write
Returns
The number of bytes successfully written

◆ write() [6/8]

virtual int Piduino::I2cDev::write ( uint8_t  data)
virtual

Writes a single byte to the current I2C transaction buffer.

Parameters
dataThe byte to write
Returns
1 if successful, 0 if the buffer is full

◆ write() [7/8]

int Piduino::I2cDev::write ( unsigned int  data)
inline

Writes an unsigned integer value (cast to uint8_t) to the current I2C transaction buffer.

Parameters
dataThe unsigned integer value to write
Returns
1 if successful, 0 if the buffer is full

◆ write() [8/8]

int Piduino::I2cDev::write ( unsigned long  data)
inline

Writes an unsigned long value (cast to uint8_t) to the current I2C transaction buffer.

Parameters
dataThe unsigned long value to write
Returns
1 if successful, 0 if the buffer is full

Member Data Documentation

◆ d_ptr

std::unique_ptr<Private> Piduino::IoDevice::d_ptr
protectedinherited

Pointer to the private implementation.

Definition at line 190 of file iodevice.h.