![]() |
PIDUINO
|
I2C device interface for communicating with I2C peripheral devices. More...
#include <i2cdev.h>
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< OpenModeFlag > | OpenMode |
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 Info & | bus () 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< I2cDev > | factory (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< Private > | d_ptr |
Pointer to the private implementation. | |
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.
|
inherited |
Type representing a combination of OpenModeFlag values.
Definition at line 70 of file iodevice.h.
|
inherited |
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.
Definition at line 48 of file iodevice.h.
Piduino::I2cDev::I2cDev | ( | ) |
Piduino::I2cDev::I2cDev | ( | const Info & | bus | ) |
Constructs an I2cDev object for the specified I2C bus.
bus | The I2C bus information |
|
explicit |
Constructs an I2cDev object using a device path.
path | The device path (e.g., "/dev/i2c-0") |
|
explicit |
Constructs an I2cDev object using a device path.
path | The device path as a string |
|
explicit |
Constructs an I2cDev object for the specified bus ID.
idBus | The I2C bus ID |
|
virtual |
Virtual destructor that properly cleans up the I2C device.
|
protected |
Protected constructor for derived classes using the PIMPL pattern.
dd | Reference to the private implementation |
uint16_t Piduino::I2cDev::available | ( | ) | const |
Gets the number of bytes available for reading from the internal buffer.
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.
slave | The 7-bit I2C slave address |
const Info & Piduino::I2cDev::bus | ( | ) | const |
Gets the current I2C bus information.
|
virtual |
Closes the I2C device and releases system resources.
Reimplemented from Piduino::IoDevice.
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).
stop | Whether to send a stop condition (default: true) |
|
virtualinherited |
Returns the code of the last error.
|
virtualinherited |
Returns a human-readable description of the last error.
|
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.
busId | The I2C bus ID |
|
virtual |
Flushes any pending data in the internal buffers. This ensures all data is sent before returning.
|
inherited |
Returns true if the device is buffered.
|
inherited |
Returns true if debug mode is enabled.
|
virtualinherited |
Returns true if the device is currently open.
|
inherited |
Returns true if the device is readable.
|
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.
|
inherited |
Returns true if text mode is enabled.
|
inherited |
Returns true if the device is writable.
|
virtual |
Opens the I2C device for communication.
mode | The opening mode (default: ReadWrite) |
Reimplemented from Piduino::IoDevice.
|
inherited |
Returns the current open mode of the device.
|
virtual |
Peeks at the next byte in the buffer without removing it.
|
virtual |
Reads a single byte from the internal buffer.
|
virtual |
Reads data from the internal buffer into the provided buffer.
buffer | Pointer to the destination buffer |
max | Maximum number of bytes to read |
|
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.
slave | The I2C slave address |
max | The maximum number of bytes to request |
stop | Whether to send a stop condition (1 = true, 0 = false, default: 1) |
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.
slave | The 7-bit I2C slave address |
max | The maximum number of bytes to request |
stop | Whether to send a stop condition after the transaction (default: true) |
void Piduino::I2cDev::setBus | ( | const Info & | bus | ) |
Sets the I2C bus using an Info object.
bus | The I2C bus information |
void Piduino::I2cDev::setBus | ( | int | idBus | ) |
Sets the I2C bus using a bus ID.
idBus | The I2C bus ID |
void Piduino::I2cDev::setBusPath | ( | const char * | path | ) |
Sets the I2C bus using a device path.
path | The device path (e.g., "/dev/i2c-0") |
void Piduino::I2cDev::setBusPath | ( | const std::string & | path | ) |
Sets the I2C bus using a device path.
path | The device path as a string |
|
inherited |
Enables or disables debug mode for the device.
enabled | True to enable debug mode, false to disable. |
|
inherited |
Enables or disables text mode for the device.
enabled | True to enable text mode, false to disable. |
|
inline |
Writes a C-style string to the current I2C transaction buffer.
str | The null-terminated string to write |
|
virtual |
Writes multiple bytes to the current I2C transaction buffer.
buffer | Pointer to the data buffer |
len | Number of bytes to write |
|
inline |
Writes an integer value (cast to uint8_t) to the current I2C transaction buffer.
data | The integer value to write |
|
inline |
Writes a long value (cast to uint8_t) to the current I2C transaction buffer.
data | The long value to write |
|
inline |
Writes a string to the current I2C transaction buffer.
str | The string to write |
|
virtual |
Writes a single byte to the current I2C transaction buffer.
data | The byte to write |
|
inline |
Writes an unsigned integer value (cast to uint8_t) to the current I2C transaction buffer.
data | The unsigned integer value to write |
|
inline |
Writes an unsigned long value (cast to uint8_t) to the current I2C transaction buffer.
data | The unsigned long value to write |
|
protectedinherited |
Pointer to the private implementation.
Definition at line 190 of file iodevice.h.