23#include <piduino/system.h>
24#include <piduino/iodevice.h>
59 inline int id()
const;
71 inline const std::string &
path()
const;
148 explicit I2cDev (
const std::string &path);
170 static std::shared_ptr<I2cDev>
factory (
int busId);
246 virtual int write (
const uint8_t *buffer, uint16_t len);
274 inline int write (
unsigned int data);
288 inline int write (
unsigned long data);
328 virtual int read (uint8_t *buffer, uint16_t max);
393 _path = busPath (
id);
405 return (_path == other._path) ;
409 return (_path != other._path) ;
417 return write (
reinterpret_cast<const uint8_t *
> (str.c_str()), str.length());
421 return write (std::string (str));
425 return write (
static_cast<uint8_t
> (data));
429 return write (
static_cast<uint8_t
> (data));
433 return write (
static_cast<uint8_t
> (data));
437 return write (
static_cast<uint8_t
> (data));
441 return requestFrom (
static_cast<uint16_t
> (slave),
static_cast<uint16_t
> (max), stop != 0);
A type-safe flags class for bitwise operations on enum values.
Information class for I2C bus configuration and management.
static std::string busPath(int id)
Generates the device path for a given I2C bus ID.
bool exists() const
Checks if the I2C bus device file exists on the system.
std::string _path
The device path (e.g., "/dev/i2c-0")
bool operator==(const Info &other)
Equality comparison operator.
int id() const
Gets the current I2C bus ID.
Info(int id=0)
Constructs an Info object with the specified bus ID.
void setId(int id)
Sets the I2C bus ID and updates the corresponding device path.
static std::deque< Info > availableBuses()
Returns a list of available I2C buses on the system.
const std::string & path() const
Gets the device path for this I2C bus.
bool setPath(const std::string &path)
Sets the device path and extracts the corresponding bus ID.
static const int MaxBuses
Maximum number of I2C buses supported by the system.
bool operator!=(const Info &other)
Inequality comparison operator.
static Info defaultBus()
Gets information about the default I2C bus on the system.
I2C device interface for communicating with I2C peripheral devices.
I2cDev(const std::string &path)
Constructs an I2cDev object using a device path.
virtual int write(uint8_t data)
Writes a single byte to the current I2C transaction buffer.
virtual int read(uint8_t *buffer, uint16_t max)
Reads data from the internal buffer into the provided buffer.
int write(const char *str)
Writes a C-style string to the current I2C transaction buffer.
virtual void close()
Closes the I2C device and releases system resources.
I2cDev(const char *path)
Constructs an I2cDev object using a device path.
virtual int read()
Reads a single byte from the internal buffer.
static std::shared_ptr< I2cDev > factory(int busId)
Factory method that creates a shared pointer to an I2cDev instance.
I2cDev(const Info &bus)
Constructs an I2cDev object for the specified I2C bus.
virtual bool open(OpenMode mode=IoDevice::ReadWrite)
Opens the I2C device for communication.
uint16_t available() const
Gets the number of bytes available for reading from the internal buffer.
virtual void flush()
Flushes any pending data in the internal buffers. This ensures all data is sent before returning.
I2cDev()
Default constructor that creates an I2cDev object without specifying a bus. The bus must be set later...
I2cDev(Private &dd)
Protected constructor for derived classes using the PIMPL pattern.
virtual int write(const uint8_t *buffer, uint16_t len)
Writes multiple bytes to the current I2C transaction buffer.
virtual ~I2cDev()
Virtual destructor that properly cleans up the I2C device.
void setBusPath(const std::string &path)
Sets the I2C bus using a device path.
void setBus(const Info &bus)
Sets the I2C bus using an Info object.
void setBusPath(const char *path)
Sets the I2C bus using a device path.
const Info & bus() const
Gets the current I2C bus information.
virtual int peek() const
Peeks at the next byte in the buffer without removing it.
int write(std::string str)
Writes a string to the current I2C transaction buffer.
void beginTransmission(uint16_t slave)
Begins an I2C transmission to the specified slave device. This must be called before writing data to ...
int write(unsigned long data)
Writes an unsigned long value (cast to uint8_t) to the current I2C transaction buffer.
I2cDev(int idBus)
Constructs an I2cDev object for the specified bus ID.
bool endTransmission(bool stop=true)
Ends the current I2C transmission and sends the data if stop is true.
void setBus(int idBus)
Sets the I2C bus using a bus ID.
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).
int write(long data)
Writes a long value (cast to uint8_t) 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.
Abstract base class for input/output devices.
@ ReadWrite
Open the device for both input and output operations.
static bool charFileExists(const char *path)
Internal implementation class for GpioDevice.
#define PIMP_DECLARE_PRIVATE(Class)
PIMP_DECLARE_PRIVATE.
Global namespace for Piduino.
I2cDev Wire
Global I2cDev object for convenient access to the default I2C bus.