ESPHome 2025.6.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
i2c_bus.h
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <cstddef>
4#include <utility>
5#include <vector>
6
7namespace esphome {
8namespace i2c {
9
22
24struct ReadBuffer {
25 uint8_t *data;
26 size_t len;
27};
28
31 const uint8_t *data;
32 size_t len;
33};
34
40class I2CBus {
41 public:
47 virtual ErrorCode read(uint8_t address, uint8_t *buffer, size_t len) {
48 ReadBuffer buf;
49 buf.data = buffer;
50 buf.len = len;
51 return readv(address, &buf, 1);
52 }
53
60 virtual ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t count) = 0;
61
62 virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len) {
63 return write(address, buffer, len, true);
64 }
65
73 virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop) {
74 WriteBuffer buf;
75 buf.data = buffer;
76 buf.len = len;
77 return writev(address, &buf, 1, stop);
78 }
79
80 virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) {
81 return writev(address, buffers, cnt, true);
82 }
83
92 virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t count, bool stop) = 0;
93
94 protected:
97 void i2c_scan_() {
98 for (uint8_t address = 8; address < 120; address++) {
99 auto err = writev(address, nullptr, 0);
100 if (err == ERROR_OK) {
101 scan_results_.emplace_back(address, true);
102 } else if (err == ERROR_UNKNOWN) {
103 scan_results_.emplace_back(address, false);
104 }
105 }
106 }
107 std::vector<std::pair<uint8_t, bool>> scan_results_;
108 bool scan_{false};
109};
110
111} // namespace i2c
112} // namespace esphome
uint8_t address
Definition bl0906.h:4
This Class provides the methods to read and write bytes from an I2CBus.
Definition i2c_bus.h:40
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:108
virtual ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t count)=0
This virtual method reads bytes from an I2CBus into an array of ReadBuffer.
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition i2c_bus.h:107
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt)
Definition i2c_bus.h:80
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t count, bool stop)=0
This virtual method writes bytes to an I2CBus from an array of WriteBuffer.
virtual ErrorCode read(uint8_t address, uint8_t *buffer, size_t len)
Creates a ReadBuffer and calls the virtual readv() method to read bytes into this buffer.
Definition i2c_bus.h:47
virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len)
Definition i2c_bus.h:62
void i2c_scan_()
Scans the I2C bus for devices.
Definition i2c_bus.h:97
virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop)
Creates a WriteBuffer and calls the writev() method to send the bytes from this buffer.
Definition i2c_bus.h:73
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_CRC
bytes received with a CRC error
Definition i2c_bus.h:20
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
@ ERROR_TOO_LARGE
requested a transfer larger than buffers can hold
Definition i2c_bus.h:18
@ ERROR_INVALID_ARGUMENT
method called invalid argument(s)
Definition i2c_bus.h:14
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:12
@ ERROR_TIMEOUT
timeout while waiting to receive bytes
Definition i2c_bus.h:16
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
Definition i2c_bus.h:15
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
Definition i2c_bus.h:17
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:302
the ReadBuffer structure stores a pointer to a read buffer and its length
Definition i2c_bus.h:24
size_t len
length of the buffer
Definition i2c_bus.h:26
uint8_t * data
pointer to the read buffer
Definition i2c_bus.h:25
the WriteBuffer structure stores a pointer to a write buffer and its length
Definition i2c_bus.h:30
size_t len
length of the buffer
Definition i2c_bus.h:32
const uint8_t * data
pointer to the write buffer
Definition i2c_bus.h:31