ESPHome 2026.6.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::pcf8574 {
9
10// PCF8574(8 pins)/PCF8575(16 pins) always read/write all pins in a single I2C transaction
11// so we use uint16_t as bank type to ensure all pins are in one bank and cached together
13 public i2c::I2CDevice,
14 public gpio_expander::CachedGpioExpander<uint16_t, 16> {
15 public:
16 PCF8574Component() = default;
17
18 void set_pcf8575(bool pcf8575) { pcf8575_ = pcf8575; }
20
22 void setup() override;
23 void loop() override;
25 void pin_mode(uint8_t pin, gpio::Flags flags);
26
27 float get_setup_priority() const override;
28
29 void dump_config() override;
30
31 protected:
32 static void IRAM_ATTR gpio_intr(PCF8574Component *arg);
33
34 bool digital_read_hw(uint8_t pin) override;
35 bool digital_read_cache(uint8_t pin) override;
36 void digital_write_hw(uint8_t pin, bool value) override;
37
38 bool read_gpio_();
39 bool write_gpio_();
40
42 uint16_t mode_mask_{0x00};
44 uint16_t output_mask_{0x00};
46 uint16_t input_mask_{0x00};
47 bool pcf8575_;
49};
50
52class PCF8574GPIOPin : public GPIOPin {
53 public:
54 void setup() override;
55 void pin_mode(gpio::Flags flags) override;
56 bool digital_read() override;
57 void digital_write(bool value) override;
58 size_t dump_summary(char *buffer, size_t len) const override;
59
60 void set_parent(PCF8574Component *parent) { parent_ = parent; }
61 void set_pin(uint8_t pin) { pin_ = pin; }
62 void set_inverted(bool inverted) { inverted_ = inverted; }
64
65 gpio::Flags get_flags() const override { return this->flags_; }
66
67 protected:
69 uint8_t pin_;
72};
73
74} // namespace esphome::pcf8574
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: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
void set_pcf8575(bool pcf8575)
Definition pcf8574.h:18
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 set_interrupt_pin(InternalGPIOPin *pin)
Definition pcf8574.h:19
Helper class to expose a PCF8574 pin as an internal input GPIO pin.
Definition pcf8574.h:52
void digital_write(bool value) override
Definition pcf8574.cpp:128
void set_inverted(bool inverted)
Definition pcf8574.h:62
void set_pin(uint8_t pin)
Definition pcf8574.h:61
void set_flags(gpio::Flags flags)
Definition pcf8574.h:63
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
gpio::Flags get_flags() const override
Definition pcf8574.h:65
PCF8574Component * parent_
Definition pcf8574.h:68
void set_parent(PCF8574Component *parent)
Definition pcf8574.h:60
uint16_t flags
const void size_t len
Definition hal.h:64