Skip to content

Commit

Permalink
Support I2C transactions with combined reads and writes (esphome#996)
Browse files Browse the repository at this point in the history
* Support I2C transactions with combined reads and writes

* Add optional send_stop parameter to I2CComponent::raw_end_transmission

* Add convenience methods to I2CDevice

* clang-format

Co-authored-by: Jesse Hills <[email protected]>
  • Loading branch information
la7dja and jesserockz authored Nov 6, 2020
1 parent 051a1e4 commit 7221337
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions esphome/components/i2c/i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ void I2CComponent::raw_begin_transmission(uint8_t address) {
ESP_LOGVV(TAG, "Beginning Transmission to 0x%02X:", address);
this->wire_->beginTransmission(address);
}
bool I2CComponent::raw_end_transmission(uint8_t address) {
uint8_t status = this->wire_->endTransmission();
bool I2CComponent::raw_end_transmission(uint8_t address, bool send_stop) {
uint8_t status = this->wire_->endTransmission(send_stop);
ESP_LOGVV(TAG, " Transmission ended. Status code: 0x%02X", status);

switch (status) {
Expand Down
13 changes: 12 additions & 1 deletion esphome/components/i2c/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class I2CComponent : public Component {
void raw_begin_transmission(uint8_t address);

/// End a write transmission to an address, return true if successful.
bool raw_end_transmission(uint8_t address);
bool raw_end_transmission(uint8_t address, bool send_stop = true);

/** Request data from an address with a number of (8-bit) bytes.
*
Expand Down Expand Up @@ -173,6 +173,17 @@ 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_); };

/// 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);
};

/// 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); };

/** Read len amount of bytes from a register into data. Optionally with a conversion time after
* writing the register value to the bus.
*
Expand Down

0 comments on commit 7221337

Please sign in to comment.