ESPHome  2024.10.2
mcp23016.cpp
Go to the documentation of this file.
1 #include "mcp23016.h"
2 #include "esphome/core/log.h"
3 #include <cstdio>
4 
5 namespace esphome {
6 namespace mcp23016 {
7 
8 static const char *const TAG = "mcp23016";
9 
11  ESP_LOGCONFIG(TAG, "Setting up MCP23016...");
12  uint8_t iocon;
13  if (!this->read_reg_(MCP23016_IOCON0, &iocon)) {
14  this->mark_failed();
15  return;
16  }
17 
18  // Read current output register state
19  this->read_reg_(MCP23016_OLAT0, &this->olat_0_);
20  this->read_reg_(MCP23016_OLAT1, &this->olat_1_);
21 
22  // all pins input
23  this->write_reg_(MCP23016_IODIR0, 0xFF);
24  this->write_reg_(MCP23016_IODIR1, 0xFF);
25 }
26 bool MCP23016::digital_read(uint8_t pin) {
27  uint8_t bit = pin % 8;
28  uint8_t reg_addr = pin < 8 ? MCP23016_GP0 : MCP23016_GP1;
29  uint8_t value = 0;
30  this->read_reg_(reg_addr, &value);
31  return value & (1 << bit);
32 }
33 void MCP23016::digital_write(uint8_t pin, bool value) {
34  uint8_t reg_addr = pin < 8 ? MCP23016_OLAT0 : MCP23016_OLAT1;
35  this->update_reg_(pin, value, reg_addr);
36 }
37 void MCP23016::pin_mode(uint8_t pin, gpio::Flags flags) {
38  uint8_t iodir = pin < 8 ? MCP23016_IODIR0 : MCP23016_IODIR1;
39  if (flags == gpio::FLAG_INPUT) {
40  this->update_reg_(pin, true, iodir);
41  } else if (flags == gpio::FLAG_OUTPUT) {
42  this->update_reg_(pin, false, iodir);
43  }
44 }
46 bool MCP23016::read_reg_(uint8_t reg, uint8_t *value) {
47  if (this->is_failed())
48  return false;
49 
50  return this->read_byte(reg, value);
51 }
52 bool MCP23016::write_reg_(uint8_t reg, uint8_t value) {
53  if (this->is_failed())
54  return false;
55 
56  return this->write_byte(reg, value);
57 }
58 void MCP23016::update_reg_(uint8_t pin, bool pin_value, uint8_t reg_addr) {
59  uint8_t bit = pin % 8;
60  uint8_t reg_value = 0;
61  if (reg_addr == MCP23016_OLAT0) {
62  reg_value = this->olat_0_;
63  } else if (reg_addr == MCP23016_OLAT1) {
64  reg_value = this->olat_1_;
65  } else {
66  this->read_reg_(reg_addr, &reg_value);
67  }
68 
69  if (pin_value) {
70  reg_value |= 1 << bit;
71  } else {
72  reg_value &= ~(1 << bit);
73  }
74 
75  this->write_reg_(reg_addr, reg_value);
76 
77  if (reg_addr == MCP23016_OLAT0) {
78  this->olat_0_ = reg_value;
79  } else if (reg_addr == MCP23016_OLAT1) {
80  this->olat_1_ = reg_value;
81  }
82 }
83 
84 void MCP23016GPIOPin::setup() { pin_mode(flags_); }
85 void MCP23016GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
86 bool MCP23016GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
87 void MCP23016GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
88 std::string MCP23016GPIOPin::dump_summary() const {
89  char buffer[32];
90  snprintf(buffer, sizeof(buffer), "%u via MCP23016", pin_);
91  return buffer;
92 }
93 
94 } // namespace mcp23016
95 } // namespace esphome
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition: mcp23016.cpp:37
void setup() override
Definition: mcp23016.cpp:10
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
bool write_reg_(uint8_t reg, uint8_t value)
Definition: mcp23016.cpp:52
void digital_write(bool value) override
Definition: mcp23016.cpp:87
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition: i2c.h:149
bool is_failed() const
Definition: component.cpp:143
std::string dump_summary() const override
Definition: mcp23016.cpp:88
void digital_write(uint8_t pin, bool value)
Definition: mcp23016.cpp:33
bool digital_read(uint8_t pin)
Definition: mcp23016.cpp:26
void pin_mode(gpio::Flags flags) override
Definition: mcp23016.cpp:85
const uint32_t flags
Definition: stm32flash.h:85
bool read_reg_(uint8_t reg, uint8_t *value)
Definition: mcp23016.cpp:46
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float get_setup_priority() const override
Definition: mcp23016.cpp:45
void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a)
Definition: mcp23016.cpp:58