ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pcf8574.cpp
Go to the documentation of this file.
1#include "pcf8574.h"
3#include "esphome/core/log.h"
4
5namespace esphome::pcf8574 {
6
7ESPHOME_LOG_TAG(TAG, "pcf8574");
8
10 if (!this->read_gpio_()) {
11 ESP_LOGE(TAG, "PCF8574 not available under 0x%02X", this->address_);
12 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
13 return;
14 }
15
16 if (this->reset_) {
17 this->write_gpio_();
18 this->read_gpio_();
19 } else {
20 this->output_mask_ = this->input_mask_;
21 this->mode_mask_ = ~this->configured_input_pins_;
22 }
23
25}
27 ESP_LOGCONFIG(TAG,
28 "PCF8574:\n"
29 " Is PCF8575: %s\n"
30 " Reset: %s",
31 YESNO(this->pcf8575_), YESNO(this->reset_));
32 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
33 LOG_I2C_DEVICE(this)
34}
36 // Read all pins from hardware into input_mask_
37 return this->read_gpio_(); // Return true if I2C read succeeded, false on error
38}
39
40bool PCF8574Component::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
41
42void PCF8574Component::digital_write_hw(uint8_t pin, bool value) {
43 assign_bits(this->output_mask_, 1 << pin, value);
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_();
53 } else if (flags == gpio::FLAG_OUTPUT) {
54 // Set mode mask bit
55 this->mode_mask_ |= 1 << pin;
56 }
57}
59 if (this->is_failed())
60 return false;
61 uint8_t data[2]{}; // Keep the bytes zero for the PCF8574's one-byte read.
62 if (!this->status_comm_result(this->read_bytes_raw(data, this->pcf8575_ ? 2 : 1)))
63 return false;
65 return true;
66}
68 if (this->is_failed())
69 return false;
70
71 uint16_t value = 0;
72 // Pins in OUTPUT mode and where pin is HIGH.
73 value |= this->mode_mask_ & this->output_mask_;
74 // Pins in INPUT mode must also be set here
75 value |= ~this->mode_mask_;
76
77 uint8_t data[2];
78 data[0] = value;
79 data[1] = value >> 8;
80 return this->status_comm_result(this->write(data, this->pcf8575_ ? 2 : 1) == i2c::ERROR_OK);
81}
82
83} // namespace esphome::pcf8574
84
85ESPHOME_EXPANDER_PIN_SUMMARY(esphome::pcf8574::PCF8574Component, "%u via PCF8574")
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void enable_polling_loop_()
Call from pin_mode() for an input pin: without an interrupt pin the loop polls instead.
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:262
uint8_t address_
store the address of the device on the bus
Definition i2c.h:327
uint16_t input_mask_
The state read in read_gpio_ - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:42
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:38
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:40
void digital_write_hw(uint8_t pin, bool value) override
Definition pcf8574.cpp:42
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition pcf8574.cpp:46
bool digital_read_cache(uint8_t pin) override
Definition pcf8574.cpp:40
bool pcf8575_
TRUE->16-channel PCF8575, FALSE->8-channel PCF8574.
Definition pcf8574.h:48
bool digital_read_hw(uint8_t pin) override
Definition pcf8574.cpp:35
uint16_t flags
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "pcf8574")
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1072
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1039