ESPHome 2026.3.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,
25 "PCF8574:\n"
26 " Is PCF8575: %s",
27 YESNO(this->pcf8575_));
28 LOG_I2C_DEVICE(this)
29 if (this->is_failed()) {
30 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
31 }
32}
34 // Read all pins from hardware into input_mask_
35 return this->read_gpio_(); // Return true if I2C read succeeded, false on error
36}
37
38bool PCF8574Component::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
39
40void PCF8574Component::digital_write_hw(uint8_t pin, bool value) {
41 if (value) {
42 this->output_mask_ |= (1 << pin);
43 } else {
44 this->output_mask_ &= ~(1 << pin);
45 }
46 this->write_gpio_();
47}
49 if (flags == gpio::FLAG_INPUT) {
50 // Clear mode mask bit
51 this->mode_mask_ &= ~(1 << pin);
52 // Write GPIO to enable input mode
53 this->write_gpio_();
54 } else if (flags == gpio::FLAG_OUTPUT) {
55 // Set mode mask bit
56 this->mode_mask_ |= 1 << pin;
57 }
58}
60 if (this->is_failed())
61 return false;
62 bool success;
63 uint8_t data[2];
64 if (this->pcf8575_) {
65 success = this->read_bytes_raw(data, 2);
66 this->input_mask_ = (uint16_t(data[1]) << 8) | (uint16_t(data[0]) << 0);
67 } else {
68 success = this->read_bytes_raw(data, 1);
69 this->input_mask_ = data[0];
70 }
71
72 if (!success) {
73 this->status_set_warning();
74 return false;
75 }
77 return true;
78}
80 if (this->is_failed())
81 return false;
82
83 uint16_t value = 0;
84 // Pins in OUTPUT mode and where pin is HIGH.
85 value |= this->mode_mask_ & this->output_mask_;
86 // Pins in INPUT mode must also be set here
87 value |= ~this->mode_mask_;
88
89 uint8_t data[2];
90 data[0] = value;
91 data[1] = value >> 8;
92 if (this->write(data, this->pcf8575_ ? 2 : 1) != i2c::ERROR_OK) {
93 this->status_set_warning();
94 return false;
95 }
96
98 return true;
99}
101
102#ifdef USE_LOOP_PRIORITY
103// Run our loop() method early to invalidate cache before any other components access the pins
104float PCF8574Component::get_loop_priority() const { return 9.0f; } // Just after WIFI
105#endif
106
109bool PCF8574GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
110void PCF8574GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
111size_t PCF8574GPIOPin::dump_summary(char *buffer, size_t len) const {
112 return buf_append_printf(buffer, len, 0, "%u via PCF8574", this->pin_);
113}
114
115} // namespace pcf8574
116} // namespace esphome
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: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:48
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:44
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:46
float get_setup_priority() const override
Definition pcf8574.cpp:100
void digital_write_hw(uint8_t pin, bool value) override
Definition pcf8574.cpp:40
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:48
float get_loop_priority() const override
Definition pcf8574.cpp:104
bool digital_read_cache(uint8_t pin) override
Definition pcf8574.cpp:38
bool pcf8575_
TRUE->16-channel PCF8575, FALSE->8-channel PCF8574.
Definition pcf8574.h:49
bool digital_read_hw(uint8_t pin) override
Definition pcf8574.cpp:33
void digital_write(bool value) override
Definition pcf8574.cpp:110
void pin_mode(gpio::Flags flags) override
Definition pcf8574.cpp:108
size_t dump_summary(char *buffer, size_t len) const override
Definition pcf8574.cpp:111
PCF8574Component * parent_
Definition pcf8574.h:69
uint16_t flags
@ 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:27
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:817