From 56974153f1106c0b14b52cec08ea9d535fd4d3c4 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Tue, 1 Jun 2021 00:36:25 +0200 Subject: [PATCH] I2c raw cmds with multiplexer (#1817) Co-authored-by: Maurice Makaay --- esphome/components/i2c/i2c.cpp | 30 +++++++++++++++++++ esphome/components/i2c/i2c.h | 14 ++++----- esphome/components/mcp4725/mcp4725.cpp | 4 +-- .../components/ssd1306_i2c/ssd1306_i2c.cpp | 4 +-- .../components/ssd1327_i2c/ssd1327_i2c.cpp | 4 +-- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/esphome/components/i2c/i2c.cpp b/esphome/components/i2c/i2c.cpp index e9a7e72932..2111711264 100644 --- a/esphome/components/i2c/i2c.cpp +++ b/esphome/components/i2c/i2c.cpp @@ -193,12 +193,36 @@ void I2CDevice::check_multiplexer_() { } #endif +void I2CDevice::raw_begin_transmission() { // NOLINT +#ifdef USE_I2C_MULTIPLEXER + this->check_multiplexer_(); +#endif + this->parent_->raw_begin_transmission(this->address_); +} +bool I2CDevice::raw_end_transmission(bool send_stop) { // NOLINT +#ifdef USE_I2C_MULTIPLEXER + this->check_multiplexer_(); +#endif + return this->parent_->raw_end_transmission(this->address_, send_stop); +} +void I2CDevice::raw_write(const uint8_t *data, uint8_t len) { // NOLINT +#ifdef USE_I2C_MULTIPLEXER + this->check_multiplexer_(); +#endif + this->parent_->raw_write(this->address_, data, len); +} bool I2CDevice::read_bytes(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion) { // NOLINT #ifdef USE_I2C_MULTIPLEXER this->check_multiplexer_(); #endif return this->parent_->read_bytes(this->address_, a_register, data, len, conversion); } +bool I2CDevice::read_bytes_raw(uint8_t *data, uint8_t len) { // NOLINT +#ifdef USE_I2C_MULTIPLEXER + this->check_multiplexer_(); +#endif + return this->parent_->read_bytes_raw(this->address_, data, len); +} bool I2CDevice::read_byte(uint8_t a_register, uint8_t *data, uint32_t conversion) { // NOLINT #ifdef USE_I2C_MULTIPLEXER this->check_multiplexer_(); @@ -211,6 +235,12 @@ bool I2CDevice::write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len #endif return this->parent_->write_bytes(this->address_, a_register, data, len); } +bool I2CDevice::write_bytes_raw(const uint8_t *data, uint8_t len) { // NOLINT +#ifdef USE_I2C_MULTIPLEXER + this->check_multiplexer_(); +#endif + return this->parent_->write_bytes_raw(this->address_, data, len); +} bool I2CDevice::write_byte(uint8_t a_register, uint8_t data) { // NOLINT #ifdef USE_I2C_MULTIPLEXER this->check_multiplexer_(); diff --git a/esphome/components/i2c/i2c.h b/esphome/components/i2c/i2c.h index 56da64c218..da791ec633 100644 --- a/esphome/components/i2c/i2c.h +++ b/esphome/components/i2c/i2c.h @@ -178,15 +178,13 @@ class I2CDevice { I2CRegister reg(uint8_t a_register) { return {this, a_register}; } /// Begin a write transmission. - void raw_begin_transmission() { this->parent_->raw_begin_transmission(this->address_); }; + void raw_begin_transmission(); /// End a write transmission, return true if successful. - bool raw_end_transmission(bool send_stop = true) { - return this->parent_->raw_end_transmission(this->address_, send_stop); - }; + bool raw_end_transmission(bool send_stop = true); /// Write len amount of bytes from data. begin_transmission_ must be called before this. - void raw_write(const uint8_t *data, uint8_t len) { this->parent_->raw_write(this->address_, data, len); }; + void raw_write(const uint8_t *data, uint8_t len); /** Read len amount of bytes from a register into data. Optionally with a conversion time after * writing the register value to the bus. @@ -198,7 +196,7 @@ class I2CDevice { * @return If the operation was successful. */ bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion = 0); - bool read_bytes_raw(uint8_t *data, uint8_t len) { return this->parent_->read_bytes_raw(this->address_, data, len); } + bool read_bytes_raw(uint8_t *data, uint8_t len); template optional> read_bytes(uint8_t a_register) { std::array res; @@ -246,9 +244,7 @@ class I2CDevice { * @return If the operation was successful. */ bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len); - bool write_bytes_raw(const uint8_t *data, uint8_t len) { - return this->parent_->write_bytes_raw(this->address_, data, len); - } + bool write_bytes_raw(const uint8_t *data, uint8_t len); /** Write a vector of data to a register. * diff --git a/esphome/components/mcp4725/mcp4725.cpp b/esphome/components/mcp4725/mcp4725.cpp index 85028d2f39..37ae7ac2b8 100644 --- a/esphome/components/mcp4725/mcp4725.cpp +++ b/esphome/components/mcp4725/mcp4725.cpp @@ -9,9 +9,9 @@ static const char *TAG = "mcp4725"; void MCP4725::setup() { ESP_LOGCONFIG(TAG, "Setting up MCP4725 (0x%02X)...", this->address_); - this->parent_->raw_begin_transmission(this->address_); + this->raw_begin_transmission(); - if (!this->parent_->raw_end_transmission(this->address_)) { + if (!this->raw_end_transmission()) { this->error_code_ = COMMUNICATION_FAILED; this->mark_failed(); diff --git a/esphome/components/ssd1306_i2c/ssd1306_i2c.cpp b/esphome/components/ssd1306_i2c/ssd1306_i2c.cpp index 34a963e532..8c535094ac 100644 --- a/esphome/components/ssd1306_i2c/ssd1306_i2c.cpp +++ b/esphome/components/ssd1306_i2c/ssd1306_i2c.cpp @@ -10,8 +10,8 @@ void I2CSSD1306::setup() { ESP_LOGCONFIG(TAG, "Setting up I2C SSD1306..."); this->init_reset_(); - this->parent_->raw_begin_transmission(this->address_); - if (!this->parent_->raw_end_transmission(this->address_)) { + this->raw_begin_transmission(); + if (!this->raw_end_transmission()) { this->error_code_ = COMMUNICATION_FAILED; this->mark_failed(); return; diff --git a/esphome/components/ssd1327_i2c/ssd1327_i2c.cpp b/esphome/components/ssd1327_i2c/ssd1327_i2c.cpp index f256c9df77..16cada0ef2 100644 --- a/esphome/components/ssd1327_i2c/ssd1327_i2c.cpp +++ b/esphome/components/ssd1327_i2c/ssd1327_i2c.cpp @@ -10,8 +10,8 @@ void I2CSSD1327::setup() { ESP_LOGCONFIG(TAG, "Setting up I2C SSD1327..."); this->init_reset_(); - this->parent_->raw_begin_transmission(this->address_); - if (!this->parent_->raw_end_transmission(this->address_)) { + this->raw_begin_transmission(); + if (!this->raw_end_transmission()) { this->error_code_ = COMMUNICATION_FAILED; this->mark_failed(); return;