ESPHome 2026.11.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
6ESPHOME_LOG_TAG(TAG, "xl9535");
7
9 // Check to see if the device can read from the register
10 uint8_t port = 0;
12 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
13 return;
14 }
15}
16
18 ESP_LOGCONFIG(TAG, "XL9535:");
19 LOG_I2C_DEVICE(this);
20}
21
23 bool state = false;
24 uint8_t port = 0;
25
26 if (pin > 7) {
28 this->status_set_warning();
29 return state;
30 }
31
32 state = (port & (1 << (pin - 10))) != 0;
33 } else {
35 this->status_set_warning();
36 return state;
37 }
38
39 state = (port & (1 << pin)) != 0;
40 }
41
43 return state;
44}
45
46void XL9535Component::digital_write(uint8_t pin, bool value) {
47 uint8_t port = 0;
48 uint8_t register_data = 0;
49
50 if (pin > 7) {
51 if (this->read_register(XL9535_OUTPUT_PORT_1_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
52 this->status_set_warning();
53 return;
54 }
55
56 register_data = register_data & (~(1 << (pin - 10)));
57 port = register_data | value << (pin - 10);
58
60 this->status_set_warning();
61 return;
62 }
63 } else {
64 if (this->read_register(XL9535_OUTPUT_PORT_0_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
65 this->status_set_warning();
66 return;
67 }
68 register_data = register_data & (~(1 << pin));
69 port = register_data | value << pin;
70
72 this->status_set_warning();
73 return;
74 }
75 }
76
78}
79
81 uint8_t port = 0;
82
83 if (pin > 7) {
85
86 if (mode == gpio::FLAG_INPUT) {
87 port = port | (1 << (pin - 10));
88 } else if (mode == gpio::FLAG_OUTPUT) {
89 port = port & (~(1 << (pin - 10)));
90 }
91
93 } else {
95
96 if (mode == gpio::FLAG_INPUT) {
97 port = port | (1 << pin);
98 } else if (mode == gpio::FLAG_OUTPUT) {
99 port = port & (~(1 << pin));
100 }
101
103 }
104}
105
106} // namespace esphome::xl9535
107
108ESPHOME_EXPANDER_PIN_SUMMARY(esphome::xl9535::XL9535Component, "%u via XL9535")
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
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:37
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:28
bool digital_read(uint8_t pin)
Definition xl9535.cpp:22
void digital_write(uint8_t pin, bool value)
Definition xl9535.cpp:46
void pin_mode(uint8_t pin, gpio::Flags mode)
Definition xl9535.cpp:80
bool state
Definition fan.h:2
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "xl9535")
@ 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