PIDUINO
Loading...
Searching...
No Matches
i2cdev.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 This file is part of the Piduino Library.
3
4 The Piduino Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 The Piduino Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16*/
17#pragma once
18
19#include <deque>
20#include <string>
21#include <map>
22#include <memory>
23#include <piduino/system.h>
24#include <piduino/iodevice.h>
25
26namespace Piduino {
27
35 class I2cDev : public IoDevice {
36 public:
37
44 class Info {
45 public:
47 static const int MaxBuses = 32;
48
53 Info (int id = 0);
54
59 inline int id() const;
60
65 inline void setId (int id);
66
71 inline const std::string &path() const;
72
78 bool setPath (const std::string &path);
79
84 inline bool exists() const;
85
91 bool operator== (const Info &other);
92
98 bool operator!= (const Info &other);
99
105 static std::string busPath (int id);
106
111 static Info defaultBus ();
112
117 static std::deque<Info> availableBuses ();
118
119 private:
121 int _id;
123 std::string _path;
124 };
125
131
136 I2cDev (const Info &bus);
137
142 explicit I2cDev (const char *path);
143
148 explicit I2cDev (const std::string &path);
149
154 explicit I2cDev (int idBus);
155
159 virtual ~I2cDev();
160
170 static std::shared_ptr<I2cDev> factory (int busId);
171
177 virtual bool open (OpenMode mode = IoDevice::ReadWrite);
178
182 virtual void close();
183
188 void setBus (const Info &bus);
189
194 void setBus (int idBus);
195
200 const Info &bus() const;
201
206 void setBusPath (const char *path);
207
212 void setBusPath (const std::string &path);
213
220 void beginTransmission (uint16_t slave);
221
231 bool endTransmission (bool stop = true);
232
238 virtual int write (uint8_t data);
239
246 virtual int write (const uint8_t *buffer, uint16_t len);
247
253 inline int write (std::string str);
254
260 inline int write (const char *str);
261
267 inline int write (int data);
268
274 inline int write (unsigned int data);
275
281 inline int write (long data);
282
288 inline int write (unsigned long data);
289
301 int requestFrom (uint16_t slave, uint16_t max, bool stop = true);
302
314 inline int requestFrom (int slave, int max, int stop = 1);
315
320 uint16_t available() const;
321
328 virtual int read (uint8_t *buffer, uint16_t max);
329
334 virtual int read();
335
340 virtual int peek() const;
341
346 virtual void flush ();
347
348 protected:
350 class Private;
351
357
358 private:
361 };
362
363 // ---------------------------------------------------------------------------
364 //
365 // Piduino I2cDev Global Object
366 //
367 // ---------------------------------------------------------------------------
368
370 extern I2cDev Wire;
371
372 #ifndef DOXYGEN
373 // ===========================================================================
374 //
375 // Inline Function Definitions
376 //
377 // ===========================================================================
378
379 // ---------------------------------------------------------------------------
380 // I2cDev::Info
381 // ---------------------------------------------------------------------------
382
383 inline I2cDev::Info::Info (int id) {
384 setId (id);
385 }
386
387 inline int I2cDev::Info::id() const {
388 return _id;
389 }
390
391 inline void I2cDev::Info::setId (int id) {
392 _id = id;
393 _path = busPath (id);
394 }
395
396 inline const std::string &I2cDev::Info::path() const {
397 return _path;
398 }
399
400 inline bool I2cDev::Info::exists() const {
401 return System::charFileExists (_path);
402 }
403
404 inline bool I2cDev::Info::operator== (const Info &other) {
405 return (_path == other._path) ;
406 }
407
408 inline bool I2cDev::Info::operator!= (const Info &other) {
409 return (_path != other._path) ;
410 }
411
412 // ---------------------------------------------------------------------------
413 // I2cDev
414 // ---------------------------------------------------------------------------
415
416 inline int I2cDev::write (std::string str) {
417 return write (reinterpret_cast<const uint8_t *> (str.c_str()), str.length());
418 }
419
420 inline int I2cDev::write (const char *str) {
421 return write (std::string (str));
422 }
423
424 inline int I2cDev::write (int data) {
425 return write (static_cast<uint8_t> (data));
426 }
427
428 inline int I2cDev::write (unsigned int data) {
429 return write (static_cast<uint8_t> (data));
430 }
431
432 inline int I2cDev::write (long data) {
433 return write (static_cast<uint8_t> (data));
434 }
435
436 inline int I2cDev::write (unsigned long data) {
437 return write (static_cast<uint8_t> (data));
438 }
439
440 inline int I2cDev::requestFrom (int slave, int max, int stop) {
441 return requestFrom (static_cast<uint16_t> (slave), static_cast<uint16_t> (max), stop != 0);
442 }
443
444 #endif // DOXYGEN
445
446}
447
448/* ========================================================================== */
A type-safe flags class for bitwise operations on enum values.
Definition flags.h:34
Information class for I2C bus configuration and management.
Definition i2cdev.h:44
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")
Definition i2cdev.h:123
bool operator==(const Info &other)
Equality comparison operator.
int _id
The I2C bus ID.
Definition i2cdev.h:121
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.
Definition i2cdev.h:47
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.
Definition i2cdev.h:35
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.
Definition iodevice.h:37
@ ReadWrite
Open the device for both input and output operations.
Definition iodevice.h:60
static bool charFileExists(const char *path)
Internal implementation class for GpioDevice.
#define PIMP_DECLARE_PRIVATE(Class)
PIMP_DECLARE_PRIVATE.
Definition global.h:82
Global namespace for Piduino.
Definition board.h:28
I2cDev Wire
Global I2cDev object for convenient access to the default I2C bus.