ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
pcf8574.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
7
8namespace esphome {
9namespace pcf8574 {
10
11// PCF8574(8 pins)/PCF8575(16 pins) always read/write all pins in a single I2C transaction
12// so we use uint16_t as bank type to ensure all pins are in one bank and cached together
14 public i2c::I2CDevice,
15 public gpio_expander::CachedGpioExpander<uint16_t, 16> {
16 public:
17 PCF8574Component() = default;
18
19 void set_pcf8575(bool pcf8575) { pcf8575_ = pcf8575; }
21
23 void setup() override;
24 void loop() override;
26 void pin_mode(uint8_t pin, gpio::Flags flags);
27
28 float get_setup_priority() const override;
29
30 void dump_config() override;
31
32 protected:
33 static void IRAM_ATTR gpio_intr(PCF8574Component *arg);
34
35 bool digital_read_hw(uint8_t pin) override;
36 bool digital_read_cache(uint8_t pin) override;
37 void digital_write_hw(uint8_t pin, bool value) override;
38
39 bool read_gpio_();
40 bool write_gpio_();
41
43 uint16_t mode_mask_{0x00};
45 uint16_t output_mask_{0x00};
47 uint16_t input_mask_{0x00};
48 bool pcf8575_;
50};
51
53class PCF8574GPIOPin : public GPIOPin {
54 public:
55 void setup() override;
56 void pin_mode(gpio::Flags flags) override;
57 bool digital_read() override;
58 void digital_write(bool value) override;
59 size_t dump_summary(char *buffer, size_t len) const override;
60
61 void set_parent(PCF8574Component *parent) { parent_ = parent; }
62 void set_pin(uint8_t pin) { pin_ = pin; }
63 void set_inverted(bool inverted) { inverted_ = inverted; }
65
66 gpio::Flags get_flags() const override { return this->flags_; }
67
68 protected:
70 uint8_t pin_;
73};
74
75} // namespace pcf8574
76} // namespace esphome
A class to cache the read state of a GPIO expander.
Definition cached_gpio.h:29
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
uint16_t input_mask_
The state read in read_gpio_ - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:47
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:43
static void IRAM_ATTR gpio_intr(PCF8574Component *arg)
Definition pcf8574.cpp:30
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition pcf8574.h:45
float get_setup_priority() const override
Definition pcf8574.cpp:122
void digital_write_hw(uint8_t pin, bool value) override
Definition pcf8574.cpp:57
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition pcf8574.cpp:65
void set_pcf8575(bool pcf8575)
Definition pcf8574.h:19
bool digital_read_cache(uint8_t pin) override
Definition pcf8574.cpp:55
InternalGPIOPin * interrupt_pin_
Definition pcf8574.h:49
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:50
void set_interrupt_pin(InternalGPIOPin *pin)
Definition pcf8574.h:20
Helper class to expose a PCF8574 pin as an internal input GPIO pin.
Definition pcf8574.h:53
void digital_write(bool value) override
Definition pcf8574.cpp:127
void set_inverted(bool inverted)
Definition pcf8574.h:63
void set_pin(uint8_t pin)
Definition pcf8574.h:62
void set_flags(gpio::Flags flags)
Definition pcf8574.h:64
void pin_mode(gpio::Flags flags) override
Definition pcf8574.cpp:125
size_t dump_summary(char *buffer, size_t len) const override
Definition pcf8574.cpp:128
gpio::Flags get_flags() const override
Definition pcf8574.h:66
PCF8574Component * parent_
Definition pcf8574.h:69
void set_parent(PCF8574Component *parent)
Definition pcf8574.h:61
uint16_t flags
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:1045