ESPHome 2026.3.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; }
20
22 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#ifdef USE_LOOP_PRIORITY
30 float get_loop_priority() const override;
31#endif
32
33 void dump_config() override;
34
35 protected:
36 bool digital_read_hw(uint8_t pin) override;
37 bool digital_read_cache(uint8_t pin) override;
38 void digital_write_hw(uint8_t pin, bool value) override;
39
40 bool read_gpio_();
41 bool write_gpio_();
42
44 uint16_t mode_mask_{0x00};
46 uint16_t output_mask_{0x00};
48 uint16_t input_mask_{0x00};
49 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: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
void set_pcf8575(bool pcf8575)
Definition pcf8574.h:19
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
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:110
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:108
size_t dump_summary(char *buffer, size_t len) const override
Definition pcf8574.cpp:111
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:817