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