ESPHome 2025.7.1
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 ESP_LOGCONFIG(TAG, "Running setup");
22 if (this->reset_) {
23 this->reg(PI4IOE5V6408_REGISTER_DEVICE_ID) |= 0b00000001;
24 this->reg(PI4IOE5V6408_REGISTER_OUT_HIGH_IMPEDENCE) = 0b00000000;
25 } else {
26 if (!this->read_gpio_modes_()) {
27 this->mark_failed();
28 ESP_LOGE(TAG, "Failed to read GPIO modes");
29 return;
30 }
31 if (!this->read_gpio_outputs_()) {
32 this->mark_failed();
33 ESP_LOGE(TAG, "Failed to read GPIO outputs");
34 return;
35 }
36 }
37}
39 ESP_LOGCONFIG(TAG, "PI4IOE5V6408:");
40 LOG_I2C_DEVICE(this)
41 if (this->is_failed()) {
42 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
43 }
44}
46 if (flags & gpio::FLAG_OUTPUT) {
47 // Set mode mask bit
48 this->mode_mask_ |= 1 << pin;
49 } else if (flags & gpio::FLAG_INPUT) {
50 // Clear mode mask bit
51 this->mode_mask_ &= ~(1 << pin);
52 if (flags & gpio::FLAG_PULLUP) {
53 this->pull_up_down_mask_ |= 1 << pin;
54 this->pull_enable_mask_ |= 1 << pin;
55 } else if (flags & gpio::FLAG_PULLDOWN) {
56 this->pull_up_down_mask_ &= ~(1 << pin);
57 this->pull_enable_mask_ |= 1 << pin;
58 }
59 }
60 // Write GPIO to enable input mode
61 this->write_gpio_modes_();
62}
63
65
67 if (this->is_failed())
68 return false;
69
70 uint8_t data;
71 if (!this->read_byte(PI4IOE5V6408_REGISTER_OUT_SET, &data)) {
72 this->status_set_warning("Failed to read output register");
73 return false;
74 }
75 this->output_mask_ = data;
77 return true;
78}
79
81 if (this->is_failed())
82 return false;
83
84 uint8_t data;
85 if (!this->read_byte(PI4IOE5V6408_REGISTER_IO_DIR, &data)) {
86 this->status_set_warning("Failed to read GPIO modes");
87 return false;
88 }
89#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
90 ESP_LOGV(TAG, "Read GPIO modes: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(data));
91#endif
92 this->mode_mask_ = data;
94 return true;
95}
96
98 if (this->is_failed())
99 return false;
100
101 uint8_t data;
102 if (!this->read_byte(PI4IOE5V6408_REGISTER_IN_STATE, &data)) {
103 this->status_set_warning("Failed to read GPIO state");
104 return false;
105 }
106 this->input_mask_ = data;
107 this->status_clear_warning();
108 return true;
109}
110
111void PI4IOE5V6408Component::digital_write_hw(uint8_t pin, bool value) {
112 if (this->is_failed())
113 return;
114
115 if (value) {
116 this->output_mask_ |= (1 << pin);
117 } else {
118 this->output_mask_ &= ~(1 << pin);
119 }
120 if (!this->write_byte(PI4IOE5V6408_REGISTER_OUT_SET, this->output_mask_)) {
121 this->status_set_warning("Failed to write output register");
122 return;
123 }
124#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
125 ESP_LOGV(TAG, "Wrote GPIO output: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(this->output_mask_));
126#endif
127 this->status_clear_warning();
128}
129
131 if (this->is_failed())
132 return false;
133
134 if (!this->write_byte(PI4IOE5V6408_REGISTER_IO_DIR, this->mode_mask_)) {
135 this->status_set_warning("Failed to write GPIO modes");
136 return false;
137 }
138 if (!this->write_byte(PI4IOE5V6408_REGISTER_PULL_SELECT, this->pull_up_down_mask_)) {
139 this->status_set_warning("Failed to write GPIO pullup/pulldown");
140 return false;
141 }
142 if (!this->write_byte(PI4IOE5V6408_REGISTER_PULL_ENABLE, this->pull_enable_mask_)) {
143 this->status_set_warning("Failed to write GPIO pull enable");
144 return false;
145 }
146#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
147 ESP_LOGV(TAG,
148 "Wrote GPIO modes: 0b" BYTE_TO_BINARY_PATTERN "\n"
149 "Wrote GPIO pullup/pulldown: 0b" BYTE_TO_BINARY_PATTERN "\n"
150 "Wrote GPIO pull enable: 0b" BYTE_TO_BINARY_PATTERN,
151 BYTE_TO_BINARY(this->mode_mask_), BYTE_TO_BINARY(this->pull_up_down_mask_),
152 BYTE_TO_BINARY(this->pull_enable_mask_));
153#endif
154 this->status_clear_warning();
155 return true;
156}
157
158bool PI4IOE5V6408Component::digital_read_cache(uint8_t pin) { return (this->input_mask_ & (1 << pin)); }
159
161
163void PI4IOE5V6408GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
164bool PI4IOE5V6408GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
166 this->parent_->digital_write(this->pin_, value != this->inverted_);
167}
168std::string PI4IOE5V6408GPIOPin::dump_summary() const { return str_sprintf("%u via PI4IOE5V6408", this->pin_); }
169
170} // namespace pi4ioe5v6408
171} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message="unspecified")
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:44
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