ESPHome  2022.6.3
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  // all pins input
19  this->write_reg_(MCP23016_IODIR0, 0xFF);
20  this->write_reg_(MCP23016_IODIR1, 0xFF);
21 }
22 bool MCP23016::digital_read(uint8_t pin) {
23  uint8_t bit = pin % 8;
24  uint8_t reg_addr = pin < 8 ? MCP23016_GP0 : MCP23016_GP1;
25  uint8_t value = 0;
26  this->read_reg_(reg_addr, &value);
27  return value & (1 << bit);
28 }
29 void MCP23016::digital_write(uint8_t pin, bool value) {
30  uint8_t reg_addr = pin < 8 ? MCP23016_OLAT0 : MCP23016_OLAT1;
31  this->update_reg_(pin, value, reg_addr);
32 }
33 void MCP23016::pin_mode(uint8_t pin, gpio::Flags flags) {
34  uint8_t iodir = pin < 8 ? MCP23016_IODIR0 : MCP23016_IODIR1;
35  if (flags == gpio::FLAG_INPUT) {
36  this->update_reg_(pin, true, iodir);
37  } else if (flags == gpio::FLAG_OUTPUT) {
38  this->update_reg_(pin, false, iodir);
39  }
40 }
42 bool MCP23016::read_reg_(uint8_t reg, uint8_t *value) {
43  if (this->is_failed())
44  return false;
45 
46  return this->read_byte(reg, value);
47 }
48 bool MCP23016::write_reg_(uint8_t reg, uint8_t value) {
49  if (this->is_failed())
50  return false;
51 
52  return this->write_byte(reg, value);
53 }
54 void MCP23016::update_reg_(uint8_t pin, bool pin_value, uint8_t reg_addr) {
55  uint8_t bit = pin % 8;
56  uint8_t reg_value = 0;
57  if (reg_addr == MCP23016_OLAT0) {
58  reg_value = this->olat_0_;
59  } else if (reg_addr == MCP23016_OLAT1) {
60  reg_value = this->olat_1_;
61  } else {
62  this->read_reg_(reg_addr, &reg_value);
63  }
64 
65  if (pin_value) {
66  reg_value |= 1 << bit;
67  } else {
68  reg_value &= ~(1 << bit);
69  }
70 
71  this->write_reg_(reg_addr, reg_value);
72 
73  if (reg_addr == MCP23016_OLAT0) {
74  this->olat_0_ = reg_value;
75  } else if (reg_addr == MCP23016_OLAT1) {
76  this->olat_1_ = reg_value;
77  }
78 }
79 
80 void MCP23016GPIOPin::setup() { pin_mode(flags_); }
81 void MCP23016GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
82 bool MCP23016GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
83 void MCP23016GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
84 std::string MCP23016GPIOPin::dump_summary() const {
85  char buffer[32];
86  snprintf(buffer, sizeof(buffer), "%u via MCP23016", pin_);
87  return buffer;
88 }
89 
90 } // namespace mcp23016
91 } // namespace esphome
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition: mcp23016.cpp:33
void setup() override
Definition: mcp23016.cpp:10
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:96
bool write_reg_(uint8_t reg, uint8_t value)
Definition: mcp23016.cpp:48
void digital_write(bool value) override
Definition: mcp23016.cpp:83
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:46
std::string dump_summary() const override
Definition: mcp23016.cpp:84
void digital_write(uint8_t pin, bool value)
Definition: mcp23016.cpp:29
bool digital_read(uint8_t pin)
Definition: mcp23016.cpp:22
uint8_t flags
Definition: bedjet_base.h:134
void pin_mode(gpio::Flags flags) override
Definition: mcp23016.cpp:81
bool read_reg_(uint8_t reg, uint8_t *value)
Definition: mcp23016.cpp:42
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:123
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:111
Definition: a4988.cpp:4
float get_setup_priority() const override
Definition: mcp23016.cpp:41
void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a)
Definition: mcp23016.cpp:54