ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
mcp23016.cpp
Go to the documentation of this file.
1#include "mcp23016.h"
2#include "esphome/core/log.h"
3#include <cstdio>
4
5namespace esphome {
6namespace mcp23016 {
7
8static const char *const TAG = "mcp23016";
9
11 uint16_t iocon;
12 // MCP23016 registers operate as paired 16-bit registers. Addressing the
13 // odd register (e.g. IOCON1) reads/writes that register first, then wraps
14 // to the even register (IOCON0) in the same pair. Starting from the odd
15 // address gives the correct byte order for 1 << pin mapping:
16 // high byte = port 1 (pins 8-15), low byte = port 0 (pins 0-7).
17 if (!this->read_reg_(MCP23016_IOCON1, &iocon)) {
18 this->mark_failed();
19 return;
20 }
21
22 // Read current output register state
23 this->read_reg_(MCP23016_OLAT1, &this->olat_);
24
25 // all pins input
26 this->write_reg_(MCP23016_IODIR1, 0xFFFF);
27}
28
30 // Invalidate cache at the start of each loop
31 this->reset_pin_cache_();
32}
33bool MCP23016::digital_read_hw(uint8_t pin) { return this->read_reg_(MCP23016_GP1, &this->input_mask_); }
34
35bool MCP23016::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
36void MCP23016::digital_write_hw(uint8_t pin, bool value) { this->update_reg_(pin, value, MCP23016_OLAT1); }
38 if (flags == gpio::FLAG_INPUT) {
39 this->update_reg_(pin, true, MCP23016_IODIR1);
40 } else if (flags == gpio::FLAG_OUTPUT) {
41 this->update_reg_(pin, false, MCP23016_IODIR1);
42 }
43}
45bool MCP23016::read_reg_(uint8_t reg, uint16_t *value) {
46 if (this->is_failed())
47 return false;
48
49 return this->read_byte_16(reg, value);
50}
51bool MCP23016::write_reg_(uint8_t reg, uint16_t value) {
52 if (this->is_failed())
53 return false;
54
55 return this->write_byte_16(reg, value);
56}
57void MCP23016::update_reg_(uint8_t pin, bool pin_value, uint8_t reg_addr) {
58 uint16_t reg_value = 0;
59
60 if (reg_addr == MCP23016_OLAT1) {
61 reg_value = this->olat_;
62 } else {
63 this->read_reg_(reg_addr, &reg_value);
64 }
65
66 if (pin_value) {
67 reg_value |= 1 << pin;
68 } else {
69 reg_value &= ~(1 << pin);
70 }
71
72 this->write_reg_(reg_addr, reg_value);
73
74 if (reg_addr == MCP23016_OLAT1) {
75 this->olat_ = reg_value;
76 }
77}
78
81bool MCP23016GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
82void MCP23016GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
83size_t MCP23016GPIOPin::dump_summary(char *buffer, size_t len) const {
84 return buf_append_printf(buffer, len, 0, "%u via MCP23016", this->pin_);
85}
86
87} // namespace mcp23016
88} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
bool digital_read(P pin)
Read the state of the given pin.
Definition cached_gpio.h:34
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
void pin_mode(gpio::Flags flags) override
Definition mcp23016.cpp:80
size_t dump_summary(char *buffer, size_t len) const override
Definition mcp23016.cpp:83
void digital_write(bool value) override
Definition mcp23016.cpp:82
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition mcp23016.cpp:37
bool digital_read_cache(uint8_t pin) override
Definition mcp23016.cpp:35
void digital_write_hw(uint8_t pin, bool value) override
Definition mcp23016.cpp:36
bool digital_read_hw(uint8_t pin) override
Definition mcp23016.cpp:33
void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a)
Definition mcp23016.cpp:57
float get_setup_priority() const override
Definition mcp23016.cpp:44
bool read_reg_(uint8_t reg, uint16_t *value)
Definition mcp23016.cpp:45
bool write_reg_(uint8_t reg, uint16_t value)
Definition mcp23016.cpp:51
uint16_t flags
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_INPUT
Definition gpio.h:27
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:27
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:817