ESPHome 2025.5.0
Loading...
Searching...
No Matches
tca9548a.cpp
Go to the documentation of this file.
1#include "tca9548a.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace tca9548a {
6
7static const char *const TAG = "tca9548a";
8
10 auto err = this->parent_->switch_to_channel(channel_);
11 if (err != i2c::ERROR_OK)
12 return err;
13 err = this->parent_->bus_->readv(address, buffers, cnt);
15 return err;
16}
17i2c::ErrorCode TCA9548AChannel::writev(uint8_t address, i2c::WriteBuffer *buffers, size_t cnt, bool stop) {
18 auto err = this->parent_->switch_to_channel(channel_);
19 if (err != i2c::ERROR_OK)
20 return err;
21 err = this->parent_->bus_->writev(address, buffers, cnt, stop);
23 return err;
24}
25
27 ESP_LOGCONFIG(TAG, "Setting up TCA9548A...");
28 uint8_t status = 0;
29 if (this->read(&status, 1) != i2c::ERROR_OK) {
30 ESP_LOGE(TAG, "TCA9548A failed");
31 this->mark_failed();
32 return;
33 }
34 ESP_LOGD(TAG, "Channels currently open: %d", status);
35}
37 ESP_LOGCONFIG(TAG, "TCA9548A:");
38 LOG_I2C_DEVICE(this);
39}
40
42 if (this->is_failed())
44
45 uint8_t channel_val = 1 << channel;
46 return this->write(&channel_val, 1);
47}
48
50 if (this->write(&TCA9548A_DISABLE_CHANNELS_COMMAND, 1) != i2c::ERROR_OK) {
51 ESP_LOGE(TAG, "Failed to disable all channels.");
52 this->status_set_error(); // couldn't disable channels, set error status
53 }
54}
55
56} // namespace tca9548a
57} // namespace esphome
uint8_t address
Definition bl0906.h:4
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_error(const char *message="unspecified")
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.
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt)
Definition i2c_bus.h:80
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:274
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
i2c::ErrorCode writev(uint8_t address, i2c::WriteBuffer *buffers, size_t cnt, bool stop) override
Definition tca9548a.cpp:17
i2c::ErrorCode readv(uint8_t address, i2c::ReadBuffer *buffers, size_t cnt) override
Definition tca9548a.cpp:9
TCA9548AComponent * parent_
Definition tca9548a.h:22
i2c::ErrorCode switch_to_channel(uint8_t channel)
Definition tca9548a.cpp:41
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
Definition i2c_bus.h:17
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
the ReadBuffer structure stores a pointer to a read buffer and its length
Definition i2c_bus.h:24
the WriteBuffer structure stores a pointer to a write buffer and its length
Definition i2c_bus.h:30