ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
pcf8574.cpp
Go to the documentation of this file.
1#include "pcf8574.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace pcf8574 {
6
7static const char *const TAG = "pcf8574";
8
10 if (!this->read_gpio_()) {
11 ESP_LOGE(TAG, "PCF8574 not available under 0x%02X", this->address_);
12 this->mark_failed();
13 return;
14 }
15
16 this->write_gpio_();
17 this->read_gpio_();
18}
20 ESP_LOGCONFIG(TAG, "PCF8574:");
21 LOG_I2C_DEVICE(this)
22 ESP_LOGCONFIG(TAG, " Is PCF8575: %s", YESNO(this->pcf8575_));
23 if (this->is_failed()) {
24 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
25 }
26}
28 this->read_gpio_();
29 return this->input_mask_ & (1 << pin);
30}
31void PCF8574Component::digital_write(uint8_t pin, bool value) {
32 if (value) {
33 this->output_mask_ |= (1 << pin);
34 } else {
35 this->output_mask_ &= ~(1 << pin);
36 }
37
38 this->write_gpio_();
39}
40void PCF8574Component::pin_mode(uint8_t pin, gpio::Flags flags) {
41 if (flags == gpio::FLAG_INPUT) {
42 // Clear mode mask bit
43 this->mode_mask_ &= ~(1 << pin);
44 // Write GPIO to enable input mode
45 this->write_gpio_();
46 } else if (flags == gpio::FLAG_OUTPUT) {
47 // Set mode mask bit
48 this->mode_mask_ |= 1 << pin;
49 }
50}
52 if (this->is_failed())
53 return false;
54 bool success;
55 uint8_t data[2];
56 if (this->pcf8575_) {
57 success = this->read_bytes_raw(data, 2);
58 this->input_mask_ = (uint16_t(data[1]) << 8) | (uint16_t(data[0]) << 0);
59 } else {
60 success = this->read_bytes_raw(data, 1);
61 this->input_mask_ = data[0];
62 }
63
64 if (!success) {
65 this->status_set_warning();
66 return false;
67 }
69 return true;
70}
72 if (this->is_failed())
73 return false;
74
75 uint16_t value = 0;
76 // Pins in OUTPUT mode and where pin is HIGH.
77 value |= this->mode_mask_ & this->output_mask_;
78 // Pins in INPUT mode must also be set here
79 value |= ~this->mode_mask_;
80
81 uint8_t data[2];
82 data[0] = value;
83 data[1] = value >> 8;
84 if (this->write(data, this->pcf8575_ ? 2 : 1) != i2c::ERROR_OK) {
85 this->status_set_warning();
86 return false;
87 }
88
90 return true;
91}
93
95void PCF8574GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
96bool PCF8574GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
97void PCF8574GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
98std::string PCF8574GPIOPin::dump_summary() const {
99 char buffer[32];
100 snprintf(buffer, sizeof(buffer), "%u via PCF8574", pin_);
101 return buffer;
102}
103
104} // namespace pcf8574
105} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
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
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:229
uint8_t address_
store the address of the device on the bus
Definition i2c.h:273
uint16_t input_mask_
The state read in read_gpio_ - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:39
void setup() override
Check i2c availability and setup masks.
Definition pcf8574.cpp:9
uint16_t mode_mask_
Mask for the pin mode - 1 means output, 0 means input.
Definition pcf8574.h:35
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:37
float get_setup_priority() const override
Definition pcf8574.cpp:92
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition pcf8574.cpp:40
bool digital_read(uint8_t pin)
Helper function to read the value of a pin.
Definition pcf8574.cpp:27
void digital_write(uint8_t pin, bool value)
Helper function to write the value of a pin.
Definition pcf8574.cpp:31
bool pcf8575_
TRUE->16-channel PCF8575, FALSE->8-channel PCF8574.
Definition pcf8574.h:40
void digital_write(bool value) override
Definition pcf8574.cpp:97
void pin_mode(gpio::Flags flags) override
Definition pcf8574.cpp:95
std::string dump_summary() const override
Definition pcf8574.cpp:98
PCF8574Component * parent_
Definition pcf8574.h:60
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_INPUT
Definition gpio.h:18
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:48
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7