ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mcp23x17_base.cpp
Go to the documentation of this file.
1#include "mcp23x17_base.h"
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "mcp23x17_base");
8
10 uint8_t data;
11 if (pin < 8) {
12 if (!this->read_reg(mcp23x17_base::MCP23X17_GPIOA, &data)) {
13 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
14 return false;
15 }
16 this->input_mask_ = encode_uint16(this->input_mask_ >> 8, data);
17 } else {
18 if (!this->read_reg(mcp23x17_base::MCP23X17_GPIOB, &data)) {
19 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
20 return false;
21 }
22 this->input_mask_ = encode_uint16(data, this->input_mask_ & 0xFF);
23 }
24 return true;
25}
26
27void MCP23X17Base::digital_write_hw(uint8_t pin, bool value) {
29 this->update_reg(pin, value, reg_addr);
30}
31
32bool MCP23X17Base::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
33
37 if (flags == gpio::FLAG_INPUT) {
38 this->update_reg(pin, true, iodir);
39 this->update_reg(pin, false, gppu);
40 } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
41 this->update_reg(pin, true, iodir);
42 this->update_reg(pin, true, gppu);
43 } else if (flags == gpio::FLAG_OUTPUT) {
44 this->update_reg(pin, false, iodir);
45 }
46 if (flags & gpio::FLAG_INPUT) {
47 // With an interrupt pin, INT fires on any change of an input
48 if (this->interrupt_pin_ != nullptr)
51 }
52}
53
58
59 switch (interrupt_mode) {
61 this->update_reg(pin, true, gpinten);
62 this->update_reg(pin, false, intcon);
63 break;
65 this->update_reg(pin, true, gpinten);
66 this->update_reg(pin, true, intcon);
67 this->update_reg(pin, false, defval);
68 break;
70 this->update_reg(pin, true, gpinten);
71 this->update_reg(pin, true, intcon);
72 this->update_reg(pin, true, defval);
73 break;
75 this->update_reg(pin, false, gpinten);
76 break;
77 }
78}
79
80void MCP23X17Base::update_reg(uint8_t pin, bool pin_value, uint8_t reg_addr) {
81 uint8_t bit = pin % 8;
82 uint8_t reg_value = 0;
83 if (reg_addr == mcp23x17_base::MCP23X17_OLATA) {
84 reg_value = this->olat_a_;
85 } else if (reg_addr == mcp23x17_base::MCP23X17_OLATB) {
86 reg_value = this->olat_b_;
87 } else {
88 this->read_reg(reg_addr, &reg_value);
89 }
90
91 assign_bits(reg_value, 1 << bit, pin_value);
92
93 this->write_reg(reg_addr, reg_value);
94
95 if (reg_addr == mcp23x17_base::MCP23X17_OLATA) {
96 this->olat_a_ = reg_value;
97 } else if (reg_addr == mcp23x17_base::MCP23X17_OLATB) {
98 this->olat_b_ = reg_value;
99 }
100}
101
102} // namespace esphome::mcp23x17_base
void enable_polling_loop_()
Call from pin_mode() for an input pin: without an interrupt pin the loop polls instead.
bool digital_read_cache(uint8_t pin) override
void pin_mode(uint8_t pin, gpio::Flags flags) override
void digital_write_hw(uint8_t pin, bool value) override
bool digital_read_hw(uint8_t pin) override
void pin_interrupt_mode(uint8_t pin, mcp23xxx_base::MCP23XXXInterruptMode interrupt_mode) override
uint16_t input_mask_
State read in digital_read_hw.
void update_reg(uint8_t pin, bool pin_value, uint8_t reg_a) override
virtual bool read_reg(uint8_t reg, uint8_t *value)=0
virtual bool write_reg(uint8_t reg, uint8_t value)=0
uint16_t flags
ESPHOME_LOG_TAG(TAG, "mcp23x17_base")
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1072
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:998