1+ /* *****************************************************************************
2+ * File: i2c_bus.h
3+ * Author: Daniel Knezevic
4+ * Year: 2025
5+ * Brief: Object-oriented wrapper around the ESP-IDF I2C master driver
6+ ******************************************************************************/
7+
8+ #ifndef i2c_bus_h
9+ #define i2c_bus_h
10+
11+ #include " driver/i2c_master.h"
12+ #include " esp_err.h"
13+
14+ class I2cBus {
15+ public:
16+ /* *
17+ * @brief Construct a new I2cBus instance.
18+ *
19+ * @param port I2C hardware port number (e.g., I2C_NUM_0 or I2C_NUM_1).
20+ * @param sda GPIO pin number used for the SDA line.
21+ * @param scl GPIO pin number used for the SCL line.
22+ */
23+ I2cBus (i2c_port_t port, gpio_num_t sda, gpio_num_t scl);
24+
25+ /* *
26+ * @brief Destructor for the I2cBus.
27+ *
28+ * Automatically deletes the I2C master bus if it was initialized.
29+ */
30+ ~I2cBus ();
31+
32+ /* *
33+ * @brief Initialize the I2C master bus.
34+ *
35+ * Configures the I2C master bus with internal pull-ups and default clock
36+ * source. Must be called before adding devices or performing transactions.
37+ *
38+ * @return
39+ * - ESP_OK on success
40+ * - ESP_ERR_INVALID_ARG if parameters are invalid
41+ * - ESP_ERR_NO_MEM if allocation fails
42+ * - Other error codes from the ESP-IDF I2C driver
43+ */
44+ esp_err_t initialize ();
45+
46+ /* *
47+ * @brief Add a device to the I2C bus.
48+ *
49+ * Each device can use its own SCL frequency. The returned device handle
50+ * can be used in subsequent read/write operations.
51+ *
52+ * @param address 7-bit I2C device address.
53+ * @param freq SCL clock frequency in Hz (e.g., 100000 or 400000).
54+ * @param dev Pointer to a handle that receives the device handle.
55+ * @return
56+ * - ESP_OK on success
57+ * - ESP_ERR_INVALID_ARG or ESP_FAIL on failure
58+ */
59+ esp_err_t addDevice (uint8_t address, uint32_t freq,
60+ i2c_master_dev_handle_t * dev);
61+
62+ /* *
63+ * @brief Write a data buffer to an I2C device.
64+ *
65+ * @param dev Device handle obtained from addDevice().
66+ * @param data Pointer to data buffer to transmit.
67+ * @param len Number of bytes to send.
68+ * @param timeout_ms Timeout for the operation, in milliseconds.
69+ * @return
70+ * - ESP_OK on success
71+ * - ESP_FAIL, ESP_ERR_TIMEOUT, or ESP_ERR_INVALID_STATE on failure
72+ */
73+ esp_err_t write (i2c_master_dev_handle_t dev, const uint8_t * data,
74+ size_t len, uint32_t timeout_ms = 1000 );
75+
76+ /* *
77+ * @brief Read data from an I2C device, optionally after writing a register
78+ * address.
79+ *
80+ * Performs a combined write-read transaction if @p reg_len > 0.
81+ *
82+ * @param dev Device handle obtained from addDevice().
83+ * @param reg Optional pointer to the register address buffer (can be
84+ * nullptr).
85+ * @param reg_len Length of the register address buffer in bytes (0 for no
86+ * write phase).
87+ * @param data Pointer to buffer for storing received data.
88+ * @param data_len Number of bytes to read.
89+ * @param timeout_ms Timeout for the operation, in milliseconds.
90+ * @return
91+ * - ESP_OK on success
92+ * - ESP_FAIL, ESP_ERR_TIMEOUT, or ESP_ERR_INVALID_STATE on failure
93+ */
94+ esp_err_t read (i2c_master_dev_handle_t dev, const uint8_t * reg,
95+ size_t reg_len, uint8_t * data, size_t data_len,
96+ uint32_t timeout_ms = 1000 );
97+
98+ private:
99+ i2c_port_t mPort ;
100+ gpio_num_t mSda ;
101+ gpio_num_t mScl ;
102+ i2c_master_bus_handle_t mBus ;
103+ };
104+
105+ #endif // i2c_bus_h
0 commit comments