ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
mcp23017.cpp
Go to the documentation of this file.
1#include "mcp23017.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace mcp23017 {
6
7static const char *const TAG = "mcp23017";
8
9static constexpr uint8_t IOCON_MIRROR = 0x40; // Mirror INTA/INTB pins
10static constexpr uint8_t IOCON_ODR = 0x04; // Open-drain output for INT pin
11
13 uint8_t iocon;
14 if (!this->read_reg(mcp23x17_base::MCP23X17_IOCONA, &iocon)) {
15 this->mark_failed();
16 return;
17 }
18
19 // Read current output register state
22
23 uint8_t iocon_flags = 0;
24 if (this->open_drain_ints_) {
25 iocon_flags |= IOCON_ODR;
26 }
27 if (this->interrupt_pin_ != nullptr) {
28 // Mirror INTA/INTB so either pin fires for changes on any port
29 iocon_flags |= IOCON_MIRROR;
30 }
31 if (iocon_flags != 0) {
32 this->write_reg(mcp23x17_base::MCP23X17_IOCONA, iocon | iocon_flags);
33 this->write_reg(mcp23x17_base::MCP23X17_IOCONB, iocon | iocon_flags);
34 }
35
37}
38
40 ESP_LOGCONFIG(TAG, "MCP23017:");
41 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
42}
43
44bool MCP23017::read_reg(uint8_t reg, uint8_t *value) {
45 if (this->is_failed())
46 return false;
47
48 return this->read_byte(reg, value);
49}
50bool MCP23017::write_reg(uint8_t reg, uint8_t value) {
51 if (this->is_failed())
52 return false;
53
54 return this->write_byte(reg, value);
55}
56
57} // namespace mcp23017
58} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool write_reg(uint8_t reg, uint8_t value) override
Definition mcp23017.cpp:50
void dump_config() override
Definition mcp23017.cpp:39
bool read_reg(uint8_t reg, uint8_t *value) override
Definition mcp23017.cpp:44
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7