ESPHome 2026.8.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
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 // Reset IPOL to 0x00: ESPHome handles 'inverted' in software.
25
26 uint8_t iocon_flags = 0;
27 if (this->open_drain_ints_) {
28 iocon_flags |= IOCON_ODR;
29 }
30 if (this->interrupt_pin_ != nullptr) {
31 // Mirror INTA/INTB so either pin fires for changes on any port
32 iocon_flags |= IOCON_MIRROR;
33 }
34 if (iocon_flags != 0) {
35 this->write_reg(mcp23x17_base::MCP23X17_IOCONA, iocon | iocon_flags);
36 this->write_reg(mcp23x17_base::MCP23X17_IOCONB, iocon | iocon_flags);
37 }
38
40}
41
43 ESP_LOGCONFIG(TAG, "MCP23017:");
44 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
45}
46
47bool MCP23017::read_reg(uint8_t reg, uint8_t *value) {
48 if (this->is_failed())
49 return false;
50
51 return this->read_byte(reg, value);
52}
53bool MCP23017::write_reg(uint8_t reg, uint8_t value) {
54 if (this->is_failed())
55 return false;
56
57 return this->write_byte(reg, value);
58}
59
60} // namespace esphome::mcp23017
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
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:53
void dump_config() override
Definition mcp23017.cpp:42
bool read_reg(uint8_t reg, uint8_t *value) override
Definition mcp23017.cpp:47