ESPHome 2026.6.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::xl9535 {
5
6static const char *const TAG = "xl9535";
7
9 // Check to see if the device can read from the register
10 uint8_t port = 0;
12 this->mark_failed();
13 return;
14 }
15}
16
18 ESP_LOGCONFIG(TAG, "XL9535:");
19 LOG_I2C_DEVICE(this);
20
21 if (this->is_failed()) {
22 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
23 }
24}
25
27 bool state = false;
28 uint8_t port = 0;
29
30 if (pin > 7) {
32 this->status_set_warning();
33 return state;
34 }
35
36 state = (port & (1 << (pin - 10))) != 0;
37 } else {
39 this->status_set_warning();
40 return state;
41 }
42
43 state = (port & (1 << pin)) != 0;
44 }
45
47 return state;
48}
49
50void XL9535Component::digital_write(uint8_t pin, bool value) {
51 uint8_t port = 0;
52 uint8_t register_data = 0;
53
54 if (pin > 7) {
55 if (this->read_register(XL9535_OUTPUT_PORT_1_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
56 this->status_set_warning();
57 return;
58 }
59
60 register_data = register_data & (~(1 << (pin - 10)));
61 port = register_data | value << (pin - 10);
62
64 this->status_set_warning();
65 return;
66 }
67 } else {
68 if (this->read_register(XL9535_OUTPUT_PORT_0_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
69 this->status_set_warning();
70 return;
71 }
72 register_data = register_data & (~(1 << pin));
73 port = register_data | value << pin;
74
76 this->status_set_warning();
77 return;
78 }
79 }
80
82}
83
85 uint8_t port = 0;
86
87 if (pin > 7) {
89
90 if (mode == gpio::FLAG_INPUT) {
91 port = port | (1 << (pin - 10));
92 } else if (mode == gpio::FLAG_OUTPUT) {
93 port = port & (~(1 << (pin - 10)));
94 }
95
97 } else {
99
100 if (mode == gpio::FLAG_INPUT) {
101 port = port | (1 << pin);
102 } else if (mode == gpio::FLAG_OUTPUT) {
103 port = port & (~(1 << pin));
104 }
105
107 }
108}
109
110void XL9535GPIOPin::setup() { this->pin_mode(this->flags_); }
111
112size_t XL9535GPIOPin::dump_summary(char *buffer, size_t len) const {
113 return buf_append_printf(buffer, len, 0, "%u via XL9535", this->pin_);
114}
115
117bool XL9535GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
118void XL9535GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
119
120} // namespace esphome::xl9535
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:34
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:25
bool digital_read(uint8_t pin)
Definition xl9535.cpp:26
void digital_write(uint8_t pin, bool value)
Definition xl9535.cpp:50
void pin_mode(uint8_t pin, gpio::Flags mode)
Definition xl9535.cpp:84
bool digital_read() override
Definition xl9535.cpp:117
XL9535Component * parent_
Definition xl9535.h:47
size_t dump_summary(char *buffer, size_t len) const override
Definition xl9535.cpp:112
void pin_mode(gpio::Flags flags) override
Definition xl9535.cpp:116
void digital_write(bool value) override
Definition xl9535.cpp:118
uint16_t flags
bool state
Definition fan.h:2
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_INPUT
Definition gpio.h:27
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
@ XL9535_INPUT_PORT_0_REGISTER
Definition xl9535.h:10
@ XL9535_INPUT_PORT_1_REGISTER
Definition xl9535.h:11
@ XL9535_OUTPUT_PORT_0_REGISTER
Definition xl9535.h:12
@ XL9535_CONFIG_PORT_1_REGISTER
Definition xl9535.h:17
@ XL9535_CONFIG_PORT_0_REGISTER
Definition xl9535.h:16
@ XL9535_OUTPUT_PORT_1_REGISTER
Definition xl9535.h:13
const void size_t len
Definition hal.h:64