ESPHome  2022.12.8
pcf8574.cpp
Go to the documentation of this file.
1 #include "pcf8574.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace pcf8574 {
6 
7 static const char *const TAG = "pcf8574";
8 
10  ESP_LOGCONFIG(TAG, "Setting up PCF8574...");
11  if (!this->read_gpio_()) {
12  ESP_LOGE(TAG, "PCF8574 not available under 0x%02X", this->address_);
13  this->mark_failed();
14  return;
15  }
16 
17  this->write_gpio_();
18  this->read_gpio_();
19 }
21  ESP_LOGCONFIG(TAG, "PCF8574:");
22  LOG_I2C_DEVICE(this)
23  ESP_LOGCONFIG(TAG, " Is PCF8575: %s", YESNO(this->pcf8575_));
24  if (this->is_failed()) {
25  ESP_LOGE(TAG, "Communication with PCF8574 failed!");
26  }
27 }
28 bool PCF8574Component::digital_read(uint8_t pin) {
29  this->read_gpio_();
30  return this->input_mask_ & (1 << pin);
31 }
32 void PCF8574Component::digital_write(uint8_t pin, bool value) {
33  if (value) {
34  this->output_mask_ |= (1 << pin);
35  } else {
36  this->output_mask_ &= ~(1 << pin);
37  }
38 
39  this->write_gpio_();
40 }
42  if (flags == gpio::FLAG_INPUT) {
43  // Clear mode mask bit
44  this->mode_mask_ &= ~(1 << pin);
45  // Write GPIO to enable input mode
46  this->write_gpio_();
47  } else if (flags == gpio::FLAG_OUTPUT) {
48  // Set mode mask bit
49  this->mode_mask_ |= 1 << pin;
50  }
51 }
53  if (this->is_failed())
54  return false;
55  bool success;
56  uint8_t data[2];
57  if (this->pcf8575_) {
58  success = this->read_bytes_raw(data, 2);
59  this->input_mask_ = (uint16_t(data[1]) << 8) | (uint16_t(data[0]) << 0);
60  } else {
61  success = this->read_bytes_raw(data, 1);
62  this->input_mask_ = data[0];
63  }
64 
65  if (!success) {
66  this->status_set_warning();
67  return false;
68  }
69  this->status_clear_warning();
70  return true;
71 }
73  if (this->is_failed())
74  return false;
75 
76  uint16_t value = 0;
77  // Pins in OUTPUT mode and where pin is HIGH.
78  value |= this->mode_mask_ & this->output_mask_;
79  // Pins in INPUT mode must also be set here
80  value |= ~this->mode_mask_;
81 
82  uint8_t data[2];
83  data[0] = value;
84  data[1] = value >> 8;
85  if (this->write(data, this->pcf8575_ ? 2 : 1) != i2c::ERROR_OK) {
86  this->status_set_warning();
87  return false;
88  }
89 
90  this->status_clear_warning();
91  return true;
92 }
94 
95 void PCF8574GPIOPin::setup() { pin_mode(flags_); }
96 void PCF8574GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
97 bool PCF8574GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
98 void PCF8574GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
99 std::string PCF8574GPIOPin::dump_summary() const {
100  char buffer[32];
101  snprintf(buffer, sizeof(buffer), "%u via PCF8574", pin_);
102  return buffer;
103 }
104 
105 } // namespace pcf8574
106 } // namespace esphome
uint16_t mode_mask_
Mask for the pin mode - 1 means output, 0 means input.
Definition: pcf8574.h:35
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition: pcf8574.cpp:41
uint16_t input_mask_
The state read in read_gpio_ - 1 means HIGH, 0 means LOW.
Definition: pcf8574.h:39
void pin_mode(gpio::Flags flags) override
Definition: pcf8574.cpp:96
bool digital_read() override
Definition: pcf8574.cpp:97
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:80
bool digital_read(uint8_t pin)
Helper function to read the value of a pin.
Definition: pcf8574.cpp:28
void status_clear_warning()
Definition: component.cpp:149
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
std::string dump_summary() const override
Definition: pcf8574.cpp:99
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition: pcf8574.h:37
float get_setup_priority() const override
Definition: pcf8574.cpp:93
void status_set_warning()
Definition: component.cpp:141
const uint32_t flags
Definition: stm32flash.h:85
uint8_t address_
Definition: i2c.h:130
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:16
bool pcf8575_
TRUE->16-channel PCF8575, FALSE->8-channel PCF8574.
Definition: pcf8574.h:40
void setup() override
Check i2c availability and setup masks.
Definition: pcf8574.cpp:9
Definition: a4988.cpp:4
void digital_write(bool value) override
Definition: pcf8574.cpp:98
void digital_write(uint8_t pin, bool value)
Helper function to write the value of a pin.
Definition: pcf8574.cpp:32