ESPHome 2026.6.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::pcf8574 {
5
6static const char *const TAG = "pcf8574";
7
9 if (!this->read_gpio_()) {
10 ESP_LOGE(TAG, "PCF8574 not available under 0x%02X", this->address_);
11 this->mark_failed();
12 return;
13 }
14
15 this->write_gpio_();
16 this->read_gpio_();
17
18 if (this->interrupt_pin_ != nullptr) {
19 this->interrupt_pin_->setup();
21 // Don't invalidate cache on read — only invalidate when interrupt fires
22 this->set_invalidate_on_read_(false);
23 }
24 // Disable loop until an input pin is configured via pin_mode()
25 // For interrupt-driven mode, loop is re-enabled by the ISR
26 // For polling mode, loop is re-enabled when pin_mode() registers an input pin
27 this->disable_loop();
28}
31 // Invalidate the cache so the next digital_read() triggers a fresh I2C read
32 this->reset_pin_cache_();
33 // Only disable the loop once INT has actually gone HIGH. Input transitions that straddle the
34 // I2C read leave INT asserted without re-firing a falling edge, which would strand us with
35 // stale state forever; keep looping until the line is released so we self-heal.
36 if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read()) {
37 this->disable_loop();
38 }
39}
41 ESP_LOGCONFIG(TAG,
42 "PCF8574:\n"
43 " Is PCF8575: %s",
44 YESNO(this->pcf8575_));
45 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
46 LOG_I2C_DEVICE(this)
47 if (this->is_failed()) {
48 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
49 }
50}
52 // Read all pins from hardware into input_mask_
53 return this->read_gpio_(); // Return true if I2C read succeeded, false on error
54}
55
56bool PCF8574Component::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
57
58void PCF8574Component::digital_write_hw(uint8_t pin, bool value) {
59 if (value) {
60 this->output_mask_ |= (1 << pin);
61 } else {
62 this->output_mask_ &= ~(1 << pin);
63 }
64 this->write_gpio_();
65}
67 if (flags == gpio::FLAG_INPUT) {
68 // Clear mode mask bit
69 this->mode_mask_ &= ~(1 << pin);
70 // Write GPIO to enable input mode
71 this->write_gpio_();
72 // Enable polling loop for input pins (not needed for interrupt-driven mode
73 // where the ISR handles re-enabling loop)
74 if (this->interrupt_pin_ == nullptr) {
75 this->enable_loop();
76 }
77 } else if (flags == gpio::FLAG_OUTPUT) {
78 // Set mode mask bit
79 this->mode_mask_ |= 1 << pin;
80 }
81}
83 if (this->is_failed())
84 return false;
85 bool success;
86 uint8_t data[2];
87 if (this->pcf8575_) {
88 success = this->read_bytes_raw(data, 2);
89 this->input_mask_ = (uint16_t(data[1]) << 8) | (uint16_t(data[0]) << 0);
90 } else {
91 success = this->read_bytes_raw(data, 1);
92 this->input_mask_ = data[0];
93 }
94
95 if (!success) {
96 this->status_set_warning();
97 return false;
98 }
100 return true;
101}
103 if (this->is_failed())
104 return false;
105
106 uint16_t value = 0;
107 // Pins in OUTPUT mode and where pin is HIGH.
108 value |= this->mode_mask_ & this->output_mask_;
109 // Pins in INPUT mode must also be set here
110 value |= ~this->mode_mask_;
111
112 uint8_t data[2];
113 data[0] = value;
114 data[1] = value >> 8;
115 if (this->write(data, this->pcf8575_ ? 2 : 1) != i2c::ERROR_OK) {
116 this->status_set_warning();
117 return false;
118 }
119
120 this->status_clear_warning();
121 return true;
122}
124
127bool PCF8574GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
128void PCF8574GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
129size_t PCF8574GPIOPin::dump_summary(char *buffer, size_t len) const {
130 return buf_append_printf(buffer, len, 0, "%u via PCF8574", this->pin_);
131}
132
133} // namespace esphome::pcf8574
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void enable_loop()
Enable this component's loop.
Definition component.h:246
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Definition component.h:289
virtual void setup()=0
virtual bool digital_read()=0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:107
bool digital_read(P pin)
Read the state of the given pin.
Definition cached_gpio.h:37
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:230
uint8_t address_
store the address of the device on the bus
Definition i2c.h:270
uint16_t input_mask_
The state read in read_gpio_ - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:46
void setup() override
Check i2c availability and setup masks.
Definition pcf8574.cpp:8
uint16_t mode_mask_
Mask for the pin mode - 1 means output, 0 means input.
Definition pcf8574.h:42
static void IRAM_ATTR gpio_intr(PCF8574Component *arg)
Definition pcf8574.cpp:29
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:44
float get_setup_priority() const override
Definition pcf8574.cpp:123
void digital_write_hw(uint8_t pin, bool value) override
Definition pcf8574.cpp:58
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition pcf8574.cpp:66
bool digital_read_cache(uint8_t pin) override
Definition pcf8574.cpp:56
InternalGPIOPin * interrupt_pin_
Definition pcf8574.h:48
bool pcf8575_
TRUE->16-channel PCF8575, FALSE->8-channel PCF8574.
Definition pcf8574.h:47
bool digital_read_hw(uint8_t pin) override
Definition pcf8574.cpp:51
void digital_write(bool value) override
Definition pcf8574.cpp:128
void pin_mode(gpio::Flags flags) override
Definition pcf8574.cpp:126
size_t dump_summary(char *buffer, size_t len) const override
Definition pcf8574.cpp:129
PCF8574Component * parent_
Definition pcf8574.h:68
uint16_t flags
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:51
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_INPUT
Definition gpio.h:27
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:39
const void size_t len
Definition hal.h:64