ESPHome  2024.4.0
pca9554.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 namespace esphome {
8 namespace pca9554 {
9 
10 class PCA9554Component : public Component, public i2c::I2CDevice {
11  public:
12  PCA9554Component() = default;
13 
15  void setup() override;
17  void loop() override;
19  bool digital_read(uint8_t pin);
21  void digital_write(uint8_t pin, bool value);
23  void pin_mode(uint8_t pin, gpio::Flags flags);
24 
25  float get_setup_priority() const override;
26 
27  float get_loop_priority() const override;
28 
29  void dump_config() override;
30 
31  void set_pin_count(size_t pin_count) { this->pin_count_ = pin_count; }
32 
33  protected:
34  bool read_inputs_();
35 
36  bool write_register_(uint8_t reg, uint16_t value);
37 
39  size_t pin_count_{8};
41  size_t reg_width_{1};
43  uint16_t config_mask_{0x00};
45  uint16_t output_mask_{0x00};
47  uint16_t input_mask_{0x00};
49  uint16_t was_previously_read_ = {0x00};
52 };
53 
55 class PCA9554GPIOPin : public GPIOPin {
56  public:
57  void setup() override;
58  void pin_mode(gpio::Flags flags) override;
59  bool digital_read() override;
60  void digital_write(bool value) override;
61  std::string dump_summary() const override;
62 
63  void set_parent(PCA9554Component *parent) { parent_ = parent; }
64  void set_pin(uint8_t pin) { pin_ = pin; }
65  void set_inverted(bool inverted) { inverted_ = inverted; }
66  void set_flags(gpio::Flags flags) { flags_ = flags; }
67 
68  protected:
70  uint8_t pin_;
71  bool inverted_;
73 };
74 
75 } // namespace pca9554
76 } // namespace esphome
Helper class to expose a PCA9554 pin as an internal input GPIO pin.
Definition: pca9554.h:55
void set_parent(PCA9554Component *parent)
Definition: pca9554.h:63
void set_pin_count(size_t pin_count)
Definition: pca9554.h:31
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition: i2c.h:149
void setup() override
Check i2c availability and setup masks.
Definition: pca9554.cpp:15
void set_pin(uint8_t pin)
Definition: pca9554.h:64
esphome::i2c::ErrorCode last_error_
Storage for last I2C error seen.
Definition: pca9554.h:51
float get_setup_priority() const override
Definition: pca9554.cpp:127
uint16_t was_previously_read_
Flags to check if read previously during this loop.
Definition: pca9554.h:49
void digital_write(uint8_t pin, bool value)
Helper function to write the value of a pin.
Definition: pca9554.cpp:70
size_t pin_count_
number of bits the expander has
Definition: pca9554.h:39
bool digital_read(uint8_t pin)
Helper function to read the value of a pin.
Definition: pca9554.cpp:56
void set_inverted(bool inverted)
Definition: pca9554.h:65
uint16_t input_mask_
The state of the actual input pin states - 1 means HIGH, 0 means LOW.
Definition: pca9554.h:47
void loop() override
Poll for input changes periodically.
Definition: pca9554.cpp:40
const uint32_t flags
Definition: stm32flash.h:85
uint16_t config_mask_
Mask for the pin config - 1 means OUTPUT, 0 means INPUT.
Definition: pca9554.h:43
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition: pca9554.h:45
size_t reg_width_
width of registers
Definition: pca9554.h:41
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition: pca9554.cpp:79
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
void set_flags(gpio::Flags flags)
Definition: pca9554.h:66
PCA9554Component * parent_
Definition: pca9554.h:69
float get_loop_priority() const override
Definition: pca9554.cpp:131
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
bool write_register_(uint8_t reg, uint16_t value)
Definition: pca9554.cpp:112