ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
i2c_bus.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <cstdint>
4#include <utility>
5#include <vector>
6
8
9namespace esphome::i2c {
10
23
29class I2CBus {
30 public:
31 virtual ~I2CBus() = default;
32
43 virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
44 size_t read_count) = 0;
45
46 // Legacy functions for compatibility
47
48 ErrorCode read(uint8_t address, uint8_t *buffer, size_t len) {
49 return this->write_readv(address, nullptr, 0, buffer, len);
50 }
51
52 ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop = true) {
53 return this->write_readv(address, buffer, len, nullptr, 0);
54 }
55
56 protected:
59 void i2c_scan_();
60 std::vector<std::pair<uint8_t, bool>> scan_results_;
61 bool scan_{false};
62};
63
64class InternalI2CBus : public I2CBus {
65 public:
68 virtual int get_port() const = 0;
69};
70
71} // namespace esphome::i2c
uint8_t address
Definition bl0906.h:4
This Class provides the methods to read and write bytes from an I2CBus.
Definition i2c_bus.h:29
virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)=0
This virtual method writes bytes to an I2CBus from an array, then reads bytes into an array of ReadBu...
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:61
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition i2c_bus.h:60
virtual ~I2CBus()=default
ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop=true)
Definition i2c_bus.h:52
void i2c_scan_()
Scans the I2C bus for devices.
Definition i2c.cpp:12
ErrorCode read(uint8_t address, uint8_t *buffer, size_t len)
Definition i2c_bus.h:48
virtual int get_port() const =0
Returns the I2C port number.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_CRC
bytes received with a CRC error
Definition i2c_bus.h:21
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
@ ERROR_TOO_LARGE
requested a transfer larger than buffers can hold
Definition i2c_bus.h:19
@ ERROR_INVALID_ARGUMENT
method called invalid argument(s)
Definition i2c_bus.h:15
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:13
@ ERROR_TIMEOUT
timeout while waiting to receive bytes
Definition i2c_bus.h:17
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
Definition i2c_bus.h:16
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
Definition i2c_bus.h:18
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:20
std::string size_t len
Definition helpers.h:817