ESPHome 2025.10.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 // Invalidate the cache at the start of each loop
21 this->reset_pin_cache_();
22}
24 ESP_LOGCONFIG(TAG, "PCF8574:");
25 LOG_I2C_DEVICE(this)
26 ESP_LOGCONFIG(TAG, " Is PCF8575: %s", YESNO(this->pcf8575_));
27 if (this->is_failed()) {
28 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
29 }
30}
32 // Read all pins from hardware into input_mask_
33 return this->read_gpio_(); // Return true if I2C read succeeded, false on error
34}
35
36bool PCF8574Component::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
37
38void PCF8574Component::digital_write_hw(uint8_t pin, bool value) {
39 if (value) {
40 this->output_mask_ |= (1 << pin);
41 } else {
42 this->output_mask_ &= ~(1 << pin);
43 }
44 this->write_gpio_();
45}
47 if (flags == gpio::FLAG_INPUT) {
48 // Clear mode mask bit
49 this->mode_mask_ &= ~(1 << pin);
50 // Write GPIO to enable input mode
51 this->write_gpio_();
52 } else if (flags == gpio::FLAG_OUTPUT) {
53 // Set mode mask bit
54 this->mode_mask_ |= 1 << pin;
55 }
56}
58 if (this->is_failed())
59 return false;
60 bool success;
61 uint8_t data[2];
62 if (this->pcf8575_) {
63 success = this->read_bytes_raw(data, 2);
64 this->input_mask_ = (uint16_t(data[1]) << 8) | (uint16_t(data[0]) << 0);
65 } else {
66 success = this->read_bytes_raw(data, 1);
67 this->input_mask_ = data[0];
68 }
69
70 if (!success) {
71 this->status_set_warning();
72 return false;
73 }
75 return true;
76}
78 if (this->is_failed())
79 return false;
80
81 uint16_t value = 0;
82 // Pins in OUTPUT mode and where pin is HIGH.
83 value |= this->mode_mask_ & this->output_mask_;
84 // Pins in INPUT mode must also be set here
85 value |= ~this->mode_mask_;
86
87 uint8_t data[2];
88 data[0] = value;
89 data[1] = value >> 8;
90 if (this->write(data, this->pcf8575_ ? 2 : 1) != i2c::ERROR_OK) {
91 this->status_set_warning();
92 return false;
93 }
94
96 return true;
97}
99
100// Run our loop() method early to invalidate cache before any other components access the pins
101float PCF8574Component::get_loop_priority() const { return 9.0f; } // Just after WIFI
102
105bool PCF8574GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
106void PCF8574GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
107std::string PCF8574GPIOPin::dump_summary() const {
108 char buffer[32];
109 snprintf(buffer, sizeof(buffer), "%u via PCF8574", pin_);
110 return buffer;
111}
112
113} // namespace pcf8574
114} // 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()
bool digital_read(P pin)
Read the state of the given pin.
Definition cached_gpio.h:34
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:184
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:231
uint8_t address_
store the address of the device on the bus
Definition i2c.h:302
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:9
uint16_t mode_mask_
Mask for the pin mode - 1 means output, 0 means input.
Definition pcf8574.h:42
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:98
void digital_write_hw(uint8_t pin, bool value) override
Definition pcf8574.cpp:38
void loop() override
Invalidate cache at start of each loop.
Definition pcf8574.cpp:19
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition pcf8574.cpp:46
float get_loop_priority() const override
Definition pcf8574.cpp:101
bool digital_read_cache(uint8_t pin) override
Definition pcf8574.cpp:36
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:31
void digital_write(bool value) override
Definition pcf8574.cpp:106
void pin_mode(gpio::Flags flags) override
Definition pcf8574.cpp:104
std::string dump_summary() const override
Definition pcf8574.cpp:107
PCF8574Component * parent_
Definition pcf8574.h:67
uint16_t flags
@ 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:33
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:47
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7