ESPHome 2026.11.0b6
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
22// Pins 0-7 are port 0 and pins 10-17 are port 1; each register pair holds port 0 then port 1
23static uint8_t port_register(uint8_t port_0_register, uint8_t pin) { return port_0_register + pin / 10; }
24static uint8_t pin_mask(uint8_t pin) { return 1 << (pin % 10); }
25
27 uint8_t port = 0;
28 if (!this->status_comm_result(this->read_byte(port_register(XL9535_INPUT_PORT_0_REGISTER, pin), &port)))
29 return false;
30 return (port & pin_mask(pin)) != 0;
31}
32
33void XL9535Component::digital_write(uint8_t pin, bool value) {
34 const uint8_t mask = pin_mask(pin);
36 this->update_register(port_register(XL9535_OUTPUT_PORT_0_REGISTER, pin), mask, value ? mask : 0));
37}
38
41 return;
42 // A set config bit makes the pin an input
43 const uint8_t mask = pin_mask(pin);
44 this->status_comm_result(this->update_register(port_register(XL9535_CONFIG_PORT_0_REGISTER, pin), mask,
45 mode == gpio::FLAG_INPUT ? mask : 0));
46}
47
48} // namespace esphome::xl9535
49
50ESPHOME_EXPANDER_PIN_SUMMARY(esphome::xl9535::XL9535Component, "%u via XL9535")
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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:45
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
bool digital_read(uint8_t pin)
Definition xl9535.cpp:26
void digital_write(uint8_t pin, bool value)
Definition xl9535.cpp:33
void pin_mode(uint8_t pin, gpio::Flags mode)
Definition xl9535.cpp:39
@ 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_OUTPUT_PORT_0_REGISTER
Definition xl9535.h:13
@ XL9535_CONFIG_PORT_0_REGISTER
Definition xl9535.h:17
uint32_t value
Definition hal.cpp:41