ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
pi4ioe5v6408.cpp
Go to the documentation of this file.
1#include "pi4ioe5v6408.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace pi4ioe5v6408 {
6
7static const uint8_t PI4IOE5V6408_REGISTER_DEVICE_ID = 0x01;
8static const uint8_t PI4IOE5V6408_REGISTER_IO_DIR = 0x03;
9static const uint8_t PI4IOE5V6408_REGISTER_OUT_SET = 0x05;
10static const uint8_t PI4IOE5V6408_REGISTER_OUT_HIGH_IMPEDENCE = 0x07;
11static const uint8_t PI4IOE5V6408_REGISTER_IN_DEFAULT_STATE = 0x09;
12static const uint8_t PI4IOE5V6408_REGISTER_PULL_ENABLE = 0x0B;
13static const uint8_t PI4IOE5V6408_REGISTER_PULL_SELECT = 0x0D;
14static const uint8_t PI4IOE5V6408_REGISTER_IN_STATE = 0x0F;
15static const uint8_t PI4IOE5V6408_REGISTER_INTERRUPT_ENABLE_MASK = 0x11;
16static const uint8_t PI4IOE5V6408_REGISTER_INTERRUPT_STATUS = 0x13;
17
18static const char *const TAG = "pi4ioe5v6408";
19
21 if (this->reset_) {
22 this->reg(PI4IOE5V6408_REGISTER_DEVICE_ID) |= 0b00000001;
23 this->reg(PI4IOE5V6408_REGISTER_OUT_HIGH_IMPEDENCE) = 0b00000000;
24 } else {
25 if (!this->read_gpio_modes_()) {
26 this->mark_failed();
27 ESP_LOGE(TAG, "Failed to read GPIO modes");
28 return;
29 }
30 if (!this->read_gpio_outputs_()) {
31 this->mark_failed();
32 ESP_LOGE(TAG, "Failed to read GPIO outputs");
33 return;
34 }
35 }
36}
38 ESP_LOGCONFIG(TAG, "PI4IOE5V6408:");
39 LOG_I2C_DEVICE(this)
40 if (this->is_failed()) {
41 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
42 }
43}
45 if (flags & gpio::FLAG_OUTPUT) {
46 // Set mode mask bit
47 this->mode_mask_ |= 1 << pin;
48 } else if (flags & gpio::FLAG_INPUT) {
49 // Clear mode mask bit
50 this->mode_mask_ &= ~(1 << pin);
51 if (flags & gpio::FLAG_PULLUP) {
52 this->pull_up_down_mask_ |= 1 << pin;
53 this->pull_enable_mask_ |= 1 << pin;
54 } else if (flags & gpio::FLAG_PULLDOWN) {
55 this->pull_up_down_mask_ &= ~(1 << pin);
56 this->pull_enable_mask_ |= 1 << pin;
57 }
58 }
59 // Write GPIO to enable input mode
60 this->write_gpio_modes_();
61}
62
64
66 if (this->is_failed())
67 return false;
68
69 uint8_t data;
70 if (!this->read_byte(PI4IOE5V6408_REGISTER_OUT_SET, &data)) {
71 this->status_set_warning("Failed to read output register");
72 return false;
73 }
74 this->output_mask_ = data;
76 return true;
77}
78
80 if (this->is_failed())
81 return false;
82
83 uint8_t data;
84 if (!this->read_byte(PI4IOE5V6408_REGISTER_IO_DIR, &data)) {
85 this->status_set_warning("Failed to read GPIO modes");
86 return false;
87 }
88#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
89 ESP_LOGV(TAG, "Read GPIO modes: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(data));
90#endif
91 this->mode_mask_ = data;
93 return true;
94}
95
97 if (this->is_failed())
98 return false;
99
100 uint8_t data;
101 if (!this->read_byte(PI4IOE5V6408_REGISTER_IN_STATE, &data)) {
102 this->status_set_warning("Failed to read GPIO state");
103 return false;
104 }
105 this->input_mask_ = data;
106 this->status_clear_warning();
107 return true;
108}
109
110void PI4IOE5V6408Component::digital_write_hw(uint8_t pin, bool value) {
111 if (this->is_failed())
112 return;
113
114 if (value) {
115 this->output_mask_ |= (1 << pin);
116 } else {
117 this->output_mask_ &= ~(1 << pin);
118 }
119 if (!this->write_byte(PI4IOE5V6408_REGISTER_OUT_SET, this->output_mask_)) {
120 this->status_set_warning("Failed to write output register");
121 return;
122 }
123#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
124 ESP_LOGV(TAG, "Wrote GPIO output: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(this->output_mask_));
125#endif
126 this->status_clear_warning();
127}
128
130 if (this->is_failed())
131 return false;
132
133 if (!this->write_byte(PI4IOE5V6408_REGISTER_IO_DIR, this->mode_mask_)) {
134 this->status_set_warning("Failed to write GPIO modes");
135 return false;
136 }
137 if (!this->write_byte(PI4IOE5V6408_REGISTER_PULL_SELECT, this->pull_up_down_mask_)) {
138 this->status_set_warning("Failed to write GPIO pullup/pulldown");
139 return false;
140 }
141 if (!this->write_byte(PI4IOE5V6408_REGISTER_PULL_ENABLE, this->pull_enable_mask_)) {
142 this->status_set_warning("Failed to write GPIO pull enable");
143 return false;
144 }
145#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
146 ESP_LOGV(TAG,
147 "Wrote GPIO modes: 0b" BYTE_TO_BINARY_PATTERN "\n"
148 "Wrote GPIO pullup/pulldown: 0b" BYTE_TO_BINARY_PATTERN "\n"
149 "Wrote GPIO pull enable: 0b" BYTE_TO_BINARY_PATTERN,
150 BYTE_TO_BINARY(this->mode_mask_), BYTE_TO_BINARY(this->pull_up_down_mask_),
151 BYTE_TO_BINARY(this->pull_enable_mask_));
152#endif
153 this->status_clear_warning();
154 return true;
155}
156
157bool PI4IOE5V6408Component::digital_read_cache(uint8_t pin) { return (this->input_mask_ & (1 << pin)); }
158
160
162void PI4IOE5V6408GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
163bool PI4IOE5V6408GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
165 this->parent_->digital_write(this->pin_, value != this->inverted_);
166}
167std::string PI4IOE5V6408GPIOPin::dump_summary() const { return str_sprintf("%u via PI4IOE5V6408", this->pin_); }
168
169} // namespace pi4ioe5v6408
170} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
uint8_t pull_enable_mask_
The mask to write as input buffer state - 1 means enabled, 0 means disabled.
uint8_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
uint8_t pull_up_down_mask_
The mask to write as pullup state - 1 means pullup, 0 means pulldown.
bool digital_read_cache(uint8_t pin) override
void digital_write_hw(uint8_t pin, bool value) override
uint8_t input_mask_
The state read in digital_read_hw - 1 means HIGH, 0 means LOW.
uint8_t mode_mask_
Mask for the pin mode - 1 means output, 0 means input.
void pin_mode(uint8_t pin, gpio::Flags flags)
void pin_mode(gpio::Flags flags) override
std::string dump_summary() const override
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_INPUT
Definition gpio.h:18
@ FLAG_PULLDOWN
Definition gpio.h:22
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:48
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:208