ESPHome 2025.10.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 float get_loop_priority() const override;
30
31 void dump_config() override;
32
33 protected:
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_;
48};
49
51class PCF8574GPIOPin : public GPIOPin {
52 public:
53 void setup() override;
54 void pin_mode(gpio::Flags flags) override;
55 bool digital_read() override;
56 void digital_write(bool value) override;
57 std::string dump_summary() const override;
58
59 void set_parent(PCF8574Component *parent) { parent_ = parent; }
60 void set_pin(uint8_t pin) { pin_ = pin; }
61 void set_inverted(bool inverted) { inverted_ = inverted; }
63
64 gpio::Flags get_flags() const override { return this->flags_; }
65
66 protected:
68 uint8_t pin_;
71};
72
73} // namespace pcf8574
74} // 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:133
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:9
uint16_t mode_mask_
Mask for the pin mode - 1 means output, 0 means input.
Definition pcf8574.h:42
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:98
void digital_write_hw(uint8_t pin, bool value) override
Definition pcf8574.cpp:38
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:46
void set_pcf8575(bool pcf8575)
Definition pcf8574.h:19
float get_loop_priority() const override
Definition pcf8574.cpp:101
bool digital_read_cache(uint8_t pin) override
Definition pcf8574.cpp:36
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:31
Helper class to expose a PCF8574 pin as an internal input GPIO pin.
Definition pcf8574.h:51
void digital_write(bool value) override
Definition pcf8574.cpp:106
void set_inverted(bool inverted)
Definition pcf8574.h:61
void set_pin(uint8_t pin)
Definition pcf8574.h:60
void set_flags(gpio::Flags flags)
Definition pcf8574.h:62
void pin_mode(gpio::Flags flags) override
Definition pcf8574.cpp:104
std::string dump_summary() const override
Definition pcf8574.cpp:107
gpio::Flags get_flags() const override
Definition pcf8574.h:64
PCF8574Component * parent_
Definition pcf8574.h:67
void set_parent(PCF8574Component *parent)
Definition pcf8574.h:59
uint16_t flags
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7