ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up XL9535...");
11
12 // Check to see if the device can read from the register
13 uint8_t port = 0;
15 this->mark_failed();
16 return;
17 }
18}
19
21 ESP_LOGCONFIG(TAG, "XL9535:");
22 LOG_I2C_DEVICE(this);
23
24 if (this->is_failed()) {
25 ESP_LOGE(TAG, "Communication with XL9535 failed!");
26 }
27}
28
30 bool state = false;
31 uint8_t port = 0;
32
33 if (pin > 7) {
35 this->status_set_warning();
36 return state;
37 }
38
39 state = (port & (1 << (pin - 10))) != 0;
40 } else {
42 this->status_set_warning();
43 return state;
44 }
45
46 state = (port & (1 << pin)) != 0;
47 }
48
50 return state;
51}
52
53void XL9535Component::digital_write(uint8_t pin, bool value) {
54 uint8_t port = 0;
55 uint8_t register_data = 0;
56
57 if (pin > 7) {
58 if (this->read_register(XL9535_OUTPUT_PORT_1_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
59 this->status_set_warning();
60 return;
61 }
62
63 register_data = register_data & (~(1 << (pin - 10)));
64 port = register_data | value << (pin - 10);
65
67 this->status_set_warning();
68 return;
69 }
70 } else {
71 if (this->read_register(XL9535_OUTPUT_PORT_0_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
72 this->status_set_warning();
73 return;
74 }
75 register_data = register_data & (~(1 << pin));
76 port = register_data | value << pin;
77
79 this->status_set_warning();
80 return;
81 }
82 }
83
85}
86
88 uint8_t port = 0;
89
90 if (pin > 7) {
92
93 if (mode == gpio::FLAG_INPUT) {
94 port = port | (1 << (pin - 10));
95 } else if (mode == gpio::FLAG_OUTPUT) {
96 port = port & (~(1 << (pin - 10)));
97 }
98
100 } else {
102
103 if (mode == gpio::FLAG_INPUT) {
104 port = port | (1 << pin);
105 } else if (mode == gpio::FLAG_OUTPUT) {
106 port = port & (~(1 << pin));
107 }
108
110 }
111}
112
113void XL9535GPIOPin::setup() { this->pin_mode(this->flags_); }
114
115std::string XL9535GPIOPin::dump_summary() const { return str_snprintf("%u via XL9535", 15, this->pin_); }
116
117void XL9535GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
118bool XL9535GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
119void XL9535GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
120
121} // namespace xl9535
122} // 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="unspecified")
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:29
void digital_write(uint8_t pin, bool value)
Definition xl9535.cpp:53
void pin_mode(uint8_t pin, gpio::Flags mode)
Definition xl9535.cpp:87
bool digital_read() override
Definition xl9535.cpp:118
std::string dump_summary() const override
Definition xl9535.cpp:115
XL9535Component * parent_
Definition xl9535.h:48
void pin_mode(gpio::Flags flags) override
Definition xl9535.cpp:117
void digital_write(bool value) override
Definition xl9535.cpp:119
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:309