ESPHome 2026.11.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"
8
9namespace esphome::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:
15 PCF8574Component() = default;
16
17 void set_pcf8575(bool pcf8575) { pcf8575_ = pcf8575; }
18 void set_reset(bool reset) { this->reset_ = reset; }
19 // Called by pin setters before any component's setup or GPIO writes.
20 void register_input_pin(uint8_t pin) { this->configured_input_pins_ |= 1 << pin; }
21
23 void setup() override;
25 void pin_mode(uint8_t pin, gpio::Flags flags);
26
27 void dump_config() override;
28
29 protected:
30 bool digital_read_hw(uint8_t pin) override;
31 bool digital_read_cache(uint8_t pin) override;
32 void digital_write_hw(uint8_t pin, bool value) override;
33
34 bool read_gpio_();
35 bool write_gpio_();
36
38 uint16_t mode_mask_{0x00};
40 uint16_t output_mask_{0x00};
42 uint16_t input_mask_{0x00};
43 // Pins configured as inputs before setup; 1 means input. With reset disabled, all other pins
44 // (including undeclared pins) are treated as outputs seeded from their observed levels.
45 uint16_t configured_input_pins_{0x00};
46 // Keep in sync with DEFAULT_RESET in __init__.py; codegen omits the setter for the default.
47 bool reset_{true};
48 bool pcf8575_;
49};
50
52
53} // namespace esphome::pcf8574
54
Base for expanders that cache reads per loop. With an interrupt pin the cache stays valid until INT f...
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
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 register_input_pin(uint8_t pin)
Definition pcf8574.h:20
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:17
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
uint16_t reset
Definition ina226.h:5
ESPHOME_EXPANDER_PIN_SUMMARY_DECL(esphome::pcf8574::PCF8574Component)