ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
xl9535.cpp
Go to the documentation of this file.
1#include "xl9535.h"
3#include "esphome/core/log.h"
4
5namespace esphome::xl9535 {
6
7ESPHOME_LOG_TAG(TAG, "xl9535");
8
9// Start from the levels the chip already holds, which survive a reboot of the MCU
15
17
19 ESP_LOGCONFIG(TAG, "XL9535:");
20 LOG_I2C_DEVICE(this);
21}
22
23// Pins 0-7 are port 0 and pins 10-17 are port 1; each register pair holds port 0 then port 1
24static uint8_t port_index(uint8_t pin) { return pin / 10; }
25static uint8_t port_register(uint8_t port_0_register, uint8_t pin) { return port_0_register + port_index(pin); }
26static uint8_t pin_mask(uint8_t pin) { return 1 << (pin % 10); }
27
29 uint8_t port = 0;
30 if (!this->status_comm_result(this->read_byte(port_register(XL9535_INPUT_PORT_0_REGISTER, pin), &port)))
31 return false;
32 return (port & pin_mask(pin)) != 0;
33}
34
35void XL9535Component::digital_write(uint8_t pin, bool value) {
36 // Without the chip's levels a write would clear the port's other outputs
37 if (this->is_failed() || (!this->outputs_loaded_ && !this->status_comm_result(this->load_outputs_())))
38 return;
39 uint8_t &output = this->output_[port_index(pin)];
40 assign_bits(output, pin_mask(pin), value);
41 this->status_comm_result(this->write_byte(port_register(XL9535_OUTPUT_PORT_0_REGISTER, pin), output));
42}
43
46 return;
47 // A set config bit makes the pin an input
48 const uint8_t mask = pin_mask(pin);
49 this->status_comm_result(this->update_register(port_register(XL9535_CONFIG_PORT_0_REGISTER, pin), mask,
50 mode == gpio::FLAG_INPUT ? mask : 0));
51}
52
53} // namespace esphome::xl9535
54
55ESPHOME_EXPANDER_PIN_SUMMARY(esphome::xl9535::XL9535Component, "%u via XL9535")
BedjetMode mode
BedJet operating mode.
bool is_failed() const
Definition component.h:285
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:233
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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:28
void digital_write(uint8_t pin, bool value)
Definition xl9535.cpp:35
void pin_mode(uint8_t pin, gpio::Flags mode)
Definition xl9535.cpp:44
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
@ XL9535_OUTPUT_PORT_1_REGISTER
Definition xl9535.h:14
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1267