ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
xl9535.cpp
Go to the documentation of this file.
1#include "xl9535.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace xl9535 {
6
7static const char *const TAG = "xl9535";
8
10 // Check to see if the device can read from the register
11 uint8_t port = 0;
13 this->mark_failed();
14 return;
15 }
16}
17
19 ESP_LOGCONFIG(TAG, "XL9535:");
20 LOG_I2C_DEVICE(this);
21
22 if (this->is_failed()) {
23 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
24 }
25}
26
28 bool state = false;
29 uint8_t port = 0;
30
31 if (pin > 7) {
33 this->status_set_warning();
34 return state;
35 }
36
37 state = (port & (1 << (pin - 10))) != 0;
38 } else {
40 this->status_set_warning();
41 return state;
42 }
43
44 state = (port & (1 << pin)) != 0;
45 }
46
48 return state;
49}
50
51void XL9535Component::digital_write(uint8_t pin, bool value) {
52 uint8_t port = 0;
53 uint8_t register_data = 0;
54
55 if (pin > 7) {
56 if (this->read_register(XL9535_OUTPUT_PORT_1_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
57 this->status_set_warning();
58 return;
59 }
60
61 register_data = register_data & (~(1 << (pin - 10)));
62 port = register_data | value << (pin - 10);
63
65 this->status_set_warning();
66 return;
67 }
68 } else {
69 if (this->read_register(XL9535_OUTPUT_PORT_0_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
70 this->status_set_warning();
71 return;
72 }
73 register_data = register_data & (~(1 << pin));
74 port = register_data | value << pin;
75
77 this->status_set_warning();
78 return;
79 }
80 }
81
83}
84
86 uint8_t port = 0;
87
88 if (pin > 7) {
90
91 if (mode == gpio::FLAG_INPUT) {
92 port = port | (1 << (pin - 10));
93 } else if (mode == gpio::FLAG_OUTPUT) {
94 port = port & (~(1 << (pin - 10)));
95 }
96
98 } else {
100
101 if (mode == gpio::FLAG_INPUT) {
102 port = port | (1 << pin);
103 } else if (mode == gpio::FLAG_OUTPUT) {
104 port = port & (~(1 << pin));
105 }
106
108 }
109}
110
111void XL9535GPIOPin::setup() { this->pin_mode(this->flags_); }
112
113std::string XL9535GPIOPin::dump_summary() const { return str_snprintf("%u via XL9535", 15, this->pin_); }
114
115void XL9535GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
116bool XL9535GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
117void XL9535GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
118
119} // namespace xl9535
120} // namespace esphome
BedjetMode mode
BedJet operating mode.
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()
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:25
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:10
bool digital_read(uint8_t pin)
Definition xl9535.cpp:27
void digital_write(uint8_t pin, bool value)
Definition xl9535.cpp:51
void pin_mode(uint8_t pin, gpio::Flags mode)
Definition xl9535.cpp:85
bool digital_read() override
Definition xl9535.cpp:116
std::string dump_summary() const override
Definition xl9535.cpp:113
XL9535Component * parent_
Definition xl9535.h:48
void pin_mode(gpio::Flags flags) override
Definition xl9535.cpp:115
void digital_write(bool value) override
Definition xl9535.cpp:117
bool state
Definition fan.h:0
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_INPUT
Definition gpio.h:18
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
@ XL9535_INPUT_PORT_0_REGISTER
Definition xl9535.h:11
@ XL9535_INPUT_PORT_1_REGISTER
Definition xl9535.h:12
@ XL9535_OUTPUT_PORT_0_REGISTER
Definition xl9535.h:13
@ XL9535_CONFIG_PORT_1_REGISTER
Definition xl9535.h:18
@ XL9535_CONFIG_PORT_0_REGISTER
Definition xl9535.h:17
@ XL9535_OUTPUT_PORT_1_REGISTER
Definition xl9535.h:14
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_snprintf(const char *fmt, size_t len,...)
Definition helpers.cpp:194